View Javadoc

1   package com.tapina.robe.swi.wimp;
2   
3   import com.tapina.robe.swi.os.Sprite;
4   
5   import javax.swing.*;
6   import java.awt.*;
7   import java.awt.event.MouseEvent;
8   import java.awt.event.MouseListener;
9   import java.util.logging.Logger;
10  
11  /***
12   * Created by IntelliJ IDEA.
13   * User: gareth
14   * Date: Sep 4, 2003
15   * Time: 6:21:30 PM
16   */
17  public final class Icon {
18      public static final int LEFT = 0, CENTRE = 1, RIGHT = 2;
19      public static final int TOP = 3, MIDDLE = 4, BOTTOM  = 5;
20  
21      public static final int IGNORE = 0, NOTIFY = 1, CLICK = 2, CLICK_ONCE = 3, RELEASE = 4, DOUBLE_CLICK = 5,
22      CLICK_ONCE_DRAG = 6, RELEASE_DRAG = 7, DOUBLE_CLICK_DRAG = 8, MENU = 9, CLICK_DOUBLE_CLICK_DRAG = 10,
23      CLICK_SELECT_DRAG = 11, CARET_DRAG = 14, CARET = 15;
24  
25      public static final int TEXTICON = 1, SPRITEICON = 2, BORDERED = 4, H_CENTER = 8, V_CENTER = 16,
26      FILLED = 32, ANTI_ALIASED = 64, NEEDS_REDRAW = 128, INDIRECTED = 256, RIGHT_JUSTIFY = 512,
27      MULTI_SELECT_ADJUST = 1024, HALF_SIZE_SPRITE = 2048, SELECTED = 1<<21, SHADED = 1<<22, DELETED = 1<<23;
28  
29      private final Logger log = Logger.getLogger(getClass().getName());
30      private Rectangle boundingBox;
31      private int iconFlags;
32      public int buttonType;
33      public int selectionGroup;
34      public int foregroundColour;
35      public int backgroundColour;
36  
37      String text;
38      int textIndirectAddress;
39      String validationString;
40      int validationIndirectAddress;
41      Sprite sprite;
42  
43      public Icon() {
44          this.boundingBox = new Rectangle();
45      }
46  
47      public Icon(Rectangle boundingBox, int iconFlags, int buttonType, int esg, int foregroundColour,
48                  int backgroundColour) {
49          this.boundingBox = boundingBox;
50          this.iconFlags = iconFlags;
51          this.buttonType = buttonType;
52          this.selectionGroup = esg;
53          this.foregroundColour = foregroundColour;
54          this.backgroundColour = backgroundColour;
55      }
56  
57  
58  
59      public final int getBackgroundColour() {
60          return backgroundColour;
61      }
62  
63      public final Rectangle getBoundingBox() {
64          return boundingBox;
65      }
66  
67      public final int getButtonType() {
68          return buttonType;
69      }
70  
71      public final int getForegroundColour() {
72          return foregroundColour;
73      }
74  
75      public final int getIconFlags() {
76          return iconFlags;
77      }
78  
79      public final int getSelectionGroup() {
80          return selectionGroup;
81      }
82  
83      public final String getText() {
84          return text;
85      }
86  
87      public final void setText(String text) {
88          this.text = text;
89      }
90  
91      public final String getValidationString() {
92          return validationString;
93      }
94  
95      public final void setValidationString(String validationString) {
96          this.validationString = validationString;
97      }
98  
99      public final Sprite getSprite() {
100         return sprite;
101     }
102 
103     public final void setSprite(Sprite sprite) {
104         this.sprite = sprite;
105     }
106 
107     public final int getTextPositionH() {
108         if (isTextIcon() && isSpriteIcon()) {
109             return ((iconFlags & H_CENTER) == 0)?
110                     (((iconFlags & RIGHT_JUSTIFY) != 0 && (iconFlags & V_CENTER) == 0)? RIGHT : LEFT)
111                     :
112                     (((iconFlags & V_CENTER) != 0 && (iconFlags & RIGHT_JUSTIFY) != 0)? RIGHT : CENTRE);
113         } else {
114             return ((iconFlags & H_CENTER) != 0)? CENTRE :
115                     (((iconFlags & RIGHT_JUSTIFY) != 0)? RIGHT : LEFT);
116         }
117     }
118 
119     public final int getTextPositionV() {
120         if ((iconFlags & V_CENTER) != 0) {
121             return MIDDLE;
122         } else if (isTextIcon() && isSpriteIcon()) {
123             return ((iconFlags & H_CENTER) != 0 && (iconFlags & RIGHT_JUSTIFY) != 0)? TOP : BOTTOM;
124         } else {
125             return TOP;
126         }
127     }
128 
129     public final int getSpritePositionH() {
130         if (isTextIcon() && isSpriteIcon()) {
131             return ((iconFlags & H_CENTER) == 0)?
132                     (((iconFlags & RIGHT_JUSTIFY) == 0)? LEFT : RIGHT)
133                     :
134                     (((iconFlags & V_CENTER) != 0 && (iconFlags & RIGHT_JUSTIFY) != 0)? LEFT : CENTRE);
135         } else {
136             return ((iconFlags & H_CENTER) != 0)? CENTRE :
137                     (((iconFlags & RIGHT_JUSTIFY) != 0)? RIGHT : LEFT);
138         }
139     }
140 
141     public final int getSpritePositionV() {
142         if ((iconFlags & V_CENTER) != 0) {
143             return MIDDLE;
144         } else if (isTextIcon() && isSpriteIcon()) {
145             return ((iconFlags & H_CENTER) != 0 && (iconFlags & RIGHT_JUSTIFY) != 0)? BOTTOM : TOP;
146         } else {
147             return TOP;
148         }
149     }
150 
151     public final boolean isTextIcon() {
152         return (iconFlags & TEXTICON) != 0;
153     }
154 
155     public final boolean isSpriteIcon() {
156         return (iconFlags & SPRITEICON) != 0;
157     }
158 
159     public void setBoundingBox(Rectangle boundingBox) {
160         this.boundingBox = boundingBox;
161     }
162 
163     /***
164      * @todo Update component to reflect this.
165      */ 
166     public void setIconFlags(int iconFlags) {
167         this.iconFlags = iconFlags;
168     }
169 
170     public void setButtonType(int buttonType) {
171         this.buttonType = buttonType;
172     }
173 
174     public void setSelectionGroup(int selectionGroup) {
175         this.selectionGroup = selectionGroup;
176     }
177 
178     public void setForegroundColour(int foregroundColour) {
179         this.foregroundColour = foregroundColour;
180     }
181 
182     public void setBackgroundColour(int backgroundColour) {
183         this.backgroundColour = backgroundColour;
184     }
185 
186     public int getTextIndirectAddress() {
187         return textIndirectAddress;
188     }
189 
190     public void setTextIndirectAddress(int textIndirectAddress) {
191         this.textIndirectAddress = textIndirectAddress;
192     }
193 
194     public int getValidationIndirectAddress() {
195         return validationIndirectAddress;
196     }
197 
198     public void setValidationIndirectAddress(int validationIndirectAddress) {
199         this.validationIndirectAddress = validationIndirectAddress;
200     }
201 
202     /***
203      * @todo Need to make this component re-check indirected data in case it has been updated when redrawing
204      */
205     public final JComponent getComponent() {
206         final JComponent component;
207         final JLabel label = new JLabel();
208         if (isTextIcon()) {
209             component = label;
210             label.setText(text);
211             if (isSpriteIcon()) {
212                 label.setIcon(new ImageIcon(sprite.getImage()));
213             }
214         } else if (isSpriteIcon()) {
215             component = label;
216             label.setIcon(new ImageIcon(sprite.getImage()));
217         } else {
218             component = new JPanel();
219         }
220         component.setBounds(boundingBox);
221         component.addMouseListener(new MouseListener() {
222             public void mouseClicked(MouseEvent e) {
223                 if (buttonType != IGNORE && buttonType != NOTIFY && buttonType != CARET) {
224                     log.info("CLICK");
225                 }
226             }
227 
228             public void mousePressed(MouseEvent e) {
229                 if (buttonType == NOTIFY) {
230                     log.info("NOTIFY");
231                 }
232             }
233 
234             public void mouseReleased(MouseEvent e) {
235 
236             }
237 
238             public void mouseEntered(MouseEvent e) {
239 
240             }
241 
242             public void mouseExited(MouseEvent e) {
243 
244             }
245         });
246 
247         return component;
248     }
249 }