[SWT-Snippet] InputDialog mit dynamischer Anzahl von Feldern

SNOWMAN-X

Mitglied
Hoi,

da es bei SWT ja keinen Eingabedialog gibt und im Netz nur einfache 1-Feld Dialoge zu finden sind (zumindest wenn man nach "SWT Inputdialog" sucht) hab ich jetzt mal einen solchen umgeschrieben, das er eine dynamische Anzahl von Eingabefeldern unterstützt.
Neben der Möglichkeit, eine Anzahl der Eingabefelder zu definieren, kann man auch ein int-Array mit übergeben in welchem für jedes Eingabefeld eine minimum Width stehen kann.
Man muss entweder für alle Felder eine minWidht angeben oder für garkeins!

Rückgabe ist immer ein String Array mit den eingegebenen Texten.
Sollte der User abbrechen steht im String Array in jedem Feld "-ABORTED-".
(Ja, ich war zu Faul das besser zu machen...)

Es gibt folgende Konstruktoren:
Code:
InputBox(Shell parent, int NumberOfInputFields);
InputBox(Shell parent, int style, int NumberOfInputFields);
InputBox(Shell parent, int NumberOfInputFields, int[] minimumWidthOfEachField);
InputBox(Shell parent, int style, int NumberOfInputFields, int[] minimumWidthOfEachField);

Hier der Quellcode:
Code:
import java.lang.reflect.Array;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class InputBox extends Dialog {
    private Shell shell;
    private Label label;
   
    private String message;
    private String result[];
   
    public InputBox(Shell parent, int iNumberOfFields, int iFieldWith[]){
    	this(parent, SWT.APPLICATION_MODAL, iNumberOfFields, iFieldWith);
    }
    
    public InputBox(Shell parent, int iNumberOfFields){
    	this(parent, SWT.APPLICATION_MODAL, iNumberOfFields, null);
    }
    
    public InputBox(Shell parent, int style, final int iNumberOfFields, int iFieldWith[]) {
        super(parent, checkStyle(style));
       
        shell = new Shell(parent, SWT.DIALOG_TRIM | checkStyle(style));
        shell.setText(getText());
        shell.setLayout(new GridLayout(2, false));
        
        if(iFieldWith != null){
	        if(Array.getLength(iFieldWith) != iNumberOfFields){
	        	SWT.error(SWT.ERROR_NULL_ARGUMENT);
	        }
        }
       
        new Label(shell, SWT.CENTER).setImage(shell.getDisplay().getSystemImage(checkImageStyle(style)));
       
        Composite body = new Composite(shell, SWT.NONE);
       
        GridData data0 = new GridData();
        data0.grabExcessHorizontalSpace = true;
        data0.grabExcessVerticalSpace = true;
        data0.horizontalAlignment = SWT.FILL;
        data0.verticalAlignment = SWT.FILL;
        body.setLayoutData(data0);
       
        body.setLayout(new GridLayout(iNumberOfFields, false));
       
        label = new Label(body, SWT.LEFT);
       
        GridData data1 = new GridData();
        data1.grabExcessHorizontalSpace = true;
        data1.grabExcessVerticalSpace = true;
        data1.horizontalAlignment = SWT.FILL;
        data1.verticalAlignment = SWT.FILL;
        data1.horizontalSpan = iNumberOfFields;
        label.setLayoutData(data1);
       
        final Text text[] = new Text[iNumberOfFields];
        for(int i = 0; i < iNumberOfFields; i++){
	         text[i] = new Text(body, SWT.SINGLE | SWT.BORDER);
	       
	        GridData data2 = new GridData();
	        data2.grabExcessHorizontalSpace = true;
	        data2.horizontalAlignment = SWT.FILL;
	        if(iFieldWith != null){
	        	data2.minimumWidth = iFieldWith[i];
	        }
	        text[i].setLayoutData(data2);
        }
       
        Composite footer = new Composite(shell, SWT.NONE);
       
        GridData data3 = new GridData();
        data3.grabExcessHorizontalSpace = true;
        data3.horizontalAlignment = SWT.FILL;
        data3.horizontalSpan = 2;
        footer.setLayoutData(data3);
       
        RowLayout layout = new RowLayout();
        layout.justify = true;
        layout.fill = true;
        footer.setLayout(layout);
       
        Button ok = new Button(footer, SWT.PUSH);
        ok.setText("        OK        ");
        ok.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                result = new String[iNumberOfFields];
                for(int i = 0; i < iNumberOfFields; i++) {
					result[i] = text[i].getText();
				}
               
                shell.dispose();
            }
        });
        shell.setDefaultButton(ok);
        
        Button cancel = new Button(footer, SWT.PUSH);
        cancel.setText("      Cancel      ");
        cancel.addSelectionListener(new SelectionAdapter() {
        	public void widgetSelected(SelectionEvent e) {
        		result = new String[iNumberOfFields];
                for(int i = 0; i < iNumberOfFields; i++) {
					result[i] = "-ABORTED-";
				}
        		shell.dispose();
        	}
        });
    }
   
    protected static int checkStyle(int style) {
        if ((style & SWT.SYSTEM_MODAL) == SWT.SYSTEM_MODAL) {
            return SWT.SYSTEM_MODAL;
        } else if ((style & SWT.PRIMARY_MODAL) == SWT.PRIMARY_MODAL) {
            return SWT.PRIMARY_MODAL;
        } else if ((style & SWT.APPLICATION_MODAL) == SWT.APPLICATION_MODAL) {
            return SWT.APPLICATION_MODAL;
        }
       
        return SWT.APPLICATION_MODAL;
    }
   
    protected static int checkImageStyle(int style) {
        if ((style & SWT.ICON_ERROR) == SWT.ICON_ERROR) {
            return SWT.ICON_ERROR;
        } else if ((style & SWT.ICON_INFORMATION) == SWT.ICON_INFORMATION) {
            return SWT.ICON_INFORMATION;
        } else if ((style & SWT.ICON_QUESTION) == SWT.ICON_QUESTION) {
            return SWT.ICON_QUESTION;
        } else if ((style & SWT.ICON_WARNING) == SWT.ICON_WARNING) {
            return SWT.ICON_WARNING;
        } else if ((style & SWT.ICON_WORKING) == SWT.ICON_WORKING) {
            return SWT.ICON_WORKING;
        }
       
        return SWT.NONE;
    }
   
    @Override
    public void setText(String string) {
        super.setText(string);
       
        shell.setText(string);
    }
   
    public String getMessage() {
        return message;
    }
   
    public void setMessage(String message) {
        if (message == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
       
        this.message = message;
        label.setText(message);
    }
   
    public Image getImage(){
    	return this.shell.getImage();
    }
    
    public void setImage(Image img){
    	if(img == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
    	
    	this.shell.setImage(img);
    }
    
    public String[] open() {
        shell.pack();
        shell.open();
        shell.layout();
       
        while (!shell.isDisposed())
            if (!shell.getDisplay().readAndDispatch()) shell.getDisplay().sleep();
       
        return result;
    }
}
 

Neue Beiträge

Zurück