1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import java.awt.BorderLayout;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.EventQueue;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.ComponentAdapter;
|
---|
11 | import java.awt.event.ComponentEvent;
|
---|
12 | import java.awt.event.WindowAdapter;
|
---|
13 | import java.awt.event.WindowEvent;
|
---|
14 |
|
---|
15 | import javax.swing.AbstractButton;
|
---|
16 | import javax.swing.BorderFactory;
|
---|
17 | import javax.swing.Box;
|
---|
18 | import javax.swing.JButton;
|
---|
19 | import javax.swing.JFrame;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
25 | import org.openstreetmap.josm.actions.HelpAction.Helpful;
|
---|
26 | import org.openstreetmap.josm.tools.GBC;
|
---|
27 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
28 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * This class is a toggle dialog that can be turned on and off. It is attached
|
---|
32 | * to a ButtonModel.
|
---|
33 | *
|
---|
34 | * @author imi
|
---|
35 | */
|
---|
36 | public class ToggleDialog extends JPanel implements Helpful {
|
---|
37 |
|
---|
38 | public final class ToggleDialogAction extends JosmAction {
|
---|
39 | public final String prefname;
|
---|
40 | public AbstractButton button;
|
---|
41 |
|
---|
42 | private ToggleDialogAction(String name, String iconName, String tooltip, Shortcut shortcut, String prefname) {
|
---|
43 | super(name, iconName, tooltip, shortcut, false);
|
---|
44 | this.prefname = prefname;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public void actionPerformed(ActionEvent e) {
|
---|
48 | if (e != null && !(e.getSource() instanceof AbstractButton))
|
---|
49 | button.setSelected(!button.isSelected());
|
---|
50 | setVisible(button.isSelected());
|
---|
51 | Main.pref.put(prefname+".visible", button.isSelected());
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * The action to toggle this dialog.
|
---|
57 | */
|
---|
58 | public ToggleDialogAction action;
|
---|
59 | public final String prefName;
|
---|
60 |
|
---|
61 | public JPanel parent;
|
---|
62 | private final JPanel titleBar = new JPanel(new GridBagLayout());
|
---|
63 |
|
---|
64 | @Deprecated
|
---|
65 | public ToggleDialog(final String name, String iconName, String tooltip, int shortcut, int preferredHeight) {
|
---|
66 | super(new BorderLayout());
|
---|
67 | this.prefName = iconName;
|
---|
68 | ToggleDialogInit(name, iconName, tooltip, Shortcut.registerShortcut("auto:"+name, tooltip, shortcut, Shortcut.GROUP_LAYER), preferredHeight);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public ToggleDialog(final String name, String iconName, String tooltip, Shortcut shortcut, int preferredHeight) {
|
---|
72 | super(new BorderLayout());
|
---|
73 | this.prefName = iconName;
|
---|
74 | ToggleDialogInit(name, iconName, tooltip, shortcut, preferredHeight);
|
---|
75 | }
|
---|
76 |
|
---|
77 | private void ToggleDialogInit(final String name, String iconName, String tooltip, Shortcut shortcut, int preferredHeight) {
|
---|
78 | setPreferredSize(new Dimension(330,preferredHeight));
|
---|
79 | action = new ToggleDialogAction(name, "dialogs/"+iconName, tooltip, shortcut, iconName);
|
---|
80 | String helpId = "Dialog/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
|
---|
81 | action.putValue("help", helpId.substring(0, helpId.length()-6));
|
---|
82 | setLayout(new BorderLayout());
|
---|
83 |
|
---|
84 | titleBar.add(new JLabel(name), GBC.std());
|
---|
85 | titleBar.add(Box.createHorizontalGlue(),GBC.std().fill(GBC.HORIZONTAL));
|
---|
86 |
|
---|
87 | JButton sticky = new JButton(ImageProvider.get("misc", "sticky"));
|
---|
88 | sticky.setBorder(BorderFactory.createEmptyBorder());
|
---|
89 | final ActionListener stickyActionListener = new ActionListener(){
|
---|
90 | public void actionPerformed(ActionEvent e) {
|
---|
91 | final JFrame f = new JFrame(name);
|
---|
92 | try {f.setAlwaysOnTop(true);} catch (SecurityException e1) {}
|
---|
93 | parent.remove(ToggleDialog.this);
|
---|
94 | f.getContentPane().add(ToggleDialog.this);
|
---|
95 | f.addWindowListener(new WindowAdapter(){
|
---|
96 | @Override public void windowClosing(WindowEvent e) {
|
---|
97 | titleBar.setVisible(true);
|
---|
98 | f.getContentPane().removeAll();
|
---|
99 | parent.add(ToggleDialog.this);
|
---|
100 | f.dispose();
|
---|
101 |
|
---|
102 | // doLayout() - workaround
|
---|
103 | setVisible(false);
|
---|
104 | setVisible(true);
|
---|
105 |
|
---|
106 | Main.pref.put(action.prefname+".docked", true);
|
---|
107 | }
|
---|
108 | });
|
---|
109 | f.addComponentListener(new ComponentAdapter(){
|
---|
110 | @Override public void componentMoved(ComponentEvent e) {
|
---|
111 | Main.pref.put(action.prefname+".bounds", f.getX()+","+f.getY()+","+f.getWidth()+","+f.getHeight());
|
---|
112 | }
|
---|
113 | });
|
---|
114 | String bounds = Main.pref.get(action.prefname+".bounds",null);
|
---|
115 | if (bounds != null) {
|
---|
116 | String[] b = bounds.split(",");
|
---|
117 | f.setBounds(Integer.parseInt(b[0]),Integer.parseInt(b[1]),Integer.parseInt(b[2]),Integer.parseInt(b[3]));
|
---|
118 | } else
|
---|
119 | f.pack();
|
---|
120 | Main.pref.put(action.prefname+".docked", false);
|
---|
121 | f.setVisible(true);
|
---|
122 | titleBar.setVisible(false);
|
---|
123 |
|
---|
124 | // doLayout() - workaround
|
---|
125 | parent.setVisible(false);
|
---|
126 | parent.setVisible(true);
|
---|
127 | }
|
---|
128 | };
|
---|
129 | sticky.addActionListener(stickyActionListener);
|
---|
130 |
|
---|
131 | titleBar.add(sticky);
|
---|
132 | add(titleBar, BorderLayout.NORTH);
|
---|
133 |
|
---|
134 | setVisible(false);
|
---|
135 | setBorder(BorderFactory.createEtchedBorder());
|
---|
136 |
|
---|
137 | if (!Main.pref.getBoolean(action.prefname+".docked", true)) {
|
---|
138 | EventQueue.invokeLater(new Runnable(){
|
---|
139 | public void run() {
|
---|
140 | stickyActionListener.actionPerformed(null);
|
---|
141 | }
|
---|
142 | });
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | public String helpTopic() {
|
---|
147 | String help = getClass().getName();
|
---|
148 | help = help.substring(help.lastIndexOf('.')+1, help.length()-6);
|
---|
149 | return "Dialog/"+help;
|
---|
150 | }
|
---|
151 | }
|
---|