1 | package org.expeditee.gui;
|
---|
2 |
|
---|
3 | import java.awt.BasicStroke;
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Container;
|
---|
6 | import java.awt.Graphics;
|
---|
7 | import java.awt.Graphics2D;
|
---|
8 | import java.awt.LayoutManager;
|
---|
9 |
|
---|
10 | import javax.swing.JComponent;
|
---|
11 | import javax.swing.JPanel;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | *
|
---|
15 | * A Custom swing popup in Expeditee.
|
---|
16 | *
|
---|
17 | * Expeditee popus can be re-used. Popups are always heavyweight (i.e. uses invalidation).
|
---|
18 | *
|
---|
19 | * @see {@link PopupManager}
|
---|
20 | *
|
---|
21 | * @author Brook Novak
|
---|
22 | *
|
---|
23 | */
|
---|
24 | public abstract class Popup extends JPanel {
|
---|
25 |
|
---|
26 | //Mike says: Can we get the border for the IW to which this popup corresponds?
|
---|
27 | // Brook says: Would be nice - but popups are actually independant from widgets
|
---|
28 | // =>Now: It is up to the user of the popup to set the border thickness
|
---|
29 | private static final BasicStroke DEFAULT_STROKE = new BasicStroke(2.0f);
|
---|
30 |
|
---|
31 | private BasicStroke _lineStroke = DEFAULT_STROKE;
|
---|
32 |
|
---|
33 | private boolean _isReadyToPaint = false;
|
---|
34 | private boolean _consumeBackClick = false;
|
---|
35 | private boolean _autoHide = true;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Creates a new popup.
|
---|
39 | * Autohide is set to true.
|
---|
40 | *
|
---|
41 | */
|
---|
42 | public Popup() {
|
---|
43 | super();
|
---|
44 | setVisible(false);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Creates a new popup.
|
---|
49 | *
|
---|
50 | * @param layout the LayoutManager to use
|
---|
51 | */
|
---|
52 | public Popup(LayoutManager layout) {
|
---|
53 | super(layout);
|
---|
54 | setVisible(false);
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public void paint(Graphics g) {
|
---|
59 | super.paint(g);
|
---|
60 |
|
---|
61 | if (_lineStroke != null) {
|
---|
62 | // Draw iwidget-like border for consistancy
|
---|
63 | ((Graphics2D)g).setStroke(_lineStroke);
|
---|
64 | g.drawRect(0, 0, getWidth(), getHeight());
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | private void ignoreAWTPainting(Component c) {
|
---|
69 |
|
---|
70 | if (c instanceof JComponent) {
|
---|
71 | ((JComponent)c).setDoubleBuffered(false);
|
---|
72 | }
|
---|
73 |
|
---|
74 | c.setIgnoreRepaint(true);
|
---|
75 |
|
---|
76 | if (c instanceof Container) {
|
---|
77 | for (Component child : ((Container) c).getComponents()) {
|
---|
78 |
|
---|
79 | if (child instanceof Container) {
|
---|
80 | ignoreAWTPainting(child);
|
---|
81 | } else {
|
---|
82 | if (child instanceof JComponent) {
|
---|
83 | ((JComponent)child).setDoubleBuffered(false);
|
---|
84 | }
|
---|
85 |
|
---|
86 | child.setIgnoreRepaint(true);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Ensures that AWT painting turned off
|
---|
95 | */
|
---|
96 | void prepareToPaint() {
|
---|
97 | if (!_isReadyToPaint) {
|
---|
98 | _isReadyToPaint = true;
|
---|
99 | ignoreAWTPainting(this);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Invoked when the popup becomes hidden, or when the popup is animating to show but cancelled.
|
---|
105 | */
|
---|
106 | public void onHide() {}
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Invoked when the popup shows. Note this might not eventuate for animated popups.
|
---|
110 | */
|
---|
111 | public void onShow() {}
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Invoked when popups is going to show.
|
---|
115 | * This always is invoked first.
|
---|
116 | */
|
---|
117 | public void onShowing() {}
|
---|
118 |
|
---|
119 | public boolean shouldConsumeBackClick() {
|
---|
120 | return _consumeBackClick;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * @param consumeBackClick
|
---|
125 | * Set to True for whenever the user clicks empty space
|
---|
126 | * to go back a frame that if this popup is visible should
|
---|
127 | * consume the back-click event.
|
---|
128 | */
|
---|
129 | protected void setConsumeBackClick(boolean consumeBackClick) {
|
---|
130 | _consumeBackClick = consumeBackClick;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * @param autoHideOn
|
---|
135 | * Set to True if this popup should auto hide. (The default).
|
---|
136 | * Set to false if this popup should be manually hidden.
|
---|
137 | */
|
---|
138 | protected void setAudoHide(boolean autoHideOn) {
|
---|
139 | _autoHide = autoHideOn;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * @return
|
---|
144 | * True if this popup auto hides.
|
---|
145 | */
|
---|
146 | public boolean doesAutoHide() {
|
---|
147 | return _autoHide;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | *
|
---|
152 | * @param thickness
|
---|
153 | * The new thickness to set. Null for no border.
|
---|
154 | */
|
---|
155 | public void setBorderThickness(float thickness) {
|
---|
156 | assert(thickness >= 0);
|
---|
157 |
|
---|
158 | if (thickness == 0) _lineStroke = null;
|
---|
159 | else _lineStroke = new BasicStroke(thickness);
|
---|
160 |
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * @return
|
---|
165 | * The border thickness of this popup. Zero or more.
|
---|
166 | */
|
---|
167 | public float getBorderThickness() {
|
---|
168 | if (_lineStroke == null) return 0.0f;
|
---|
169 | return _lineStroke.getLineWidth();
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | }
|
---|