KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > gtk > BlueprintStyle


1 /*
2  * @(#)BlueprintStyle.java 1.12 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.gtk;
8
9 import java.awt.*;
10 import java.util.*;
11 import javax.swing.*;
12 import java.security.*;
13 import javax.swing.plaf.synth.*;
14 import sun.swing.plaf.synth.DefaultSynthStyle;
15
16 /**
17  * BlueprintStyle extends GTKStyle adding support for a set of <code>Info</code>s.
18  *
19  * @version 1.12 12/19/03
20  * @author Scott Violet
21  */

22 class BlueprintStyle extends GTKStyle implements GTKConstants {
23     /**
24      * There should only ever be one blueprint engine.
25      */

26     private static final GTKEngine BLUEPRINT_ENGINE = new BlueprintEngine();
27     
28     private static final BlueprintGraphicsUtils BLUEPRINT_GRAPHICS =
29             new BlueprintGraphicsUtils();
30
31     /**
32      * Set of Infos used to determine what to paint.
33      */

34     private Info[] info;
35     
36     /**
37      * Comes from the top level icon_colorize setting in rc files.
38      */

39     private boolean iconColorize;
40     
41     /**
42      * Comes from the top level icon_colorize_ancestor_type setting
43      * in rec files. These Strings will all be interned by the parser.
44      */

45     private String JavaDoc[] iconAncestorTypes;
46
47     /**
48      * Comes from the top level colorize_color setting in rc files.
49      */

50     private Color colorizeColor;
51
52
53     /**
54      * Creates a duplicate of the passed in style.
55      */

56     public BlueprintStyle(DefaultSynthStyle style) {
57         super(style);
58         if (style instanceof BlueprintStyle) {
59             BlueprintStyle bpStyle = (BlueprintStyle)style;
60             this.info = bpStyle.info;
61             this.iconColorize = bpStyle.iconColorize;
62             this.iconAncestorTypes = bpStyle.iconAncestorTypes;
63             this.colorizeColor = bpStyle.colorizeColor;
64         }
65     }
66
67     /**
68      * Creates a BlueprintStyle from the passed in arguments.
69      */

70     public BlueprintStyle(StateInfo[] states,
71                        CircularIdentityList classSpecificValues,
72                        Font font,
73                        int xThickness, int yThickness,
74                        GTKStockIconInfo[] icons,
75                        Info[] info,
76                        boolean iconColorize,
77                        String JavaDoc[] iconAncestorTypes,
78                        Color colorizeColor) {
79         super(states, classSpecificValues, font, xThickness, yThickness, icons);
80         this.info = info;
81         this.iconColorize = iconColorize;
82         this.iconAncestorTypes = iconAncestorTypes;
83         this.colorizeColor = colorizeColor;
84     }
85
86     public SynthGraphicsUtils getGraphicsUtils(SynthContext context) {
87         return BLUEPRINT_GRAPHICS;
88     }
89
90     /**
91      * Adds the state of this BlueprintStyle to that of <code>s</code>
92      * returning a combined SynthStyle.
93      */

94     public DefaultSynthStyle addTo(DefaultSynthStyle s) {
95         if (!(s instanceof BlueprintStyle)) {
96             s = new BlueprintStyle(s);
97         }
98         BlueprintStyle style = (BlueprintStyle)super.addTo(s);
99         if (info != null) {
100             if (style.info == null) {
101                 style.info = info;
102             }
103             else {
104                 // Place the more specific first.
105
Info[] merged = new Info[style.info.length + info.length];
106                 System.arraycopy(info, 0, merged, 0, info.length);
107                 System.arraycopy(style.info, 0, merged, info.length,
108                                  style.info.length);
109                 style.info = merged;
110             }
111         }
112         
113         // like the real blueprint, we only overwrite when iconColorize is true
114

115         if (iconColorize) {
116             style.iconColorize = true;
117             style.colorizeColor = colorizeColor;
118         }
119
120         // like the real blueprint, we always overwrite
121

122         style.iconAncestorTypes = iconAncestorTypes;
123
124         return style;
125     }
126
127     /**
128      * Creates a copy of the reciever and returns it.
129      */

130     public Object JavaDoc clone() {
131         BlueprintStyle style = (BlueprintStyle)super.clone();
132
133         // These fields are immutable, no need to clone them
134
style.info = this.info;
135         style.iconAncestorTypes = this.iconAncestorTypes;
136         style.colorizeColor = this.colorizeColor;
137
138         return style;
139     }
140
141     /**
142      * Returns a GTKEngine to use for rendering.
143      */

144     public GTKEngine getEngine(SynthContext context) {
145         return BLUEPRINT_ENGINE;
146     }
147
148     /**
149      * Returns the first instance of Info that matches the past in args, may
150      * return null if nothing matches.
151      *
152      * @param function String name for the painting method
153      * @param detail Description of what is being painted
154      * @param componentState State of the Component
155      * @param shadow Shadow type
156      * @param orientation Orientation of what is being painted
157      * @param gapSide Side of the gap being drawn
158      * @param arrowDirection direction of the arrow.
159      * @return Best matching Info, or null if none match
160      */

161     public Info getInfo(String JavaDoc function, String JavaDoc detail, int componentState,
162                         int shadow, int orientation, int gapSide,
163                         int arrowDirection, String JavaDoc parentType) {
164         if (info != null) {
165             for (int counter = 0, max = info.length; counter < max;counter++) {
166                 if (info[counter].getFunction() == function && info[counter].
167                               getMatchCount(detail, componentState, shadow,
168                               orientation, gapSide, arrowDirection,
169                               parentType) != -1) {
170                     return info[counter];
171                 }
172             }
173         }
174         return null;
175     }
176
177     /**
178      * Returns the number of non-null arugments and arguments that are
179      * != UNDEFINED.
180      */

181     private int getMaxMatchCount(int componentState, int shadow,
182                                  int orientation, int gapSide,
183                                  int arrowDirection, String JavaDoc detail) {
184         int count = 0;
185
186         if (detail != null) {
187             count++;
188         }
189         if (componentState != UNDEFINED) {
190             count++;
191         }
192         if (shadow != UNDEFINED) {
193             count++;
194         }
195         if (orientation != UNDEFINED) {
196             count++;
197         }
198         if (gapSide != UNDEFINED) {
199             count++;
200         }
201         if (arrowDirection != UNDEFINED) {
202             count++;
203         }
204         return count;
205     }
206
207
208     /**
209      * A description of how to paint a portion of a widget.
210      */

211     public static class Info {
212         // match data
213
private String JavaDoc function = null;
214         private String JavaDoc detail = null;
215         int gapSide = UNDEFINED;
216         int orientation = UNDEFINED;
217         int componentState = UNDEFINED;
218         int shadow = UNDEFINED;
219         int arrowDirection = UNDEFINED;
220
221         // strings in this list will be interned
222
// this list could be null
223
ArrayList parentTypeList = null;
224
225         boolean useAsBkgMask = false;
226
227         // background
228
Object JavaDoc image = null;
229         Insets fileInsets = null;
230         boolean stretch = false;
231         boolean recolorable = false;
232         Color colorizeColor = null;
233
234         // overlay
235
Object JavaDoc overlayImage = null;
236         Insets overlayInsets = null;
237         boolean overlayStretch = false;
238         boolean overlayRecolorable = false;
239         Color overlayColorizeColor = null;
240
241         // gap start
242
Object JavaDoc gapStartImage = null;
243         Insets gapStartInsets = null;
244
245         // gap
246
Object JavaDoc gapImage = null;
247         Insets gapInsets = null;
248
249         // gap end
250
Object JavaDoc gapEndImage = null;
251         Insets gapEndInsets = null;
252
253         public void setFunction(String JavaDoc function) {
254             this.function = function.intern();
255         }
256
257         public void setDetail(String JavaDoc detail) {
258             this.detail = detail.intern();
259         }
260
261         public String JavaDoc getFunction() {
262             return function;
263         }
264
265         public Image getImage() {
266             image = getImage(image);
267             return (Image)image;
268         }
269
270         public boolean isRecolorable() {
271             return recolorable;
272         }
273
274         public Color getColorizeColor() {
275             return colorizeColor;
276         }
277
278         public boolean isBkgMask() {
279             return useAsBkgMask;
280         }
281
282         public Insets getImageInsets() {
283             return fileInsets;
284         }
285
286         public boolean getStretch() {
287             return stretch;
288         }
289
290         public String JavaDoc getDetail() {
291             return detail;
292         }
293
294         public int getComponentState() {
295             return componentState;
296         }
297
298         public int getShadow() {
299             return shadow;
300         }
301
302         public int getGapSide() {
303             return gapSide;
304         }
305
306         public Image getGapImage() {
307             gapImage = getImage(gapImage);
308             return (Image)gapImage;
309         }
310
311         public Insets getGapInsets() {
312             return gapInsets;
313         }
314
315         public Image getGapStartImage() {
316             gapStartImage = getImage(gapStartImage);
317             return (Image)gapStartImage;
318         }
319
320         public Insets getGapStartInsets() {
321             return gapStartInsets;
322         }
323
324         public Image getGapEndImage() {
325             gapEndImage = getImage(gapEndImage);
326             return (Image)gapEndImage;
327         }
328
329         public Insets getGapEndInsets() {
330             return gapEndInsets;
331         }
332
333         public Image getOverlayImage() {
334             overlayImage = getImage(overlayImage);
335             return (Image)overlayImage;
336         }
337
338         public Insets getOverlayInsets() {
339             return overlayInsets;
340         }
341
342         public boolean getOverlayStretch() {
343             return overlayStretch;
344         }
345
346         public boolean getOverlayRecolorable() {
347             return overlayRecolorable;
348         }
349
350         public Color getOverlayColorizeColor() {
351             return overlayColorizeColor;
352         }
353
354         public int getArrowDirection() {
355             return arrowDirection;
356         }
357
358         public int getOrientation() {
359             return orientation;
360         }
361
362
363         private Image getImage(final Object JavaDoc o) {
364             if (o == null || o instanceof Image) {
365                 return (Image)o;
366             }
367
368             ImageIcon ii = (ImageIcon)AccessController.doPrivileged(new PrivilegedAction() {
369                 public Object JavaDoc run() {
370                     return new ImageIcon((String JavaDoc)o);
371                 }
372             });
373
374             if (ii.getIconWidth() > 0 && ii.getIconHeight() > 0) {
375                 return ii.getImage();
376             }
377             
378             return null;
379         }
380
381         /**
382          * Will return < 0 if doesn't match, otherwise return the
383          * number of parameters that match.
384          */

385         int getMatchCount(String JavaDoc detail, int componentState, int shadow,
386                           int orientation, int gapSide, int arrowDirection,
387                           String JavaDoc parentType) {
388             int matchCount = 0;
389
390             if (this.componentState != UNDEFINED) {
391                 if (componentState != UNDEFINED &&
392                              this.componentState != componentState) {
393                     return -1;
394                 }
395                 matchCount++;
396             }
397             if (this.shadow != UNDEFINED) {
398                 if (shadow != UNDEFINED && this.shadow != shadow) {
399                     return -1;
400                 }
401                 matchCount++;
402             }
403             if (this.arrowDirection != UNDEFINED) {
404                 if (arrowDirection != UNDEFINED &&
405                         this.arrowDirection != arrowDirection) {
406                     return -1;
407                 }
408                 matchCount++;
409             }
410             if (this.orientation != UNDEFINED) {
411                 if (orientation != UNDEFINED &&
412                                    this.orientation != orientation) {
413                     return -1;
414                 }
415                 matchCount++;
416             }
417             if (this.gapSide != UNDEFINED) {
418                 if (gapSide != UNDEFINED && this.gapSide != gapSide) {
419                     return -1;
420                 }
421                 matchCount++;
422             }
423             if (this.detail != null) {
424                 if (this.detail != detail) {
425                     return -1;
426                 }
427                 matchCount++;
428             }
429             if (this.parentTypeList != null) {
430                 boolean found = false;
431
432                 String JavaDoc type;
433                 Iterator itr = parentTypeList.iterator();
434                 while (itr.hasNext() && !found) {
435                     type = (String JavaDoc)itr.next();
436                     // NOTE: Maybe we should compare all lowercase.
437
if (type == parentType) {
438                         found = true;
439                     }
440                 }
441                 if (!found) {
442                     return -1;
443                 }
444                 matchCount++;
445             }
446             return matchCount;
447         }
448
449         /**
450          * Returns true if this Info matches that of the passed in Info.
451          * This differs from equals in so far as this only compares the
452          * properties that are used in lookup vs the actual images or
453          * insets.
454          *
455          * @return true if the receiver and info can be considered equal
456          * for lookup.
457          */

458         boolean matches(Info info) {
459             return (info.function == function && info.detail == detail &&
460                     info.componentState == componentState &&
461                     info.shadow == shadow && info.gapSide == gapSide &&
462                     info.arrowDirection == arrowDirection &&
463                     info.orientation == orientation);
464         }
465
466         public String JavaDoc toString() {
467             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
468
469             buf.append("IMAGE:\n");
470
471             if (function != null) {
472                 buf.append(" function=").append(function).append('\n');
473             }
474
475             if (detail != null) {
476                 buf.append(" detail=").append(detail).append('\n');
477             }
478
479             if (gapSide != UNDEFINED) {
480                 buf.append(" gapSide=");
481                 buf.append(getSideName(gapSide)).append('\n');
482             }
483
484             if (orientation != UNDEFINED) {
485                 buf.append(" orientation=");
486                 buf.append(getOrientName(orientation)).append('\n');
487             }
488
489             if (componentState != UNDEFINED) {
490                 buf.append(" componentState=");
491                 buf.append(getStateName(componentState, "UNDEFINED")).append('\n');
492             }
493
494             if (shadow != UNDEFINED) {
495                 buf.append(" shadow=");
496                 buf.append(getShadowName(shadow)).append('\n');
497             }
498
499             if (arrowDirection != UNDEFINED) {
500                 buf.append(" arrowDirection=");
501                 buf.append(getArrowDirectionName(arrowDirection)).append('\n');
502             }
503
504             if (parentTypeList != null) {
505                 buf.append(" parent_type={");
506                 for (Iterator iter = parentTypeList.iterator();
507                         iter.hasNext(); ) {
508                     buf.append(iter.next()).append(", ");
509                 }
510                 buf.deleteCharAt(buf.length() - 1).deleteCharAt(buf.length() - 1);
511                 buf.append("}\n");
512             }
513
514             if (useAsBkgMask != false) {
515                 buf.append(" use_as_bkg_mask=").append(useAsBkgMask).append('\n');
516             }
517
518             if (image != null) {
519                 buf.append(" image=").append(image).append('\n');
520             }
521
522             if (fileInsets != null) {
523                 buf.append(" fileInsets=").append(fileInsets).append('\n');
524             }
525
526             if (stretch != false) {
527                 buf.append(" stretch=").append(stretch).append('\n');
528             }
529
530             if (recolorable != false) {
531                 buf.append(" recolorable=").append(recolorable).append('\n');
532             }
533
534             if (colorizeColor != null) {
535                 buf.append(" colorize_color=");
536                 buf.append(getColorStringWithAlpha(colorizeColor)).append('\n');
537             }
538
539             if (overlayImage != null) {
540                 buf.append(" overlayImage=").append(overlayImage).append('\n');
541             }
542
543             if (overlayInsets != null) {
544                 buf.append(" overlayInsets=").append(overlayInsets).append('\n');
545             }
546
547             if (overlayStretch != false) {
548                 buf.append(" overlayStretch=").append(overlayStretch).append('\n');
549             }
550
551             if (overlayRecolorable != false) {
552                 buf.append(" overlay_recolorable=").append(overlayRecolorable).append('\n');
553             }
554
555             if (overlayColorizeColor != null) {
556                 buf.append(" overlay_colorize_color=");
557                 buf.append(getColorStringWithAlpha(overlayColorizeColor)).append('\n');
558             }
559
560             if (gapStartImage != null) {
561                 buf.append(" gapStartImage=").append(gapStartImage).append('\n');
562             }
563
564             if (gapStartInsets != null) {
565                 buf.append(" gapStartInsets=").append(gapStartInsets).append('\n');
566             }
567
568             if (gapImage != null) {
569                 buf.append(" gapImage=").append(gapImage).append('\n');
570             }
571
572             if (gapInsets != null) {
573                 buf.append(" gapInsets=").append(gapInsets).append('\n');
574             }
575
576             if (gapEndImage != null) {
577                 buf.append(" gapEndImage=").append(gapEndImage).append('\n');
578             }
579
580             if (gapEndInsets != null) {
581                 buf.append(" gapEndInsets=").append(gapEndInsets).append('\n');
582             }
583
584             buf.deleteCharAt(buf.length() - 1);
585
586             return buf.toString();
587         }
588         
589         private static String JavaDoc getSideName(int side) {
590             switch(side) {
591                 case TOP: return "TOP";
592                 case BOTTOM: return "BOTTOM";
593                 case LEFT: return "LEFT";
594                 case RIGHT: return "RIGHT";
595                 case UNDEFINED: return "UNDEFINED";
596             }
597             
598             return "UNKNOWN";
599         }
600         
601         private static String JavaDoc getOrientName(int orient) {
602             switch(orient) {
603                 case HORIZONTAL: return "HORIZONTAL";
604                 case VERTICAL: return "VERTICAL";
605                 case UNDEFINED: return "UNDEFINED";
606             }
607             
608             return "UNKNOWN";
609         }
610         
611         private static String JavaDoc getShadowName(int shadow) {
612             switch(shadow) {
613                 case SHADOW_IN: return "SHADOW_IN";
614                 case SHADOW_OUT: return "SHADOW_OUT";
615                 case SHADOW_ETCHED_IN: return "SHADOW_ETCHED_IN";
616                 case SHADOW_ETCHED_OUT: return "SHADOW_ETCHED_OUT";
617                 case SHADOW_NONE: return "SHADOW_NONE";
618                 case UNDEFINED: return "UNDEFINED";
619             }
620             
621             return "UNKNOWN";
622         }
623
624         private static String JavaDoc getArrowDirectionName(int dir) {
625             switch(dir) {
626                 case ARROW_UP: return "ARROW_UP";
627                 case ARROW_DOWN: return "ARROW_DOWN";
628                 case ARROW_LEFT: return "ARROW_LEFT";
629                 case ARROW_RIGHT: return "ARROW_RIGHT";
630                 case UNDEFINED: return "UNDEFINED";
631             }
632
633             return "UNKNOWN";
634         }
635     }
636
637     private static String JavaDoc getColorStringWithAlpha(Color c) {
638         if (c == null) {
639             return "null";
640         }
641
642         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
643         buf.append('{');
644         buf.append(c.getRed()).append(", ");
645         buf.append(c.getGreen()).append(", ");
646         buf.append(c.getBlue()).append(", ");
647         buf.append(c.getAlpha()).append("}");
648         return buf.toString();
649     }
650
651     public String JavaDoc toString() {
652         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(super.toString());
653
654         if (buf.length() > 0) {
655             buf.append('\n');
656         }
657
658         buf.append("*** Blueprint Engine Info ***\n");
659
660         buf.append("icon_colorize = " + iconColorize + '\n');
661         buf.append("icon_colorize_ancestor_type = ");
662         if (iconAncestorTypes == null) {
663             buf.append("null\n");
664         } else {
665             buf.append('{');
666             for (int i = 0; i < iconAncestorTypes.length; i++) {
667                 buf.append(iconAncestorTypes[i] + ", ");
668             }
669
670             buf.deleteCharAt(buf.length() - 1);
671             buf.deleteCharAt(buf.length() - 1);
672
673             buf.append("}\n");
674         }
675
676         buf.append("colorize_color = ");
677         buf.append(getColorStringWithAlpha(colorizeColor));
678         buf.append('\n');
679
680         if (info != null) {
681             for (int i = 0; i < info.length; i++) {
682                 buf.append(info[i].toString()).append('\n');
683             }
684         }
685
686         // remove last newline
687
buf.deleteCharAt(buf.length() - 1);
688
689         return buf.toString();
690     }
691
692 }
693
Popular Tags