KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > BooleanIcon


1 package net.suberic.pooka.gui;
2 import net.suberic.pooka.Pooka;
3 import net.suberic.util.gui.IconManager;
4 import javax.swing.*;
5 import java.util.HashMap JavaDoc;
6 import java.util.MissingResourceException JavaDoc;
7 import java.awt.Component JavaDoc;
8
9 public class BooleanIcon implements TableCellIcon {
10   public boolean bool;
11   public String JavaDoc iconProperty;
12   public static HashMap JavaDoc labelTable = new HashMap JavaDoc();
13   protected static Component JavaDoc blankImage = new JLabel();
14   String JavaDoc mIconId = "";
15
16   public BooleanIcon(boolean boolValue, String JavaDoc newIconFile, String JavaDoc pId) {
17     bool=boolValue;
18     iconProperty = newIconFile;
19     ((JLabel)blankImage).setOpaque(true);
20     mIconId = pId;
21   }
22
23     /**
24      * This returns a JLabel. If the value of this BooleanIcon is true,
25      * then it returns the configued image. If it's false, then it just
26      * returns a blank JLabel.
27      */

28     public Component JavaDoc getIcon() {
29     if (bool) {
30         return getIcon(iconProperty);
31     } else
32         return blankImage;
33     }
34
35     public Component JavaDoc getIcon(String JavaDoc imageKey) {
36     
37     if (labelTable.containsKey(imageKey))
38         return (Component JavaDoc)labelTable.get(imageKey);
39     else
40         return loadImage(imageKey);
41     }
42
43   /**
44    * This attempts to load an image for the given imageKey.
45    */

46   public Component JavaDoc loadImage(String JavaDoc imageKey) {
47     Component JavaDoc returnValue = null;
48     IconManager iconManager = Pooka.getUIFactory().getIconManager();
49     ImageIcon icon = iconManager.getIcon(imageKey);
50     if (icon != null) {
51       returnValue = new JLabel(icon);
52       ((JLabel)returnValue).setOpaque(true);
53       labelTable.put(imageKey, returnValue);
54     } else {
55       returnValue = null;
56     }
57     
58     return returnValue;
59   }
60   
61   public int compareTo(Object JavaDoc o) {
62     if (o instanceof BooleanIcon) {
63       boolean oValue = ((BooleanIcon)o).bool;
64       if (bool == oValue)
65     return 0;
66       else if (bool == true)
67     return 1;
68       else
69     return -1;
70     }
71     throw new ClassCastException JavaDoc("object is not a BooleanIcon.");
72   }
73
74   public String JavaDoc toString() {
75     return "";
76   }
77   
78   /**
79    * Returns the icon id.
80    */

81   public String JavaDoc getIconId() {
82     return mIconId;
83   }
84
85   /**
86    * Returns the icon value.
87    */

88   public boolean iconValue() {
89     return bool;
90   }
91
92 }
93
Popular Tags