View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.dialogs;
23  
24  import org.eclipse.jface.window.Window;
25  import org.eclipse.swt.SWT;
26  import org.eclipse.swt.events.SelectionAdapter;
27  import org.eclipse.swt.events.SelectionEvent;
28  import org.eclipse.swt.layout.GridData;
29  import org.eclipse.swt.layout.GridLayout;
30  import org.eclipse.swt.widgets.Button;
31  import org.eclipse.swt.widgets.Composite;
32  import org.eclipse.swt.widgets.Control;
33  import org.eclipse.swt.widgets.Label;
34  import org.eclipse.swt.widgets.Listener;
35  import org.eclipse.swt.widgets.Shell;
36  import org.eclipse.swt.widgets.Text;
37  
38  /**
39   * superclass for all the connect dialogs. all subclasses have to instantiate 
40   * the two members FIELDS and DEFAULTS as arrays providing the labels and the 
41   * default values for the fields available. <br/>
42   * <br/>
43   * <code>FIELDS = new String[]{ "test", "me" };</code><br/>
44   * <code>DEFAULTS = new String [] { "myTestDefault", "memuuDefault" };</code><br/>
45   * will create two fields with labels "test" and "me" with the respective 
46   * default values.
47   * 
48   * @author sawielan
49   *
50   */
51  public abstract class ConnectDialog extends org.eclipse.jface.dialogs.Dialog {
52  	
53  	/** the label of the fields. */
54  	public String [] FIELDS;
55  	
56  	/** the default values for the fields. */
57  	public String [] DEFAULTS;
58  	
59  	/** the values collected from the fields. */
60  	public String [] values;
61  	
62  	/** the text fields. */
63  	protected Text []txts;
64  	
65  	/** the caption. */
66  	protected final String caption;
67  	
68  	/** the grid settings for the label fields. */
69  	protected GridData gridLabel = new GridData(GridData.FILL_BOTH);
70  	
71  	/** the grid settings for the text fields. */
72  	protected GridData gridText = new GridData(GridData.FILL_BOTH);
73  	
74  	/** the grid settings for a horizontal filler. */
75  	protected GridData gridAll = new GridData(GridData.FILL_BOTH);
76  	
77  	/**
78  	 * create a new connect dialog.
79  	 * @param shell the parent shell.
80  	 * @param caption the caption for the dialog.
81  	 */
82  	public ConnectDialog(Shell shell, String caption) {
83  		super(shell);
84  		this.caption = caption;
85  	}
86  	
87  	/**
88  	 * sets the layout for the dialog.
89  	 * @param parent the parent where to set the layout.
90  	 */
91  	protected void setLayout(Composite parent) {
92  		GridLayout layout = new GridLayout();
93  		layout.numColumns = 3;
94  		
95  		gridLabel.verticalSpan = 1;
96  		gridLabel.horizontalSpan = 1;
97  		gridLabel.widthHint=100;
98  		gridLabel.heightHint = 20;
99  
100 		gridText.verticalSpan = 1;
101 		gridText.horizontalSpan = 2;
102 		gridText.widthHint=200;	
103 		gridText.heightHint = 20;
104 
105 		gridAll.verticalSpan = 1;
106 		gridAll.horizontalSpan = 3;
107 		gridAll.heightHint = 20;
108 		
109 		parent.getShell().setLayout(layout);
110 		parent.getShell().setText(caption);
111 	}
112 	
113 	/**
114 	 * registers listeners to the text fields with the OK button as the parent.
115 	 * @param btnOK the ok button to use as parent.
116 	 */
117 	protected void registerTextFieldListeners(Button btnOK) {
118 		// add the selection listeners.
119 		for (int i=0; i<DEFAULTS.length; i++) {
120 			Listener listener = getListener(txts[i], i, btnOK);
121 			if (null != listener) {
122 				// add a listener
123 				txts[i].addListener(SWT.Modify, listener);
124 			}
125 		}
126 	}
127 	
128 	/**
129 	 * adds the text fields.
130 	 * @param parent the parent where to add.
131 	 */
132 	protected void addTextFields(Composite parent) {
133 		values = new String[DEFAULTS.length];
134 		txts = new Text[DEFAULTS.length];
135 		for (int i=0; i<FIELDS.length; i++) {
136 			Label label = new Label(parent, SWT.NONE);
137 			label.setText(FIELDS[i]);
138 			label.setLayoutData(gridLabel);
139 			
140 			txts[i] = new Text(parent, SWT.BORDER);
141 			txts[i].setText(DEFAULTS[i]);
142 			txts[i].setLayoutData(gridText);
143 		}
144 	}
145 	
146 	/**
147 	 * adds a Cancel button.
148 	 * @param parent the parent where to add.
149 	 */
150 	protected void addCancelButton(Composite parent) {		
151 		final Button btnCancel = new Button(parent, SWT.PUSH);
152 		btnCancel.setText("Cancel");
153 		btnCancel.setLayoutData(gridLabel);
154 		btnCancel.addSelectionListener(new SelectionAdapter() {
155 		      public void widgetSelected(SelectionEvent e) {
156 		    	  setReturnCode(Window.CANCEL);
157 		    	  close();
158 		      }
159 		    });
160 	}
161 	
162 	/**
163 	 * adds a OK button and installs the necessary listeners.
164 	 * @param parent the parent where to add.
165 	 */
166 	protected void addOKButton(Composite parent) {
167 		final Button btnOK = new Button(parent, SWT.PUSH);
168 		btnOK.setText("OK");
169 		btnOK.setLayoutData(gridLabel);
170 		
171 		btnOK.addSelectionListener(new SelectionAdapter() {
172 		      public void widgetSelected(SelectionEvent e) {
173 		    	  for (int i=0; i<DEFAULTS.length; i++) {
174 		    		  values[i] = txts[i].getText();
175 		    	  }
176 				
177 		    	  setReturnCode(Window.OK);
178 		    	  close();
179 		      }
180 		    });
181 		
182 		// add the selection listeners.
183 		for (int i=0; i<DEFAULTS.length; i++) {
184 			Listener listener = getListener(txts[i], i, btnOK);
185 			if (null != listener) {
186 				// add a listener
187 				txts[i].addListener(SWT.Modify, listener);
188 			}
189 		}
190 	}
191 	
192 	/**
193 	 * adds an invisible button to the grid to keep the alignment.
194 	 * @param parent the parent where to add.
195 	 */
196 	protected void addInvisibleButton(Composite parent) {
197 		// empty invisible button to make nice alignment
198 		final Button none = new Button(parent, SWT.NONE);
199 		none.setVisible(false);
200 		none.setLayoutData(gridLabel);
201 	}
202 	
203 	/**
204 	 * Create GUI elements in the dialog.
205 	 */
206 	protected Control createContents(Composite parent) {
207 		
208 		setLayout(parent);
209 		
210 		addTextFields(parent);
211 		addInvisibleButton(parent);
212 		addOKButton(parent);
213 		addCancelButton(parent);
214 
215 		parent.pack();	
216 		return parent;
217 	}
218 	
219 	/**
220 	 * this method allows the subclasses to put constraints via listeners 
221 	 * on the content of the value fields. you can use the offset to determine 
222 	 * the field.
223 	 * @param txt the field holding the changed text.
224 	 * @param offset the offset of the field. 
225 	 * @param ok the OK button.
226 	 * @return null if no constraint, otherwise the listener.
227 	 */
228 	public abstract Listener getListener(final Text txt, int offset, final Button ok);
229 }