KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > FileSelector


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.i18n;
21
22 import java.awt.*;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.beans.*;
26 import javax.swing.*;
27 import javax.swing.border.Border JavaDoc;
28 import javax.swing.event.DocumentEvent JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
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 /**
43  * Panel for selecting a properties file (by browsing a project tree). Also
44  * allows to create a new file. Should be displayed as a dialog - using
45  * getDialog(...) method. Substitutes use of NodeOperation.select which does
46  * not provide a satisfactory UI.
47  */

48 public class FileSelector extends JPanel implements PropertyChangeListener, ExplorerManager.Provider {
49
50     // [this could be configurable to make the file selector more general]
51
private static final String JavaDoc PROPERTIES_EXT = ".properties"; // NOI18N
52
private static final String JavaDoc DEFAULT_BUNDLE_NAME = "Bundle"; // NOI18N
53

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")); // NOI18N
86
newButton.addActionListener(new ActionListener JavaDoc() {
87                 public void actionPerformed(ActionEvent JavaDoc ev) {
88                     if (selectedFolder == null)
89                         return;
90
91                     String JavaDoc fileName = fileNameTextField.getText();
92                     try {
93                         if (fileName.equals(""))
94                             fileName = DEFAULT_BUNDLE_NAME; // NOI18N
95
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                         // select created
100
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 JavaDoc ex) { // TODO report failure
113
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
114                     }
115                 }
116             });
117             newButton.setEnabled(false);
118         }
119         okButton = new JButton(NbBundle.getMessage(FileSelector.class, "CTL_OKButton")); // NOI18N
120
okButton.addActionListener(new ActionListener JavaDoc() {
121             public void actionPerformed(ActionEvent JavaDoc ev) {
122                 confirmed = true;
123             }
124         });
125         okButton.setEnabled(false);
126         cancelButton = new JButton(NbBundle.getMessage(FileSelector.class, "CTL_CancelButton")); // NOI18N
127

128         BeanTreeView treeView = new BeanTreeView ();
129         treeView.setPopupAllowed(false);
130         treeView.setDefaultActionAllowed(false);
131         treeView.setBorder((Border JavaDoc)UIManager.get("Nb.ScrollPane.border")); // NOI18N
132
treeView.getAccessibleContext().setAccessibleName(NbBundle.getMessage(FileSelector.class, "ACSN_FileSelectorTreeView")); // NOI18N
133
treeView.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FileSelector.class, "ACSD_FileSelectorTreeView")); // NOI18N
134
this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(FileSelector.class, "ACSD_FileSelectorPanel")); // NOI18N
135

136         // label and text field with mnemonic
137
JLabel label = new JLabel();
138         Mnemonics.setLocalizedText(label, NbBundle.getMessage(FileSelector.class, "LBL_FileName")); // NOI18N
139
fileNameTextField = new JTextField();
140         fileNameTextField.getDocument().addDocumentListener(new DocumentListener JavaDoc() { // NOI18N
141
public void changedUpdate(DocumentEvent JavaDoc e) {
142             }
143             public void insertUpdate(DocumentEvent JavaDoc e) {
144                 checkFileName();
145             }
146             public void removeUpdate(DocumentEvent JavaDoc 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     /**
170      * Creates a modal dialog containing the file selector with given title.
171      * Use ActionListener to be informed about pressing OK button.
172      * @param title
173      * @param listener ActionListener attached to the OK button (if not null)
174      */

175     public Dialog getDialog(String JavaDoc title, ActionListener JavaDoc 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(""); // NOI18N
219
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 JavaDoc fileName = fileNameTextField.getText();
245         if ("".equals(fileName)) { // NOI18N
246
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     /**
271      * Implementation of ExplorerManager.Provider. Needed for the tree view to work.
272      */

273     public ExplorerManager getExplorerManager() {
274         return manager;
275     }
276 }
277
Popular Tags