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

Last change on this file since 1180 was 1180, checked in by stoecker, 17 years ago

fixed bug #1871, removed all deprecations

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.dialogs;
3
4import java.awt.BorderLayout;
5import java.awt.Dimension;
6import java.awt.EventQueue;
7import java.awt.GridBagLayout;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.awt.event.ComponentAdapter;
11import java.awt.event.ComponentEvent;
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;
28import 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 */
36public 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 public ToggleDialog(final String name, String iconName, String tooltip, Shortcut shortcut, int preferredHeight) {
65 super(new BorderLayout());
66 this.prefName = iconName;
67 ToggleDialogInit(name, iconName, tooltip, shortcut, preferredHeight);
68 }
69
70 private void ToggleDialogInit(final String name, String iconName, String tooltip, Shortcut shortcut, int preferredHeight) {
71 setPreferredSize(new Dimension(330,preferredHeight));
72 action = new ToggleDialogAction(name, "dialogs/"+iconName, tooltip, shortcut, iconName);
73 String helpId = "Dialog/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
74 action.putValue("help", helpId.substring(0, helpId.length()-6));
75 setLayout(new BorderLayout());
76
77 titleBar.add(new JLabel(name), GBC.std());
78 titleBar.add(Box.createHorizontalGlue(),GBC.std().fill(GBC.HORIZONTAL));
79
80 JButton sticky = new JButton(ImageProvider.get("misc", "sticky"));
81 sticky.setBorder(BorderFactory.createEmptyBorder());
82 final ActionListener stickyActionListener = new ActionListener(){
83 public void actionPerformed(ActionEvent e) {
84 final JFrame f = new JFrame(name);
85 try {f.setAlwaysOnTop(true);} catch (SecurityException e1) {}
86 parent.remove(ToggleDialog.this);
87 f.getContentPane().add(ToggleDialog.this);
88 f.addWindowListener(new WindowAdapter(){
89 @Override public void windowClosing(WindowEvent e) {
90 titleBar.setVisible(true);
91 f.getContentPane().removeAll();
92 parent.add(ToggleDialog.this);
93 f.dispose();
94
95 // doLayout() - workaround
96 setVisible(false);
97 setVisible(true);
98
99 Main.pref.put(action.prefname+".docked", true);
100 }
101 });
102 f.addComponentListener(new ComponentAdapter(){
103 @Override public void componentMoved(ComponentEvent e) {
104 Main.pref.put(action.prefname+".bounds", f.getX()+","+f.getY()+","+f.getWidth()+","+f.getHeight());
105 }
106 });
107 String bounds = Main.pref.get(action.prefname+".bounds",null);
108 if (bounds != null) {
109 String[] b = bounds.split(",");
110 f.setBounds(Integer.parseInt(b[0]),Integer.parseInt(b[1]),Integer.parseInt(b[2]),Integer.parseInt(b[3]));
111 } else
112 f.pack();
113 Main.pref.put(action.prefname+".docked", false);
114 f.setVisible(true);
115 titleBar.setVisible(false);
116
117 // doLayout() - workaround
118 parent.setVisible(false);
119 parent.setVisible(true);
120 }
121 };
122 sticky.addActionListener(stickyActionListener);
123
124 titleBar.add(sticky);
125 add(titleBar, BorderLayout.NORTH);
126
127 setVisible(false);
128 setBorder(BorderFactory.createEtchedBorder());
129
130 if (!Main.pref.getBoolean(action.prefname+".docked", true)) {
131 EventQueue.invokeLater(new Runnable(){
132 public void run() {
133 stickyActionListener.actionPerformed(null);
134 }
135 });
136 }
137 }
138
139 public String helpTopic() {
140 String help = getClass().getName();
141 help = help.substring(help.lastIndexOf('.')+1, help.length()-6);
142 return "Dialog/"+help;
143 }
144}
Note: See TracBrowser for help on using the repository browser.