source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java@ 10378

Last change on this file since 10378 was 10378, checked in by Don-vip, 8 years ago

Checkstyle 6.19: enable SingleSpaceSeparator and fix violations

  • Property svn:eol-style set to native
File size: 34.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.AWTEvent;
7import java.awt.BorderLayout;
8import java.awt.Component;
9import java.awt.Container;
10import java.awt.Dimension;
11import java.awt.FlowLayout;
12import java.awt.Graphics;
13import java.awt.GraphicsEnvironment;
14import java.awt.GridBagLayout;
15import java.awt.GridLayout;
16import java.awt.Rectangle;
17import java.awt.Toolkit;
18import java.awt.event.AWTEventListener;
19import java.awt.event.ActionEvent;
20import java.awt.event.ActionListener;
21import java.awt.event.ComponentAdapter;
22import java.awt.event.ComponentEvent;
23import java.awt.event.MouseEvent;
24import java.awt.event.WindowAdapter;
25import java.awt.event.WindowEvent;
26import java.beans.PropertyChangeEvent;
27import java.util.ArrayList;
28import java.util.Arrays;
29import java.util.Collection;
30import java.util.LinkedList;
31import java.util.List;
32
33import javax.swing.AbstractAction;
34import javax.swing.BorderFactory;
35import javax.swing.ButtonGroup;
36import javax.swing.ImageIcon;
37import javax.swing.JButton;
38import javax.swing.JCheckBoxMenuItem;
39import javax.swing.JComponent;
40import javax.swing.JDialog;
41import javax.swing.JLabel;
42import javax.swing.JMenu;
43import javax.swing.JPanel;
44import javax.swing.JPopupMenu;
45import javax.swing.JRadioButtonMenuItem;
46import javax.swing.JScrollPane;
47import javax.swing.JToggleButton;
48import javax.swing.Scrollable;
49import javax.swing.SwingUtilities;
50
51import org.openstreetmap.josm.Main;
52import org.openstreetmap.josm.actions.JosmAction;
53import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
54import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
55import org.openstreetmap.josm.data.preferences.BooleanProperty;
56import org.openstreetmap.josm.data.preferences.ParametrizedEnumProperty;
57import org.openstreetmap.josm.gui.MainMenu;
58import org.openstreetmap.josm.gui.ShowHideButtonListener;
59import org.openstreetmap.josm.gui.SideButton;
60import org.openstreetmap.josm.gui.dialogs.DialogsPanel.Action;
61import org.openstreetmap.josm.gui.help.HelpUtil;
62import org.openstreetmap.josm.gui.help.Helpful;
63import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
64import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
65import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
66import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
67import org.openstreetmap.josm.gui.util.GuiHelper;
68import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
69import org.openstreetmap.josm.tools.Destroyable;
70import org.openstreetmap.josm.tools.GBC;
71import org.openstreetmap.josm.tools.ImageProvider;
72import org.openstreetmap.josm.tools.Shortcut;
73import org.openstreetmap.josm.tools.WindowGeometry;
74import org.openstreetmap.josm.tools.WindowGeometry.WindowGeometryException;
75
76/**
77 * This class is a toggle dialog that can be turned on and off.
78 * @since 8
79 */
80public class ToggleDialog extends JPanel implements ShowHideButtonListener, Helpful, AWTEventListener, Destroyable, PreferenceChangedListener {
81
82 /**
83 * The button-hiding strategy in toggler dialogs.
84 */
85 public enum ButtonHidingType {
86 /** Buttons are always shown (default) **/
87 ALWAYS_SHOWN,
88 /** Buttons are always hidden **/
89 ALWAYS_HIDDEN,
90 /** Buttons are dynamically hidden, i.e. only shown when mouse cursor is in dialog */
91 DYNAMIC
92 }
93
94 /**
95 * Property to enable dynamic buttons globally.
96 * @since 6752
97 */
98 public static final BooleanProperty PROP_DYNAMIC_BUTTONS = new BooleanProperty("dialog.dynamic.buttons", false);
99
100 private final transient ParametrizedEnumProperty<ButtonHidingType> propButtonHiding =
101 new ParametrizedEnumProperty<ToggleDialog.ButtonHidingType>(ButtonHidingType.class, ButtonHidingType.DYNAMIC) {
102 @Override
103 protected String getKey(String... params) {
104 return preferencePrefix + ".buttonhiding";
105 }
106
107 @Override
108 protected ButtonHidingType parse(String s) {
109 try {
110 return super.parse(s);
111 } catch (IllegalArgumentException e) {
112 // Legacy settings
113 return Boolean.parseBoolean(s) ? ButtonHidingType.DYNAMIC : ButtonHidingType.ALWAYS_SHOWN;
114 }
115 }
116 };
117
118 /** The action to toggle this dialog */
119 protected final ToggleDialogAction toggleAction;
120 protected String preferencePrefix;
121 protected final String name;
122
123 /** DialogsPanel that manages all ToggleDialogs */
124 protected DialogsPanel dialogsPanel;
125
126 protected TitleBar titleBar;
127
128 /**
129 * Indicates whether the dialog is showing or not.
130 */
131 protected boolean isShowing;
132
133 /**
134 * If isShowing is true, indicates whether the dialog is docked or not, e. g.
135 * shown as part of the main window or as a separate dialog window.
136 */
137 protected boolean isDocked;
138
139 /**
140 * If isShowing and isDocked are true, indicates whether the dialog is
141 * currently minimized or not.
142 */
143 protected boolean isCollapsed;
144
145 /**
146 * Indicates whether dynamic button hiding is active or not.
147 */
148 protected ButtonHidingType buttonHiding;
149
150 /** the preferred height if the toggle dialog is expanded */
151 private int preferredHeight;
152
153 /** the JDialog displaying the toggle dialog as undocked dialog */
154 protected JDialog detachedDialog;
155
156 protected JToggleButton button;
157 private JPanel buttonsPanel;
158 private final transient List<javax.swing.Action> buttonActions = new ArrayList<>();
159
160 /** holds the menu entry in the windows menu. Required to properly
161 * toggle the checkbox on show/hide
162 */
163 protected JCheckBoxMenuItem windowMenuItem;
164
165 private final JRadioButtonMenuItem alwaysShown = new JRadioButtonMenuItem(new AbstractAction(tr("Always shown")) {
166 @Override
167 public void actionPerformed(ActionEvent e) {
168 setIsButtonHiding(ButtonHidingType.ALWAYS_SHOWN);
169 }
170 });
171
172 private final JRadioButtonMenuItem dynamic = new JRadioButtonMenuItem(new AbstractAction(tr("Dynamic")) {
173 @Override
174 public void actionPerformed(ActionEvent e) {
175 setIsButtonHiding(ButtonHidingType.DYNAMIC);
176 }
177 });
178
179 private final JRadioButtonMenuItem alwaysHidden = new JRadioButtonMenuItem(new AbstractAction(tr("Always hidden")) {
180 @Override
181 public void actionPerformed(ActionEvent e) {
182 setIsButtonHiding(ButtonHidingType.ALWAYS_HIDDEN);
183 }
184 });
185
186 /**
187 * The linked preferences class (optional). If set, accessible from the title bar with a dedicated button
188 */
189 protected Class<? extends PreferenceSetting> preferenceClass;
190
191 /**
192 * Constructor
193 *
194 * @param name the name of the dialog
195 * @param iconName the name of the icon to be displayed
196 * @param tooltip the tool tip
197 * @param shortcut the shortcut
198 * @param preferredHeight the preferred height for the dialog
199 */
200 public ToggleDialog(String name, String iconName, String tooltip, Shortcut shortcut, int preferredHeight) {
201 this(name, iconName, tooltip, shortcut, preferredHeight, false);
202 }
203
204 /**
205 * Constructor
206
207 * @param name the name of the dialog
208 * @param iconName the name of the icon to be displayed
209 * @param tooltip the tool tip
210 * @param shortcut the shortcut
211 * @param preferredHeight the preferred height for the dialog
212 * @param defShow if the dialog should be shown by default, if there is no preference
213 */
214 public ToggleDialog(String name, String iconName, String tooltip, Shortcut shortcut, int preferredHeight, boolean defShow) {
215 this(name, iconName, tooltip, shortcut, preferredHeight, defShow, null);
216 }
217
218 /**
219 * Constructor
220 *
221 * @param name the name of the dialog
222 * @param iconName the name of the icon to be displayed
223 * @param tooltip the tool tip
224 * @param shortcut the shortcut
225 * @param preferredHeight the preferred height for the dialog
226 * @param defShow if the dialog should be shown by default, if there is no preference
227 * @param prefClass the preferences settings class, or null if not applicable
228 */
229 public ToggleDialog(String name, String iconName, String tooltip, Shortcut shortcut, int preferredHeight, boolean defShow,
230 Class<? extends PreferenceSetting> prefClass) {
231 super(new BorderLayout());
232 this.preferencePrefix = iconName;
233 this.name = name;
234 this.preferenceClass = prefClass;
235
236 /** Use the full width of the parent element */
237 setPreferredSize(new Dimension(0, preferredHeight));
238 /** Override any minimum sizes of child elements so the user can resize freely */
239 setMinimumSize(new Dimension(0, 0));
240 this.preferredHeight = preferredHeight;
241 toggleAction = new ToggleDialogAction(name, "dialogs/"+iconName, tooltip, shortcut);
242 String helpId = "Dialog/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
243 toggleAction.putValue("help", helpId.substring(0, helpId.length()-6));
244
245 isShowing = Main.pref.getBoolean(preferencePrefix+".visible", defShow);
246 isDocked = Main.pref.getBoolean(preferencePrefix+".docked", true);
247 isCollapsed = Main.pref.getBoolean(preferencePrefix+".minimized", false);
248 buttonHiding = propButtonHiding.get();
249
250 /** show the minimize button */
251 titleBar = new TitleBar(name, iconName);
252 add(titleBar, BorderLayout.NORTH);
253
254 setBorder(BorderFactory.createEtchedBorder());
255
256 Main.redirectToMainContentPane(this);
257 Main.pref.addPreferenceChangeListener(this);
258
259 windowMenuItem = MainMenu.addWithCheckbox(Main.main.menu.windowMenu,
260 (JosmAction) getToggleAction(),
261 MainMenu.WINDOW_MENU_GROUP.TOGGLE_DIALOG);
262 }
263
264 /**
265 * The action to toggle the visibility state of this toggle dialog.
266 *
267 * Emits {@link PropertyChangeEvent}s for the property <tt>selected</tt>:
268 * <ul>
269 * <li>true, if the dialog is currently visible</li>
270 * <li>false, if the dialog is currently invisible</li>
271 * </ul>
272 *
273 */
274 public final class ToggleDialogAction extends JosmAction {
275
276 private ToggleDialogAction(String name, String iconName, String tooltip, Shortcut shortcut) {
277 super(name, iconName, tooltip, shortcut, false);
278 }
279
280 @Override
281 public void actionPerformed(ActionEvent e) {
282 toggleButtonHook();
283 if (getValue("toolbarbutton") instanceof JButton) {
284 ((JButton) getValue("toolbarbutton")).setSelected(!isShowing);
285 }
286 if (isShowing) {
287 hideDialog();
288 if (dialogsPanel != null) {
289 dialogsPanel.reconstruct(Action.ELEMENT_SHRINKS, null);
290 }
291 hideNotify();
292 } else {
293 showDialog();
294 if (isDocked && isCollapsed) {
295 expand();
296 }
297 if (isDocked && dialogsPanel != null) {
298 dialogsPanel.reconstruct(Action.INVISIBLE_TO_DEFAULT, ToggleDialog.this);
299 }
300 showNotify();
301 }
302 }
303 }
304
305 /**
306 * Shows the dialog
307 */
308 public void showDialog() {
309 setIsShowing(true);
310 if (!isDocked) {
311 detach();
312 } else {
313 dock();
314 this.setVisible(true);
315 }
316 // toggling the selected value in order to enforce PropertyChangeEvents
317 setIsShowing(true);
318 windowMenuItem.setState(true);
319 toggleAction.putValue("selected", Boolean.FALSE);
320 toggleAction.putValue("selected", Boolean.TRUE);
321 }
322
323 /**
324 * Changes the state of the dialog such that the user can see the content.
325 * (takes care of the panel reconstruction)
326 */
327 public void unfurlDialog() {
328 if (isDialogInDefaultView())
329 return;
330 if (isDialogInCollapsedView()) {
331 expand();
332 dialogsPanel.reconstruct(Action.COLLAPSED_TO_DEFAULT, this);
333 } else if (!isDialogShowing()) {
334 showDialog();
335 if (isDocked && isCollapsed) {
336 expand();
337 }
338 if (isDocked) {
339 dialogsPanel.reconstruct(Action.INVISIBLE_TO_DEFAULT, this);
340 }
341 showNotify();
342 }
343 }
344
345 @Override
346 public void buttonHidden() {
347 if ((Boolean) toggleAction.getValue("selected")) {
348 toggleAction.actionPerformed(null);
349 }
350 }
351
352 @Override
353 public void buttonShown() {
354 unfurlDialog();
355 }
356
357 /**
358 * Hides the dialog
359 */
360 public void hideDialog() {
361 closeDetachedDialog();
362 this.setVisible(false);
363 windowMenuItem.setState(false);
364 setIsShowing(false);
365 toggleAction.putValue("selected", Boolean.FALSE);
366 }
367
368 /**
369 * Displays the toggle dialog in the toggle dialog view on the right
370 * of the main map window.
371 *
372 */
373 protected void dock() {
374 detachedDialog = null;
375 titleBar.setVisible(true);
376 setIsDocked(true);
377 }
378
379 /**
380 * Display the dialog in a detached window.
381 *
382 */
383 protected void detach() {
384 setContentVisible(true);
385 this.setVisible(true);
386 titleBar.setVisible(false);
387 if (!GraphicsEnvironment.isHeadless()) {
388 detachedDialog = new DetachedDialog();
389 detachedDialog.setVisible(true);
390 }
391 setIsShowing(true);
392 setIsDocked(false);
393 }
394
395 /**
396 * Collapses the toggle dialog to the title bar only
397 *
398 */
399 public void collapse() {
400 if (isDialogInDefaultView()) {
401 setContentVisible(false);
402 setIsCollapsed(true);
403 setPreferredSize(new Dimension(0, 20));
404 setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
405 setMinimumSize(new Dimension(Integer.MAX_VALUE, 20));
406 titleBar.lblMinimized.setIcon(ImageProvider.get("misc", "minimized"));
407 } else
408 throw new IllegalStateException();
409 }
410
411 /**
412 * Expands the toggle dialog
413 */
414 protected void expand() {
415 if (isDialogInCollapsedView()) {
416 setContentVisible(true);
417 setIsCollapsed(false);
418 setPreferredSize(new Dimension(0, preferredHeight));
419 setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
420 titleBar.lblMinimized.setIcon(ImageProvider.get("misc", "normal"));
421 } else
422 throw new IllegalStateException();
423 }
424
425 /**
426 * Sets the visibility of all components in this toggle dialog, except the title bar
427 *
428 * @param visible true, if the components should be visible; false otherwise
429 */
430 protected void setContentVisible(boolean visible) {
431 Component[] comps = getComponents();
432 for (Component comp : comps) {
433 if (comp != titleBar && (!visible || comp != buttonsPanel || buttonHiding != ButtonHidingType.ALWAYS_HIDDEN)) {
434 comp.setVisible(visible);
435 }
436 }
437 }
438
439 @Override
440 public void destroy() {
441 closeDetachedDialog();
442 if (isShowing) {
443 hideNotify();
444 }
445 Main.main.menu.windowMenu.remove(windowMenuItem);
446 Toolkit.getDefaultToolkit().removeAWTEventListener(this);
447 Main.pref.removePreferenceChangeListener(this);
448 destroyComponents(this, false);
449 }
450
451 private static void destroyComponents(Component component, boolean destroyItself) {
452 if (component instanceof Container) {
453 for (Component c: ((Container) component).getComponents()) {
454 destroyComponents(c, true);
455 }
456 }
457 if (destroyItself && component instanceof Destroyable) {
458 ((Destroyable) component).destroy();
459 }
460 }
461
462 /**
463 * Closes the detached dialog if this toggle dialog is currently displayed in a detached dialog.
464 */
465 public void closeDetachedDialog() {
466 if (detachedDialog != null) {
467 detachedDialog.setVisible(false);
468 detachedDialog.getContentPane().removeAll();
469 detachedDialog.dispose();
470 }
471 }
472
473 /**
474 * Called when toggle dialog is shown (after it was created or expanded). Descendants may overwrite this
475 * method, it's a good place to register listeners needed to keep dialog updated
476 */
477 public void showNotify() {
478 // Do nothing
479 }
480
481 /**
482 * Called when toggle dialog is hidden (collapsed, removed, MapFrame is removed, ...). Good place to unregister listeners
483 */
484 public void hideNotify() {
485 // Do nothing
486 }
487
488 /**
489 * The title bar displayed in docked mode
490 */
491 protected class TitleBar extends JPanel {
492 /** the label which shows whether the toggle dialog is expanded or collapsed */
493 private final JLabel lblMinimized;
494 /** the label which displays the dialog's title **/
495 private final JLabel lblTitle;
496 private final JComponent lblTitleWeak;
497 /** the button which shows whether buttons are dynamic or not */
498 private final JButton buttonsHide;
499 /** the contextual menu **/
500 private DialogPopupMenu popupMenu;
501
502 public TitleBar(String toggleDialogName, String iconName) {
503 setLayout(new GridBagLayout());
504
505 lblMinimized = new JLabel(ImageProvider.get("misc", "normal"));
506 add(lblMinimized);
507
508 // scale down the dialog icon
509 ImageIcon icon = new ImageProvider("dialogs", iconName).setSize(ImageProvider.ImageSizes.SMALLICON).get();
510 lblTitle = new JLabel("", icon, JLabel.TRAILING);
511 lblTitle.setIconTextGap(8);
512
513 JPanel conceal = new JPanel();
514 conceal.add(lblTitle);
515 conceal.setVisible(false);
516 add(conceal, GBC.std());
517
518 // Cannot add the label directly since it would displace other elements on resize
519 lblTitleWeak = new JComponent() {
520 @Override
521 public void paintComponent(Graphics g) {
522 lblTitle.paint(g);
523 }
524 };
525 lblTitleWeak.setPreferredSize(new Dimension(Integer.MAX_VALUE, 20));
526 lblTitleWeak.setMinimumSize(new Dimension(0, 20));
527 add(lblTitleWeak, GBC.std().fill(GBC.HORIZONTAL));
528
529 buttonsHide = new JButton(ImageProvider.get("misc", buttonHiding != ButtonHidingType.ALWAYS_SHOWN
530 ? /* ICON(misc/)*/ "buttonhide" : /* ICON(misc/)*/ "buttonshow"));
531 buttonsHide.setToolTipText(tr("Toggle dynamic buttons"));
532 buttonsHide.setBorder(BorderFactory.createEmptyBorder());
533 buttonsHide.addActionListener(
534 new ActionListener() {
535 @Override
536 public void actionPerformed(ActionEvent e) {
537 JRadioButtonMenuItem item = (buttonHiding == ButtonHidingType.DYNAMIC) ? alwaysShown : dynamic;
538 item.setSelected(true);
539 item.getAction().actionPerformed(null);
540 }
541 }
542 );
543 add(buttonsHide);
544
545 // show the pref button if applicable
546 if (preferenceClass != null) {
547 JButton pref = new JButton(new ImageProvider("preference").setSize(ImageProvider.ImageSizes.SMALLICON).get());
548 pref.setToolTipText(tr("Open preferences for this panel"));
549 pref.setBorder(BorderFactory.createEmptyBorder());
550 pref.addActionListener(
551 new ActionListener() {
552 @Override
553 @SuppressWarnings("unchecked")
554 public void actionPerformed(ActionEvent e) {
555 final PreferenceDialog p = new PreferenceDialog(Main.parent);
556 if (TabPreferenceSetting.class.isAssignableFrom(preferenceClass)) {
557 p.selectPreferencesTabByClass((Class<? extends TabPreferenceSetting>) preferenceClass);
558 } else if (SubPreferenceSetting.class.isAssignableFrom(preferenceClass)) {
559 p.selectSubPreferencesTabByClass((Class<? extends SubPreferenceSetting>) preferenceClass);
560 }
561 p.setVisible(true);
562 }
563 }
564 );
565 add(pref);
566 }
567
568 // show the sticky button
569 JButton sticky = new JButton(ImageProvider.get("misc", "sticky"));
570 sticky.setToolTipText(tr("Undock the panel"));
571 sticky.setBorder(BorderFactory.createEmptyBorder());
572 sticky.addActionListener(
573 new ActionListener() {
574 @Override
575 public void actionPerformed(ActionEvent e) {
576 detach();
577 dialogsPanel.reconstruct(Action.ELEMENT_SHRINKS, null);
578 }
579 }
580 );
581 add(sticky);
582
583 // show the close button
584 JButton close = new JButton(ImageProvider.get("misc", "close"));
585 close.setToolTipText(tr("Close this panel. You can reopen it with the buttons in the left toolbar."));
586 close.setBorder(BorderFactory.createEmptyBorder());
587 close.addActionListener(
588 new ActionListener() {
589 @Override
590 public void actionPerformed(ActionEvent e) {
591 hideDialog();
592 dialogsPanel.reconstruct(Action.ELEMENT_SHRINKS, null);
593 hideNotify();
594 }
595 }
596 );
597 add(close);
598 setToolTipText(tr("Click to minimize/maximize the panel content"));
599 setTitle(toggleDialogName);
600 }
601
602 public void setTitle(String title) {
603 lblTitle.setText(title);
604 lblTitleWeak.repaint();
605 }
606
607 public String getTitle() {
608 return lblTitle.getText();
609 }
610
611 /**
612 * This is the popup menu used for the title bar.
613 */
614 public class DialogPopupMenu extends JPopupMenu {
615
616 /**
617 * Constructs a new {@code DialogPopupMenu}.
618 */
619 DialogPopupMenu() {
620 alwaysShown.setSelected(buttonHiding == ButtonHidingType.ALWAYS_SHOWN);
621 dynamic.setSelected(buttonHiding == ButtonHidingType.DYNAMIC);
622 alwaysHidden.setSelected(buttonHiding == ButtonHidingType.ALWAYS_HIDDEN);
623 ButtonGroup buttonHidingGroup = new ButtonGroup();
624 JMenu buttonHidingMenu = new JMenu(tr("Side buttons"));
625 for (JRadioButtonMenuItem rb : new JRadioButtonMenuItem[]{alwaysShown, dynamic, alwaysHidden}) {
626 buttonHidingGroup.add(rb);
627 buttonHidingMenu.add(rb);
628 }
629 add(buttonHidingMenu);
630 for (javax.swing.Action action: buttonActions) {
631 add(action);
632 }
633 }
634 }
635
636 /**
637 * Registers the mouse listeners.
638 * <p>
639 * Should be called once after this title was added to the dialog.
640 */
641 public final void registerMouseListener() {
642 popupMenu = new DialogPopupMenu();
643 addMouseListener(new MouseEventHandler());
644 }
645
646 class MouseEventHandler extends PopupMenuLauncher {
647 /**
648 * Constructs a new {@code MouseEventHandler}.
649 */
650 MouseEventHandler() {
651 super(popupMenu);
652 }
653
654 @Override
655 public void mouseClicked(MouseEvent e) {
656 if (SwingUtilities.isLeftMouseButton(e)) {
657 if (isCollapsed) {
658 expand();
659 dialogsPanel.reconstruct(Action.COLLAPSED_TO_DEFAULT, ToggleDialog.this);
660 } else {
661 collapse();
662 dialogsPanel.reconstruct(Action.ELEMENT_SHRINKS, null);
663 }
664 }
665 }
666 }
667 }
668
669 /**
670 * The dialog class used to display toggle dialogs in a detached window.
671 *
672 */
673 private class DetachedDialog extends JDialog {
674 DetachedDialog() {
675 super(GuiHelper.getFrameForComponent(Main.parent));
676 getContentPane().add(ToggleDialog.this);
677 addWindowListener(new WindowAdapter() {
678 @Override public void windowClosing(WindowEvent e) {
679 rememberGeometry();
680 getContentPane().removeAll();
681 dispose();
682 if (dockWhenClosingDetachedDlg()) {
683 dock();
684 if (isDialogInCollapsedView()) {
685 expand();
686 }
687 dialogsPanel.reconstruct(Action.INVISIBLE_TO_DEFAULT, ToggleDialog.this);
688 } else {
689 hideDialog();
690 hideNotify();
691 }
692 }
693 });
694 addComponentListener(new ComponentAdapter() {
695 @Override
696 public void componentMoved(ComponentEvent e) {
697 rememberGeometry();
698 }
699
700 @Override
701 public void componentResized(ComponentEvent e) {
702 rememberGeometry();
703 }
704 });
705
706 try {
707 new WindowGeometry(preferencePrefix+".geometry").applySafe(this);
708 } catch (WindowGeometryException e) {
709 ToggleDialog.this.setPreferredSize(ToggleDialog.this.getDefaultDetachedSize());
710 pack();
711 setLocationRelativeTo(Main.parent);
712 }
713 super.setTitle(titleBar.getTitle());
714 HelpUtil.setHelpContext(getRootPane(), helpTopic());
715 }
716
717 protected void rememberGeometry() {
718 if (detachedDialog != null) {
719 new WindowGeometry(detachedDialog).remember(preferencePrefix+".geometry");
720 }
721 }
722 }
723
724 /**
725 * Replies the action to toggle the visible state of this toggle dialog
726 *
727 * @return the action to toggle the visible state of this toggle dialog
728 */
729 public AbstractAction getToggleAction() {
730 return toggleAction;
731 }
732
733 /**
734 * Replies the prefix for the preference settings of this dialog.
735 *
736 * @return the prefix for the preference settings of this dialog.
737 */
738 public String getPreferencePrefix() {
739 return preferencePrefix;
740 }
741
742 /**
743 * Sets the dialogsPanel managing all toggle dialogs.
744 * @param dialogsPanel The panel managing all toggle dialogs
745 */
746 public void setDialogsPanel(DialogsPanel dialogsPanel) {
747 this.dialogsPanel = dialogsPanel;
748 }
749
750 /**
751 * Replies the name of this toggle dialog
752 */
753 @Override
754 public String getName() {
755 return "toggleDialog." + preferencePrefix;
756 }
757
758 /**
759 * Sets the title.
760 * @param title The dialog's title
761 */
762 public void setTitle(String title) {
763 titleBar.setTitle(title);
764 if (detachedDialog != null) {
765 detachedDialog.setTitle(title);
766 }
767 }
768
769 protected void setIsShowing(boolean val) {
770 isShowing = val;
771 Main.pref.put(preferencePrefix+".visible", val);
772 stateChanged();
773 }
774
775 protected void setIsDocked(boolean val) {
776 if (buttonsPanel != null) {
777 buttonsPanel.setVisible(!val || buttonHiding != ButtonHidingType.ALWAYS_HIDDEN);
778 }
779 isDocked = val;
780 Main.pref.put(preferencePrefix+".docked", val);
781 stateChanged();
782 }
783
784 protected void setIsCollapsed(boolean val) {
785 isCollapsed = val;
786 Main.pref.put(preferencePrefix+".minimized", val);
787 stateChanged();
788 }
789
790 protected void setIsButtonHiding(ButtonHidingType val) {
791 buttonHiding = val;
792 propButtonHiding.put(val);
793 refreshHidingButtons();
794 }
795
796 /**
797 * Returns the preferred height of this dialog.
798 * @return The preferred height if the toggle dialog is expanded
799 */
800 public int getPreferredHeight() {
801 return preferredHeight;
802 }
803
804 @Override
805 public String helpTopic() {
806 String help = getClass().getName();
807 help = help.substring(help.lastIndexOf('.')+1, help.length()-6);
808 return "Dialog/"+help;
809 }
810
811 @Override
812 public String toString() {
813 return name;
814 }
815
816 /**
817 * Determines if this dialog is showing either as docked or as detached dialog.
818 * @return {@code true} if this dialog is showing either as docked or as detached dialog
819 */
820 public boolean isDialogShowing() {
821 return isShowing;
822 }
823
824 /**
825 * Determines if this dialog is docked and expanded.
826 * @return {@code true} if this dialog is docked and expanded
827 */
828 public boolean isDialogInDefaultView() {
829 return isShowing && isDocked && (!isCollapsed);
830 }
831
832 /**
833 * Determines if this dialog is docked and collapsed.
834 * @return {@code true} if this dialog is docked and collapsed
835 */
836 public boolean isDialogInCollapsedView() {
837 return isShowing && isDocked && isCollapsed;
838 }
839
840 /**
841 * Sets the button from the button list that is used to display this dialog.
842 * <p>
843 * Note: This is ignored by the {@link ToggleDialog} for now.
844 * @param button The button for this dialog.
845 */
846 public void setButton(JToggleButton button) {
847 this.button = button;
848 }
849
850 /**
851 * Gets the button from the button list that is used to display this dialog.
852 * @return button The button for this dialog.
853 */
854 public JToggleButton getButton() {
855 return button;
856 }
857
858 /*
859 * The following methods are intended to be overridden, in order to customize
860 * the toggle dialog behavior.
861 */
862
863 /**
864 * Returns the default size of the detached dialog.
865 * Override this method to customize the initial dialog size.
866 * @return the default size of the detached dialog
867 */
868 protected Dimension getDefaultDetachedSize() {
869 return new Dimension(dialogsPanel.getWidth(), preferredHeight);
870 }
871
872 /**
873 * Do something when the toggleButton is pressed.
874 */
875 protected void toggleButtonHook() {
876 // Do nothing
877 }
878
879 protected boolean dockWhenClosingDetachedDlg() {
880 return true;
881 }
882
883 /**
884 * primitive stateChangedListener for subclasses
885 */
886 protected void stateChanged() {
887 // Do nothing
888 }
889
890 /**
891 * Create a component with the given layout for this component.
892 * @param data The content to be displayed
893 * @param scroll <code>true</code> if it should be wrapped in a {@link JScrollPane}
894 * @param buttons The buttons to add.
895 * @return The component.
896 */
897 protected Component createLayout(Component data, boolean scroll, Collection<SideButton> buttons) {
898 return createLayout(data, scroll, buttons, (Collection<SideButton>[]) null);
899 }
900
901 @SafeVarargs
902 protected final Component createLayout(Component data, boolean scroll, Collection<SideButton> firstButtons,
903 Collection<SideButton>... nextButtons) {
904 if (scroll) {
905 JScrollPane sp = new JScrollPane(data);
906 if (!(data instanceof Scrollable)) {
907 GuiHelper.setDefaultIncrement(sp);
908 }
909 data = sp;
910 }
911 LinkedList<Collection<SideButton>> buttons = new LinkedList<>();
912 buttons.addFirst(firstButtons);
913 if (nextButtons != null) {
914 buttons.addAll(Arrays.asList(nextButtons));
915 }
916 add(data, BorderLayout.CENTER);
917 if (!buttons.isEmpty() && buttons.get(0) != null && !buttons.get(0).isEmpty()) {
918 buttonsPanel = new JPanel(new GridLayout(buttons.size(), 1));
919 for (Collection<SideButton> buttonRow : buttons) {
920 if (buttonRow == null) {
921 continue;
922 }
923 final JPanel buttonRowPanel = new JPanel(Main.pref.getBoolean("dialog.align.left", false)
924 ? new FlowLayout(FlowLayout.LEFT) : new GridLayout(1, buttonRow.size()));
925 buttonsPanel.add(buttonRowPanel);
926 for (SideButton button : buttonRow) {
927 buttonRowPanel.add(button);
928 javax.swing.Action action = button.getAction();
929 if (action != null) {
930 buttonActions.add(action);
931 } else {
932 Main.warn("Button " + button + " doesn't have action defined");
933 Main.error(new Exception());
934 }
935 }
936 }
937 add(buttonsPanel, BorderLayout.SOUTH);
938 dynamicButtonsPropertyChanged();
939 } else {
940 titleBar.buttonsHide.setVisible(false);
941 }
942
943 // Register title bar mouse listener only after buttonActions has been initialized to have a complete popup menu
944 titleBar.registerMouseListener();
945
946 return data;
947 }
948
949 @Override
950 public void eventDispatched(AWTEvent event) {
951 if (isShowing() && !isCollapsed && isDocked && buttonHiding == ButtonHidingType.DYNAMIC) {
952 if (buttonsPanel != null) {
953 Rectangle b = this.getBounds();
954 b.setLocation(getLocationOnScreen());
955 if (b.contains(((MouseEvent) event).getLocationOnScreen())) {
956 if (!buttonsPanel.isVisible()) {
957 buttonsPanel.setVisible(true);
958 }
959 } else if (buttonsPanel.isVisible()) {
960 buttonsPanel.setVisible(false);
961 }
962 }
963 }
964 }
965
966 @Override
967 public void preferenceChanged(PreferenceChangeEvent e) {
968 if (e.getKey().equals(PROP_DYNAMIC_BUTTONS.getKey())) {
969 dynamicButtonsPropertyChanged();
970 }
971 }
972
973 private void dynamicButtonsPropertyChanged() {
974 boolean propEnabled = PROP_DYNAMIC_BUTTONS.get();
975 if (propEnabled) {
976 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.MOUSE_MOTION_EVENT_MASK);
977 } else {
978 Toolkit.getDefaultToolkit().removeAWTEventListener(this);
979 }
980 titleBar.buttonsHide.setVisible(propEnabled);
981 refreshHidingButtons();
982 }
983
984 private void refreshHidingButtons() {
985 titleBar.buttonsHide.setIcon(ImageProvider.get("misc", buttonHiding != ButtonHidingType.ALWAYS_SHOWN
986 ? /* ICON(misc/)*/ "buttonhide" : /* ICON(misc/)*/ "buttonshow"));
987 titleBar.buttonsHide.setEnabled(buttonHiding != ButtonHidingType.ALWAYS_HIDDEN);
988 if (buttonsPanel != null) {
989 buttonsPanel.setVisible(buttonHiding != ButtonHidingType.ALWAYS_HIDDEN || !isDocked);
990 }
991 stateChanged();
992 }
993}
Note: See TracBrowser for help on using the repository browser.