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.wizards;
23  
24  import org.eclipse.core.resources.IContainer;
25  import org.eclipse.core.resources.IFile;
26  import org.eclipse.core.resources.IResource;
27  import org.eclipse.core.resources.ResourcesPlugin;
28  import org.eclipse.core.runtime.Path;
29  import org.eclipse.jface.viewers.ISelection;
30  import org.eclipse.jface.viewers.IStructuredSelection;
31  import org.eclipse.jface.wizard.WizardPage;
32  import org.eclipse.swt.SWT;
33  import org.eclipse.swt.events.ModifyEvent;
34  import org.eclipse.swt.events.ModifyListener;
35  import org.eclipse.swt.events.SelectionAdapter;
36  import org.eclipse.swt.events.SelectionEvent;
37  import org.eclipse.swt.events.SelectionListener;
38  import org.eclipse.swt.layout.GridData;
39  import org.eclipse.swt.layout.GridLayout;
40  import org.eclipse.swt.widgets.Button;
41  import org.eclipse.swt.widgets.Combo;
42  import org.eclipse.swt.widgets.Composite;
43  import org.eclipse.swt.widgets.Label;
44  import org.eclipse.swt.widgets.Text;
45  import org.eclipse.ui.dialogs.ContainerSelectionDialog;
46  
47  /**
48   * Wizard Page that lets the user enter the folder, the file name and the 
49   * message type of the new LLRP message.
50   *
51   * @author Ulrich Etter, ETHZ
52   *
53   */
54  public class NewLLRPMessageWizardPage extends WizardPage {
55  	
56  	private final String WIZARD_PAGE_TITLE = "LLRP Message";
57  	private final String WIZARD_PAGE_DESCRIPTION = "Create a new LLRP message.";
58  	private final String FILE_EXTENSION = "llrp";
59  	
60  	private final String DEFAULT_MESSAGE_TYPE = "ADD_ROSPEC";
61  	
62  	private final String[] MESSAGE_TYPES = {
63  			"GET_READER_CAPABILITIES",
64  			"GET_READER_CONFIG",
65  			"SET_READER_CONFIG",
66  			
67  			"GET_ROSPECS",
68  			"ADD_ROSPEC",
69  			"ENABLE_ROSPEC",
70  			"DISABLE_ROSPEC",
71  			"START_ROSPEC",
72  			"STOP_ROSPEC",
73  			"DELETE_ROSPEC",
74  			
75  			"GET_ACCESSSPECS",
76  			"ADD_ACCESSSPEC",
77  			"ENABLE_ACCESSSPEC",
78  			"DISABLE_ACCESSSPEC",
79  			"DELETE_ACCESSSPEC",
80  			
81  			"GET_REPORT",
82  			"ENABLE_EVENTS_AND_REPORTS",
83  			"CLOSE_CONNECTION",
84  			"CUSTOM_MESSAGE",
85  	};
86  	
87  	private Text folderText;
88  	private Text fileText;
89  	private Combo messageTypeCombo;
90  
91  	private ISelection selection;
92  
93  	/**
94  	 * Constructor for NewLLRPMessageWizardPage.
95  	 * 
96  	 * @param selection a selection page.
97  	 */
98  	public NewLLRPMessageWizardPage(ISelection selection) {
99  		super("wizardPage");
100 		setTitle(WIZARD_PAGE_TITLE);
101 		setDescription(WIZARD_PAGE_DESCRIPTION);
102 		this.selection = selection;
103 	}
104 
105 	/**
106 	 * @see IDialogPage#createControl(Composite)
107 	 */
108 	public void createControl(Composite parent) {
109 		Composite container = new Composite(parent, SWT.NULL);
110 		GridLayout layout = new GridLayout();
111 		container.setLayout(layout);
112 		layout.numColumns = 3;
113 		layout.verticalSpacing = 9;
114 		
115 		Label label = new Label(container, SWT.NULL);
116 		label.setText("&Folder:");
117 
118 		folderText = new Text(container, SWT.BORDER | SWT.SINGLE);
119 		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
120 		folderText.setLayoutData(gd);
121 		folderText.addModifyListener(new ModifyListener() {
122 			public void modifyText(ModifyEvent e) {
123 				dialogChanged();
124 			}
125 		});
126 
127 		Button button = new Button(container, SWT.PUSH);
128 		button.setText("Browse...");
129 		button.addSelectionListener(new SelectionAdapter() {
130 			public void widgetSelected(SelectionEvent e) {
131 				handleBrowse();
132 			}
133 		});
134 		
135 		label = new Label(container, SWT.NULL);
136 		label.setText("&File name:");
137 
138 		fileText = new Text(container, SWT.BORDER | SWT.SINGLE);
139 		gd = new GridData(GridData.FILL_HORIZONTAL);
140 		fileText.setLayoutData(gd);
141 		fileText.addModifyListener(new ModifyListener() {
142 			public void modifyText(ModifyEvent e) {
143 				dialogChanged();
144 			}
145 		});
146 		
147 		label = new Label(container, SWT.NULL);
148 		label.setText("." + FILE_EXTENSION);
149 		
150 		
151 		label = new Label(container, SWT.NULL);
152 		label.setText("&Message type:");
153 		
154 		messageTypeCombo = new Combo(container, SWT.READ_ONLY);
155 		for (int i = 0; i < MESSAGE_TYPES.length; i++){
156 			messageTypeCombo.add(MESSAGE_TYPES[i]);
157 		}
158 		gd = new GridData(GridData.FILL_HORIZONTAL);
159 		messageTypeCombo.setLayoutData(gd);
160 		messageTypeCombo.addSelectionListener(new SelectionListener(){
161 
162 			public void widgetDefaultSelected(SelectionEvent e) {
163 				widgetSelected(e);
164 			}
165 
166 			public void widgetSelected(SelectionEvent e) {
167 				fileText.setText(messageTypeCombo.getText().toLowerCase());
168 			}
169 			
170 		});
171 		
172 		initialize();
173 		dialogChanged();
174 		setControl(container);
175 	}
176 
177 	/**
178 	 * Initializes the input fields.
179 	 */
180 	private void initialize() {
181 		if (selection != null && selection.isEmpty() == false
182 				&& selection instanceof IStructuredSelection) {
183 			IStructuredSelection ssel = (IStructuredSelection) selection;
184 			if (ssel.size() == 1){
185 				Object obj = ssel.getFirstElement();
186 				if (obj instanceof IResource) {
187 					IContainer container;
188 					if (obj instanceof IContainer)
189 						container = (IContainer) obj;
190 					else
191 						container = ((IResource) obj).getParent();
192 					folderText.setText(container.getFullPath().toString());
193 				}
194 			}
195 		}
196 		fileText.setText(DEFAULT_MESSAGE_TYPE.toLowerCase());
197 		messageTypeCombo.setText(DEFAULT_MESSAGE_TYPE);
198 	}
199 
200 	/**
201 	 * Uses the standard container selection dialog to choose the new value for
202 	 * the folder field.
203 	 */
204 	private void handleBrowse() {
205 		ContainerSelectionDialog dialog = new ContainerSelectionDialog(
206 				getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
207 				"Please select a folder.");
208 		if (dialog.open() == ContainerSelectionDialog.OK) {
209 			Object[] result = dialog.getResult();
210 			if (result.length == 1) {
211 				folderText.setText(((Path) result[0]).toString());
212 			}
213 		}
214 	}
215 
216 	/**
217 	 * Checks whether the input is valid. If not, an error message is shown.
218 	 */
219 	private void dialogChanged() {
220 		IContainer container = (IContainer) ResourcesPlugin.getWorkspace().getRoot()
221 				.findMember(new Path(getFolderName()));
222 		String fileName = getFileName();
223 
224 		if (getFolderName().length() == 0) {
225 			updateStatus("Folder must be specified.");
226 			return;
227 		}
228 		if (container == null
229 				|| (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
230 			updateStatus("Folder must exist.");
231 			return;
232 		}
233 		if (!container.isAccessible()) {
234 			updateStatus("Project must be writable.");
235 			return;
236 		}
237 		if (fileName.length() == 0) {
238 			updateStatus("File name must be specified.");
239 			return;
240 		}
241 		if (fileName.trim().equals("." + FILE_EXTENSION)) {
242 			updateStatus("File name must not be empty.");
243 			return;
244 		}
245 		IFile file = container.getFile(new Path(fileName));
246 		if (file.exists()){
247 			updateStatus("File '" + fileName + "' does already exist.");
248 			return;
249 		}
250 		updateStatus(null);
251 	}
252 
253 	private void updateStatus(String message) {
254 		setErrorMessage(message);
255 		setPageComplete(message == null);
256 	}
257 
258 	public String getFolderName() {
259 		return folderText.getText();
260 	}
261 
262 	public String getFileName() {
263 		return fileText.getText() + "." + FILE_EXTENSION;
264 	}
265 	
266 	public String getLLRPMessageType() {
267 		return messageTypeCombo.getText();
268 	}
269 }