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.ACTION_LOGIN;
024 import static org.apache.qpid.management.ui.Constants.CONSOLE_IMAGE;
025 import static org.apache.qpid.management.ui.Constants.INFO_PASSWORD;
026 import static org.apache.qpid.management.ui.Constants.INFO_USERNAME;
027 import static org.apache.qpid.management.ui.Constants.PASSWORD;
028 import static org.apache.qpid.management.ui.Constants.USERNAME;
029
030 import org.apache.qpid.management.ui.ApplicationRegistry;
031 import org.apache.qpid.management.ui.Constants;
032 import org.apache.qpid.management.ui.exceptions.InfoRequiredException;
033 import org.apache.qpid.management.ui.views.TreeObject;
034 import org.apache.qpid.management.ui.views.ViewUtility;
035 import org.eclipse.jface.action.IAction;
036 import org.eclipse.swt.SWT;
037 import org.eclipse.swt.events.KeyAdapter;
038 import org.eclipse.swt.events.KeyEvent;
039 import org.eclipse.swt.events.SelectionAdapter;
040 import org.eclipse.swt.events.SelectionEvent;
041 import org.eclipse.swt.layout.GridData;
042 import org.eclipse.swt.layout.GridLayout;
043 import org.eclipse.swt.widgets.Button;
044 import org.eclipse.swt.widgets.Composite;
045 import org.eclipse.swt.widgets.Control;
046 import org.eclipse.swt.widgets.Display;
047 import org.eclipse.swt.widgets.Label;
048 import org.eclipse.swt.widgets.Shell;
049 import org.eclipse.swt.widgets.Text;
050 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
051
052 public class ReconnectServer extends AbstractAction implements IWorkbenchWindowActionDelegate
053 {
054 private String _title;
055 private String _serverName;
056 private String _user;
057 private String _password;
058 private boolean _connect;
059
060 public void run(IAction action)
061 {
062 if(_window == null)
063 return;
064
065 try
066 {
067 reset();
068 // Check if a server node is selected to be reconnected.
069 TreeObject serverNode = getNavigationView().getSelectedServerNode();
070 _serverName = serverNode.getName();
071 _title = ACTION_LOGIN + " (" + _serverName + ")";
072
073 // Get the login details(username/password)
074 createLoginPopup();
075
076 if (_connect)
077 {
078 // Connect the server
079 getNavigationView().reconnect(_user, _password);
080 }
081 }
082 catch(InfoRequiredException ex)
083 {
084 ViewUtility.popupInfoMessage("Reconnect Qpid server", ex.getMessage());
085 }
086 catch (Exception ex)
087 {
088 handleException(ex, null, null);
089 }
090 }
091
092 private void reset()
093 {
094 _connect = false;
095 _user = null;
096 _password = null;
097 }
098
099 // Create the login popup fot th user to enter usernaem and password
100 private void createLoginPopup()
101 {
102 Display display = Display.getCurrent();
103 final Shell shell = new Shell(display, SWT.BORDER | SWT.CLOSE);
104 shell.setText(_title);
105 shell.setImage(ApplicationRegistry.getImage(CONSOLE_IMAGE));
106 shell.setLayout(new GridLayout());
107
108 createWidgets(shell);
109 shell.pack();
110
111 //get current size dialog, and screen size
112 int displayWidth = display.getBounds().width;
113 int displayHeight = display.getBounds().height;
114 int currentShellWidth = shell.getSize().x;
115 int currentShellHeight = shell.getSize().y;
116
117 //default sizes for the dialog
118 int minShellWidth = 350;
119 int minShellHeight= 200;
120 //ensure this is large enough, increase it if its not
121 int newShellWidth = currentShellWidth > minShellWidth ? currentShellWidth : minShellWidth;
122 int newShellHeight = currentShellHeight > minShellHeight ? currentShellHeight : minShellHeight;
123
124 //set the final size and centre the dialog
125 shell.setBounds((displayWidth - newShellWidth)/2 , (displayHeight - newShellHeight)/2, newShellWidth, newShellHeight);
126
127 shell.open();
128 _window.getShell().setEnabled(false);
129
130 while (!shell.isDisposed())
131 {
132 if (!display.readAndDispatch())
133 {
134 display.sleep();
135 }
136 }
137
138 // enable the main shell
139 _window.getShell().setEnabled(true);
140 _window.getShell().open();
141 }
142
143 // Creates the SWT widgets in the popup shell, to enter username and password.
144 // Adds listeners to the widgets to take appropriate action
145 private void createWidgets(final Shell shell)
146 {
147 Composite composite = new Composite(shell, SWT.NONE);
148 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
149 GridLayout layout = new GridLayout(2, false);
150 layout.horizontalSpacing = 10;
151 layout.verticalSpacing = 10;
152 layout.marginHeight = 20;
153 layout.marginWidth = 20;
154 composite.setLayout(layout);
155
156 Label user = new Label(composite, SWT.NONE);
157 user.setText(USERNAME);
158 user.setLayoutData(new GridData(SWT.TRAIL, SWT.TOP, false, false));
159
160 final Text textUser = new Text(composite, SWT.BORDER);
161 textUser.setText("");
162 textUser.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
163 // Put cursor on this field
164 textUser.setFocus();
165
166 Label password = new Label(composite, SWT.NONE);
167 password.setText(PASSWORD);
168 password.setLayoutData(new GridData(SWT.TRAIL, SWT.TOP, false, false));
169
170 final Text textPwd = new Text(composite, SWT.BORDER | SWT.SINGLE | SWT.PASSWORD);
171 textPwd.setText("");
172 textPwd.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
173
174 //Get the text widgets
175 Control[] widgets = composite.getChildren();
176 for (int i=0; i < widgets.length; i++)
177 {
178 widgets[i].addKeyListener(new KeyAdapter()
179 {
180 public void keyPressed(KeyEvent event)
181 {
182 if (event.character == SWT.ESC)
183 {
184 //Escape key acts as cancel on all widgets
185 shell.close();
186 }
187 }
188 });
189 }
190
191 Composite buttonsComposite = new Composite(composite, SWT.NONE);
192 buttonsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
193 buttonsComposite.setLayout(new GridLayout(2, true));
194
195 final Button connectButton = new Button(buttonsComposite, SWT.PUSH | SWT.CENTER);
196 connectButton.setText(Constants.BUTTON_CONNECT);
197 GridData gridData = new GridData (SWT.TRAIL, SWT.BOTTOM, true, true);
198 gridData.widthHint = 100;
199 connectButton.setLayoutData(gridData);
200 connectButton.setFont(ApplicationRegistry.getFont(Constants.FONT_BUTTON));
201 connectButton.addSelectionListener(new SelectionAdapter(){
202 public void widgetSelected(SelectionEvent event)
203 {
204 _user = textUser.getText();
205 if ((_user == null) || (_user.trim().length() == 0))
206 {
207 ViewUtility.popupInfoMessage(_title, INFO_USERNAME);
208 textUser.setText("");
209 textUser.setFocus();
210 return;
211 }
212
213 _password = textPwd.getText();
214 if (_password == null)
215 {
216 ViewUtility.popupInfoMessage(_title, INFO_PASSWORD);
217 textPwd.setText("");
218 textPwd.setFocus();
219 return;
220 }
221
222 _connect = true;
223 shell.dispose();
224 }
225 });
226
227 final Button cancelButton = new Button(buttonsComposite, SWT.PUSH);
228 cancelButton.setText(Constants.BUTTON_CANCEL);
229 gridData = new GridData (SWT.LEAD, SWT.BOTTOM, true, true);
230 gridData.widthHint = 100;
231 cancelButton.setLayoutData(gridData);
232 cancelButton.setFont(ApplicationRegistry.getFont(Constants.FONT_BUTTON));
233 cancelButton.addSelectionListener(new SelectionAdapter(){
234 public void widgetSelected(SelectionEvent event)
235 {
236 shell.dispose();
237 }
238 });
239
240 //Get the ok/cancel button widgets and add a new key listener
241 widgets = buttonsComposite.getChildren();
242 for (int i=0; i < widgets.length; i++)
243 {
244 widgets[i].addKeyListener(new KeyAdapter()
245 {
246 public void keyPressed(KeyEvent event)
247 {
248 if (event.character == SWT.ESC)
249 {
250 //Escape key acts as cancel on all widgets
251 shell.close();
252 }
253 }
254 });
255 }
256
257 shell.setDefaultButton(connectButton);
258 }
259
260 }
|