1 19 package org.netbeans.modules.j2ee.websphere6.ui.wizard; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStreamReader ; 26 import java.util.*; 27 import java.awt.*; 28 import java.awt.event.*; 29 import javax.swing.*; 30 import javax.swing.event.*; 31 import javax.swing.filechooser.*; 32 33 import org.openide.*; 34 import org.openide.util.*; 35 36 43 public class ServerLocationPanel extends JPanel 44 implements WizardDescriptor.Panel { 45 49 private final static String PROP_ERROR_MESSAGE = 50 "WizardPanel_errorMessage"; 52 55 private transient WizardDescriptor wizardDescriptor; 56 57 60 private transient WSInstantiatingIterator instantiatingIterator; 61 62 72 public ServerLocationPanel(String [] steps, int index, 73 ChangeListener listener, 74 WSInstantiatingIterator instantiatingIterator) { 75 this.instantiatingIterator = instantiatingIterator; 77 78 putClientProperty("WizardPanel_contentData", steps); putClientProperty("WizardPanel_contentSelectedIndex", new Integer (index)); 83 84 addChangeListener(listener); 86 87 setName(steps[index]); 89 90 init(); 92 } 93 94 99 public HelpCtx getHelp() { 100 return new HelpCtx("j2eeplugins_registering_app_" + "server_websphere"); } 103 104 110 public Component getComponent() { 111 return this; 112 } 113 114 120 public boolean isValid() { 121 wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, ""); 123 124 if (!isValidServerRoot(locationField.getText())) { 127 wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, 128 NbBundle.getMessage(ServerLocationPanel.class, 129 "ERR_INVALID_SERVER_ROOT")); return false; 131 } 132 133 instantiatingIterator.setServerRoot(locationField.getText()); 135 136 return true; 138 } 139 140 private JButton locationBrowseButton; 144 private JLabel locationLabel; 145 private JTextField locationField; 146 private JPanel formattingPanel; 147 148 151 private void init() { 152 GridBagConstraints gridBagConstraints; 155 156 locationLabel = new JLabel(); 158 locationField = new JTextField(); 159 locationBrowseButton = new JButton(); 160 formattingPanel = new JPanel(); 161 162 setLayout(new GridBagLayout()); 164 165 locationLabel.setText(NbBundle.getMessage(ServerLocationPanel.class, 167 "LBL_SERVER_LOCATION")); locationLabel.setDisplayedMnemonic(NbBundle.getMessage(ServerLocationPanel.class, 169 "MNE_SERVER_LOCATION").charAt(0)); 170 locationLabel.setLabelFor(locationField); 171 gridBagConstraints = new GridBagConstraints(); 172 gridBagConstraints.gridx = 0; 173 gridBagConstraints.gridy = 0; 174 gridBagConstraints.anchor = GridBagConstraints.EAST; 175 add(locationLabel, gridBagConstraints); 176 177 locationField.addKeyListener(new LocationKeyListener()); 179 if(System.getProperty("websphere.home")==null || 180 System.getProperty("websphere.home").equals("")) { 181 String home = System.getProperty("user.home"); 182 if(home!=null) { 183 try{ 184 File f = new File (home + File.separator + ".WASRegistry"); 185 BufferedReader reader = new BufferedReader ( 186 new InputStreamReader ( 187 new FileInputStream (f))); 188 String string; 189 while((string=reader.readLine())!=null) { 190 if(string.length()>1 && new File (string).exists()) { 191 System.setProperty("websphere.home",string); 192 } 193 } 194 } catch (IOException e){ 195 e=null; 196 } 198 } 199 } 200 201 if (System.getProperty("websphere.home") != null) { locationField.setText(System.getProperty( 203 "websphere.home")); } 205 gridBagConstraints = new GridBagConstraints(); 206 gridBagConstraints.gridx = 1; 207 gridBagConstraints.gridy = 0; 208 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 209 gridBagConstraints.weightx = 1.0; 210 gridBagConstraints.insets = new Insets(0, 10, 0, 10); 211 locationField.getAccessibleContext().setAccessibleName( 212 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/websphere6/ui/wizard/Bundle").getString("TTL_Location")); 213 locationField.getAccessibleContext().setAccessibleDescription( 214 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/websphere6/ui/wizard/Bundle").getString("MSG_Location")); 215 216 add(locationField, gridBagConstraints); 217 218 locationBrowseButton.setText(NbBundle.getMessage( 220 ServerLocationPanel.class, "LBL_BROWSE_BUTTON")); locationBrowseButton.setMnemonic(KeyEvent.VK_O); 222 locationBrowseButton.setDisplayedMnemonicIndex(2); 223 locationBrowseButton.addActionListener(new BrowseActionListener()); 224 gridBagConstraints = new GridBagConstraints(); 225 gridBagConstraints.gridx = 2; 226 gridBagConstraints.gridy = 0; 227 gridBagConstraints.anchor = GridBagConstraints.WEST; 228 locationBrowseButton.getAccessibleContext().setAccessibleName( 229 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/websphere6/ui/wizard/Bundle").getString("TTL_Browse")); 230 locationBrowseButton.getAccessibleContext().setAccessibleDescription( 231 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/websphere6/ui/wizard/Bundle").getString("MSG_Browse")); 232 233 add(locationBrowseButton, gridBagConstraints); 234 235 gridBagConstraints = new GridBagConstraints(); 237 gridBagConstraints.gridx = 0; 238 gridBagConstraints.gridy = 1; 239 gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; 240 gridBagConstraints.weighty = 1.0; 241 add(formattingPanel, gridBagConstraints); 242 getAccessibleContext().setAccessibleDescription( 243 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/websphere6/ui/wizard/Bundle"). 244 getString("MSG_ServerLocationPanelDescription")); 245 } 246 247 251 private JFileChooser fileChooser = new JFileChooser(); 252 253 257 private void showFileChooser() { 258 fileChooser.setFileFilter(new DirectoryFileFilter()); 260 fileChooser.setMultiSelectionEnabled(false); 261 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 262 263 File currentLocation = new File (locationField.getText()); 265 if (currentLocation.exists() && currentLocation.isDirectory()) { 266 fileChooser.setCurrentDirectory(currentLocation.getParentFile()); 267 fileChooser.setSelectedFile(currentLocation); 268 } 269 270 if (fileChooser.showOpenDialog(this) == fileChooser.APPROVE_OPTION) { 273 locationField.setText(fileChooser.getSelectedFile().getPath()); 274 fireChangeEvent(); 275 } 276 } 277 278 284 private static boolean isValidServerRoot(String path) { 285 String [] children = { 288 "bin", "cloudscape", "profiles", "properties/wsadmin.properties", "lib/j2ee.jar", "lib/wjmxapp.jar" }; 295 return hasChildren(path, children); 296 } 297 298 303 private static boolean hasChildren(String parent, String [] children) { 304 if (parent == null) { 306 return false; 307 } 308 309 if (children == null) { 311 return true; 312 } 313 314 for (int i = 0; i < children.length; i++) { 317 if (!(new File (parent + File.separator + children[i]).exists())) { 318 return false; 319 } 320 } 321 322 return true; 324 } 325 326 335 public void readSettings(Object object) { 336 this.wizardDescriptor = (WizardDescriptor) object; 337 } 338 339 343 public void storeSettings(Object object) { 344 } 345 346 352 private Vector listeners = new Vector(); 353 354 359 public void removeChangeListener(ChangeListener listener) { 360 if (listeners != null) { 361 synchronized (listeners) { 362 listeners.remove(listener); 363 } 364 } 365 } 366 367 372 public void addChangeListener(ChangeListener listener) { 373 synchronized (listeners) { 374 listeners.add(listener); 375 } 376 } 377 378 381 private void fireChangeEvent() { 382 ChangeEvent event = new ChangeEvent(this); 383 fireChangeEvent(event); 384 } 385 386 391 private void fireChangeEvent(ChangeEvent event) { 392 Vector targetListeners; 393 synchronized (listeners) { 394 targetListeners = (Vector) listeners.clone(); 395 } 396 397 for (int i = 0; i < targetListeners.size(); i++) { 398 ChangeListener listener = (ChangeListener) targetListeners. 399 elementAt(i); 400 listener.stateChanged(event); 401 } 402 } 403 404 412 private class LocationKeyListener extends KeyAdapter { 413 416 public void keyTyped(KeyEvent event) { 417 fireChangeEvent(); 418 } 419 420 423 public void keyReleased(KeyEvent event) { 424 fireChangeEvent(); 425 } 426 } 427 428 433 private class BrowseActionListener implements ActionListener { 434 438 public void actionPerformed(ActionEvent event) { 439 showFileChooser(); 440 } 441 } 442 443 449 private static class DirectoryFileFilter extends FileFilter { 450 456 public boolean accept(File file) { 457 if (file.exists() && file.isDirectory()) { 459 return true; 460 } 461 462 return false; 464 } 465 466 471 public String getDescription() { 472 return NbBundle.getMessage(ServerLocationPanel.class, 473 "DIRECTORIES_FILTER_NAME"); } 475 } 476 } 477 | Popular Tags |