source: trunk/src/org/expeditee/gui/Popup.java@ 309

Last change on this file since 309 was 283, checked in by bjn8, 16 years ago

Popups can change border size and color

File size: 5.0 KB
Line 
1package org.expeditee.gui;
2
3import java.awt.BasicStroke;
4import java.awt.Color;
5import java.awt.Component;
6import java.awt.Container;
7import java.awt.Graphics;
8import java.awt.Graphics2D;
9import java.awt.LayoutManager;
10
11import javax.swing.JComponent;
12import javax.swing.JPanel;
13
14import org.expeditee.items.ItemUtils;
15
16/**
17 *
18 * A Custom swing popup in Expeditee.
19 *
20 * Expeditee popus can be re-used. Popups are always heavyweight (i.e. uses invalidation).
21 *
22 * @see {@link PopupManager}
23 *
24 * @author Brook Novak
25 *
26 */
27public abstract class Popup extends JPanel {
28
29 //Mike says: Can we get the border for the IW to which this popup corresponds?
30 // Brook says: Would be nice - but popups are actually independant from widgets
31 // =>Now: It is up to the user of the popup to set the border thickness
32 private static final BasicStroke DEFAULT_STROKE = new BasicStroke(2.0f);
33
34 private BasicStroke _borderStroke = DEFAULT_STROKE;
35 private Color _borderColor = Color.BLACK;
36
37 private boolean _isReadyToPaint = false;
38 private boolean _consumeBackClick = false;
39 private boolean _autoHide = true;
40
41 /**
42 * Creates a new popup.
43 * Autohide is set to true.
44 *
45 */
46 public Popup() {
47 super();
48 setVisible(false);
49 }
50
51 /**
52 * Creates a new popup.
53 *
54 * @param layout the LayoutManager to use
55 */
56 public Popup(LayoutManager layout) {
57 super(layout);
58 setVisible(false);
59 }
60
61 @Override
62 public void paint(Graphics g) {
63 super.paint(g);
64
65 // Draw border - if not transparent
66 if (_borderStroke != null && _borderColor != null) {
67 g.setColor(_borderColor);
68 ((Graphics2D)g).setStroke(_borderStroke);
69 g.drawRect(0, 0, getWidth(), getHeight());
70 }
71 }
72
73 private void ignoreAWTPainting(Component c) {
74
75 if (c instanceof JComponent) {
76 ((JComponent)c).setDoubleBuffered(false);
77 }
78
79 c.setIgnoreRepaint(true);
80
81 if (c instanceof Container) {
82 for (Component child : ((Container) c).getComponents()) {
83
84 if (child instanceof Container) {
85 ignoreAWTPainting(child);
86 } else {
87 if (child instanceof JComponent) {
88 ((JComponent)child).setDoubleBuffered(false);
89 }
90
91 child.setIgnoreRepaint(true);
92 }
93 }
94 }
95
96 }
97
98 /**
99 * Ensures that AWT painting turned off
100 */
101 void prepareToPaint() {
102 if (!_isReadyToPaint) {
103 _isReadyToPaint = true;
104 ignoreAWTPainting(this);
105 }
106 }
107
108 /**
109 * Invoked when the popup becomes hidden, or when the popup is animating to show but cancelled.
110 */
111 public void onHide() {}
112
113 /**
114 * Invoked when the popup shows. Note this might not eventuate for animated popups.
115 */
116 public void onShow() {}
117
118 /**
119 * Invoked when popups is going to show.
120 * This always is invoked first.
121 */
122 public void onShowing() {}
123
124 public boolean shouldConsumeBackClick() {
125 return _consumeBackClick;
126 }
127
128 /**
129 * @param consumeBackClick
130 * Set to True for whenever the user clicks empty space
131 * to go back a frame that if this popup is visible should
132 * consume the back-click event.
133 */
134 protected void setConsumeBackClick(boolean consumeBackClick) {
135 _consumeBackClick = consumeBackClick;
136 }
137
138 /**
139 * @param autoHideOn
140 * Set to True if this popup should auto hide. (The default).
141 * Set to false if this popup should be manually hidden.
142 */
143 protected void setAudoHide(boolean autoHideOn) {
144 _autoHide = autoHideOn;
145 }
146
147 /**
148 * @return
149 * True if this popup auto hides.
150 */
151 public boolean doesAutoHide() {
152 return _autoHide;
153 }
154
155 /**
156 * Invalidates self.
157 *
158 * @param thickness
159 * The new thickness to set. Null for no border.
160 */
161 public void setBorderThickness(float thickness) {
162 assert(thickness >= 0);
163
164 if (_borderStroke != null && _borderStroke.getLineWidth() == thickness)
165 return;
166
167 boolean posInvalidate = true;
168
169 if (thickness < _borderStroke.getLineWidth()) {
170 invalidateAppearance();
171 posInvalidate = false;
172 }
173
174 if (thickness == 0) _borderStroke = null;
175 else _borderStroke = new BasicStroke(thickness);
176
177 if (posInvalidate) invalidateAppearance();
178
179 }
180
181 /**
182 * @return
183 * The border thickness of this popup. Zero or more.
184 */
185 public float getBorderThickness() {
186 if (_borderStroke == null) return 0.0f;
187 return _borderStroke.getLineWidth();
188
189 }
190
191 /**
192 * Sets the border color around the popup.
193 * Invalidates self.
194 *
195 * @param c
196 * The new color. Null for transparent.
197 */
198 public void setBorderColor(Color c) {
199
200 if (c == null && _borderColor != null)
201 invalidateAppearance();
202
203 if (c != _borderColor) {
204 _borderColor = c;
205 invalidateAppearance();
206 }
207 }
208
209 /**
210 *
211 * @return
212 * The border color for the popup. NUll if transparent
213 */
214 public Color getBorderColor() {
215 return _borderColor;
216 }
217
218 /**
219 * Invalidates the whole popup so that it must be fully repainted.
220 */
221 public void invalidateAppearance() {
222
223 if (_borderColor != null && _borderStroke != null && _borderStroke.getLineWidth() > 0) { // border
224 FrameGraphics.invalidateArea(ItemUtils.expandRectangle(getBounds(),
225 (int)Math.ceil(getBorderThickness()) + 1));
226 } else { // no border
227 FrameGraphics.invalidateArea(getBounds());
228 }
229
230 }
231
232
233
234}
Note: See TracBrowser for help on using the repository browser.