KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > UIUtilities


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  * UIUtilities.java
21  *
22  * Created on June 29, 2006, 3:11 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.xml.schema.abe;
29
30 import java.awt.Color JavaDoc;
31 import java.awt.Component JavaDoc;
32 import java.awt.Cursor JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Font JavaDoc;
35 import java.awt.Point JavaDoc;
36 import java.awt.Rectangle JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.List JavaDoc;
39 import javax.swing.Icon JavaDoc;
40 import javax.swing.JFrame JavaDoc;
41 import javax.swing.JLabel JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.JViewport JavaDoc;
44 import javax.swing.SwingConstants JavaDoc;
45 import javax.swing.SwingUtilities JavaDoc;
46 import org.netbeans.modules.xml.axi.AXIComponent;
47 import org.netbeans.modules.xml.axi.AXIModel;
48 import org.netbeans.modules.xml.axi.AXIModelFactory;
49 import org.netbeans.modules.xml.axi.AXIType;
50 import org.netbeans.modules.xml.axi.AbstractElement;
51 import org.netbeans.modules.xml.axi.Attribute;
52 import org.netbeans.modules.xml.axi.ContentModel;
53 import org.netbeans.modules.xml.axi.Element;
54 import org.netbeans.modules.xml.axi.datatype.Datatype;
55 import org.netbeans.modules.xml.schema.model.SchemaComponent;
56 import org.netbeans.modules.xml.xam.Model;
57 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie;
58 import org.openide.filesystems.FileObject;
59 import org.openide.loaders.DataObject;
60 import org.openide.loaders.DataObjectNotFoundException;
61 import org.openide.util.NbBundle;
62
63 /**
64  *
65  * @author girix
66  */

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