KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > wizard > Util


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.wizard;
21
22 import java.util.*;
23 import org.netbeans.api.queries.VisibilityQuery;
24 import org.netbeans.modules.i18n.FactoryRegistry;
25 import org.netbeans.modules.i18n.I18nUtil;
26 import org.netbeans.modules.properties.PropertiesDataObject;
27 import org.netbeans.api.java.classpath.GlobalPathRegistry;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.api.project.Project;
30 import org.openide.filesystems.FileStateInvalidException;
31 import org.openide.loaders.DataFolder;
32 import org.openide.loaders.DataObject;
33 import org.openide.loaders.DataObjectNotFoundException;
34 import org.openide.loaders.DataFilter;
35 import org.openide.nodes.Node;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.AbstractNode;
38 import org.openide.nodes.FilterNode;
39 import org.openide.util.*;
40 import org.openide.cookies.EditorCookie;
41 import org.openide.filesystems.FileObject;
42 import org.netbeans.api.project.FileOwnerQuery;
43
44 /**
45  * Bundle access, ...
46  *
47  * @author Petr Kuzel
48  */

49 final class Util extends org.netbeans.modules.i18n.Util {
50     
51     public static String JavaDoc getString(String JavaDoc key) {
52         return NbBundle.getMessage(org.netbeans.modules.i18n.wizard.Util.class, key);
53     }
54     
55     public static char getChar(String JavaDoc key) {
56         return getString(key).charAt(0);
57     }
58
59     // Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60

61     /**
62      * Create empty settings used in i18n wizards.
63      */

64     public static Map createWizardSourceMap() {
65         return new TreeMap(new DataObjectComparator());
66     }
67     
68     /**
69      * Create settings based on selected nodes. Finds all accepted data objects.
70      * Used by actions to populate wizard.
71      * @param activatedNodes selected nodes
72      * @return map with accepted data objects as keys or empty map if no such
73      * data objec were found.
74      */

75     public static Map createWizardSourceMap(Node[] activatedNodes) {
76         Map sourceMap = createWizardSourceMap();
77         
78         if (activatedNodes != null && activatedNodes.length > 0) {
79             final VisibilityQuery visQuery = VisibilityQuery.getDefault();
80             for (int i = 0; i < activatedNodes.length; i++) {
81                 DataObject dobj = (DataObject) activatedNodes[i].getCookie(DataObject.class);
82                 if (dobj != null && !visQuery.isVisible(dobj.getPrimaryFile())) {
83                     continue;
84                 }
85
86                 DataObject.Container container = (DataObject.Container) activatedNodes[i].getCookie(DataObject.Container.class);
87                 
88                 if (container != null) {
89                     Iterator it = I18nUtil.getAcceptedDataObjects(container).iterator();
90                     
91                     while(it.hasNext()) {
92                         addSource(sourceMap, (DataObject)it.next());
93                     }
94                 }
95
96                 if (dobj == null) continue;
97
98                 if (FactoryRegistry.hasFactory(dobj.getClass())) {
99                     addSource(sourceMap, dobj);
100                 }
101             }
102         }
103         
104         return sourceMap;
105     }
106     
107     /** Adds source to source map (I18N wizard settings). If there is already no change is done.
108      * If it's added anew then it is tried to find correspondin reousrce, i.e.
109      * first resource from the same folder.
110      * @param sourceMap settings where to add teh sources
111      * @param source source to add */

112     public static void addSource(Map sourceMap, DataObject source) {
113         if(sourceMap.containsKey(source))
114             return;
115         
116         DataFolder folder = source.getFolder();
117         
118         if(folder == null) {
119             sourceMap.put(source, null);
120             return;
121         }
122
123         // try to associate Bundle file
124

125         DataObject[] children = folder.getChildren();
126         
127         for(int i = 0; i < children.length; i++) {
128             if(children[i] instanceof PropertiesDataObject) { // PENDING
129
sourceMap.put(source, new SourceData(children[i]));
130                 return;
131             }
132         }
133         
134         // No resource found in the same folder.
135
sourceMap.put(source, null);
136     }
137
138     /** Shared enableness logic. Either DataObject.Container or EditorCookie must be present on all nodes.*/
139     static boolean wizardEnabled(Node[] activatedNodes) {
140         if (activatedNodes == null || activatedNodes.length == 0) {
141             return false;
142         }
143
144         for (int i = 0; i<activatedNodes.length; i++) {
145             Object JavaDoc o;
146             DataObject dobj = null;
147             Node node = activatedNodes[i];
148             
149             /*
150              * This block of code fixes IssueZilla bug #63461:
151              *
152              * Apisupport modules visualizes the contents of layer.xml
153              * in project view. The popup for folders in the layer.xml
154              * contains Tools->Internationalize->* actions.
155              *
156              * Generally should hide on nonlocal files, I suppose.
157              *
158              * Local files are recognized by protocol of the corresponding URL -
159              * local files are those that have protocol "file".
160              */

161             o = node.getCookie(DataObject.class);
162             if (o != null) {
163                 dobj = (DataObject) o;
164                 FileObject primaryFile = dobj.getPrimaryFile();
165                 
166                 boolean isLocal;
167                 try {
168                     isLocal = !primaryFile.isVirtual()
169                               && primaryFile.isValid()
170                               && primaryFile.getURL().getProtocol()
171                                       .equals("file"); //NOI18N
172
} catch (FileStateInvalidException ex) {
173                     isLocal = false;
174                 }
175                 
176                 if (isLocal == false) {
177                     return false;
178                 }
179             }
180             
181             Object JavaDoc container = node.getCookie(DataObject.Container.class);
182             if (container != null) continue;
183 // if (node.getCookie(EditorCookie.class) == null) {
184
// return false;
185
// }
186

187         if (dobj == null) return false;
188         
189         // check that the node has project
190
if (FileOwnerQuery.getOwner(dobj.getPrimaryFile()) == null) return false;
191         }
192         return true;
193     }
194
195     /**
196      * Compare data objects according their package and name.
197      */

198     private static class DataObjectComparator implements Comparator {
199
200         /** Implements <code>Comparator</code> interface. */
201         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
202             if(!(o1 instanceof DataObject) || !(o2 instanceof DataObject))
203                 return 0;
204             
205             DataObject d1 = (DataObject)o1;
206             DataObject d2 = (DataObject)o2;
207             
208             if(d1 == d2)
209                 return 0;
210             
211             if(d1 == null)
212                 return -1;
213             
214             if(d2 == null)
215                 return 1;
216
217             //return d1.getPrimaryFile().getPackageName('.').compareTo(d2.getPrimaryFile().getPackageName('.'));
218
return d1.getPrimaryFile().getPath().compareTo( d2.getPrimaryFile().getPath() );
219         }
220         
221         /** Implements <code>Comparator</code> interface method. */
222         public boolean equals(Object JavaDoc obj) {
223             if(this == obj)
224                 return true;
225             else
226                 return false;
227         }
228     }
229
230 }
231
Popular Tags