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  
37  /**
38   * dialog to add a new adaptor to the reader explorer.
39   * @author sawielan
40   *
41   */
42  public class AddFCDialog extends ConnectDialog {
43  	
44  	/** the index for the adaptor name in the values array. */
45  	private static final int VALUE_NAME = 0;
46  	
47  	/** the index for the adaptor ip in the values array. */
48  	private static final int VALUE_IP = 1;
49  	
50  	/** handle to the local/remote button. */
51  	protected Button localAdapter;
52  	
53  	/** flag, whether local or remote adapter. */
54  	private boolean isLocalAdapter = false;
55  
56  	/**
57  	 * create a new add adaptor dialog.
58  	 * @param aShell the parent shell.
59  	 */
60  	public AddFCDialog(Shell aShell) {
61  		super(aShell, "Add Adapter");
62  		FIELDS = new String[] { "Adapter Name", "IP" };
63  		DEFAULTS = new String [] { "AdapterName", "127.0.0.1" };
64  	}
65  
66  	/**
67  	 * @return Logical Name of connection resource
68  	 */
69  	public String getName() {
70  		return values[VALUE_NAME];
71  	}
72  	
73  	/**
74  	 * if set to true, the adapter management will create a local adapter and 
75  	 * therefore ignoring the IP-address provided.
76  	 * @return true if user requests a local adapter, false otherwise. 
77  	 */
78  	public boolean isLocalAdapter() {
79  		return isLocalAdapter;
80  	}
81  	
82  	/**
83  	 * @return IP Address of connection resource
84  	 */
85  	public String getIP() {
86  		return values[VALUE_IP];
87  	}
88  	
89  	@Override
90  	protected Control createContents(Composite parent) {
91  		setLayout(parent);
92  		
93  		addTextFields(parent);
94  		
95  		// we need to create a special grid data object for the check-box 
96  		// without width-hint as otherwise the check-box will not be displayed 
97  		// in *nix ...
98  		GridData gridNoWidthHint = new GridData();
99  		gridNoWidthHint.horizontalSpan = 3;
100 		
101 		localAdapter = new Button(parent, SWT.CHECK);
102 		localAdapter.setText("local Adapter");
103 		localAdapter.setLayoutData(gridNoWidthHint);
104 		localAdapter.setSelection(false);
105 		
106 		addInvisibleButton(parent);
107 		addOKButton(parent);
108 		addCancelButton(parent);
109 
110 		parent.pack();
111 		return parent;
112 	}
113 	
114 	@Override
115 	protected void addOKButton(Composite parent) {
116 		final Button btnOK = new Button(parent, SWT.PUSH);
117 		btnOK.setText("OK");
118 		btnOK.setLayoutData(gridLabel);
119 		
120 		btnOK.addSelectionListener(new SelectionAdapter() {
121 		      public void widgetSelected(SelectionEvent e) {
122 		    	  for (int i=0; i<DEFAULTS.length; i++) {
123 		    		  values[i] = txts[i].getText();
124 		    	  }
125 		    	  
126 		    	  isLocalAdapter = localAdapter.getSelection();
127 		    	  
128 		    	  setReturnCode(Window.OK);
129 		    	  close();
130 		      }
131 		    });
132 		
133 		
134 		registerTextFieldListeners(btnOK);
135 	}
136 
137 	@Override
138 	public Listener getListener(final Text txt, int offset, final Button ok) {
139 		Listener listener = null;
140 		switch (offset) {
141 		case VALUE_NAME:
142 			listener = new Listener() {
143 				public void handleEvent(Event event) {
144 					try {
145 						if ((txt.getText() == null) || (txt.getText().length() < 3)) {
146 							ok.setEnabled(false);
147 						} else {
148 							ok.setEnabled(true);
149 						}
150 					} catch (Exception e) {
151 						ok.setEnabled(false);
152 					}
153 				}
154 			};
155 			break;
156 		case VALUE_IP:
157 			// we don't care about the IP format (hope that user does it right).
158 			break;
159 		}
160 		return listener;
161 	}
162 }
163