Debian Codegraph @

src/com/jidesoft/plaf/basic/BasicJidePopupUI.java

raw
1 /*
2 * @(#)BasicJidePopupUI.java 2/25/2005
3 *
4 * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
5 */
6
7 package com.jidesoft.plaf.basic;
8
9 import com.jidesoft.plaf.PopupUI;
10 import com.jidesoft.plaf.UIDefaultsLookup;
11 import com.jidesoft.popup.JidePopup;
12 import com.jidesoft.swing.Gripper;
13
14 import javax.swing.*;
15 import javax.swing.plaf.ActionMapUIResource;
16 import javax.swing.plaf.ComponentUI;
17 import javax.swing.plaf.UIResource;
18 import java.awt.*;
19 import java.beans.PropertyChangeEvent;
20 import java.beans.PropertyChangeListener;
21
22
23 /**
24 * A basic L&F implementation of Popup.
25 */
26 public class BasicJidePopupUI extends PopupUI {
27
28 protected JidePopup _popup;
29
30 // protected MouseInputAdapter _borderListener;
31 protected PropertyChangeListener _propertyChangeListener;
32 protected LayoutManager _dockableFrameLayout;
33
34 protected JComponent _northPane;
35 protected JComponent _southPane;
36 protected JComponent _westPane;
37 protected JComponent _eastPane;
38
39 protected Gripper _titlePane; // access needs this
40
41 private boolean keyBindingRegistered = false;
42 private boolean keyBindingActive = false;
43
44 /////////////////////////////////////////////////////////////////////////////
45 // ComponentUI Interface Implementation methods
46 /////////////////////////////////////////////////////////////////////////////
47
48 public static ComponentUI createUI(JComponent b) {
49 return new BasicJidePopupUI((JidePopup) b);
50 }
51
52 public BasicJidePopupUI() {
53 }
54
55 public BasicJidePopupUI(JidePopup f) {
56 _popup = f;
57 }
58
59 @Override
60 public void installUI(JComponent c) {
61
62 _popup = (JidePopup) c;
63
64 installDefaults();
65 installListeners();
66 installComponents();
67 installKeyboardActions();
68 _popup.setOpaque(true);
69 }
70
71 @Override
72 public void uninstallUI(JComponent c) {
73 if (c != _popup)
74 throw new IllegalComponentStateException(this + " was asked to deinstall() "
75 + c + " when it only knows about "
76 + _popup + ".");
77
78 uninstallKeyboardActions();
79 uninstallComponents();
80 uninstallListeners();
81 uninstallDefaults();
82 _popup = null;
83 }
84
85 protected void installDefaults() {
86 JComponent contentPane = (JComponent) _popup.getContentPane();
87 if (contentPane != null) {
88 Color bg = contentPane.getBackground();
89 if (bg instanceof UIResource)
90 contentPane.setBackground(null);
91 }
92 _popup.setLayout(_dockableFrameLayout = createLayoutManager());
93 _popup.setBackground(UIDefaultsLookup.getColor("JideButton.background"));
94
95 LookAndFeel.installBorder(_popup, "Popup.border");
96
97 }
98
99 protected void installKeyboardActions() {
100 ActionMap actionMap = getActionMap();
101 SwingUtilities.replaceUIActionMap(_popup, actionMap);
102 }
103
104 ActionMap getActionMap() {
105 ActionMap map = (ActionMap) UIDefaultsLookup.get("Popup.actionMap");
106 if (map == null) {
107 map = createActionMap();
108 if (map != null) {
109 UIManager.getLookAndFeelDefaults().put("Popup.actionMap",
110 map);
111 }
112 }
113 return map;
114 }
115
116 ActionMap createActionMap() {
117 ActionMap map = new ActionMapUIResource();
118 // we don't use it right now. Leave it since we might use it later.
119 return map;
120 }
121
122 protected void installComponents() {
123 setNorthPane(createNorthPane(_popup));
124 setSouthPane(createSouthPane(_popup));
125 setEastPane(createEastPane(_popup));
126 setWestPane(createWestPane(_popup));
127 }
128
129 /*
130 * @since 1.3
131 */
132 protected void installListeners() {
133 _propertyChangeListener = createPropertyChangeListener();
134 _popup.addPropertyChangeListener(_propertyChangeListener);
135 }
136
137 InputMap getInputMap(int condition) {
138 if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
139 return createInputMap(condition);
140 }
141 return null;
142 }
143
144 InputMap createInputMap(int condition) {
145 if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
146 Object[] bindings = (Object[]) UIDefaultsLookup.get("Popup.windowBindings");
147
148 if (bindings != null) {
149 return LookAndFeel.makeComponentInputMap(_popup, bindings);
150 }
151 }
152 return null;
153 }
154
155 protected void uninstallDefaults() {
156 _dockableFrameLayout = null;
157 _popup.setLayout(null);
158 LookAndFeel.uninstallBorder(_popup);
159 }
160
161 protected void uninstallComponents() {
162 setNorthPane(null);
163 setSouthPane(null);
164 setEastPane(null);
165 setWestPane(null);
166 _titlePane = null;
167 }
168
169 /*
170 * @since 1.3
171 */
172 protected void uninstallListeners() {
173 _popup.removePropertyChangeListener(_propertyChangeListener);
174 _propertyChangeListener = null;
175 }
176
177 protected void uninstallKeyboardActions() {
178 SwingUtilities.replaceUIInputMap(_popup, JComponent.
179 WHEN_IN_FOCUSED_WINDOW, null);
180 SwingUtilities.replaceUIActionMap(_popup, null);
181
182 }
183
184 @Override
185 public Component getGripper() {
186 return _titlePane;
187 }
188
189 protected LayoutManager createLayoutManager() {
190 return new PopupLayout();
191 }
192
193 protected PropertyChangeListener createPropertyChangeListener() {
194 return new PopupPropertyChangeListener();
195 }
196
197
198 @Override
199 public Dimension getPreferredSize(JComponent x) {
200 if (_popup == x && _popup.getLayout() != null)
201 return _popup.getLayout().preferredLayoutSize(x);
202 return new Dimension(100, 100);
203 }
204
205 @Override
206 public Dimension getMinimumSize(JComponent x) {
207 if (_popup == x) {
208 return _popup.getLayout().minimumLayoutSize(x);
209 }
210 return new Dimension(0, 0);
211 }
212
213 @Override
214 public Dimension getMaximumSize(JComponent x) {
215 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
216 }
217
218
219 /**
220 * Installs necessary mouse handlers on <code>newPane</code> and adds it to the frame. Reverse process for the
221 * <code>currentPane</code>.
222 */
223 protected void replacePane(JComponent currentPane, JComponent newPane) {
224 if (currentPane != null) {
225 deinstallMouseHandlers(currentPane);
226 _popup.remove(currentPane);
227 }
228 if (newPane != null) {
229 _popup.add(newPane);
230 installMouseHandlers(newPane);
231 }
232 }
233
234 protected void deinstallMouseHandlers(JComponent c) {
235 }
236
237 protected void installMouseHandlers(JComponent c) {
238 }
239
240 protected JComponent createNorthPane(JidePopup w) {
241 if (w.getGripperLocation() == SwingConstants.NORTH && w.isMovable()) {
242 _titlePane = new Gripper();
243 _titlePane.setOrientation(SwingConstants.VERTICAL);
244 _titlePane.setRolloverEnabled(true);
245 _titlePane.setOpaque(true);
246 return _titlePane;
247 }
248 else {
249 return null;
250 }
251 }
252
253
254 protected JComponent createSouthPane(JidePopup w) {
255 if (w.getGripperLocation() == SwingConstants.SOUTH && w.isMovable()) {
256 _titlePane = new Gripper();
257 _titlePane.setOrientation(SwingConstants.VERTICAL);
258 _titlePane.setRolloverEnabled(true);
259 _titlePane.setOpaque(true);
260 return _titlePane;
261 }
262 else {
263 return null;
264 }
265 }
266
267 protected JComponent createWestPane(JidePopup w) {
268 if (w.getGripperLocation() == SwingConstants.WEST && w.isMovable()) {
269 _titlePane = new Gripper();
270 _titlePane.setOrientation(SwingConstants.HORIZONTAL);
271 _titlePane.setRolloverEnabled(true);
272 _titlePane.setOpaque(true);
273 return _titlePane;
274 }
275 else {
276 return null;
277 }
278 }
279
280 protected JComponent createEastPane(JidePopup w) {
281 if (w.getGripperLocation() == SwingConstants.EAST && w.isMovable()) {
282 _titlePane = new Gripper();
283 _titlePane.setOrientation(SwingConstants.HORIZONTAL);
284 _titlePane.setRolloverEnabled(true);
285 _titlePane.setOpaque(true);
286 return _titlePane;
287 }
288 else {
289 return null;
290 }
291 }
292
293
294 protected final boolean isKeyBindingRegistered() {
295 return keyBindingRegistered;
296 }
297
298 protected final void setKeyBindingRegistered(boolean b) {
299 keyBindingRegistered = b;
300 }
301
302 public final boolean isKeyBindingActive() {
303 return keyBindingActive;
304 }
305
306 protected final void setKeyBindingActive(boolean b) {
307 keyBindingActive = b;
308 }
309
310
311 protected void setupMenuOpenKey() {
312 // PENDING(hania): Why are these WHEN_IN_FOCUSED_WINDOWs? Shouldn't
313 // they be WHEN_ANCESTOR_OF_FOCUSED_COMPONENT?
314 // Also, no longer registering on the DesktopIcon, the previous
315 // action did nothing.
316 InputMap map = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
317 SwingUtilities.replaceUIInputMap(_popup,
318 JComponent.WHEN_IN_FOCUSED_WINDOW, map);
319 //ActionMap actionMap = getActionMap();
320 //SwingUtilities.replaceUIActionMap(frame, actionMap);
321 }
322
323 protected void setupMenuCloseKey() {
324 }
325
326 public JComponent getNorthPane() {
327 return _northPane;
328 }
329
330 protected void setNorthPane(JComponent c) {
331 replacePane(_northPane, c);
332 _northPane = c;
333 }
334
335 public JComponent getSouthPane() {
336 return _southPane;
337 }
338
339 protected void setSouthPane(JComponent c) {
340 replacePane(_southPane, c);
341 _southPane = c;
342 }
343
344 public JComponent getWestPane() {
345 return _westPane;
346 }
347
348 protected void setWestPane(JComponent c) {
349 replacePane(_westPane, c);
350 _westPane = c;
351 }
352
353 public JComponent getEastPane() {
354 return _eastPane;
355 }
356
357 protected void setEastPane(JComponent c) {
358 replacePane(_eastPane, c);
359 _eastPane = c;
360 }
361
362 public class PopupPropertyChangeListener implements PropertyChangeListener {
363 /**
364 * Detects changes in state from the Popup and handles actions.
365 */
366 public void propertyChange(PropertyChangeEvent evt) {
367 String prop = evt.getPropertyName();
368 JidePopup f = (JidePopup) evt.getSource();
369 Object newValue = evt.getNewValue();
370 Object oldValue = evt.getOldValue();
371 if (JidePopup.MOVABLE_PROPERTY.equals(prop)) {
372 f.updateUI();
373 }
374 if (JidePopup.PROPERTY_GRIPPER_LOCATION.equals(prop)) {
375 f.updateUI();
376 }
377 }
378 }
379
380 public class PopupLayout implements LayoutManager {
381 public void addLayoutComponent(String name, Component c) {
382 }
383
384 public void removeLayoutComponent(Component c) {
385 }
386
387 public Dimension preferredLayoutSize(Container c) {
388 Dimension result;
389 Insets i = _popup.getInsets();
390
391 result = new Dimension(_popup.getRootPane().getPreferredSize());
392 result.width += i.left + i.right;
393 result.height += i.top + i.bottom;
394
395 if (getNorthPane() != null) {
396 Dimension d = getNorthPane().getPreferredSize();
397 result.width = Math.max(d.width, result.width);
398 result.height += d.height;
399 }
400
401 if (getSouthPane() != null) {
402 Dimension d = getSouthPane().getPreferredSize();
403 result.width = Math.max(d.width, result.width);
404 result.height += d.height;
405 }
406
407 if (getEastPane() != null) {
408 Dimension d = getEastPane().getPreferredSize();
409 result.width += d.width;
410 result.height = Math.max(d.height, result.height);
411 }
412
413 if (getWestPane() != null) {
414 Dimension d = getWestPane().getPreferredSize();
415 result.width += d.width;
416 result.height = Math.max(d.height, result.height);
417 }
418
419 return result;
420 }
421
422 public Dimension minimumLayoutSize(Container c) {
423
424 // The minimum size of the dockable frame only takes into account the
425 // _title pane since you are allowed to resize the frames to the point
426 // where just the _title pane is visible.
427 Dimension result = new Dimension();
428 if (getNorthPane() != null) {
429 result = new Dimension(getNorthPane().getMinimumSize());
430 }
431 if (getSouthPane() != null) {
432 Dimension minimumSize = getSouthPane().getMinimumSize();
433 result.width = Math.max(result.width, minimumSize.width);
434 result.height += minimumSize.height;
435 }
436 if (getEastPane() != null) {
437 Dimension minimumSize = getEastPane().getMinimumSize();
438 result.width += minimumSize.width;
439 result.height = Math.max(result.height, minimumSize.height);
440 }
441 if (getWestPane() != null) {
442 Dimension minimumSize = getWestPane().getMinimumSize();
443 result.width = Math.max(result.width, minimumSize.width);
444 result.height += minimumSize.height;
445 }
446 Dimension alter = _popup.getContentPane().getMinimumSize();
447
448 if (alter.width > result.width) {
449 result.width = alter.width;
450 }
451 result.height += alter.height;
452
453 Insets i = _popup.getInsets();
454 result.width += i.left + i.right;
455 result.height += i.top + i.bottom;
456
457 return result;
458 }
459
460 public void layoutContainer(Container c) {
461 Insets i = _popup.getInsets();
462 int cx, cy, cw, ch;
463
464 cx = i.left;
465 cy = i.top;
466 cw = _popup.getWidth() - i.left - i.right;
467 ch = _popup.getHeight() - i.top - i.bottom;
468
469 if (getNorthPane() != null) {
470 getNorthPane().setVisible(true);
471 Dimension size = getNorthPane().getPreferredSize();
472 getNorthPane().setBounds(cx, cy, cw, size.height);
473 cy += size.height;
474 ch -= size.height;
475 }
476 if (getSouthPane() != null) {
477 Dimension size = getSouthPane().getPreferredSize();
478 getSouthPane().setBounds(cx, _popup.getHeight()
479 - i.bottom - size.height,
480 cw, size.height);
481 ch -= size.height;
482 }
483 if (getWestPane() != null) {
484 Dimension size = getWestPane().getPreferredSize();
485 getWestPane().setBounds(cx, cy, size.width, ch);
486 cw -= size.width;
487 cx += size.width;
488 }
489 if (getEastPane() != null) {
490 Dimension size = getEastPane().getPreferredSize();
491 getEastPane().setBounds(cw - size.width, cy, size.width, ch);
492 cw -= size.width;
493 }
494
495 if (_popup.getRootPane() != null) {
496 _popup.getRootPane().setBounds(cx, cy, cw, ch);
497 }
498 }
499 }
500 }