001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.i18n.wizard;
043:
044: import java.util.Comparator;
045: import java.util.Map;
046: import java.util.TreeMap;
047: import org.netbeans.api.queries.VisibilityQuery;
048: import org.netbeans.modules.i18n.FactoryRegistry;
049: import org.netbeans.modules.i18n.I18nUtil;
050: import org.netbeans.modules.properties.PropertiesDataObject;
051: import org.openide.filesystems.FileStateInvalidException;
052: import org.openide.loaders.DataFolder;
053: import org.openide.loaders.DataObject;
054: import org.openide.nodes.Node;
055: import org.openide.util.NbBundle;
056: import org.openide.filesystems.FileObject;
057: import org.netbeans.api.project.FileOwnerQuery;
058:
059: /**
060: * Bundle access, ...
061: *
062: * @author Petr Kuzel
063: */
064: final class Util extends org.netbeans.modules.i18n.Util {
065:
066: public static String getString(String key) {
067: return NbBundle.getMessage(
068: org.netbeans.modules.i18n.wizard.Util.class, key);
069: }
070:
071: public static char getChar(String key) {
072: return getString(key).charAt(0);
073: }
074:
075: // Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
076:
077: /**
078: * Create empty settings used in i18n wizards.
079: */
080: public static Map<DataObject, SourceData> createWizardSourceMap() {
081: return new TreeMap<DataObject, SourceData>(
082: new DataObjectComparator());
083: }
084:
085: /**
086: * Create settings based on selected nodes. Finds all accepted data objects.
087: * Used by actions to populate wizard.
088: * @param activatedNodes selected nodes
089: * @return map with accepted data objects as keys or empty map if no such
090: * data objec were found.
091: */
092: public static Map<DataObject, SourceData> createWizardSourceMap(
093: Node[] activatedNodes) {
094: Map<DataObject, SourceData> sourceMap = createWizardSourceMap();
095:
096: if (activatedNodes != null && activatedNodes.length > 0) {
097: final VisibilityQuery visQuery = VisibilityQuery
098: .getDefault();
099: for (Node node : activatedNodes) {
100: DataObject dobj = node.getCookie(DataObject.class);
101: if (dobj != null
102: && !visQuery.isVisible(dobj.getPrimaryFile())) {
103: continue;
104: }
105:
106: DataObject.Container container = node
107: .getCookie(DataObject.Container.class);
108: if (container != null) {
109: for (DataObject dataObj : I18nUtil
110: .getAcceptedDataObjects(container)) {
111: addSource(sourceMap, dataObj);
112: }
113: }
114:
115: if (dobj == null) {
116: continue;
117: }
118:
119: if (FactoryRegistry.hasFactory(dobj.getClass())) {
120: addSource(sourceMap, dobj);
121: }
122: }
123: }
124:
125: return sourceMap;
126: }
127:
128: /** Adds source to source map (I18N wizard settings). If there is already no change is done.
129: * If it's added anew then it is tried to find correspondin reousrce, i.e.
130: * first resource from the same folder.
131: * @param sourceMap settings where to add teh sources
132: * @param source source to add */
133: public static void addSource(Map<DataObject, SourceData> sourceMap,
134: DataObject source) {
135: if (sourceMap.containsKey(source)) {
136: return;
137: }
138:
139: DataFolder folder = source.getFolder();
140:
141: if (folder == null) {
142: sourceMap.put(source, null);
143: return;
144: }
145:
146: // try to associate Bundle file
147:
148: for (DataObject child : folder.getChildren()) {
149: if (child instanceof PropertiesDataObject) { // PENDING
150: sourceMap.put(source, new SourceData(child));
151: return;
152: }
153: }
154:
155: // No resource found in the same folder.
156: sourceMap.put(source, null);
157: }
158:
159: /** Shared enableness logic. Either DataObject.Container or EditorCookie must be present on all nodes.*/
160: static boolean wizardEnabled(Node[] activatedNodes) {
161: if (activatedNodes == null || activatedNodes.length == 0) {
162: return false;
163: }
164:
165: for (Node node : activatedNodes) {
166:
167: /*
168: * This block of code fixes IssueZilla bug #63461:
169: *
170: * Apisupport modules visualizes the contents of layer.xml
171: * in project view. The popup for folders in the layer.xml
172: * contains Tools->Internationalize->* actions.
173: *
174: * Generally should hide on nonlocal files, I suppose.
175: *
176: * Local files are recognized by protocol of the corresponding URL -
177: * local files are those that have protocol "file".
178: */
179: DataObject dobj = node.getCookie(DataObject.class);
180: if (dobj != null) {
181: FileObject primaryFile = dobj.getPrimaryFile();
182:
183: boolean isLocal;
184: try {
185: isLocal = !primaryFile.isVirtual()
186: && primaryFile.isValid()
187: && primaryFile.getURL().getProtocol()
188: .equals("file"); //NOI18N
189: } catch (FileStateInvalidException ex) {
190: isLocal = false;
191: }
192:
193: if (isLocal == false) {
194: return false;
195: }
196: }
197:
198: Object container = node
199: .getCookie(DataObject.Container.class);
200: if (container != null) {
201: continue;
202: }
203: // if (node.getCookie(EditorCookie.class) == null) {
204: // return false;
205: // }
206:
207: if (dobj == null) {
208: return false;
209: }
210:
211: // check that the node has project
212: if (FileOwnerQuery.getOwner(dobj.getPrimaryFile()) == null) {
213: return false;
214: }
215: }
216: return true;
217: }
218:
219: /**
220: * Compare data objects according their package and name.
221: */
222: private static class DataObjectComparator implements
223: Comparator<DataObject> {
224:
225: /** Implements <code>Comparator</code> interface. */
226: public int compare(DataObject d1, DataObject d2) {
227: if (d1 == d2)
228: return 0;
229:
230: if (d1 == null)
231: return -1;
232:
233: if (d2 == null)
234: return 1;
235:
236: //return d1.getPrimaryFile().getPackageName('.').compareTo(d2.getPrimaryFile().getPackageName('.'));
237: return d1.getPrimaryFile().getPath().compareTo(
238: d2.getPrimaryFile().getPath());
239: }
240:
241: /** Implements <code>Comparator</code> interface method. */
242: @Override
243: public boolean equals(Object obj) {
244: return (this == obj);
245: }
246:
247: @Override
248: public int hashCode() {
249: return System.identityHashCode(this);
250: }
251: }
252:
253: }
|