KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)PixmapStyle.java 1.19 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 javax.swing.plaf.synth.*;
10 import java.awt.*;
11 import java.util.*;
12 import javax.swing.*;
13 import java.security.*;
14 import sun.swing.plaf.synth.*;
15
16 /**
17  * PixmapStyle extends GTKStyle adding support for a set of <code>Info</code>s.
18  *
19  * @version 1.19, 12/19/03
20  * @author Scott Violet
21  */

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

26     private static final GTKEngine PIXMAP_ENGINE = new PixmapEngine();
27
28
29     /**
30      * Set of Infos used to determine what to paint.
31      */

32     private Info[] info;
33
34
35     /**
36      * Creates a duplicate of the passed in style.
37      */

38     public PixmapStyle(DefaultSynthStyle style) {
39         super(style);
40         if (style instanceof PixmapStyle) {
41             this.info = ((PixmapStyle)style).info;
42         }
43     }
44
45     /**
46      * Creates a PixmapStyle from the passed in arguments.
47      */

48     public PixmapStyle(StateInfo[] states,
49                        CircularIdentityList classSpecificValues,
50                        Font font,
51                        int xThickness, int yThickness,
52                        GTKStockIconInfo[] icons,
53                        Info[] info) {
54         super(states, classSpecificValues, font, xThickness, yThickness, icons);
55         this.info = info;
56     }
57
58     /**
59      * Adds the state of this PixmapStyle to that of <code>s</code>
60      * returning a combined SynthStyle.
61      */

62     public DefaultSynthStyle addTo(DefaultSynthStyle s) {
63         if (!(s instanceof PixmapStyle)) {
64             s = new PixmapStyle(s);
65         }
66         PixmapStyle style = (PixmapStyle)super.addTo(s);
67         if (info != null) {
68             if (style.info == null) {
69                 style.info = info;
70             }
71             else {
72                 // Place the more specific first.
73
Info[] merged = new Info[style.info.length + info.length];
74                 System.arraycopy(info, 0, merged, 0, info.length);
75                 System.arraycopy(style.info, 0, merged, info.length,
76                                  style.info.length);
77                 style.info = merged;
78             }
79         }
80         return style;
81     }
82
83     /**
84      * Creates a copy of the reciever and returns it.
85      */

86     public Object JavaDoc clone() {
87         PixmapStyle style = (PixmapStyle)super.clone();
88
89         // Info is immutable, no need to clone it.
90
style.info = this.info;
91         return style;
92     }
93
94     /**
95      * Returns a GTKEngine to use for rendering.
96      */

97     public GTKEngine getEngine(SynthContext context) {
98         return PIXMAP_ENGINE;
99     }
100
101     /**
102      * Returns the first instance of Info that matches the past in args, may
103      * return null if nothing matches.
104      *
105      * @param function String name for the painting method
106      * @param detail Description of what is being painted
107      * @param componentState State of the Component
108      * @param shadow Shadow type
109      * @param orientation Orientation of what is being painted
110      * @param gapSide Side of the gap being drawn
111      * @param arrowDirection direction of the arrow.
112      * @return Best matching Info, or null if none match
113      */

114     public Info getInfo(String JavaDoc function, String JavaDoc detail, int componentState,
115                         int shadow, int orientation, int gapSide,
116                         int arrowDirection) {
117         if (info != null) {
118             for (int counter = 0, max = info.length; counter < max;counter++) {
119                 if (info[counter].getFunction() == function && info[counter].
120                               getMatchCount(detail, componentState, shadow,
121                               orientation, gapSide, arrowDirection) != -1) {
122                     return info[counter];
123                 }
124             }
125         }
126         return null;
127     }
128
129     /**
130      * Returns the number of non-null arugments and arguments that are
131      * != UNDEFINED.
132      */

133     private int getMaxMatchCount(int componentState, int shadow,
134                                  int orientation, int gapSide,
135                                  int arrowDirection, String JavaDoc detail) {
136         int count = 0;
137
138         if (detail != null) {
139             count++;
140         }
141         if (componentState != UNDEFINED) {
142             count++;
143         }
144         if (shadow != UNDEFINED) {
145             count++;
146         }
147         if (orientation != UNDEFINED) {
148             count++;
149         }
150         if (gapSide != UNDEFINED) {
151             count++;
152         }
153         if (arrowDirection != UNDEFINED) {
154             count++;
155         }
156         return count;
157     }
158
159
160     /**
161      * A description of how to paint a portion of a widget.
162      */

163     public static class Info {
164         // match data
165
private String JavaDoc function = null;
166         private String JavaDoc detail = null;
167         int gapSide = UNDEFINED;
168         int orientation = UNDEFINED;
169         int componentState = UNDEFINED;
170         int shadow = UNDEFINED;
171         int arrowDirection = UNDEFINED;
172
173         boolean recolorable = false;
174
175         // background
176
Object JavaDoc image = null;
177         Insets fileInsets = null;
178         boolean stretch = false;
179
180         // overlay
181
Object JavaDoc overlayImage = null;
182         Insets overlayInsets = null;
183         boolean overlayStretch = false;
184
185         // gap start
186
Object JavaDoc gapStartImage = null;
187         Insets gapStartInsets = null;
188
189         // gap
190
Object JavaDoc gapImage = null;
191         Insets gapInsets = null;
192
193         // gap end
194
Object JavaDoc gapEndImage = null;
195         Insets gapEndInsets = null;
196
197         public void setFunction(String JavaDoc function) {
198             this.function = function.intern();
199         }
200
201         public void setDetail(String JavaDoc detail) {
202             this.detail = detail.intern();
203         }
204
205         public String JavaDoc getFunction() {
206             return function;
207         }
208
209         public Image getImage() {
210             image = getImage(image);
211             return (Image)image;
212         }
213
214         public boolean isRecolorable() {
215             return recolorable;
216         }
217
218         public Insets getImageInsets() {
219             return fileInsets;
220         }
221
222         public boolean getStretch() {
223             return stretch;
224         }
225
226         public String JavaDoc getDetail() {
227             return detail;
228         }
229
230         public int getComponentState() {
231             return componentState;
232         }
233
234         public int getShadow() {
235             return shadow;
236         }
237
238         public int getGapSide() {
239             return gapSide;
240         }
241
242         public Image getGapImage() {
243             gapImage = getImage(gapImage);
244             return (Image)gapImage;
245         }
246
247         public Insets getGapInsets() {
248             return gapInsets;
249         }
250
251         public Image getGapStartImage() {
252             gapStartImage = getImage(gapStartImage);
253             return (Image)gapStartImage;
254         }
255
256         public Insets getGapStartInsets() {
257             return gapStartInsets;
258         }
259
260         public Image getGapEndImage() {
261             gapEndImage = getImage(gapEndImage);
262             return (Image)gapEndImage;
263         }
264
265         public Insets getGapEndInsets() {
266             return gapEndInsets;
267         }
268
269         public Image getOverlayImage() {
270             overlayImage = getImage(overlayImage);
271             return (Image)overlayImage;
272         }
273
274         public Insets getOverlayInsets() {
275             return overlayInsets;
276         }
277
278         public boolean getOverlayStretch() {
279             return overlayStretch;
280         }
281
282         public int getArrowDirection() {
283             return arrowDirection;
284         }
285
286         public int getOrientation() {
287             return orientation;
288         }
289
290
291         private Image getImage(final Object JavaDoc o) {
292             if (o == null || o instanceof Image) {
293                 return (Image)o;
294             }
295
296             ImageIcon ii = (ImageIcon)AccessController.doPrivileged(new PrivilegedAction() {
297                 public Object JavaDoc run() {
298                     return new ImageIcon((String JavaDoc)o);
299                 }
300             });
301
302             if (ii.getIconWidth() > 0 && ii.getIconHeight() > 0) {
303                 return ii.getImage();
304             }
305             
306             return null;
307         }
308
309         /**
310          * Will return < 0 if doesn't match, otherwise return the
311          * number of parameters that match.
312          */

313         int getMatchCount(String JavaDoc detail, int componentState, int shadow,
314                           int orientation, int gapSide, int arrowDirection) {
315             int matchCount = 0;
316
317             if (this.componentState != UNDEFINED) {
318                 if (componentState != UNDEFINED &&
319                              this.componentState != componentState) {
320                     return -1;
321                 }
322                 matchCount++;
323             }
324             if (this.shadow != UNDEFINED) {
325                 if (shadow != UNDEFINED && this.shadow != shadow) {
326                     return -1;
327                 }
328                 matchCount++;
329             }
330             if (this.orientation != UNDEFINED) {
331                 if (orientation != UNDEFINED &&
332                                    this.orientation != orientation) {
333                     return -1;
334                 }
335                 matchCount++;
336             }
337             if (this.gapSide != UNDEFINED) {
338                 if (gapSide != UNDEFINED && this.gapSide != gapSide) {
339                     return -1;
340                 }
341                 matchCount++;
342             }
343             if (this.arrowDirection != UNDEFINED) {
344                 if (arrowDirection != UNDEFINED &&
345                                       this.arrowDirection != arrowDirection) {
346                     return -1;
347                 }
348                 matchCount++;
349             }
350             if (this.detail != null) {
351                 if (this.detail != detail) {
352                     return -1;
353                 }
354                 matchCount++;
355             }
356             return matchCount;
357         }
358
359         /**
360          * Returns true if this Info matches that of the passed in Info.
361          * This differs from equals in so far as this only compares the
362          * properties that are used in lookup vs the actual images or
363          * insets.
364          *
365          * @return true if the receiver and info can be considered equal
366          * for lookup.
367          */

368         boolean matches(Info info) {
369             return (info.function == function && info.detail == detail &&
370                     info.componentState == componentState &&
371                     info.shadow == shadow && info.gapSide == gapSide &&
372                     info.arrowDirection == arrowDirection &&
373                     info.orientation == orientation);
374         }
375
376         public String JavaDoc toString() {
377             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
378
379             buf.append("IMAGE:\n");
380
381             if (function != null) {
382                 buf.append(" function=").append(function).append('\n');
383             }
384
385             if (detail != null) {
386                 buf.append(" detail=").append(detail).append('\n');
387             }
388
389             if (gapSide != UNDEFINED) {
390                 buf.append(" gapSide=");
391                 buf.append(getSideName(gapSide)).append('\n');
392             }
393
394             if (orientation != UNDEFINED) {
395                 buf.append(" orientation=");
396                 buf.append(getOrientName(orientation)).append('\n');
397             }
398
399             if (componentState != UNDEFINED) {
400                 buf.append(" componentState=");
401                 buf.append(getStateName(componentState, "UNDEFINED")).append('\n');
402             }
403
404             if (shadow != UNDEFINED) {
405                 buf.append(" shadow=");
406                 buf.append(getShadowName(shadow)).append('\n');
407             }
408
409             if (arrowDirection != UNDEFINED) {
410                 buf.append(" arrowDirection=");
411                 buf.append(getArrowDirectionName(arrowDirection)).append('\n');
412             }
413
414             if (recolorable != false) {
415                 buf.append(" recolorable=").append(recolorable).append('\n');
416             }
417
418             if (image != null) {
419                 buf.append(" image=").append(image).append('\n');
420             }
421
422             if (fileInsets != null) {
423                 buf.append(" fileInsets=").append(fileInsets).append('\n');
424             }
425
426             if (stretch != false) {
427                 buf.append(" stretch=").append(stretch).append('\n');
428             }
429
430             if (overlayImage != null) {
431                 buf.append(" overlayImage=").append(overlayImage).append('\n');
432             }
433
434             if (overlayInsets != null) {
435                 buf.append(" overlayInsets=").append(overlayInsets).append('\n');
436             }
437
438             if (overlayStretch != false) {
439                 buf.append(" overlayStretch=").append(overlayStretch).append('\n');
440             }
441
442             if (gapStartImage != null) {
443                 buf.append(" gapStartImage=").append(gapStartImage).append('\n');
444             }
445
446             if (gapStartInsets != null) {
447                 buf.append(" gapStartInsets=").append(gapStartInsets).append('\n');
448             }
449
450             if (gapImage != null) {
451                 buf.append(" gapImage=").append(gapImage).append('\n');
452             }
453
454             if (gapInsets != null) {
455                 buf.append(" gapInsets=").append(gapInsets).append('\n');
456             }
457
458             if (gapEndImage != null) {
459                 buf.append(" gapEndImage=").append(gapEndImage).append('\n');
460             }
461
462             if (gapEndInsets != null) {
463                 buf.append(" gapEndInsets=").append(gapEndInsets).append('\n');
464             }
465
466             buf.deleteCharAt(buf.length() - 1);
467
468             return buf.toString();
469         }
470         
471         private static String JavaDoc getSideName(int side) {
472             switch(side) {
473                 case TOP: return "TOP";
474                 case BOTTOM: return "BOTTOM";
475                 case LEFT: return "LEFT";
476                 case RIGHT: return "RIGHT";
477                 case UNDEFINED: return "UNDEFINED";
478             }
479             
480             return "UNKNOWN";
481         }
482         
483         private static String JavaDoc getOrientName(int orient) {
484             switch(orient) {
485                 case HORIZONTAL: return "HORIZONTAL";
486                 case VERTICAL: return "VERTICAL";
487                 case UNDEFINED: return "UNDEFINED";
488             }
489             
490             return "UNKNOWN";
491         }
492         
493         private static String JavaDoc getShadowName(int shadow) {
494             switch(shadow) {
495                 case SHADOW_IN: return "SHADOW_IN";
496                 case SHADOW_OUT: return "SHADOW_OUT";
497                 case SHADOW_ETCHED_IN: return "SHADOW_ETCHED_IN";
498                 case SHADOW_ETCHED_OUT: return "SHADOW_ETCHED_OUT";
499                 case SHADOW_NONE: return "SHADOW_NONE";
500                 case UNDEFINED: return "UNDEFINED";
501             }
502             
503             return "UNKNOWN";
504         }
505
506         private static String JavaDoc getArrowDirectionName(int dir) {
507             switch(dir) {
508                 case ARROW_UP: return "ARROW_UP";
509                 case ARROW_DOWN: return "ARROW_DOWN";
510                 case ARROW_LEFT: return "ARROW_LEFT";
511                 case ARROW_RIGHT: return "ARROW_RIGHT";
512                 case UNDEFINED: return "UNDEFINED";
513             }
514
515             return "UNKNOWN";
516         }
517     }
518
519     public String JavaDoc toString() {
520         if (info == null) {
521             return super.toString();
522         } else {
523             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(super.toString());
524             
525             if (buf.length() > 0) {
526                 buf.append('\n');
527             }
528
529             buf.append("*** Pixmap Engine Info ***\n");
530             for (int i = 0; i < info.length; i++) {
531                 buf.append(info[i].toString()).append('\n');
532             }
533
534             // remove last newline
535
buf.deleteCharAt(buf.length() - 1);
536             
537             return buf.toString();
538         }
539     }
540
541 }
542
Popular Tags