1 | package org.openstreetmap.josm.gui.dialogs;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.EventQueue;
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.KeyEvent;
|
---|
7 |
|
---|
8 | import javax.swing.AbstractButton;
|
---|
9 | import javax.swing.BorderFactory;
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JPanel;
|
---|
12 | import javax.swing.KeyStroke;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * This class is a toggle dialog that can be turned on and off. It is attached
|
---|
19 | * to a ButtonModel.
|
---|
20 | *
|
---|
21 | * @author imi
|
---|
22 | */
|
---|
23 | public class ToggleDialog extends JPanel {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * The action to toggle this dialog.
|
---|
27 | */
|
---|
28 | public JosmAction action;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Create a new ToggleDialog.
|
---|
32 | * @param title The title of the dialog.
|
---|
33 | * @param prefName Name of the base preference setting string (prefix)
|
---|
34 | * with the final . (e.g.: "layerlist.")
|
---|
35 | */
|
---|
36 | public ToggleDialog(String title, String name, String iconName, String tooltip, String shortCutName, int shortCut, final String prefName) {
|
---|
37 | action = new JosmAction(name, "dialogs/"+iconName, tooltip, "Alt-"+shortCutName, KeyStroke.getKeyStroke(shortCut, KeyEvent.ALT_MASK)){
|
---|
38 | public void actionPerformed(ActionEvent e) {
|
---|
39 | boolean show = !isVisible();
|
---|
40 | if (e != null && e.getSource() instanceof AbstractButton)
|
---|
41 | show = ((AbstractButton)e.getSource()).isSelected();
|
---|
42 | setVisible(show);
|
---|
43 | Main.pref.put(prefName+"visible", show);
|
---|
44 | }
|
---|
45 | };
|
---|
46 | setLayout(new BorderLayout());
|
---|
47 | add(new JLabel(title), BorderLayout.NORTH);
|
---|
48 | setVisible(false);
|
---|
49 | setBorder(BorderFactory.createEtchedBorder());
|
---|
50 | if (Main.pref.getBoolean(prefName+"visible")) {
|
---|
51 | EventQueue.invokeLater(new Runnable(){
|
---|
52 | public void run() {
|
---|
53 | action.putValue("active", true);
|
---|
54 | action.actionPerformed(null);
|
---|
55 | }
|
---|
56 | });
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|