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