1 19 20 package org.netbeans.modules.i18n; 21 22 import java.awt.*; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.beans.*; 26 import javax.swing.*; 27 import javax.swing.border.Border ; 28 import javax.swing.event.DocumentEvent ; 29 import javax.swing.event.DocumentListener ; 30 import org.jdesktop.layout.GroupLayout; 31 import org.openide.*; 32 import org.openide.awt.Mnemonics; 33 import org.openide.explorer.ExplorerManager; 34 import org.openide.explorer.view.BeanTreeView; 35 import org.openide.filesystems.FileObject; 36 import org.openide.loaders.*; 37 import org.openide.nodes.Node; 38 import org.openide.util.HelpCtx; 39 import org.openide.util.NbBundle; 40 import org.netbeans.modules.properties.PropertiesDataObject; 41 42 48 public class FileSelector extends JPanel implements PropertyChangeListener, ExplorerManager.Provider { 49 50 private static final String PROPERTIES_EXT = ".properties"; private static final String DEFAULT_BUNDLE_NAME = "Bundle"; 54 private DataObject template; 55 56 private ExplorerManager manager; 57 58 private DataObject selectedDataObject; 59 private DataFolder selectedFolder; 60 private boolean confirmed; 61 62 private JButton newButton; 63 private JButton okButton; 64 private JButton cancelButton; 65 private JTextField fileNameTextField; 66 67 public FileSelector(FileObject fileInProject, DataObject template) { 68 this(SelectorUtils.bundlesNode(null, fileInProject, template == null), template); 69 } 70 71 private FileSelector(Node root, DataObject template) { 72 this.template = template; 73 74 manager = new ExplorerManager(); 75 manager.setRootContext(root); 76 try { 77 manager.setSelectedNodes (new Node[] { root }); 78 } catch(PropertyVetoException ex) { 79 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 80 } 81 manager.addPropertyChangeListener(this); 82 83 if (template != null) { 84 newButton = new JButton(); 85 Mnemonics.setLocalizedText(newButton, NbBundle.getMessage(FileSelector.class, "CTL_CreateNewButton")); newButton.addActionListener(new ActionListener () { 87 public void actionPerformed(ActionEvent ev) { 88 if (selectedFolder == null) 89 return; 90 91 String fileName = fileNameTextField.getText(); 92 try { 93 if (fileName.equals("")) 94 fileName = DEFAULT_BUNDLE_NAME; else if (fileName.toLowerCase().endsWith(PROPERTIES_EXT)) 96 fileName = fileName.substring(0, fileName.length()-PROPERTIES_EXT.length()); 97 98 selectedDataObject = FileSelector.this.template.createFromTemplate(selectedFolder, fileName); 99 Node[] selected = manager.getSelectedNodes(); 101 if (selected != null && selected.length == 1 102 && selected[0].getCookie(DataObject.class) == selectedFolder) { 103 Node[] sub = selected[0].getChildren().getNodes(true); 104 for (int i=0; i < sub.length; i++) { 105 if (sub[i].getCookie(DataObject.class) == selectedDataObject) { 106 manager.setSelectedNodes(new Node[] { sub[i] }); 107 break; 108 } 109 } 110 } 111 } 112 catch (Exception ex) { ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 114 } 115 } 116 }); 117 newButton.setEnabled(false); 118 } 119 okButton = new JButton(NbBundle.getMessage(FileSelector.class, "CTL_OKButton")); okButton.addActionListener(new ActionListener () { 121 public void actionPerformed(ActionEvent ev) { 122 confirmed = true; 123 } 124 }); 125 okButton.setEnabled(false); 126 cancelButton = new JButton(NbBundle.getMessage(FileSelector.class, "CTL_CancelButton")); 128 BeanTreeView treeView = new BeanTreeView (); 129 treeView.setPopupAllowed(false); 130 treeView.setDefaultActionAllowed(false); 131 treeView.setBorder((Border )UIManager.get("Nb.ScrollPane.border")); treeView.getAccessibleContext().setAccessibleName(NbBundle.getMessage(FileSelector.class, "ACSN_FileSelectorTreeView")); treeView.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FileSelector.class, "ACSD_FileSelectorTreeView")); this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FileSelector.class, "ACSD_FileSelectorPanel")); 136 JLabel label = new JLabel(); 138 Mnemonics.setLocalizedText(label, NbBundle.getMessage(FileSelector.class, "LBL_FileName")); fileNameTextField = new JTextField(); 140 fileNameTextField.getDocument().addDocumentListener(new DocumentListener () { public void changedUpdate(DocumentEvent e) { 142 } 143 public void insertUpdate(DocumentEvent e) { 144 checkFileName(); 145 } 146 public void removeUpdate(DocumentEvent e) { 147 checkFileName(); 148 } 149 }); 150 label.setLabelFor(fileNameTextField); 151 152 GroupLayout layout = new GroupLayout(this); 153 setLayout(layout); 154 layout.setAutocreateGaps(true); 155 layout.setAutocreateContainerGaps(true); 156 157 layout.setHorizontalGroup(layout.createParallelGroup() 158 .add(treeView, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 159 .add(layout.createSequentialGroup() 160 .add(label) 161 .add(fileNameTextField))); 162 layout.setVerticalGroup(layout.createSequentialGroup() 163 .add(treeView, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 164 .add(layout.createParallelGroup(GroupLayout.BASELINE) 165 .add(label) 166 .add(fileNameTextField))); 167 } 168 169 175 public Dialog getDialog(String title, ActionListener listener) { 176 DialogDescriptor dd = new DialogDescriptor( 177 this, title, true, 178 newButton != null ? 179 new JButton[] { newButton, okButton, cancelButton } : 180 new JButton[] { okButton, cancelButton }, 181 okButton, 182 DialogDescriptor.DEFAULT_ALIGN, HelpCtx.DEFAULT_HELP, 183 null 184 ); 185 dd.setClosingOptions(new JButton[] { okButton, cancelButton }); 186 if (listener != null) 187 okButton.addActionListener(listener); 188 return DialogDisplayer.getDefault().createDialog(dd); 189 } 190 191 public void addNotify() { 192 confirmed = false; 193 super.addNotify(); 194 } 195 196 boolean isConfirmed() { 197 return confirmed; 198 } 199 200 public DataObject getSelectedDataObject() { 201 return selectedDataObject; 202 } 203 204 public void propertyChange (PropertyChangeEvent ev) { 205 if (ev.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) { 206 Node[] nodes = manager.getSelectedNodes(); 207 selectedDataObject = null; 208 selectedFolder = null; 209 if (nodes != null && nodes.length == 1) { 210 DataObject dobj = (DataObject) nodes[0].getCookie(DataObject.class); 211 if (dobj != null) { 212 if (dobj instanceof PropertiesDataObject) { 213 fileNameTextField.setText(dobj.getName()); 214 selectedDataObject = dobj; 215 selectedFolder = dobj.getFolder(); 216 } 217 else if (dobj instanceof DataFolder) { 218 fileNameTextField.setText(""); selectedFolder = (DataFolder) dobj; 220 } 221 else selectedFolder = dobj.getFolder(); 222 } 223 } 224 okButton.setEnabled(selectedDataObject != null); 225 if (newButton != null) 226 newButton.setEnabled(selectedFolder != null 227 && selectedDataObject == null 228 && !checkForDefaultBundle()); 229 } 230 } 231 232 private boolean checkForDefaultBundle() { 233 if (selectedFolder != null) { 234 return selectedFolder.getPrimaryFile().getFileObject(DEFAULT_BUNDLE_NAME + PROPERTIES_EXT) != null; 235 } 236 return false; 237 } 238 239 private void checkFileName() { 240 if (selectedFolder == null) 241 return; 242 243 selectedDataObject = null; 244 String fileName = fileNameTextField.getText(); 245 if ("".equals(fileName)) { okButton.setEnabled(false); 247 if (newButton != null) 248 newButton.setEnabled(!checkForDefaultBundle()); 249 } 250 else { 251 if (!fileName.toLowerCase().endsWith(PROPERTIES_EXT)) 252 fileName = fileName + PROPERTIES_EXT; 253 254 FileObject fo = selectedFolder.getPrimaryFile().getFileObject(fileName); 255 if (fo != null) { 256 try { 257 selectedDataObject = DataObject.find(fo); 258 } 259 catch (DataObjectNotFoundException ex) { 260 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 261 } 262 } 263 264 okButton.setEnabled(selectedDataObject != null); 265 if (newButton != null) 266 newButton.setEnabled(selectedDataObject == null); 267 } 268 } 269 270 273 public ExplorerManager getExplorerManager() { 274 return manager; 275 } 276 } 277 | Popular Tags |