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.dialogs.Dialog;
25  import org.eclipse.jface.window.Window;
26  import org.eclipse.swt.SWT;
27  import org.eclipse.swt.events.SelectionAdapter;
28  import org.eclipse.swt.events.SelectionEvent;
29  import org.eclipse.swt.graphics.Color;
30  import org.eclipse.swt.layout.GridData;
31  import org.eclipse.swt.layout.GridLayout;
32  import org.eclipse.swt.widgets.Button;
33  import org.eclipse.swt.widgets.Composite;
34  import org.eclipse.swt.widgets.Control;
35  import org.eclipse.swt.widgets.Label;
36  import org.eclipse.swt.widgets.Shell;
37  import org.eclipse.swt.widgets.Text;
38  import org.fosstrak.llrp.adaptor.AdaptorManagement;
39  import org.fosstrak.llrp.adaptor.ReaderMetaData;
40  
41  /**
42   * models a dialog to set the options for the message box view.
43   * @author sawielan
44   *
45   */
46  public class ReaderSettingsDialog extends Dialog {
47  	
48  	private String reader;
49  	private String adaptor;
50  
51  	/**
52  	 * create a new options dialog. 
53  	 * @param aShell Shell instance.
54  	 * @param adaptor a name of the adapter where the reader belongs to.
55  	 * @param reader the name of the reader to display the settings of.
56  	 */
57  	public ReaderSettingsDialog(Shell aShell, String adaptor, String reader) {
58  		super(aShell);
59  		this.adaptor = adaptor;
60  		this.reader = reader;
61  	}
62  		
63  	/**
64  	 * Create GUI elements in the dialog.
65  	 */
66  	protected Control createContents(Composite parent) {
67  	
68  		GridLayout layout = new GridLayout();
69  		layout.numColumns = 2;
70  		
71  		parent.getShell().setLayout(layout);
72  		parent.getShell().setText("Messagebox View Options");
73  		
74  		GridData gridAll = new GridData(GridData.FILL_BOTH);
75  		gridAll.verticalSpan = 1;
76  		gridAll.horizontalSpan = 2;
77  		
78  		final Button logKAMsg = new Button(parent, SWT.CHECK);
79  		logKAMsg.setText("Log Keep-Alive Messages");
80  		// we need to create a special grid data object for the check-box 
81  		// without width-hint as otherwise the check-box will not be displayed 
82  		// in *nix ...
83  		GridData gridDataKAMsg = new GridData();
84  		gridDataKAMsg.horizontalSpan = 2;
85  		logKAMsg.setLayoutData(gridDataKAMsg);
86  		
87  		try {
88  			logKAMsg.setSelection(
89  					AdaptorManagement.getInstance().getAdaptor(adaptor).
90  						getReader(reader).isReportKeepAlive());
91  		} catch (Exception e) {
92  			e.printStackTrace();
93  			logKAMsg.setSelection(false);
94  		} 
95  		
96  
97  		try {
98  			ReaderMetaData metaData = AdaptorManagement.getInstance().
99  				getAdaptor(adaptor).getReader(reader).getMetaData();
100 			
101 			final Text[] txts = new Text[14];
102 			final Label[] lbls = new Label[14];
103 			int i=0;
104 			
105 			lbls[i] = new Label(parent, SWT.NONE);
106 			lbls[i].setText("Reader Name:");
107 			txts[i] = new Text(parent, SWT.NONE);
108 			txts[i].setText(metaData.getReaderName());
109 			i++;
110 			
111 			lbls[i] = new Label(parent, SWT.NONE);
112 			lbls[i].setText("Reader Address:");
113 			txts[i] = new Text(parent, SWT.NONE);
114 			txts[i].setText(metaData.getReaderAddress());
115 			i++;
116 			
117 			lbls[i] = new Label(parent, SWT.NONE);
118 			lbls[i].setText("Port:");
119 			txts[i] = new Text(parent, SWT.NONE);
120 			txts[i].setText(String.format("%d", metaData.getPort()));
121 			i++;
122 			
123 			lbls[i] = new Label(parent, SWT.NONE);
124 			lbls[i].setText("Is Alive:");
125 			txts[i] = new Text(parent, SWT.NONE);
126 			txts[i].setText(String.format("%b", metaData.isAlive()));
127 			i++;
128 			
129 			lbls[i] = new Label(parent, SWT.NONE);
130 			lbls[i].setText("Connected to Reader:");
131 			txts[i] = new Text(parent, SWT.NONE);
132 			txts[i].setText(String.format("%b", metaData.isConnected()));
133 			i++;
134 			
135 			lbls[i] = new Label(parent, SWT.NONE);
136 			lbls[i].setText("Connect immediately:");
137 			txts[i] = new Text(parent, SWT.NONE);
138 			txts[i].setText(String.format("%b", metaData.isConnectImmediately()));
139 			i++;
140 			
141 			lbls[i] = new Label(parent, SWT.NONE);
142 			lbls[i].setText("Client initiated:");
143 			txts[i] = new Text(parent, SWT.NONE);
144 			txts[i].setText(String.format("%b", metaData.isClientInitiated()));
145 			i++;
146 			
147 			lbls[i] = new Label(parent, SWT.NONE);
148 			lbls[i].setText("Report Keepalive:");
149 			txts[i] = new Text(parent, SWT.NONE);
150 			txts[i].setText(String.format("%b", metaData.isReportKeepAlive()));
151 			i++;
152 			
153 			lbls[i] = new Label(parent, SWT.NONE);
154 			lbls[i].setText("Packages sent:");
155 			txts[i] = new Text(parent, SWT.NONE);
156 			txts[i].setText(String.format("%d", metaData.getPackagesSent()));
157 			i++;
158 			
159 			lbls[i] = new Label(parent, SWT.NONE);
160 			lbls[i].setText("Packages received:");
161 			txts[i] = new Text(parent, SWT.NONE);
162 			txts[i].setText(String.format("%d", metaData.getPackagesReceived()));
163 			i++;
164 			
165 			lbls[i] = new Label(parent, SWT.NONE);
166 			lbls[i].setText("Packages Current Session sent:");
167 			txts[i] = new Text(parent, SWT.NONE);
168 			txts[i].setText(String.format("%d", metaData.getPackagesCurrentSessionSent()));
169 			i++;
170 			
171 			lbls[i] = new Label(parent, SWT.NONE);
172 			lbls[i].setText("Packages Current Session received:");
173 			txts[i] = new Text(parent, SWT.NONE);
174 			txts[i].setText(String.format("%d", metaData.getPackagesCurrentSessionReceived()));
175 			i++;
176 			
177 			lbls[i] = new Label(parent, SWT.NONE);
178 			lbls[i].setText("Allowed Keepalive misses:");
179 			txts[i] = new Text(parent, SWT.NONE);
180 			txts[i].setText(String.format("%d", metaData.getAllowNKeepAliveMisses()));
181 			i++;
182 			
183 			lbls[i] = new Label(parent, SWT.NONE);
184 			lbls[i].setText("Keepalive Period:");
185 			txts[i] = new Text(parent, SWT.NONE);
186 			txts[i].setText(String.format("%d", metaData.getKeepAlivePeriod()));
187 			
188 			if (null != txts) {
189 				Color color = getShell().getDisplay().getSystemColor(
190 						SWT.COLOR_WHITE);
191 				for (int j=0; j<txts.length; j++) {
192 					if (null != txts[j]) {
193 						txts[j].setEditable(false);
194 						txts[j].setFont(parent.getFont());
195 						txts[j].setBackground(color);
196 					}
197 				}
198 			}
199 			if (null != lbls) {
200 				for (int j=0; j<lbls.length; j++) {
201 					if (null != lbls[j]) {
202 						lbls[j].setFont(parent.getFont());
203 					}
204 				}
205 			}
206 		} catch (Exception ex) {
207 			ex.printStackTrace();
208 		}
209 
210 		Composite buttonGroup = new Composite(parent, SWT.NONE);
211 		buttonGroup.setFont(parent.getFont());
212 		buttonGroup.setLayout(layout);
213 
214 		final Button btnOK = new Button(buttonGroup, SWT.PUSH);
215 		btnOK.setText("OK");
216 		// set the keyboard focus
217 		btnOK.setFocus();
218 		btnOK.setFont(parent.getFont());
219 		GridData gd = new GridData();
220 		gd.widthHint = 75;
221 		btnOK.setLayoutData(gd);
222 		
223 		btnOK.addSelectionListener(new SelectionAdapter() {
224 		      public void widgetSelected(SelectionEvent e) {
225 	    			try {
226 	    				AdaptorManagement.getInstance().getAdaptor(adaptor).
227 	    					getReader(reader).setReportKeepAlive(logKAMsg.getSelection());
228 	    			} catch (Exception e1) {
229 	    				e1.printStackTrace();
230 	    				logKAMsg.setSelection(false);
231 	    			}
232 					setReturnCode(Window.OK);
233 					close();
234 		      }
235 		    });
236 
237 		
238 		final Button btnCancel = new Button(buttonGroup, SWT.PUSH);
239 		btnCancel.setText("Cancel");
240 		btnCancel.addSelectionListener(new SelectionAdapter() {
241 		      public void widgetSelected(SelectionEvent e) {
242 		    	  setReturnCode(Window.CANCEL);
243 		    	  close();
244 		      }
245 		    });
246 		btnCancel.setFont(parent.getFont());
247 		btnCancel.setLayoutData(gd);
248 		
249 		parent.pack();
250 		return parent;
251 	}
252 }