KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.netbeans.modules.properties;
22
23
24 import java.awt.Component JavaDoc;
25 import java.awt.datatransfer.Transferable JavaDoc;
26 import java.awt.Dialog JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.text.MessageFormat JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.openide.DialogDescriptor;
36 import org.openide.filesystems.FileLock;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileSystem;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.loaders.DataFolder;
41 import org.openide.loaders.DataNode;
42 import org.openide.loaders.DataObject;
43 import org.openide.nodes.Children;
44 import org.openide.nodes.Node;
45 import org.openide.nodes.NodeTransfer;
46 import org.openide.NotifyDescriptor;
47 import org.openide.DialogDisplayer;
48 import org.openide.cookies.SaveCookie;
49 import org.openide.loaders.FileEntry;
50 import org.openide.util.WeakListeners;
51 import org.openide.util.datatransfer.NewType;
52 import org.openide.util.datatransfer.PasteType;
53 import org.openide.util.NbBundle;
54
55 /**
56  * Node representing a <code>PropertiesDataObject</code>.
57  * Its children ({@link PropertiesLocaleNode}s) represent
58  * the {@link PropertyFileEntry PropertyFileEntries}.
59  *
60  * @author Petr Jiricka, Peter Zavadsky
61  * @see PropertiesDataObject
62  * @see org.openide.loaders.DataNode
63  */

64 public class PropertiesDataNode extends DataNode {
65
66     /**
67      * Listener for changes on <code>propDataObject</code> name and cookie properties.
68      * Changes display name of components accordingly.
69      */

70     private final transient PropertyChangeListener JavaDoc dataObjectListener;
71     
72     /** Creates data node for a given data object.
73      * The provided children object will be used to hold all child nodes.
74      * @param obj object to work with
75      * @param ch children container for the node
76      */

77     public PropertiesDataNode(DataObject dataObject, Children children) {
78         super(dataObject, children);
79         setIconBaseWithExtension("org/netbeans/modules/properties/propertiesObject.png"); // NOI18N
80

81         dataObjectListener = new NameUpdater();
82         dataObject.addPropertyChangeListener(
83                 WeakListeners.propertyChange(dataObjectListener, dataObject));
84     }
85
86     /**
87      * Listener which listens on changes of the set of
88      * <code>PropertiesDataObject</code>'s files.
89      * When the set of files changes, we fire a change of the DataObject's name,
90      * thus forcing update of the display name. We need this update because
91      * the CVS status of the PropertiesDataObject may change when the set
92      * of files is changed.
93      */

94     final class NameUpdater implements PropertyChangeListener JavaDoc {
95         
96         /**
97          */

98         public void propertyChange(PropertyChangeEvent JavaDoc e) {
99             if (DataObject.PROP_FILES.equals(e.getPropertyName())) {
100                 ((PropertiesDataObject) getDataObject()).fireNameChange();
101             }
102         }
103         
104     }
105     
106     /** Gets new types that can be created in this node.
107      * @return array with <code>NewLocaleType</code> */

108     public NewType[] getNewTypes() {
109         return new NewType[] {new NewLocaleType()};
110     }
111     
112     /** Indicates whether this node has customizer. Overrides superclass method.
113      * @return true */

114     public boolean hasCustomizer() {
115         return true;
116     }
117     
118     /** Gets node customizer. Overrides superclass method.
119      * @return <code>BundleNodeCustomizer</code> instance.
120      * @see BundleNodeCustomizer */

121     public Component JavaDoc getCustomizer() {
122         return new BundleNodeCustomizer((PropertiesDataObject)getDataObject());
123     }
124     
125     /** Creates paste types for this node. Overrides superclass method.
126      * @param transferable transferable in clipboard
127      * @param types <code>PasteType</code>'s valid for this node. */

128     public void createPasteTypes(Transferable JavaDoc transferable, List JavaDoc<PasteType> types) {
129         super.createPasteTypes(transferable, types);
130
131         // Copy/paste mode?
132
int mode = NodeTransfer.COPY;
133         
134         Node node = NodeTransfer.node(transferable, mode);
135         
136         if(node == null || !(node instanceof PropertiesLocaleNode)) {
137             // Cut/paste mode?
138
mode = NodeTransfer.MOVE;
139         
140             node = NodeTransfer.node(transferable, mode);
141
142             if(node == null || !(node instanceof PropertiesLocaleNode))
143                 return;
144             
145             PropertiesFileEntry entry = (PropertiesFileEntry)((PropertiesLocaleNode)node).getFileEntry();
146             if(((PropertiesDataObject)getDataObject()).files().contains(entry.getFile())) {
147                 return;
148             }
149         }
150
151         PropertiesFileEntry entry = (PropertiesFileEntry)((PropertiesLocaleNode)node).getFileEntry();
152         types.add(new EntryPasteType(entry, mode));
153     }
154
155     /** Paste type for <code>PropertiesDataNode</code>. */
156     private class EntryPasteType extends PasteType {
157
158         /** Entry to copy/move. */
159         private PropertiesFileEntry entry;
160         
161         /** Flag for copying/moving. */
162         private int flag;
163         
164
165         /** Constructor.
166          * @param entry entry to copy/move
167          * @param flag flag for moving/copying */

168         public EntryPasteType(PropertiesFileEntry entry, int flag) {
169             this.entry = entry;
170             this.flag = flag;
171         }
172         
173         /** Peforms paste action. Implements superclass abstract method.
174          * @exception IOException if error occured */

175         public Transferable JavaDoc paste() throws IOException JavaDoc {
176             DataFolder dataFolder = PropertiesDataNode.this.getDataObject().getFolder();
177             
178             if(dataFolder == null)
179                 return null;
180             
181             FileObject folder = dataFolder.getPrimaryFile();
182             
183             String JavaDoc newName = getDataObject().getPrimaryFile().getName() + Util.getLocaleSuffix(entry);
184             
185             int entryIndex = ((PropertiesDataObject)getDataObject()).getBundleStructure().getEntryIndexByFileName(newName);
186             
187             // Has such item -> find brother.
188
if(entryIndex != -1) {
189                 newName = FileUtil.findFreeFileName(folder, newName, entry.getFile().getExt());
190             }
191             
192             if(flag == NodeTransfer.COPY) {
193                 FileObject fileObject = entry.getFile();
194                 fileObject.copy(folder, newName, fileObject.getExt());
195                 
196             } else if(flag == NodeTransfer.MOVE) {
197                 FileObject fileObject = entry.getFile();
198                 FileLock lock = entry.takeLock();
199                 
200                 // removing secondary entry from original data object
201
((PropertiesDataObject) entry.getDataObject()).removeSecondaryEntry2(entry);
202                 try {
203                     FileObject fo2 = fileObject.move(lock, folder, newName, fileObject.getExt());
204                     try {
205                         // Invokes the method for recognition fo2's primary fila and data object.
206
// Secondary entry in destination data object is created and registered
207
DataObject.find(fo2);
208                     }
209                     catch (Exception JavaDoc e) {
210                     }
211                 } finally {
212                     lock.releaseLock ();
213                 }
214             }
215             
216             return null;
217         }
218         
219     } // End of class EntryPasteType.
220

221
222     /** New type for properties node. It creates new locale for ths bundle. */
223     private class NewLocaleType extends NewType {
224
225         /** Overrides superclass method. */
226         public String JavaDoc getName() {
227             return NbBundle.getBundle(PropertiesDataNode.class).getString("LAB_NewLocaleAction");
228         }
229
230         /** Overrides superclass method. */
231         public void create() throws IOException JavaDoc {
232             final PropertiesDataObject propertiesDataObject = (PropertiesDataObject)getCookie(DataObject.class);
233
234             final Dialog JavaDoc[] dialog = new Dialog JavaDoc[1];
235             final LocalePanel panel = new LocalePanel();
236
237             DialogDescriptor dialogDescriptor = new DialogDescriptor(
238                 panel,
239                 NbBundle.getBundle(PropertiesDataNode.class).getString("CTL_NewLocaleTitle"),
240                 true,
241                 DialogDescriptor.OK_CANCEL_OPTION,
242                 DialogDescriptor.OK_OPTION,
243                 new ActionListener JavaDoc() {
244                     public void actionPerformed(ActionEvent JavaDoc evt) {
245                         if (evt.getSource() == DialogDescriptor.OK_OPTION) {
246                         // OK pressed
247
Util.createLocaleFile(propertiesDataObject, panel.getLocale().toString(), true);
248                         }
249                         dialog[0].setVisible(false);
250                         dialog[0].dispose();
251                     }
252                 }
253             );
254             
255             dialog[0] = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
256             dialog[0].setVisible(true);
257         }
258
259     } // End of NewLocaleType class.
260

261 }
262
Popular Tags