1 /* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */ 2 3 package mlterm; 4 5 import org.eclipse.swt.*; 6 import org.eclipse.swt.widgets.*; 7 import org.eclipse.swt.layout.*; 8 import org.eclipse.swt.events.*; 9 10 public class ConnectDialog extends Dialog { 11 private String proto = "ssh"; 12 private boolean okPressed = false; 13 private boolean cancelPressed = false; 14 ConnectDialog(Shell parent, int style)15 public ConnectDialog(Shell parent, int style) { 16 super(parent, style); 17 } 18 ConnectDialog(Shell parent)19 public ConnectDialog(Shell parent) { 20 this(parent, 0); 21 } 22 23 /* 24 * Return value: 25 * String[0] -> host 26 * String[1] -> password 27 * String[2] -> encoding (can be null) 28 * String[3] -> exec cmd (can be null) 29 */ open(String uri)30 public String[] open(String uri) { 31 Shell shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 32 shell.setText(getText()); 33 shell.setLayout(new GridLayout(2, false)); 34 35 if (uri == null) { 36 uri = ""; 37 } else { 38 String[] array = uri.split("://"); 39 if (array.length == 2) { 40 proto = array[0]; 41 uri = array[1]; 42 } 43 } 44 45 /* Protocol */ 46 Label label = new Label(shell, SWT.NONE); 47 label.setText("Protocol"); 48 49 Composite comp = new Composite(shell, SWT.NONE); 50 RowLayout rowLayout = new RowLayout(); 51 rowLayout.fill = true; 52 rowLayout.center = true; 53 rowLayout.justify = true; 54 comp.setLayout(rowLayout); 55 56 Button ssh = new Button(comp, SWT.RADIO); 57 ssh.setText("SSH"); 58 if (proto.equals("ssh")) { 59 ssh.setSelection(true); 60 } 61 ssh.addSelectionListener(new SelectionAdapter() { 62 public void widgetSelected(SelectionEvent e) { 63 proto = "ssh"; 64 } 65 }); 66 67 Button telnet = new Button(comp, SWT.RADIO); 68 telnet.setText("TELNET"); 69 if (proto.equals("telnet")) { 70 telnet.setSelection(true); 71 } 72 telnet.addSelectionListener(new SelectionAdapter() { 73 public void widgetSelected(SelectionEvent e) { 74 proto = "telnet"; 75 } 76 }); 77 78 Button rlogin = new Button(comp, SWT.RADIO); 79 rlogin.setText("RLOGIN"); 80 if (proto.equals("rlogin")) { 81 rlogin.setSelection(true); 82 } 83 rlogin.addSelectionListener(new SelectionAdapter() { 84 public void widgetSelected(SelectionEvent e) { 85 proto = "rlogin"; 86 } 87 }); 88 89 comp.pack(); 90 91 /* Server */ 92 label = new Label(shell, SWT.NONE); 93 label.setText("Server"); 94 Text server = new Text(shell, SWT.BORDER); 95 GridData textGrid = new GridData(GridData.FILL_HORIZONTAL); 96 server.setLayoutData(textGrid); 97 98 /* Port */ 99 label = new Label(shell, SWT.NONE); 100 label.setText("Port"); 101 Text port = new Text(shell, SWT.BORDER); 102 port.setLayoutData(textGrid); 103 104 /* User */ 105 label = new Label(shell, SWT.NONE); 106 label.setText("User"); 107 Text user = new Text(shell, SWT.BORDER); 108 user.setLayoutData(textGrid); 109 110 /* Password */ 111 label = new Label(shell, SWT.NONE); 112 label.setText("Password"); 113 Text pass = new Text(shell, SWT.BORDER | SWT.PASSWORD); 114 pass.setLayoutData(textGrid); 115 116 /* Encoding */ 117 label = new Label(shell, SWT.NONE); 118 label.setText("Encoding"); 119 Text encoding = new Text(shell, SWT.BORDER); 120 encoding.setLayoutData(textGrid); 121 122 /* Exec cmd */ 123 label = new Label(shell, SWT.NONE); 124 label.setText("Exec cmd"); 125 Text execCmd = new Text(shell, SWT.BORDER); 126 execCmd.setLayoutData(textGrid); 127 128 /* OK/Cancel */ 129 comp = new Composite(shell, SWT.NONE); 130 comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); 131 comp.setLayout(rowLayout); 132 133 Button ok = new Button(comp, SWT.PUSH); 134 ok.setText(" OK "); 135 ok.addSelectionListener(new SelectionAdapter() { 136 public void widgetSelected(SelectionEvent e) { 137 okPressed = true; 138 } 139 }); 140 141 Button cancel = new Button(comp, SWT.PUSH); 142 cancel.setText("Cancel"); 143 cancel.addSelectionListener(new SelectionAdapter() { 144 public void widgetSelected(SelectionEvent e) { 145 cancelPressed = true; 146 } 147 }); 148 149 comp.pack(); 150 151 String[] array = uri.split("@"); 152 if (array.length == 2) { 153 user.setText(array[0]); 154 uri = array[1]; 155 } 156 157 array = uri.split(":"); 158 server.setText(array[0]); 159 if (array.length == 2) { 160 boolean isPort = true; 161 try { 162 Integer.parseInt(array[1]); 163 } catch (NumberFormatException e) { 164 isPort = false; 165 } 166 167 if (isPort) { 168 port.setText(array[1]); 169 } else { 170 encoding.setText(array[1]); 171 } 172 } else if (array.length == 3) { 173 port.setText(array[1]); 174 encoding.setText(array[2]); 175 } 176 177 final Control[] tabList = new Control[] { 178 ssh, telnet, rlogin, server, port, user, pass, encoding, execCmd, ok, cancel}; 179 180 KeyAdapter keyAdapter = 181 new KeyAdapter() 182 { 183 public void keyPressed( KeyEvent e) 184 { 185 if( e.keyCode == SWT.TAB) 186 { 187 /* Tab list doesn't work in applet, so implement by myself. */ 188 189 for( int count = 0 ; 190 count < tabList.length; count++) 191 { 192 if (e.widget == tabList[count]) { 193 if ((e.stateMask & SWT.SHIFT) != 0) { 194 if (count < 3) { 195 count = tabList.length - 1; 196 } else { 197 count--; 198 199 if (count < 3) { 200 for (; count >= 0; count--) { 201 if (((Button) tabList[count]).getSelection()) { 202 break; 203 } 204 } 205 } 206 } 207 } else { 208 if (count + 1 == tabList.length) { 209 for (count = 0; count < 3; count++) { 210 if (((Button) tabList[count]).getSelection()) { 211 break; 212 } 213 } 214 } else { 215 if (count < 3) { 216 count = 3; 217 } else { 218 count++; 219 } 220 } 221 } 222 223 tabList[count].forceFocus(); 224 225 break; 226 } 227 } 228 } 229 else if (e.keyCode == SWT.CR) { 230 okPressed = true; 231 } 232 } 233 } 234 ; 235 236 for (int count = 0; count < tabList.length; count++) { 237 tabList[count].addKeyListener(keyAdapter); 238 } 239 240 shell.pack(); 241 shell.open(); 242 Display display = shell.getDisplay(); 243 array = null; 244 while (!shell.isDisposed()) { 245 if (okPressed) { 246 uri = server.getText(); 247 if (!uri.equals("")) { 248 array = new String[4]; 249 250 String str = user.getText(); 251 if (!str.equals("")) { 252 uri = str + "@" + uri; 253 } 254 255 uri = proto + "://" + uri; 256 257 str = port.getText(); 258 if (!str.equals("")) { 259 uri = uri + ":" + str; 260 } 261 262 array[0] = uri; 263 array[1] = pass.getText(); 264 array[2] = encoding.getText(); 265 if (array[2].equals("")) { 266 array[2] = null; 267 } 268 array[3] = execCmd.getText(); 269 if (array[3].equals("")) { 270 array[3] = null; 271 } 272 } 273 } else if (!cancelPressed) { 274 if (!display.readAndDispatch()) { 275 display.sleep(); 276 } 277 278 continue; 279 } 280 281 shell.dispose(); 282 283 break; 284 } 285 286 return array; 287 } 288 } 289