AddServer.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.ui.actions;
022 
023 import static org.apache.qpid.management.ui.Constants.*;
024 
025 import org.apache.qpid.management.ui.ApplicationRegistry;
026 import org.apache.qpid.management.ui.exceptions.InfoRequiredException;
027 import org.apache.qpid.management.ui.views.NumberVerifyListener;
028 import org.apache.qpid.management.ui.views.ViewUtility;
029 import org.eclipse.jface.action.IAction;
030 import org.eclipse.swt.SWT;
031 import org.eclipse.swt.events.KeyAdapter;
032 import org.eclipse.swt.events.KeyEvent;
033 import org.eclipse.swt.events.SelectionAdapter;
034 import org.eclipse.swt.events.SelectionEvent;
035 import org.eclipse.swt.layout.GridData;
036 import org.eclipse.swt.layout.GridLayout;
037 import org.eclipse.swt.widgets.Button;
038 import org.eclipse.swt.widgets.Combo;
039 import org.eclipse.swt.widgets.Composite;
040 import org.eclipse.swt.widgets.Control;
041 import org.eclipse.swt.widgets.Display;
042 import org.eclipse.swt.widgets.Label;
043 import org.eclipse.swt.widgets.Shell;
044 import org.eclipse.swt.widgets.Text;
045 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
046 
047 public class AddServer extends AbstractAction implements IWorkbenchWindowActionDelegate 
048 {
049     private static final String[] _domains ={"org.apache.qpid"};
050     
051     private String _transport = DEFAULT_PROTOCOL;
052     private String _host;
053     private String _port;
054     private String _domain;
055     private String _user;
056     private String _password;
057     
058     private boolean _addServer;
059   
060     public AddServer()
061     {
062         
063     }
064     
065     public void run(IAction action)
066     {
067         if(_window == null)
068             return;
069         
070         reset();
071         createAddServerPopup();
072         try
073         {
074             if (_addServer)
075             {
076                 getNavigationView().addNewServer(_transport, _host, Integer.parseInt(_port), _domain, _user, _password);
077             }
078         }
079         catch(InfoRequiredException ex)
080         {
081             ViewUtility.popupInfoMessage(ACTION_ADDSERVER, ex.getMessage());
082         }
083         catch (Exception ex)
084         {
085             handleException(ex, null, null);
086         }
087     }
088     
089     private void reset()
090     {
091         _addServer = false;
092         _host = null;
093         _port = null;
094         _domain = null;
095         _user = null;
096         _password = null;
097     }
098     
099     /**
100      * Creates the shell and then opens the popup where user can enter new connection details.
101      * Connects to the new server and adds the server in the navigation page.
102      * Pops up any error occured in connecting to the new server
103      */
104     private void createAddServerPopup()
105     {
106         Display display = Display.getCurrent();
107         final Shell shell = new Shell(display, SWT.BORDER | SWT.CLOSE);
108         shell.setText(ACTION_ADDSERVER);
109         shell.setImage(ApplicationRegistry.getImage(CONSOLE_IMAGE));
110         shell.setLayout(new GridLayout());
111         
112         createWidgets(shell);
113         shell.pack();
114         
115         //get current size dialog, and screen size
116         int displayWidth = display.getBounds().width;
117         int displayHeight = display.getBounds().height;        
118         int currentShellWidth = shell.getSize().x;
119         int currentShellHeight = shell.getSize().y;
120         
121         //default sizes for the dialog
122         int minShellWidth = 425;
123         int minShellHeight= 290;        
124         //ensure this is large enough, increase it if its not
125         int newShellWidth =  currentShellWidth > minShellWidth ? currentShellWidth : minShellWidth;
126         int newShellHeight = currentShellHeight > minShellHeight ? currentShellHeight : minShellHeight;
127         
128         //set the final size and centre the dialog
129         shell.setBounds((displayWidth - newShellWidth)/(displayHeight - newShellHeight)/2, newShellWidth, newShellHeight);
130         
131         shell.open();
132         _window.getShell().setEnabled(false);
133         
134         while (!shell.isDisposed())
135         {   
136             if (!display.readAndDispatch())
137             {
138                 display.sleep();
139             }
140         }
141         
142         // enable the main shell
143         _window.getShell().setEnabled(true);
144         _window.getShell().open();
145     }
146     
147     // Creates SWT widgets for the user to add server connection details.
148     // Adds listeners to the widgets to take appropriate action
149     private void createWidgets(final Shell shell)
150     {
151         Composite composite = new Composite(shell, SWT.NONE);
152         composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
153         GridLayout layout = new GridLayout(2false);
154         layout.horizontalSpacing = 10;
155         layout.verticalSpacing = 10;
156         layout.marginHeight = 20;
157         layout.marginWidth = 20;
158         composite.setLayout(layout);
159         
160         /* Commenting this, as there is only one protocol at the moment.
161          * This can be uncommented and enhanced, if more protocols are added in future
162         Label name = new Label(composite, SWT.NONE);
163         name.setText("Connection Type");
164         GridData layoutData = new GridData(SWT.TRAIL, SWT.TOP, false, false);
165         name.setLayoutData(layoutData);
166         
167         final Combo comboTransport = new Combo(composite, SWT.READ_ONLY);
168         comboTransport.setItems(CONNECTION_PROTOCOLS);
169         comboTransport.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
170         comboTransport.select(0);
171         */
172         
173         Label host = new Label(composite, SWT.NONE);
174         host.setText("Host");
175         host.setLayoutData(new GridData(SWT.TRAIL, SWT.TOP, false, false));
176         
177         final Text textHost = new Text(composite, SWT.BORDER);
178         textHost.setText("");
179         textHost.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
180         textHost.setFocus();
181         
182         Label port = new Label(composite, SWT.NONE);
183         port.setText("Port");
184         port.setLayoutData(new GridData(SWT.TRAIL, SWT.TOP, false, false));
185         
186         final Text textPort = new Text(composite, SWT.BORDER);
187         textPort.setText("");
188         textPort.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
189         // Verify if the value entered is numeric
190         textPort.addVerifyListener(new NumberVerifyListener());
191         
192         
193         Label domain = new Label(composite, SWT.NONE);
194         domain.setText("Domain");
195         domain.setLayoutData(new GridData(SWT.TRAIL, SWT.TOP, false, false));
196         
197         final Combo comboDomain = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
198         comboDomain.setItems(_domains);
199         comboDomain.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
200         comboDomain.select(0);
201         
202         
203         Label user = new Label(composite, SWT.NONE);
204         user.setText(USERNAME);
205         user.setLayoutData(new GridData(SWT.TRAIL, SWT.TOP, false, false));
206         
207         final Text textUser = new Text(composite, SWT.BORDER);
208         textUser.setText("");
209         textUser.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
210         
211         Label password = new Label(composite, SWT.NONE);
212         password.setText(PASSWORD);
213         password.setLayoutData(new GridData(SWT.TRAIL, SWT.TOP, false, false));
214         
215         final Text textPwd = new Text(composite, SWT.BORDER | SWT.SINGLE | SWT.PASSWORD);
216         textPwd.setText("");
217         //textPwd.setEchoChar('*');
218         textPwd.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
219         
220         //Get the text widgets
221         Control[] widgets = composite.getChildren();
222         for (int i=0; i < widgets.length; i++)
223         {
224             widgets[i].addKeyListener(new KeyAdapter()
225             {
226                 public void keyPressed(KeyEvent event)
227                 {
228                     if (event.character == SWT.ESC)
229                     {
230                       //Escape key acts as cancel on all widgets
231                         shell.close();
232                     }
233                 }
234             });
235         }
236         
237         Composite buttonsComposite  = new Composite(composite, SWT.NONE);
238         buttonsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 21));
239         buttonsComposite.setLayout(new GridLayout(2true));
240         
241         final Button connectButton = new Button(buttonsComposite, SWT.PUSH | SWT.CENTER);       
242         connectButton.setText(BUTTON_CONNECT);
243         GridData gridData = new GridData (SWT.TRAIL, SWT.BOTTOM, true, true);
244         gridData.widthHint = 100;
245         connectButton.setLayoutData(gridData);
246         connectButton.setFont(ApplicationRegistry.getFont(FONT_BUTTON));
247         connectButton.addSelectionListener(new SelectionAdapter(){
248             public void widgetSelected(SelectionEvent event)
249             {                
250                 _host = textHost.getText();               
251                 if ((_host == null|| (_host.trim().length() == 0))
252                 {
253                     ViewUtility.popupInfoMessage(ACTION_ADDSERVER, INFO_HOST_ADDRESS);
254                     textHost.setText("");
255                     textHost.setFocus();
256                     return;
257                 }
258                 
259                 _port = textPort.getText();
260                 if ((_port == null|| (_port.trim().length() == 0))
261                 {
262                     ViewUtility.popupInfoMessage(ACTION_ADDSERVER, INFO_HOST_PORT);
263                     textPort.setText("");
264                     textPort.setFocus();
265                     return;
266                 }
267                 
268                 _user = textUser.getText();
269                 if ((_user == null|| (_user.trim().length() == 0))
270                 {
271                     ViewUtility.popupInfoMessage(ACTION_ADDSERVER, INFO_USERNAME);
272                     textUser.setText("");
273                     textUser.setFocus();
274                     return;
275                 }
276                 
277                 _password = textPwd.getText();
278                 if (_password == null)
279                 {
280                     ViewUtility.popupInfoMessage(ACTION_ADDSERVER, INFO_PASSWORD);
281                     textPwd.setText("");
282                     textPwd.setFocus();
283                     return;
284                 }
285                 
286                 _domain = comboDomain.getText();
287                 _addServer = true;
288                 shell.dispose();                                     
289             }
290         });
291         
292         final Button cancelButton = new Button(buttonsComposite, SWT.PUSH);
293         cancelButton.setText(BUTTON_CANCEL);
294         gridData = new GridData (SWT.LEAD, SWT.BOTTOM, true, true);
295         gridData.widthHint = 100;
296         cancelButton.setLayoutData(gridData);
297         cancelButton.setFont(ApplicationRegistry.getFont(FONT_BUTTON));
298         cancelButton.addSelectionListener(new SelectionAdapter()
299         {
300             public void widgetSelected(SelectionEvent event)
301             {
302                 shell.dispose();
303             }
304         });
305         
306         //Get the ok/cancel button widgets and add a new key listener
307         widgets = buttonsComposite.getChildren();
308         for (int i=0; i < widgets.length; i++)
309         {
310             widgets[i].addKeyListener(new KeyAdapter()
311             {
312                 public void keyPressed(KeyEvent event)
313                 {
314                     if (event.character == SWT.ESC)
315                     {
316                         //Escape key acts as cancel on all widgets
317                         shell.close();
318                     }
319                 }
320             });
321         }
322         
323         shell.setDefaultButton(connectButton);
324     }
325 
326 }