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