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

Last change on this file since 1237 was 1237, checked in by stoecker, 15 years ago

fixed #1602 patch by xeen

  • Property svn:eol-style set to native
File size: 9.6 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Dimension;
8import java.awt.EventQueue;
9import java.awt.GridBagLayout;
10import java.awt.Component;
11import java.awt.Image;
12import java.awt.event.ActionEvent;
13import java.awt.event.ActionListener;
14import java.awt.event.ComponentAdapter;
15import java.awt.event.ComponentEvent;
16import java.awt.event.MouseEvent;
17import java.awt.event.MouseListener;
18import java.awt.event.WindowAdapter;
19import java.awt.event.WindowEvent;
20
21import javax.swing.AbstractButton;
22import javax.swing.BorderFactory;
23import javax.swing.Box;
24import javax.swing.JButton;
25import javax.swing.JFrame;
26import javax.swing.JLabel;
27import javax.swing.JPanel;
28import javax.swing.ImageIcon;
29
30import org.openstreetmap.josm.Main;
31import org.openstreetmap.josm.actions.JosmAction;
32import org.openstreetmap.josm.actions.HelpAction.Helpful;
33import org.openstreetmap.josm.tools.GBC;
34import org.openstreetmap.josm.tools.ImageProvider;
35import org.openstreetmap.josm.tools.Shortcut;
36
37/**
38 * This class is a toggle dialog that can be turned on and off. It is attached
39 * to a ButtonModel.
40 *
41 * @author imi
42 */
43public class ToggleDialog extends JPanel implements Helpful {
44
45 public final class ToggleDialogAction extends JosmAction {
46 public final String prefname;
47 public AbstractButton button;
48
49 private ToggleDialogAction(String name, String iconName, String tooltip, Shortcut shortcut, String prefname) {
50 super(name, iconName, tooltip, shortcut, false);
51 this.prefname = prefname;
52 }
53
54 public void actionPerformed(ActionEvent e) {
55 if (e != null && !(e.getSource() instanceof AbstractButton))
56 button.setSelected(!button.isSelected());
57 setVisible(button.isSelected());
58 Main.pref.put(prefname+".visible", button.isSelected());
59 }
60 }
61
62 /**
63 * The action to toggle this dialog.
64 */
65 public ToggleDialogAction action;
66 public final String prefName;
67
68 public JPanel parent;
69 private final JPanel titleBar = new JPanel(new GridBagLayout());
70 public JLabel label = new JLabel();
71
72 public ToggleDialog(final String name, String iconName, String tooltip, Shortcut shortcut, int preferredHeight) {
73 super(new BorderLayout());
74 this.prefName = iconName;
75 ToggleDialogInit(name, iconName, tooltip, shortcut, preferredHeight);
76 }
77
78 private void ToggleDialogInit(final String name, String iconName, String tooltip, Shortcut shortcut, final int preferredHeight) {
79 setPreferredSize(new Dimension(330,preferredHeight));
80 action = new ToggleDialogAction(name, "dialogs/"+iconName, tooltip, shortcut, iconName);
81 String helpId = "Dialog/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
82 action.putValue("help", helpId.substring(0, helpId.length()-6));
83 setLayout(new BorderLayout());
84
85 // show the minimize button
86 final JLabel minimize = new JLabel(ImageProvider.get("misc", "normal"));
87 titleBar.add(minimize);
88
89 // scale down the dialog icon
90 ImageIcon inIcon = ImageProvider.get("dialogs", iconName);
91 ImageIcon smallIcon = new ImageIcon(inIcon.getImage().getScaledInstance(16 , 16, Image.SCALE_SMOOTH));
92 JLabel labelSmallIcon = new JLabel(smallIcon);
93 titleBar.add(labelSmallIcon);
94
95 final ActionListener hideActionListener = new ActionListener(){
96 public void actionPerformed(ActionEvent e) {
97 boolean nowVisible = false;
98 Component comps[] = getComponents();
99 for(int i=0; i<comps.length; i++)
100 {
101 if(comps[i] != titleBar)
102 {
103 if(comps[i].isVisible()) {
104 comps[i].setVisible(false);
105 } else {
106 comps[i].setVisible(true);
107 nowVisible = true;
108 }
109 }
110 }
111
112 Main.pref.put(action.prefname+".minimized", !nowVisible);
113 if(nowVisible == true) {
114 setPreferredSize(new Dimension(330,preferredHeight));
115 setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
116 minimize.setIcon(ImageProvider.get("misc", "normal"));
117 } else {
118 setPreferredSize(new Dimension(330,20));
119 setMaximumSize(new Dimension(330,20));
120 minimize.setIcon(ImageProvider.get("misc", "minimized"));
121 }
122 // doLayout() - workaround
123 parent.setVisible(false);
124 parent.setVisible(true);
125 }
126 };
127 //hide.addActionListener(hideActionListener);
128
129 final MouseListener titleMouseListener = new MouseListener(){
130 public void mouseClicked(MouseEvent e) {
131 hideActionListener.actionPerformed(null);
132 }
133 public void mouseEntered(MouseEvent e) {}
134 public void mouseExited(MouseEvent e) {}
135 public void mousePressed(MouseEvent e) {}
136 public void mouseReleased(MouseEvent e) {}
137 };
138 titleBar.addMouseListener(titleMouseListener);
139
140 // add some padding space (there must be a better way to do this!)
141 JLabel padding = new JLabel(" ");
142 titleBar.add(padding);
143
144 // show the title label
145 label.setText(name);
146 titleBar.add(label, GBC.std());
147 titleBar.add(Box.createHorizontalGlue(),GBC.std().fill(GBC.HORIZONTAL));
148
149 // show the sticky button
150 JButton sticky = new JButton(ImageProvider.get("misc", "sticky"));
151 sticky.setToolTipText(tr("Undock the panel"));
152 sticky.setBorder(BorderFactory.createEmptyBorder());
153 final ActionListener stickyActionListener = new ActionListener(){
154 public void actionPerformed(ActionEvent e) {
155 final JFrame f = new JFrame(name);
156 try {f.setAlwaysOnTop(true);} catch (SecurityException e1) {}
157 parent.remove(ToggleDialog.this);
158 f.getContentPane().add(ToggleDialog.this);
159 f.addWindowListener(new WindowAdapter(){
160 @Override public void windowClosing(WindowEvent e) {
161 titleBar.setVisible(true);
162 f.getContentPane().removeAll();
163 parent.add(ToggleDialog.this);
164 f.dispose();
165
166 // doLayout() - workaround
167 setVisible(false);
168 setVisible(true);
169
170 Main.pref.put(action.prefname+".docked", true);
171 }
172 });
173 f.addComponentListener(new ComponentAdapter(){
174 @Override public void componentMoved(ComponentEvent e) {
175 Main.pref.put(action.prefname+".bounds", f.getX()+","+f.getY()+","+f.getWidth()+","+f.getHeight());
176 }
177 });
178 String bounds = Main.pref.get(action.prefname+".bounds",null);
179 if (bounds != null) {
180 String[] b = bounds.split(",");
181 f.setBounds(Integer.parseInt(b[0]),Integer.parseInt(b[1]),Integer.parseInt(b[2]),Integer.parseInt(b[3]));
182 } else
183 f.pack();
184 Main.pref.put(action.prefname+".docked", false);
185 f.setVisible(true);
186 titleBar.setVisible(false);
187
188 // doLayout() - workaround
189 parent.setVisible(false);
190 parent.setVisible(true);
191 }
192 };
193 sticky.addActionListener(stickyActionListener);
194 titleBar.add(sticky);
195
196 // show the close button
197 JButton close = new JButton(ImageProvider.get("misc", "close"));
198 close.setToolTipText(tr("Close this panel. You can reopen it with the buttons in the left toolbar."));
199 close.setBorder(BorderFactory.createEmptyBorder());
200 final ActionListener closeActionListener = new ActionListener(){
201 public void actionPerformed(ActionEvent e) {
202 // fake an event to toggle dialog
203 action.actionPerformed(new ActionEvent(titleBar, 0, ""));
204 }
205 };
206 close.addActionListener(closeActionListener);
207 titleBar.add(close);
208
209 add(titleBar, BorderLayout.NORTH);
210 titleBar.setToolTipText(tr("Click to minimize/maximize the panel content"));
211
212 setVisible(false);
213 setBorder(BorderFactory.createEtchedBorder());
214
215 if (!Main.pref.getBoolean(action.prefname+".docked", true)) {
216 EventQueue.invokeLater(new Runnable(){
217 public void run() {
218 stickyActionListener.actionPerformed(null);
219 }
220 });
221 }
222 if (Main.pref.getBoolean(action.prefname+".minimized", false)) {
223 EventQueue.invokeLater(new Runnable(){
224 public void run() {
225 titleMouseListener.mouseClicked(null);
226 }
227 });
228 }
229 }
230
231 public void setTitle(String title, boolean active) {
232 if(active) {
233 label.setText("<html><b>" + title + "</b>");
234 } else {
235 label.setText(title);
236 }
237 }
238
239 public String helpTopic() {
240 String help = getClass().getName();
241 help = help.substring(help.lastIndexOf('.')+1, help.length()-6);
242 return "Dialog/"+help;
243 }
244}
Note: See TracBrowser for help on using the repository browser.