1 19 20 package org.openide.loaders; 21 22 import java.awt.event.KeyEvent ; 23 import java.beans.PropertyDescriptor ; 24 import java.io.IOException ; 25 import java.lang.ref.*; 26 import java.lang.reflect.Method ; 27 import javax.swing.*; 28 import javax.swing.event.*; 29 import org.openide.explorer.propertysheet.*; 30 import org.openide.filesystems.*; 31 import org.openide.util.*; 32 33 37 final class TemplateWizard2 extends javax.swing.JPanel implements DocumentListener { 38 39 40 private ChangeListener listener; 41 42 private static final String PROP_LOCATION_FOLDER = "locationFolder"; private DataFolder locationFolder; 44 private Reference<FileSystem> fileSystemRef = new WeakReference<FileSystem>(null); 45 private DefaultPropertyModel locationFolderModel; 46 47 50 private String extension; 51 52 53 public TemplateWizard2() { 54 initLocationFolder (); 55 initComponents (); 56 setName (DataObject.getString("LAB_TargetLocationPanelName")); java.util.ResourceBundle bundle = org.openide.util.NbBundle.getBundle(TemplateWizard2.class); 59 newObjectName.getDocument().addDocumentListener(this); 60 newObjectName.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)); 61 newObjectName.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_NewObjectName")); } 63 64 69 private void initLocationFolder () { 70 PropertyDescriptor pd = null; 71 try { 72 Method getterMethod = this.getClass ().getDeclaredMethod("getLocationFolder", new Class [] {}); getterMethod.setAccessible (true); 74 Method setterMethod = this.getClass ().getDeclaredMethod("setLocationFolder", new Class [] {DataFolder.class}); setterMethod.setAccessible (true); 76 pd = new PropertyDescriptor (PROP_LOCATION_FOLDER, getterMethod, setterMethod); 77 } catch (java.beans.IntrospectionException ie) { 78 Exceptions.printStackTrace(ie); 79 } catch (NoSuchMethodException nsme) { 80 Exceptions.printStackTrace(nsme); 81 } 82 locationFolderModel = new DefaultPropertyModel (this, pd); 83 } 84 85 88 private static String defaultNewObjectName () { 89 return DataObject.getString ("FMT_DefaultNewObjectName"); } 91 92 97 private void initComponents() { 99 100 namePanel = new javax.swing.JPanel (); 101 jLabel1 = new javax.swing.JLabel (); 102 newObjectName = new javax.swing.JTextField (); 103 dataFolderPanel = dataFolderPanel = new PropertyPanel (locationFolderModel, PropertyPanel.PREF_CUSTOM_EDITOR); 104 105 FormListener formListener = new FormListener(); 106 107 setLayout(new java.awt.BorderLayout ()); 108 109 namePanel.setLayout(new java.awt.BorderLayout (12, 0)); 110 111 jLabel1.setLabelFor(newObjectName); 112 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/openide/loaders/Bundle"); org.openide.awt.Mnemonics.setLocalizedText(jLabel1, bundle.getString("CTL_NewObjectName")); namePanel.add(jLabel1, java.awt.BorderLayout.WEST); 115 116 newObjectName.addFocusListener(formListener); 117 namePanel.add(newObjectName, java.awt.BorderLayout.CENTER); 118 119 add(namePanel, java.awt.BorderLayout.NORTH); 120 add(dataFolderPanel, java.awt.BorderLayout.CENTER); 121 } 122 123 125 private class FormListener implements java.awt.event.FocusListener { 126 FormListener() {} 127 public void focusGained(java.awt.event.FocusEvent evt) { 128 if (evt.getSource() == newObjectName) { 129 TemplateWizard2.this.newObjectNameFocusGained(evt); 130 } 131 } 132 133 public void focusLost(java.awt.event.FocusEvent evt) { 134 } 135 } 137 private void newObjectNameFocusGained(java.awt.event.FocusEvent evt) { newObjectName.selectAll (); 139 } 141 146 void addChangeListener(ChangeListener l) { 147 if (listener != null) throw new IllegalStateException (); 148 149 listener = l; 150 } 151 152 public void addNotify () { 153 super.addNotify(); 154 newObjectName.requestFocus(); 156 getAccessibleContext().setAccessibleDescription( 157 NbBundle.getBundle(TemplateWizard2.class).getString ("ACSD_TemplateWizard2") ); 159 } 160 161 173 void implReadSettings(Object settings) { 174 TemplateWizard wizard = (TemplateWizard)settings; 175 176 DataObject template = wizard.getTemplate (); 177 if (template != null) { 178 extension = template.getPrimaryFile().getExt(); 179 } 180 181 setNewObjectName (wizard.getTargetName ()); 182 183 try { 184 setLocationFolder (wizard.getTargetFolder ()); 185 } catch (IOException ioe) { 186 setLocationFolder (null); 187 } 188 } 189 190 194 public void removeChangeListener(ChangeListener l) { 195 listener = null; 196 } 197 198 213 void implStoreSettings(Object settings) { 214 TemplateWizard wizard = (TemplateWizard)settings; 215 216 wizard.setTargetFolder (locationFolder); 217 218 String name = newObjectName.getText (); 219 if (name.equals (defaultNewObjectName ())) { 220 name = null; 221 } 222 wizard.setTargetName (name); 223 } 224 225 231 String implIsValid () { 232 FileSystem fs = fileSystemRef.get(); 234 if (locationFolder == null || fs == null) 235 return NbBundle.getMessage(TemplateWizard2.class, "MSG_fs_or_folder_does_not_exist"); 237 if (fileSystemRef.get().isReadOnly()) 239 return NbBundle.getMessage(TemplateWizard2.class, "MSG_fs_is_readonly"); 241 if (locationFolder == null) locationFolder = DataFolder.findFolder (fs.getRoot()); 242 243 StringBuffer sb = new StringBuffer (); 245 sb.append (locationFolder.getPrimaryFile ().getPath ()); 246 sb.append ("/"); 247 sb.append (newObjectName.getText ()); 248 if ("" != extension) { sb.append ('.'); 250 sb.append (extension); 251 } 252 253 FileObject f = fs.findResource (sb.toString ()); 254 if (f != null) { 255 return NbBundle.getMessage(TemplateWizard2.class, "MSG_file_already_exist", sb.toString()); } 257 258 if ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) { 259 if (TemplateWizard.checkCaseInsensitiveName (locationFolder.getPrimaryFile (), newObjectName.getText (), extension)) { 260 return NbBundle.getMessage(TemplateWizard2.class, "MSG_file_already_exist", newObjectName.getText ()); } 262 } 263 264 return null; 266 } 267 268 273 public void changedUpdate(DocumentEvent e) { 274 if (e.getDocument () == newObjectName.getDocument ()) { 275 SwingUtilities.invokeLater (new Updater ()); 276 } 277 } 278 279 285 public void insertUpdate(DocumentEvent e) { 286 changedUpdate (e); 287 } 288 289 296 public void removeUpdate(DocumentEvent e) { 297 if (e.getDocument () == newObjectName.getDocument ()) { 299 SwingUtilities.invokeLater (new Updater ()); 300 } 301 } 302 303 306 private class Updater implements Runnable { 307 Updater() {} 308 public void run () { 309 if (newObjectName.getText().equals ("")) { setNewObjectName (""); } 312 313 fireStateChanged (); 314 } 315 } 316 317 319 public void requestFocus () { 320 newObjectName.requestFocus(); 321 newObjectName.selectAll (); 322 } 323 324 327 private void setNewObjectName (String name) { 328 String n = name; 329 if (name == null || name.length () == 0) { 330 n = defaultNewObjectName (); 331 } 332 333 newObjectName.getDocument().removeDocumentListener(this); 334 newObjectName.setText (n); 335 newObjectName.getDocument().addDocumentListener(this); 336 337 if (name == null || name.length () == 0) { 338 newObjectName.selectAll (); 339 } 340 } 341 342 344 private void fireStateChanged () { 345 if (listener != null) { 346 listener.stateChanged (new ChangeEvent (this)); 347 } 348 } 349 350 private org.openide.explorer.propertysheet.PropertyPanel dataFolderPanel; 352 private javax.swing.JLabel jLabel1; 353 private javax.swing.JPanel namePanel; 354 private javax.swing.JTextField newObjectName; 355 357 public void setLocationFolder (DataFolder fd) { 358 if (locationFolder == fd) 359 return ; 360 if (locationFolder != null && locationFolder.equals (fd)) 361 return ; 362 DataFolder oldLocation = locationFolder; 363 locationFolder = fd; 364 firePropertyChange (PROP_LOCATION_FOLDER, oldLocation, locationFolder); 365 if (fd != null) { 366 try { 367 fileSystemRef = new WeakReference<FileSystem>(fd.getPrimaryFile().getFileSystem()); 368 } catch (org.openide.filesystems.FileStateInvalidException fsie) { 369 fileSystemRef = new WeakReference<FileSystem>(null); 370 } 371 } 372 fireStateChanged (); 373 } 374 375 public DataFolder getLocationFolder () { 376 return locationFolder; 377 } 378 379 } 380 | Popular Tags |