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

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

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