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

Last change on this file since 199 was 199, checked in by imi, 18 years ago
  • Appletversion support of bookmarks
  • Applet does not crash on toggling dialogs
  • Pluginname works even if plugin is loaded not from .jar file
File size: 4.5 KB
Line 
1package org.openstreetmap.josm.gui.dialogs;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.EventQueue;
6import java.awt.GridBagLayout;
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9import java.awt.event.ComponentAdapter;
10import java.awt.event.ComponentEvent;
11import java.awt.event.KeyEvent;
12import java.awt.event.WindowAdapter;
13import java.awt.event.WindowEvent;
14
15import javax.swing.AbstractButton;
16import javax.swing.BorderFactory;
17import javax.swing.Box;
18import javax.swing.JButton;
19import javax.swing.JFrame;
20import javax.swing.JLabel;
21import javax.swing.JPanel;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.actions.JosmAction;
25import org.openstreetmap.josm.actions.HelpAction.Helpful;
26import org.openstreetmap.josm.tools.GBC;
27import 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 */
35public 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 try {f.setAlwaysOnTop(true);} catch (SecurityException e1) {}
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}
Note: See TracBrowser for help on using the repository browser.