KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > properties > PropertiesLocaleNode


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.properties;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.awt.Dialog JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.swing.Action JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32
33 import org.openide.actions.*;
34 import org.openide.DialogDescriptor;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.nodes.CookieSet;
38 import org.openide.nodes.Node;
39 import org.openide.nodes.NodeTransfer;
40 import org.openide.NotifyDescriptor;
41 import org.openide.DialogDisplayer;
42 import org.openide.util.actions.SystemAction;
43 import org.openide.util.datatransfer.NewType;
44 import org.openide.util.datatransfer.PasteType;
45 import org.openide.util.HelpCtx;
46 import org.openide.util.NbBundle;
47
48
49 /**
50  * Node representing a single properties file.
51  *
52  * @see PropertiesDataNode
53  * @author Ian Formanek
54  */

55 public final class PropertiesLocaleNode extends FileEntryNode
56                                         implements CookieSet.Factory,
57                                                    Node.Cookie {
58
59     /** Icon base for the <code>PropertiesDataNode</code> node. */
60     private static final String JavaDoc LOCALE_ICON_BASE = "org/netbeans/modules/properties/propertiesLocale.gif"; // NOI18N
61

62     
63     /** Creates a new PropertiesLocaleNode for the given locale-specific file */
64     public PropertiesLocaleNode (PropertiesFileEntry fe) {
65         super(fe, fe.getChildren());
66         setDisplayName(Util.getLocaleLabel(fe));
67         
68         setIconBaseWithExtension(LOCALE_ICON_BASE);
69         setShortDescription(messageToolTip());
70
71         getCookieSet().add(PropertiesOpen.class, this);
72         getCookieSet().add(fe.getDataObject());
73     }
74             
75     /** Implements <code>CookieSet.Factory</code> interface method. */
76     @SuppressWarnings JavaDoc("unchecked")
77     public <T extends Node.Cookie> T createCookie(Class JavaDoc<T> clazz) {
78         if(clazz.isAssignableFrom(PropertiesOpen.class)) {
79             return (T) ((PropertiesDataObject) getFileEntry().getDataObject()).getOpenSupport();
80         } else {
81             return null;
82         }
83     }
84     
85     /** Lazily initialize set of node's actions.
86      * Overrides superclass method.
87      *
88      * @return array of actions for this node
89      */

90     protected SystemAction[] createActions () {
91         return new SystemAction[] {
92             SystemAction.get(EditAction.class),
93             SystemAction.get(OpenAction.class),
94             SystemAction.get(FileSystemAction.class),
95             null,
96             SystemAction.get(CutAction.class),
97             SystemAction.get(CopyAction.class),
98             SystemAction.get(PasteAction.class),
99             null,
100             SystemAction.get(DeleteAction.class),
101             SystemAction.get(LangRenameAction.class),
102             null,
103             SystemAction.get(NewAction.class),
104             SystemAction.get(SaveAsTemplateAction.class),
105             null,
106             SystemAction.get(ToolsAction.class),
107             SystemAction.get(PropertiesAction.class)
108         };
109     }
110
111     public Action JavaDoc getPreferredAction() {
112         return getActions(false)[0];
113     }
114
115     /** Gets the name. Note: It gets only the local part of the name (e.g. "de_DE_EURO").
116      * Reason is to allow user change only this part of name by renaming (on Node).
117      * Overrides superclass method.
118      *
119      * @return locale part of name
120      */

121     public String JavaDoc getName() {
122         String JavaDoc localeName = "invalid"; // NOI18N
123
if (getFileEntry().getFile().isValid() && !getFileEntry().getFile().isVirtual()) {
124             localeName = Util.getLocaleSuffix (getFileEntry());
125             if (localeName.length() > 0)
126                 if (localeName.charAt(0) == PropertiesDataLoader.PRB_SEPARATOR_CHAR)
127                     localeName = localeName.substring(1);
128         }
129         return localeName;
130     }
131     
132     /** Sets the system name. Overrides superclass method.
133      *
134      * @param name the new name
135      */

136     public void setName (String JavaDoc name) {
137         if(!name.startsWith(getFileEntry().getDataObject().getPrimaryFile().getName())) {
138             name = Util.assembleName (getFileEntry().getDataObject().getPrimaryFile().getName(), name);
139         }
140         
141         // new name is same as old one, do nothing
142
if (name.equals(super.getName())) return;
143
144         super.setName (name);
145         setDisplayName(Util.getLocaleLabel(getFileEntry()));
146         setShortDescription(messageToolTip());
147     }
148
149     /** Gets tooltip message for this node. Helper method. */
150     private String JavaDoc messageToolTip () {
151         FileObject fo = getFileEntry().getFile();
152         return FileUtil.getFileDisplayName(fo);
153     }
154     
155     /** This node can be renamed. Overrides superclass method. */
156     public boolean canRename() {
157         return getFileEntry().isDeleteAllowed ();
158     }
159
160     /** Returns all the item in addition to "normal" cookies. Overrides superclass method. */
161     @SuppressWarnings JavaDoc("unchecked")
162     public <T extends Node.Cookie> T getCookie(Class JavaDoc<T> cls) {
163         if (cls.isInstance(getFileEntry())) return (T) getFileEntry();
164         if (cls == PropertiesLocaleNode.class) return (T) this;
165         return super.getCookie(cls);
166     }
167
168     /** List new types that can be created in this node. Overrides superclass method.
169      * @return new types
170      */

171     public NewType[] getNewTypes () {
172         return new NewType[] {
173             new NewType() {
174
175                 /** Getter for name property. */
176                 public String JavaDoc getName() {
177                     return NbBundle.getBundle(PropertiesLocaleNode.class).getString("LAB_NewPropertyAction");
178                 }
179                 
180                 /** Gets help context. */
181                 public HelpCtx getHelpCtx() {
182                     return new HelpCtx(Util.HELP_ID_ADDING);
183                 }
184
185                 /** Creates new type. */
186                 public void create() throws IOException JavaDoc {
187                     final Dialog JavaDoc[] dialog = new Dialog JavaDoc[1];
188                     final Element.ItemElem item = new Element.ItemElem(
189                         null,
190                         new Element.KeyElem(null, ""), // NOI18N
191
new Element.ValueElem(null, ""), // NOI18N
192
new Element.CommentElem(null, "") // NOI18N
193
);
194                     final JPanel JavaDoc panel = new PropertyPanel(item);
195
196                     DialogDescriptor dd = new DialogDescriptor(
197                         panel,
198                         NbBundle.getBundle(PropertiesLocaleNode.class).getString("CTL_NewPropertyTitle"),
199                         true,
200                         DialogDescriptor.OK_CANCEL_OPTION,
201                         DialogDescriptor.OK_OPTION,
202                         new ActionListener JavaDoc() {
203                             private boolean bulkFlag = false;
204                             public void actionPerformed(ActionEvent JavaDoc evt) {
205
206                                 // prevent double notification #11364
207
if (bulkFlag) return;
208                                 bulkFlag =true;
209
210                                 // OK pressed
211
if(evt.getSource() == DialogDescriptor.OK_OPTION) {
212                                     dialog[0].setVisible(false);
213                                     dialog[0].dispose();
214
215
216                                     String JavaDoc key = item.getKey();
217                                     String JavaDoc value = item.getValue();
218                                     String JavaDoc comment = item.getComment();
219
220                                     // add key to all entries
221
if(!((PropertiesFileEntry)getFileEntry()).getHandler().getStructure().addItem(key, value, comment)) {
222                                         NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
223                                             MessageFormat.format(
224                                                 NbBundle.getBundle(PropertiesLocaleNode.class).getString("MSG_KeyExists"),
225                                                 new Object JavaDoc[] {
226                                                     item.getKey(),
227                                                     Util.getLocaleLabel(getFileEntry())
228                                                 }
229                                             ),
230                                             NotifyDescriptor.ERROR_MESSAGE);
231                                         DialogDisplayer.getDefault().notify(msg);
232                                     }
233
234                                 // Cancel pressed
235
} else if (evt.getSource() == DialogDescriptor.CANCEL_OPTION) {
236                                     dialog[0].setVisible(false);
237                                     dialog[0].dispose();
238                                 }
239                             }
240                         }
241                     );
242
243                     dialog[0] = DialogDisplayer.getDefault().createDialog(dd);
244                     dialog[0].setVisible(true);
245
246                 }
247                 
248             } // End of annonymous class.
249
};
250     }
251
252     /** Indicates if this node has a customizer. Overrides superclass method.
253      * @return true */

254     public boolean hasCustomizer() {
255         return true;
256     }
257     
258     /** Gets node customizer. Overrides superclass method. */
259     public Component JavaDoc getCustomizer() {
260         return new LocaleNodeCustomizer((PropertiesFileEntry)getFileEntry());
261     }
262     
263     /** Creates paste types for this node. Overrides superclass method. */
264     protected void createPasteTypes(Transferable JavaDoc t, List JavaDoc<PasteType> s) {
265         super.createPasteTypes(t, s);
266         Element.ItemElem item;
267         Node n = NodeTransfer.node(t, NodeTransfer.MOVE);
268         // cut
269
if (n != null && n.canDestroy ()) {
270             item = n.getCookie(Element.ItemElem.class);
271             if (item != null) {
272                 // are we pasting into the same node
273
Node n2 = getChildren().findChild(item.getKey());
274                 if (n == n2) {
275                     return;
276                 }
277                 s.add(new KeyPasteType(item, n, KeyPasteType.MODE_PASTE_WITH_VALUE));
278                 s.add(new KeyPasteType(item, n, KeyPasteType.MODE_PASTE_WITHOUT_VALUE));
279                 return;
280             }
281         }
282         // copy
283
else {
284             item = NodeTransfer.cookie(t, NodeTransfer.COPY, Element.ItemElem.class);
285             if (item != null) {
286                 s.add(new KeyPasteType(item, null, KeyPasteType.MODE_PASTE_WITH_VALUE));
287                 s.add(new KeyPasteType(item, null, KeyPasteType.MODE_PASTE_WITHOUT_VALUE));
288                 return;
289             }
290         }
291     }
292
293     
294     /** Paste type for keys. */
295     private class KeyPasteType extends PasteType {
296         
297         /** Transferred item. */
298         private Element.ItemElem item;
299
300         /** The node to destroy or null. */
301         private Node node;
302
303         /** Paste mode. */
304         int mode;
305
306         /** Paste with value mode. */
307         public static final int MODE_PASTE_WITH_VALUE = 1;
308         
309         /** Paste without value mode. */
310         public static final int MODE_PASTE_WITHOUT_VALUE = 2;
311
312         
313         /** Constructs new <code>KeyPasteType</code> for the specific type of operation paste. */
314         public KeyPasteType(Element.ItemElem item, Node node, int mode) {
315             this.item = item;
316             this.node = node;
317             this.mode = mode;
318         }
319
320         /** Gets name.
321          * @return human presentable name of this paste type. */

322         public String JavaDoc getName() {
323             String JavaDoc pasteKey = mode == 1 ? "CTL_PasteKeyValue" : "CTL_PasteKeyNoValue";
324             return NbBundle.getBundle(PropertiesLocaleNode.class).getString(pasteKey);
325         }
326
327         /** Performs the paste action.
328          * @return <code>Transferable</code> which should be inserted into the clipboard after
329          * paste action. It can be null, which means that clipboard content
330          * should stay the same
331          */

332         public Transferable JavaDoc paste() throws IOException JavaDoc {
333             PropertiesStructure ps = ((PropertiesFileEntry)getFileEntry()).getHandler().getStructure();
334             String JavaDoc value;
335             if (mode == MODE_PASTE_WITH_VALUE)
336                 value = item.getValue();
337             else
338                 value = "";
339             if (ps != null) {
340                 Element.ItemElem newItem = ps.getItem(item.getKey());
341                 if (newItem == null) {
342                     ps.addItem(item.getKey(), value, item.getComment());
343                 }
344                 else {
345                     newItem.setValue(value);
346                     newItem.setComment(item.getComment());
347                 }
348                 if (node != null)
349                     node.destroy();
350             }
351
352             return null;
353         }
354     } // End of inner KeyPasteType class.
355

356 }
357
Popular Tags