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-2006 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.websvc.rest.wizard;
043:
044: import java.awt.Component;
045: import javax.swing.JLabel;
046: import java.awt.Container;
047: import javax.swing.JComponent;
048: import java.util.Vector;
049: import java.util.Iterator;
050: import java.util.Collection;
051: import java.util.HashMap;
052: import java.util.List;
053: import java.util.Map;
054: import java.util.logging.Level;
055: import java.util.logging.Logger;
056: import org.netbeans.api.java.classpath.ClassPath;
057: import org.netbeans.api.project.Project;
058: import org.netbeans.api.project.ProjectUtils;
059: import org.netbeans.api.project.SourceGroup;
060: import org.netbeans.api.project.Sources;
061: import org.netbeans.modules.websvc.rest.codegen.EntityResourcesGenerator;
062: import org.netbeans.modules.websvc.rest.codegen.model.EntityResourceBean;
063: import org.netbeans.modules.websvc.rest.support.Inflector;
064: import org.netbeans.modules.websvc.rest.support.SourceGroupSupport;
065: import org.openide.WizardDescriptor;
066: import org.openide.filesystems.FileObject;
067: import org.openide.util.Utilities;
068:
069: /**
070: * Copy of j2ee/utilities Util class
071: *
072: * TODO: Should move some of the methods into o.n.m.w.r.support.Utils class
073: * since that's the package used for sharing all the utility classes.
074: *
075: */
076: public class Util {
077: public static final String TYPE_DOC_ROOT = "doc_root"; //NOI18N
078:
079: /*
080: * Changes the text of a JLabel in component from oldLabel to newLabel
081: */
082: public static void changeLabelInComponent(JComponent component,
083: String oldLabel, String newLabel) {
084: JLabel label = findLabel(component, oldLabel);
085: if (label != null) {
086: label.setText(newLabel);
087: }
088: }
089:
090: /*
091: * Hides a JLabel and the component that it is designated to labelFor, if any
092: */
093: public static void hideLabelAndLabelFor(JComponent component,
094: String lab) {
095: JLabel label = findLabel(component, lab);
096: if (label != null) {
097: label.setVisible(false);
098: Component c = label.getLabelFor();
099: if (c != null) {
100: c.setVisible(false);
101: }
102: }
103: }
104:
105: /*
106: * Recursively gets all components in the components array and puts it in allComponents
107: */
108: public static void getAllComponents(Component[] components,
109: Collection allComponents) {
110: for (int i = 0; i < components.length; i++) {
111: if (components[i] != null) {
112: allComponents.add(components[i]);
113: if (((Container) components[i]).getComponentCount() != 0) {
114: getAllComponents(((Container) components[i])
115: .getComponents(), allComponents);
116: }
117: }
118: }
119: }
120:
121: /*
122: * Recursively finds a JLabel that has labelText in comp
123: */
124: public static JLabel findLabel(JComponent comp, String labelText) {
125: Vector allComponents = new Vector();
126: getAllComponents(comp.getComponents(), allComponents);
127: Iterator iterator = allComponents.iterator();
128: while (iterator.hasNext()) {
129: Component c = (Component) iterator.next();
130: if (c instanceof JLabel) {
131: JLabel label = (JLabel) c;
132: if (label.getText().equals(labelText)) {
133: return label;
134: }
135: }
136: }
137: return null;
138: }
139:
140: /**
141: * Returns the simple class for the passed fully-qualified class name.
142: */
143: public static String getClassName(String fqClassName) {
144: int dot = fqClassName.lastIndexOf("."); // NOI18N
145: if (dot >= 0 && dot < fqClassName.length() - 1) {
146: return fqClassName.substring(dot + 1);
147: } else {
148: return fqClassName;
149: }
150: }
151:
152: /**
153: * Returns the package name of the passed fully-qualified class name.
154: */
155: public static String getPackageName(String fqClassName) {
156: int dot = fqClassName.lastIndexOf("."); // NOI18N
157: if (dot >= 0 && dot < fqClassName.length() - 1) {
158: return fqClassName.substring(0, dot);
159: } else {
160: return ""; // NOI18N
161: }
162: }
163:
164: /**
165: * Returns the SourceGroup of the passesd project which contains the
166: * fully-qualified class name.
167: */
168: public static SourceGroup getClassSourceGroup(Project project,
169: String fqClassName) {
170: String classFile = fqClassName.replace('.', '/') + ".java"; // NOI18N
171: SourceGroup[] sourceGroups = SourceGroupSupport
172: .getJavaSourceGroups(project);
173:
174: for (SourceGroup sourceGroup : sourceGroups) {
175: FileObject classFO = sourceGroup.getRootFolder()
176: .getFileObject(classFile);
177: if (classFO != null) {
178: return sourceGroup;
179: }
180: }
181: return null;
182: }
183:
184: static final String WIZARD_PANEL_CONTENT_DATA = "WizardPanel_contentData"; // NOI18N
185: static final String WIZARD_PANEL_CONTENT_SELECTED_INDEX = "WizardPanel_contentSelectedIndex"; //NOI18N;
186:
187: public static void mergeSteps(WizardDescriptor wizard,
188: WizardDescriptor.Panel[] panels, String[] steps) {
189: Object prop = wizard.getProperty(WIZARD_PANEL_CONTENT_DATA);
190: String[] beforeSteps;
191: int offset;
192: if (prop instanceof String[]) {
193: beforeSteps = (String[]) prop;
194: offset = beforeSteps.length;
195: if (offset > 0 && ("...".equals(beforeSteps[offset - 1]))) {// NOI18N
196: offset--;
197: }
198: } else {
199: beforeSteps = null;
200: offset = 0;
201: }
202: String[] resultSteps = new String[(offset) + panels.length];
203: for (int i = 0; i < offset; i++) {
204: resultSteps[i] = beforeSteps[i];
205: }
206: setSteps(panels, steps, resultSteps, offset);
207: }
208:
209: private static void setSteps(WizardDescriptor.Panel[] panels,
210: String[] steps, String[] resultSteps, int offset) {
211: int n = steps == null ? 0 : steps.length;
212: for (int i = 0; i < panels.length; i++) {
213: final JComponent component = (JComponent) panels[i]
214: .getComponent();
215: String step = i < n ? steps[i] : null;
216: if (step == null) {
217: step = component.getName();
218: }
219: component.putClientProperty(WIZARD_PANEL_CONTENT_DATA,
220: resultSteps);
221: component
222: .putClientProperty(
223: WIZARD_PANEL_CONTENT_SELECTED_INDEX,
224: new Integer(i));
225: component.getAccessibleContext().setAccessibleDescription(
226: step);
227: resultSteps[i + offset] = step;
228: }
229: }
230:
231: public static void setSteps(WizardDescriptor.Panel[] panels,
232: String[] steps) {
233: setSteps(panels, steps, steps, 0);
234: }
235:
236: public static String lowerFirstChar(String name) {
237: if (name.length() == 0)
238: return name;
239:
240: StringBuilder sb = new StringBuilder(name);
241: sb.setCharAt(0, Character.toLowerCase(name.charAt(0)));
242: return sb.toString();
243: }
244:
245: public static String upperFirstChar(String name) {
246: if (name.length() == 0)
247: return name;
248:
249: StringBuilder sb = new StringBuilder(name);
250: sb.setCharAt(0, Character.toUpperCase(name.charAt(0)));
251: return sb.toString();
252: }
253:
254: public static String deriveResourceClassName(String resourceName) {
255: return upperFirstChar(resourceName)
256: + EntityResourcesGenerator.RESOURCE_SUFFIX;
257: }
258:
259: public static String deriveUri(String resourceName,
260: String currentUri) {
261: if (resourceName.length() == 0 || currentUri == null
262: || currentUri.length() == 0
263: || currentUri.charAt(0) != '/') {
264: return currentUri;
265: }
266: resourceName = lowerFirstChar(resourceName);
267: resourceName = pluralize(resourceName);
268: String root = currentUri;
269: String params = null;
270: int lastIndex = currentUri.indexOf('{');
271: if (lastIndex > -1) {
272: params = root.substring(lastIndex - 1);
273: root = root.substring(0, lastIndex - 1); /* ../{id} we are excluding the ending '/' */
274: if (root.length() == 0) {
275: return currentUri;
276: }
277: }
278:
279: lastIndex = root.lastIndexOf('/');
280: if (lastIndex == -1) {
281: return currentUri;
282: }
283:
284: root = root.substring(0, lastIndex);
285: String ret = root + "/" + resourceName;
286: if (params != null) {
287: ret += params;
288: }
289: return ret;
290: }
291:
292: public static String deriveContainerClassName(String resourceName) {
293: return deriveResourceClassName(Inflector.getInstance()
294: .pluralize((resourceName)));
295: }
296:
297: public static String singularize(String name) {
298: // get around inflector bug: 'address' -> 'addres'
299: if (name.endsWith("ss")) {
300: String plural = Inflector.getInstance().pluralize(name);
301: if (!name.equals(plural)) {
302: return name;
303: }
304: }
305: return Inflector.getInstance().singularize(name);
306: }
307:
308: public static String pluralize(String name) {
309: return Inflector.getInstance().pluralize(singularize(name));
310: }
311:
312: public static String getPluralName(EntityResourceBean bean) {
313: if (bean.isContainer()) {
314: return bean.getName();
315: } else {
316: return pluralize(bean.getName());
317: }
318: }
319:
320: public static String getSingularName(EntityResourceBean bean) {
321: if (bean.isContainer()) {
322: return singularize(bean.getName());
323: } else {
324: return bean.getName();
325: }
326: }
327:
328: public static String[] ensureTypes(String[] types) {
329: if (types == null || types.length == 0
330: || types[0].length() == 0) {
331: types = new String[] { String.class.getName() };
332: }
333: return types;
334: }
335:
336: public static SourceGroup[] getSourceGroups(Project project) {
337: SourceGroup[] sourceGroups = null;
338:
339: Sources sources = ProjectUtils.getSources(project);
340: SourceGroup[] docRoot = sources.getSourceGroups(TYPE_DOC_ROOT);
341: SourceGroup[] srcRoots = SourceGroupSupport
342: .getJavaSourceGroups(project);
343:
344: if (docRoot != null && srcRoots != null) {
345: sourceGroups = new SourceGroup[docRoot.length
346: + srcRoots.length];
347: System.arraycopy(docRoot, 0, sourceGroups, 0,
348: docRoot.length);
349: System.arraycopy(srcRoots, 0, sourceGroups, docRoot.length,
350: srcRoots.length);
351: }
352:
353: if (sourceGroups == null || sourceGroups.length == 0) {
354: sourceGroups = sources
355: .getSourceGroups(Sources.TYPE_GENERIC);
356: }
357: return sourceGroups;
358: }
359:
360: private static Map<String, Class> primitiveTypes;
361:
362: public static Class getType(Project project, String typeName) {
363: List<ClassPath> classPaths = SourceGroupSupport
364: .gerClassPath(project);
365:
366: for (ClassPath cp : classPaths) {
367: try {
368: Class ret = Util.getPrimitiveType(typeName);
369: if (ret != null) {
370: return ret;
371: }
372: ClassLoader cl = cp.getClassLoader(true);
373: ret = getGenericRawType(typeName, cl);
374: if (ret != null) {
375: return ret;
376: }
377: if (cl != null) {
378: return cl.loadClass(typeName);
379: }
380: } catch (ClassNotFoundException ex) {
381: //Logger.global.log(Level.INFO, ex.getLocalizedMessage(), ex);
382: }
383: }
384: return null;
385: }
386:
387: public static Class getPrimitiveType(String typeName) {
388: if (primitiveTypes == null) {
389: primitiveTypes = new HashMap<String, Class>();
390: primitiveTypes.put("int", Integer.class);
391: primitiveTypes.put("int[]", Integer[].class);
392: primitiveTypes.put("boolean", Boolean.class);
393: primitiveTypes.put("boolean[]", Boolean[].class);
394: primitiveTypes.put("byte", Byte.class);
395: primitiveTypes.put("byte[]", Byte[].class);
396: primitiveTypes.put("char", Character.class);
397: primitiveTypes.put("char[]", Character[].class);
398: primitiveTypes.put("double", Double.class);
399: primitiveTypes.put("double[]", Double[].class);
400: primitiveTypes.put("float", Float.class);
401: primitiveTypes.put("float[]", Float[].class);
402: primitiveTypes.put("long", Long.class);
403: primitiveTypes.put("long[]", Long[].class);
404: primitiveTypes.put("short", Short.class);
405: primitiveTypes.put("short[]", Short[].class);
406: }
407: return primitiveTypes.get(typeName);
408: }
409:
410: public static Class getGenericRawType(String typeName,
411: ClassLoader loader) {
412: int i = typeName.indexOf('<');
413: if (i < 1) {
414: return null;
415: }
416: String raw = typeName.substring(0, i);
417: try {
418: return loader.loadClass(raw);
419: } catch (ClassNotFoundException ex) {
420: Logger.global.log(Level.INFO, "", ex);
421: return null;
422: }
423: }
424:
425: public static boolean isValidPackageName(String packageName) {
426: if (packageName == null || packageName.endsWith(".")) {
427: return false;
428: }
429:
430: String[] segments = packageName.split("\\.");
431: for (String s : segments) {
432: if (!Utilities.isJavaIdentifier(s)) {
433: return false;
434: }
435: }
436: return true;
437: }
438: }
|