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: * UIUtilities.java
043: *
044: * Created on June 29, 2006, 3:11 PM
045: *
046: * To change this template, choose Tools | Template Manager
047: * and open the template in the editor.
048: */
049:
050: package org.netbeans.modules.xml.schema.abe;
051:
052: import java.awt.Color;
053: import java.awt.Component;
054: import java.awt.Cursor;
055: import java.awt.Dimension;
056: import java.awt.Font;
057: import java.awt.Point;
058: import java.awt.Rectangle;
059: import java.util.ArrayList;
060: import java.util.List;
061: import javax.swing.Icon;
062: import javax.swing.JFrame;
063: import javax.swing.JLabel;
064: import javax.swing.JPanel;
065: import javax.swing.JViewport;
066: import javax.swing.SwingConstants;
067: import javax.swing.SwingUtilities;
068: import org.netbeans.modules.xml.axi.AXIComponent;
069: import org.netbeans.modules.xml.axi.AXIModel;
070: import org.netbeans.modules.xml.axi.AXIModelFactory;
071: import org.netbeans.modules.xml.axi.AXIType;
072: import org.netbeans.modules.xml.axi.AbstractElement;
073: import org.netbeans.modules.xml.axi.Attribute;
074: import org.netbeans.modules.xml.axi.ContentModel;
075: import org.netbeans.modules.xml.axi.Element;
076: import org.netbeans.modules.xml.axi.datatype.Datatype;
077: import org.netbeans.modules.xml.schema.model.SchemaComponent;
078: import org.netbeans.modules.xml.xam.Model;
079: import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie;
080: import org.openide.filesystems.FileObject;
081: import org.openide.loaders.DataObject;
082: import org.openide.loaders.DataObjectNotFoundException;
083: import org.openide.util.NbBundle;
084:
085: /**
086: *
087: * @author girix
088: */
089: public class UIUtilities {
090:
091: public static String standardizeString(String str) {
092: str = str.toLowerCase();
093: StringBuffer strbuff = new StringBuffer(str);
094: strbuff.replace(0, 1, str.substring(0, 1).toUpperCase());
095: return new String(strbuff);
096: }
097:
098: public static String getConstraintsString(String minOccurs,
099: String maxOccurs) {
100: int min;
101: int max;
102:
103: if (minOccurs == null) {
104: min = 1;
105: } else {
106: try {
107: min = Integer.parseInt(minOccurs);
108: } catch (NumberFormatException e) {
109: min = 1;
110: }
111: }
112:
113: if (maxOccurs == null) {
114: max = 1;
115: } else if (maxOccurs.equalsIgnoreCase("unbounded")) { //noi18n
116: max = Integer.MAX_VALUE;
117: } else {
118: try {
119: max = Integer.parseInt(maxOccurs);
120: } catch (NumberFormatException e) {
121: max = 1;
122: }
123: }
124:
125: String str;
126: if (min == max) {
127: if (max == 1) {
128: return "";
129: }
130: return "[" + min + ".." + max + "]";
131: }
132: if (min < max) {
133: String maxStr;
134: if (max == Integer.MAX_VALUE)
135: maxStr = "*"; //noi18n
136: else
137: maxStr = "" + max;
138: return "[" + min + ".." + maxStr + "]";
139: }
140: //min > max?...error case. Should not happen
141: return null;
142: }
143:
144: private static final String LOC_SIMPLE_TYPE = NbBundle.getMessage(
145: UIUtilities.class, "LBL_SIMPLE_TYPE");
146: private static final String LOC_COMPLEX_TYPE = NbBundle.getMessage(
147: UIUtilities.class, "LBL_COMPLEX_TYPE");
148: private static final String LOC_FROM = NbBundle.getMessage(
149: UIUtilities.class, "LBL_FROM");
150: private static final String LOC_BELONGS_TO = NbBundle.getMessage(
151: UIUtilities.class, "LBL_INSTANCE_OF");
152:
153: public static JLabel getContentModelInfoLabel(
154: AXIComponent component, boolean simpleTypeOnly,
155: boolean withIcon, final InstanceUIContext context) {
156: HyperlinkLabel contentModelInfoLabel = new HyperlinkLabel();
157: contentModelInfoLabel.setForeground(new Color(139, 139, 139));
158:
159: Font font = contentModelInfoLabel.getFont();
160: font = new Font(font.getFontName(), Font.PLAIN,
161: InstanceDesignConstants.PROPS_FONT_SIZE);
162: contentModelInfoLabel.setFont(font);
163:
164: if (component instanceof Element) {
165: Element element = (Element) component;
166: AXIType dt = element.getType();
167:
168: if (element.isReference()) {
169: //this is for element reference
170: try {
171: Element aelm = ((Element) getSuperDefn(element));
172: while (aelm.isReference())
173: aelm = (Element) aelm.getReferent();
174: contentModelInfoLabel.setText(aelm.getName());
175: contentModelInfoLabel
176: .setIcon(getImageIcon("element.png"));
177: contentModelInfoLabel.setToolTipText(NbBundle
178: .getMessage(UIUtilities.class,
179: "TTP_REFERENCE_TO_GE", element));
180: final AbstractElement tmpaelm = aelm;
181: contentModelInfoLabel
182: .setHyperlinkClickHandler(new HyperlinkLabel.HyperlinkClickHandler() {
183: public void handleClick() {
184: showDefinition(context, tmpaelm,
185: false);
186: }
187:
188: });
189: return contentModelInfoLabel;
190: } catch (Throwable e) {
191: //might get casting exception so ignore
192: return null;
193: }
194: }
195:
196: if (dt == null)
197: return null;
198: String value = dt.getName();
199: if (value == null) {
200: contentModelInfoLabel.setText(NbBundle.getMessage(
201: UIUtilities.class, "LBL_ANONYMOUS_TYPE"));
202: return contentModelInfoLabel;
203: }
204:
205: contentModelInfoLabel.setText(value);
206: String typeStr = null;
207: typeStr = (dt instanceof Datatype) ? LOC_SIMPLE_TYPE
208: : LOC_COMPLEX_TYPE;
209: if (withIcon) {
210: String iconStr = (dt instanceof Datatype) ? "simpletype.png"
211: : "complextype.png";
212: contentModelInfoLabel.setIcon(getImageIcon(iconStr));
213: }
214: String infoStr = " " + LOC_BELONGS_TO + " " + typeStr
215: + ": " + value + " ";
216: contentModelInfoLabel.setToolTipText(infoStr);
217: if (dt instanceof ContentModel) {
218: final ContentModel cm = (ContentModel) dt;
219: contentModelInfoLabel
220: .setHyperlinkClickHandler(new HyperlinkLabel.HyperlinkClickHandler() {
221: public void handleClick() {
222: showDefinition(context, cm, false);
223: }
224:
225: });
226: }
227: return contentModelInfoLabel;
228: }
229:
230: if (!simpleTypeOnly) {
231: final ContentModel cm = component.getContentModel();
232: if (cm != null) {
233: String type = getContentModelTypeString(cm.getType());
234: String locFromStr = (component instanceof AbstractElement) ? LOC_BELONGS_TO
235: : LOC_FROM;
236: String infoStr = " " + locFromStr + " " + type + ": "
237: + cm.getName() + " ";
238: contentModelInfoLabel.setToolTipText(infoStr);
239: contentModelInfoLabel.setText(cm.getName());
240: if (withIcon)
241: contentModelInfoLabel
242: .setIcon(getContentModelTypeIcon(cm
243: .getType()));
244: contentModelInfoLabel
245: .setHyperlinkClickHandler(new HyperlinkLabel.HyperlinkClickHandler() {
246: public void handleClick() {
247: showDefinition(context, cm, false);
248: }
249:
250: });
251: return contentModelInfoLabel;
252: }
253: }
254: return null;
255: }
256:
257: public static org.netbeans.modules.xml.xam.Component getSuperDefn(
258: AXIComponent axiComponent) {
259: AXIComponent original = axiComponent.getOriginal();
260: if (original == axiComponent) {
261: if (original instanceof Element) {
262: Element elm = (Element) original;
263: if (elm.isReference())
264: return elm.getReferent();
265: }
266: if (original instanceof Attribute) {
267: Attribute attr = (Attribute) original;
268: if (attr.isReference())
269: return attr.getReferent();
270: }
271: return null;
272: }
273: return original;
274: }
275:
276: public static void showDefinition(InstanceUIContext context,
277: AXIComponent axiComponent, boolean showSuper) {
278: if (context != null) {
279: setBusyCursor(context);
280: try {
281: FileObject fo = (FileObject) axiComponent.getModel()
282: .getSchemaModel().getModelSource().getLookup()
283: .lookup(FileObject.class);
284: DataObject sdo = null;
285: try {
286: sdo = DataObject.find(fo);
287: } catch (DataObjectNotFoundException ex) {
288: return;
289: }
290: if (sdo == null)
291: return;
292: ViewComponentCookie vcc = (ViewComponentCookie) sdo
293: .getCookie(ViewComponentCookie.class);
294: if (vcc != null) {
295: AXIComponent tmp = axiComponent;
296: if (showSuper)
297: tmp = (AXIComponent) getSuperDefn(axiComponent);
298: vcc.view(ViewComponentCookie.View.SUPER, tmp);
299: }
300: } finally {
301: setDefaultCursor(context);
302: }
303: }
304: }
305:
306: public static String getContentModelTypeString(
307: ContentModel.ContentModelType cmType) {
308: switch (cmType) {
309: case COMPLEX_TYPE:
310: return NbBundle.getMessage(UIUtilities.class,
311: "LBL_COMPLEX_TYPE"); //noi8n
312: case GROUP:
313: return NbBundle.getMessage(UIUtilities.class, "LBL_GROUP"); //noi8n
314: case ATTRIBUTE_GROUP:
315: return NbBundle.getMessage(UIUtilities.class,
316: "LBL_ATTRIBUTE_GROUP"); //noi8n
317: }
318: return "";
319: }
320:
321: public static Icon getContentModelTypeIcon(
322: ContentModel.ContentModelType cmType) {
323: switch (cmType) {
324: case COMPLEX_TYPE:
325: return new javax.swing.ImageIcon(
326: UIUtilities.class
327: .getResource("/org/netbeans/modules/xml/schema/abe/resources/complextype.png"));
328: case GROUP:
329: return new javax.swing.ImageIcon(
330: UIUtilities.class
331: .getResource("/org/netbeans/modules/xml/schema/abe/resources/group.png"));
332: case ATTRIBUTE_GROUP:
333: return new javax.swing.ImageIcon(
334: UIUtilities.class
335: .getResource("/org/netbeans/modules/xml/schema/abe/resources/attributeGroup.png"));
336: }
337: return null;
338: }
339:
340: public static Icon getImageIcon(String iconFileName) {
341: return new javax.swing.ImageIcon(
342: UIUtilities.class
343: .getResource("/org/netbeans/modules/xml/schema/abe/resources/"
344: + iconFileName));
345: }
346:
347: public static void scrollViewTo(Component comp,
348: InstanceUIContext context) {
349: if (!context.getTopComponent().isShowing())
350: return;
351: JViewport viewPort = context.getInstanceDesignerScrollPane()
352: .getViewport();
353: Rectangle rect = comp.getBounds();
354: rect = SwingUtilities.convertRectangle(comp.getParent(), rect,
355: viewPort);
356: rect.height += 100;
357: //rect.width += 100;
358: viewPort.scrollRectToVisible(rect);
359: }
360:
361: public static AXIComponent findMatchingAXIComponent(
362: SchemaComponent c) {
363: AXIComponent matchingComponent = null;
364: AXIModel model = AXIModelFactory.getDefault().getModel(
365: c.getModel());
366: assert model != null;
367: if (model.getState() == Model.State.VALID) {
368: List<SchemaComponent> pathFromRoot = findSchemaComponentsFromRoot(c);
369: matchingComponent = findClosestAXIComponent(pathFromRoot,
370: model);
371: }
372: return matchingComponent;
373: }
374:
375: private static List<SchemaComponent> findSchemaComponentsFromRoot(
376: SchemaComponent s) {
377: List<SchemaComponent> components = new ArrayList<SchemaComponent>();
378: components.add(0, s);
379: SchemaComponent currentSC = s;
380: while (currentSC.getParent() != currentSC.getModel()
381: .getRootComponent()) {
382: currentSC = currentSC.getParent();
383: components.add(0, currentSC);
384: }
385: return components;
386: }
387:
388: private static AXIComponent findClosestAXIComponent(
389: List<SchemaComponent> components, AXIModel model) {
390: assert !components.isEmpty();
391: SchemaComponent root = components.remove(0);
392: AXIComponent lastFound = findChildComponent(root, model
393: .getRoot());
394: if (lastFound != null) {
395: for (SchemaComponent sc : components) {
396: AXIComponent newRoot = findChildComponent(sc, lastFound);
397: if (newRoot != null) {
398: lastFound = newRoot;
399: }
400: }
401: }
402: return lastFound;
403: }
404:
405: private static AXIComponent findChildComponent(
406: SchemaComponent component, AXIComponent searchFrom) {
407: AXIComponent found = null;
408: for (AXIComponent axiComponent : searchFrom.getChildren()) {
409: if (axiComponent.getPeer() == component) {
410: found = axiComponent;
411: }
412: }
413: return found;
414: }
415:
416: public static void showBulbMessage(String message,
417: InstanceUIContext context) {
418: showBulbMessageFor(message, context, null);
419: }
420:
421: public static void showBulbMessageFor(String message,
422: InstanceUIContext context, Component messageForComp) {
423: showMessage(UIUtilities.getImageIcon("bulb.png"), null, null,
424: message, context, null, messageForComp);
425: }
426:
427: public static void showErrorMessage(String message,
428: InstanceUIContext context) {
429: showErrorMessageFor(message, context, null);
430: }
431:
432: public static void showErrorMessageFor(String message,
433: InstanceUIContext context, Component messageForComp) {
434: showMessage(UIUtilities.getImageIcon("error.png"), Color.RED,
435: null, message, context, null, messageForComp);
436: }
437:
438: public static void showErrorMessage(String message,
439: InstanceUIContext context, JPanel glass) {
440: showErrorMessageFor(message, context, glass, null);
441: }
442:
443: public static void showErrorMessageFor(String message,
444: InstanceUIContext context, JPanel glass,
445: Component messageForComp) {
446: showMessage(UIUtilities.getImageIcon("error.png"), Color.RED,
447: null, message, context, glass, messageForComp);
448: }
449:
450: public static void showMessage(Icon icon, Color color,
451: String message, InstanceUIContext context) {
452: showMessageFor(icon, color, message, context, null);
453: }
454:
455: public static void showMessageFor(Icon icon, Color color,
456: String message, InstanceUIContext context,
457: Component messageForComp) {
458: showMessage(icon, color, null, message, context, null, null);
459: }
460:
461: static JLabel infoLabel;
462: static JPanel glassReference;
463:
464: protected static void showMessage(Icon icon, Color foreGroundColor,
465: Color backgroundColor, String message,
466: InstanceUIContext context, JPanel glass,
467: Component messageForComp) {
468: if (message == null)
469: return;
470: if ((infoLabel != null) && (infoLabel.getParent() != null)) {
471: if (infoLabel.getText().equals(message))
472: return;
473: }
474: infoLabel = null;
475: if (icon != null)
476: infoLabel = new TranslucentLabel(icon, SwingConstants.LEFT);
477: else
478: infoLabel = new TranslucentLabel(" ", SwingConstants.LEFT);
479:
480: Font font = infoLabel.getFont();
481: font = new Font(font.getName(), Font.BOLD, font.getSize());
482: if (foreGroundColor != null)
483: infoLabel.setForeground(foreGroundColor);
484: else
485: infoLabel.setForeground(Color.black);
486:
487: if (backgroundColor != null)
488: infoLabel.setBackground(backgroundColor);
489: else
490: infoLabel
491: .setBackground(InstanceDesignConstants.LIGHT_YELLOW);
492:
493: infoLabel.setFont(font);
494: infoLabel.setText(message);
495: Component panel = (messageForComp != null) ? messageForComp
496: : context.getNamespacePanel();
497: Rectangle rect = panel.getBounds();
498: if (glass == null)
499: glass = NBGlassPaneAccessSupport.getNBGlassPane(context
500: .getInstanceDesignerPanel());
501: else
502: glassReference = glass;
503: if (glass == null)
504: return;
505: rect = SwingUtilities.convertRectangle(panel.getParent(), rect,
506: glass);
507: glass.add(infoLabel);
508: infoLabel.setOpaque(false);
509: if (messageForComp != null) {
510: rect.y -= 20;
511: Dimension dim = infoLabel.getPreferredSize();
512: rect.width = dim.width;
513: rect.height = dim.height;
514: }
515: infoLabel.setBounds(rect);
516: glass.setVisible(true);
517: }
518:
519: public static void hideGlassMessage(boolean disposeGlass) {
520: if ((infoLabel != null) && (glassReference != null)) {
521: glassReference.remove(infoLabel);
522: glassReference.revalidate();
523: glassReference.repaint();
524: }
525:
526: if (disposeGlass)
527: NBGlassPaneAccessSupport.forceDisposeNBGlassPane();
528: }
529:
530: public static void hideGlassMessage() {
531: hideGlassMessage(true);
532: }
533:
534: public static List<Point> getBrokenTapePoints(Point start, int end,
535: int xgap, int ygap, boolean rightHanded) {
536: List<Point> result = new ArrayList<Point>();
537: int begin = start.y;
538: int stop = end;
539: int current = begin;
540: boolean sharpeEdge = true;
541: result.add(new Point(start.x, current));
542: current += ygap;
543: while (current < stop) {
544: if (sharpeEdge) {
545: if (rightHanded)
546: result.add(new Point((start.x + xgap), current));
547: else
548: result.add(new Point((start.x - xgap), current));
549: sharpeEdge = false;
550: } else {
551: if (rightHanded)
552: result.add(new Point((start.x - xgap), current));
553: else
554: result.add(new Point((start.x + xgap), current));
555: //result.add(new Point((start.x), current));
556: sharpeEdge = true;
557: }
558: current += ygap;
559: }
560: result.add(new Point(start.x, end));
561: return result;
562: }
563:
564: public static String getUniqueName(String name, AXIComponent elm) {
565: int count = 1;
566: String result = null;
567: String nowName = name;
568: if (elm.getChildren().size() < 0)
569: return name;
570: while (result == null) {
571: boolean gotResult = true;
572: for (AXIComponent child : elm.getChildren()) {
573: if (child.toString().indexOf(nowName) != -1) {
574: nowName = name + count++;
575: gotResult = false;
576: break;
577: }
578: }
579: if (gotResult) {
580: result = nowName;
581: break;
582: }
583: }
584: return result;
585: }
586:
587: public static void setBusyCursor(InstanceUIContext context) {
588: if (context == null)
589: return;
590: JFrame NBFRAME = NBGlassPaneAccessSupport.getNBFRAME(context
591: .getTopComponent());
592: if (NBFRAME == null)
593: return;
594: NBFRAME.setCursor(new Cursor(Cursor.WAIT_CURSOR));
595: }
596:
597: public static void setDefaultCursor(InstanceUIContext context) {
598: if (context == null)
599: return;
600: JFrame NBFRAME = NBGlassPaneAccessSupport.getNBFRAME(context
601: .getTopComponent());
602: if (NBFRAME == null)
603: return;
604: NBFRAME.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
605: }
606:
607: }
|