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.widgets.Button;
30  import org.eclipse.swt.widgets.Composite;
31  import org.eclipse.swt.widgets.Control;
32  import org.eclipse.swt.widgets.Event;
33  import org.eclipse.swt.widgets.Listener;
34  import org.eclipse.swt.widgets.Shell;
35  import org.eclipse.swt.widgets.Text;
36  import org.fosstrak.llrp.adaptor.AdaptorManagement;
37  
38  /**
39   * dialog to add a new reader to the reader explorer.
40   * @author sawielan
41   *
42   */
43  public class AddReaderDialog extends ConnectDialog {
44  	
45  	/** the index for the reader name in the values array. */
46  	private static final int VALUE_READER_NAME = 0;
47  	
48  	/** the index for the reader ip in the values array. */
49  	private static final int VALUE_READER_IP = 1;
50  	
51  	/** the index for the reader port in the values array. */
52  	private static final int VALUE_READER_PORT = 2;
53  	
54  	/** increasing number added automatically to the reader name. */
55  	private static long num = 0;
56  	
57  	/** flag whether client initiated or not. */
58  	private boolean ci = true;
59  	
60  	/** handle to the client initiated connection button. */
61  	protected Button cICon;
62  	
63  	/** flag whether connect immediately or not. */
64  	private boolean connectImmediate = true;
65  	
66  	/** handle to the connect immediately button. */
67  	protected Button conImmed;
68  	
69  	/**
70  	 * create a new add reader dialog.
71  	 * @param aShell the parent shell.
72  	 */
73  	public AddReaderDialog(Shell aShell) {
74  		super(aShell, "Add Local Reader");
75  		FIELDS = new String[] { "Reader Name", "IP", "Port" };
76  		
77  		// make sure, we propose a unique reader name
78  		String readerName = String.format("ReaderName%d", num++);
79  		try {
80  			while (AdaptorManagement.getInstance().getAdaptor(
81  					AdaptorManagement.DEFAULT_ADAPTOR_NAME).containsReader(
82  							readerName)) {
83  				
84  				readerName = String.format("ReaderName%d", num++);
85  			}
86  		} catch (Exception e) {
87  			readerName = String.format("ReaderName%d", 
88  					System.currentTimeMillis());
89  		}
90  		DEFAULTS = new String [] { 	readerName, "127.0.0.1", "5084" };
91  	}
92  	
93  	@Override
94  	protected void addOKButton(Composite parent) {
95  		final Button btnOK = new Button(parent, SWT.PUSH);
96  		btnOK.setText("OK");
97  		btnOK.setLayoutData(gridLabel);
98  		
99  		btnOK.addSelectionListener(new SelectionAdapter() {
100 		      public void widgetSelected(SelectionEvent e) {
101 		    	  for (int i=0; i<DEFAULTS.length; i++) {
102 		    		  values[i] = txts[i].getText();
103 		    	  }
104 				
105 		    	  ci = cICon.getSelection();
106 		    	  connectImmediate = conImmed.getSelection();
107 		    	  
108 		    	  setReturnCode(Window.OK);
109 		    	  close();
110 		      }
111 		    });
112 		
113 		registerTextFieldListeners(btnOK);
114 	}
115 	
116 	/**
117 	 * Create GUI elements in the dialog.
118 	 */
119 	protected Control createContents(Composite parent) {
120 		setLayout(parent);
121 		
122 		addTextFields(parent);
123 		
124 		// we need to create a special grid data object for the check-box 
125 		// without width-hint as otherwise the check-box will not be displayed 
126 		// in *nix ...
127 		GridData gridNoWidthHint = new GridData();
128 		gridNoWidthHint.horizontalSpan = 3;
129 		
130 		conImmed = new Button(parent, SWT.CHECK);
131 		conImmed.setText("Connect immediately");
132 		conImmed.setLayoutData(gridNoWidthHint);
133 		conImmed.setSelection(true);
134 		
135 		cICon = new Button(parent, SWT.CHECK);
136 		cICon.setText("Connector mode (initiate connection to the reader)");
137 		cICon.setLayoutData(gridNoWidthHint);
138 		cICon.setSelection(true);
139 		
140 		addInvisibleButton(parent);
141 		addOKButton(parent);
142 		addCancelButton(parent);
143 
144 		parent.pack();
145 		return parent;
146 	}
147 	
148 	/**
149 	 * @return Logical Name of connection resource
150 	 */
151 	public String getName() {
152 		return values[VALUE_READER_NAME];
153 	}
154 
155 	/**
156 	 * @return IP Address of connection resource
157 	 */
158 	public String getIP() {
159 		return values[VALUE_READER_IP];
160 	}
161 
162 	/**
163 	 * @return IP Port of connection resource
164 	 */
165 	public int getPort() {
166 		return Integer.parseInt(values[VALUE_READER_PORT]);
167 	}
168 	
169 	/**
170 	 * @return true if client initiates connection, false otherwise.
171 	 */
172 	public boolean isClientInitiated() {
173 		return ci;
174 	}
175 	
176 	/**
177 	 * @return true if connect immediately, false otherwise.
178 	 */
179 	public boolean isConnectImmediately() {
180 		return connectImmediate;
181 	}
182 	
183 	@Override
184 	public Listener getListener(final Text txt, int offset, final Button ok) {
185 		Listener listener = null;
186 		switch (offset) {
187 		case VALUE_READER_NAME:
188 			listener = new Listener() {
189 				public void handleEvent(Event event) {
190 					try {
191 						// do not allow:
192 						// - empty name
193 						// - name shorter than 3
194 						// - name that is already contained
195 						if ((txt.getText() == null) || (txt.getText().length() < 3) || 
196 								(AdaptorManagement.getInstance().
197 										getAdaptor(AdaptorManagement.DEFAULT_ADAPTOR_NAME).
198 											containsReader(txt.getText()))) {
199 							ok.setEnabled(false);
200 							
201 						} else {
202 							ok.setEnabled(true);
203 						}
204 					} catch (Exception e) {
205 						ok.setEnabled(false);
206 					}
207 				}
208 			};
209 			break;
210 		case VALUE_READER_IP:
211 			// we don't care about the IP format (hope that user does it right).
212 			break;		
213 		case VALUE_READER_PORT:
214 			listener = new Listener() {
215 				public void handleEvent(Event event) {
216 					try {
217 						// try to parse the port.
218 						final int port = Integer.parseInt(txt.getText());
219 						ok.setEnabled(true);
220 					} catch (Exception e) {
221 						ok.setEnabled(false);
222 					}
223 				}
224 			};
225 			break;
226 		}
227 
228 		return listener;
229 	}
230 }