Ticket #3473: toggle.patch
| File toggle.patch, 10.9 KB (added by , 17 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java
7 7 import java.awt.Dimension; 8 8 import java.awt.GridBagLayout; 9 9 import java.awt.Image; 10 import java.awt.Rectangle; 10 11 import java.awt.event.ActionEvent; 11 12 import java.awt.event.ActionListener; 12 13 import java.awt.event.ComponentAdapter; … … 40 41 * 41 42 */ 42 43 public class ToggleDialog extends JPanel implements Helpful { 43 private static final Logger logger = Logger.getLogger(ToggleDialog.class.getName());44 // private static final Logger logger = Logger.getLogger(ToggleDialog.class.getName()); 44 45 45 46 /** 46 47 * The action to toggle the visibility state of this toggle dialog. … … 80 81 private TitleBar titleBar; 81 82 private String title; 82 83 83 /** indicates whether the dialog is currently minimized or not */ 84 private boolean collapsed; 85 /** indicates whether the dialog is docked or not */ 86 private boolean docked; 87 /** indicates whether the dialog is showing or not */ 84 /** 85 * Indicates whether the dialog is showing or not. 86 */ 88 87 private boolean isShowing; 88 /** 89 * If isShowing is true, indicates whether the dialog is docked or not, e. g. 90 * shown as part of the main window or as a seperate dialog window. 91 */ 92 private boolean isDocked; 93 /** 94 * If isShowing and isDocked are true, indicates whether the dialog is 95 * currently minimized or not. 96 */ 97 private boolean isCollapsed; 89 98 99 /** the preferred width of all docked toggle dialogs */ 100 final private int TOGGLE_DIALOG_WIDTH = 330; 90 101 /** the preferred height if the toggle dialog is expanded */ 91 102 private int preferredHeight; 92 103 /** the label in the title bar which shows whether the toggle dialog is expanded or collapsed */ … … 94 105 /** the JDialog displaying the toggle dialog as undocked dialog */ 95 106 private JDialog detachedDialog; 96 107 97 98 108 /** 99 109 * Constructor 100 110 * … … 111 121 } 112 122 113 123 /** 114 * Sets the visibility of all components in this toggle dialog, except the title bar115 *116 * @param visible true, if the components should be visible; false otherwise117 */118 protected void setContentVisible(boolean visible) {119 Component comps[] = getComponents();120 for(int i=0; i<comps.length; i++) {121 if(comps[i] != titleBar) {122 comps[i].setVisible(visible);123 }124 }125 }126 127 /**128 124 * Initializes the toggle dialog 129 125 * 130 126 * @param name … … 134 130 * @param preferredHeight 135 131 */ 136 132 private void init(String name, String iconName, String tooltip, Shortcut shortcut, final int preferredHeight) { 137 setPreferredSize(new Dimension(330,preferredHeight)); 133 setPreferredSize(new Dimension(TOGGLE_DIALOG_WIDTH, preferredHeight)); 134 this.preferredHeight = preferredHeight; 138 135 toggleAction = new ToggleDialogAction(name, "dialogs/"+iconName, tooltip, shortcut, iconName); 139 136 String helpId = "Dialog/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1); 140 137 toggleAction.putValue("help", helpId.substring(0, helpId.length()-6)); … … 149 146 setVisible(false); 150 147 setBorder(BorderFactory.createEtchedBorder()); 151 148 152 docked = Main.pref.getBoolean(preferencePrefix+".docked", true);153 collapsed = Main.pref.getBoolean(preferencePrefix+".minimized", false);149 isDocked = Main.pref.getBoolean(preferencePrefix+".docked", true); 150 isCollapsed = Main.pref.getBoolean(preferencePrefix+".minimized", false); 154 151 } 155 152 156 153 /** 154 * Sets the visibility of all components in this toggle dialog, except the title bar 155 * 156 * @param visible true, if the components should be visible; false otherwise 157 */ 158 protected void setContentVisible(boolean visible) { 159 Component comps[] = getComponents(); 160 for(int i=0; i<comps.length; i++) { 161 if(comps[i] != titleBar) { 162 comps[i].setVisible(visible); 163 } 164 } 165 } 166 167 /** 168 * Toggles between collapsed and expanded state 169 * 170 */ 171 protected void toggleExpandedState() { 172 if (isCollapsed) { 173 expand(); 174 } else { 175 collapse(); 176 } 177 } 178 179 /** 157 180 * Collapses the toggle dialog to the title bar only 158 181 * 159 182 */ 160 183 protected void collapse() { 161 184 setContentVisible(false); 162 this.collapsed = true;185 isCollapsed = true; 163 186 Main.pref.put(preferencePrefix+".minimized", true); 164 setPreferredSize(new Dimension( 330,20));165 setMaximumSize(new Dimension( 330,20));187 setPreferredSize(new Dimension(TOGGLE_DIALOG_WIDTH,20)); 188 setMaximumSize(new Dimension(TOGGLE_DIALOG_WIDTH,20)); 166 189 lblMinimized.setIcon(ImageProvider.get("misc", "minimized")); 167 190 refreshToggleDialogsView(); 168 191 } … … 172 195 */ 173 196 protected void expand() { 174 197 setContentVisible(true); 175 this.collapsed = false;198 isCollapsed = false; 176 199 Main.pref.put(preferencePrefix+".minimized", false); 177 setPreferredSize(new Dimension( 330,preferredHeight));200 setPreferredSize(new Dimension(TOGGLE_DIALOG_WIDTH,preferredHeight)); 178 201 setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); 179 202 lblMinimized.setIcon(ImageProvider.get("misc", "normal")); 180 203 refreshToggleDialogsView(); … … 227 250 setVisible(false); 228 251 } 229 252 titleBar.setVisible(true); 230 collapsed = Main.pref.getBoolean(preferencePrefix+".minimized", false);231 if ( collapsed) {253 isCollapsed = Main.pref.getBoolean(preferencePrefix+".minimized", false); 254 if (isCollapsed) { 232 255 collapse(); 233 256 } else { 234 257 expand(); 235 258 } 236 docked = true;237 Main.pref.put(preferencePrefix+".docked", docked);259 isDocked = true; 260 Main.pref.put(preferencePrefix+".docked", isDocked); 238 261 } 239 262 240 263 /** … … 244 267 protected void detach() { 245 268 setContentVisible(true); 246 269 setVisible(true); 247 248 270 // replace the toggle dialog by an invisible place holder. Makes sure 249 271 // we can place the toggle dialog where it was when it becomes docked 250 272 // again. … … 259 281 } 260 282 parent.remove(ToggleDialog.this); 261 283 } 284 262 285 263 286 titleBar.setVisible(false); 264 287 detachedDialog = new DetachedDialog(); 265 288 detachedDialog.setVisible(true); 266 289 refreshToggleDialogsView(); 267 docked = false;268 Main.pref.put(preferencePrefix+".docked", docked);290 isDocked = false; 291 Main.pref.put(preferencePrefix+".docked", isDocked); 269 292 } 270 293 271 294 /** 272 295 * Hides the dialog 273 296 */ 274 297 public void hideDialog() { 275 if (detachedDialog != null) { 276 detachedDialog.setVisible(false); 277 detachedDialog.getContentPane().removeAll(); 278 detachedDialog.dispose(); 279 } 298 closeDetachedDialog(); 280 299 setVisible(false); 281 300 isShowing = false; 282 301 Main.pref.put(preferencePrefix+".visible", false); … … 295 314 * Shows the dialog 296 315 */ 297 316 public void showDialog() { 298 if (! docked) {317 if (!isDocked) { 299 318 detach(); 300 319 } else { 301 320 dock(); 302 if (! collapsed) {321 if (!isCollapsed) { 303 322 expand(); 304 323 setVisible(true); 305 324 refreshToggleDialogsView(); … … 310 329 } 311 330 isShowing = true; 312 331 // toggling the selected value in order to enforce PropertyChangeEvents 313 //314 332 toggleAction.putValue("selected", false); 315 333 toggleAction.putValue("selected", true); 316 334 Main.pref.put(preferencePrefix+".visible", true); 317 335 } 318 336 319 337 /** 320 * Toggles between collapsed and expanded state321 *322 */323 protected void toggleExpandedState() {324 if (this.collapsed) {325 expand();326 } else {327 collapse();328 }329 }330 331 /**332 338 * Refreshes the layout of the parent toggle dialog view 333 339 * 334 340 */ … … 346 352 public void closeDetachedDialog() { 347 353 if (detachedDialog != null) { 348 354 detachedDialog.setVisible(false); 349 detachedDialog. removeAll();355 detachedDialog.getContentPane().removeAll(); 350 356 detachedDialog.dispose(); 351 357 } 352 358 } … … 386 392 387 393 /** 388 394 * Replies the name of this toggle dialog 395 * 389 396 */ 390 397 @Override 391 398 public String getName() { … … 473 480 */ 474 481 private class DetachedDialog extends JDialog { 475 482 public DetachedDialog() { 476 super(JOptionPane.getFrameForComponent(Main.parent) ,false /* not modal*/);483 super(JOptionPane.getFrameForComponent(Main.parent)); 477 484 getContentPane().add(ToggleDialog.this); 478 485 addWindowListener(new WindowAdapter(){ 479 486 @Override public void windowClosing(WindowEvent e) { 487 rememberGeometry(); 480 488 getContentPane().removeAll(); 481 489 dispose(); 482 490 dock(); 483 491 } 484 492 }); 485 addComponentListener(new ComponentAdapter(){486 @Override public void componentMoved(ComponentEvent e) {487 rememberGeometry();488 }489 });490 493 String bounds = Main.pref.get(preferencePrefix+".bounds",null); 491 494 if (bounds != null) { 492 495 String[] b = bounds.split(","); 493 setBounds(Integer.parseInt(b[0]),Integer.parseInt(b[1]),Integer.parseInt(b[2]),Integer.parseInt(b[3])); 496 setBounds(getDetachedGeometry(new Rectangle( 497 Integer.parseInt(b[0]),Integer.parseInt(b[1]),Integer.parseInt(b[2]),Integer.parseInt(b[3])))); 494 498 } else { 499 ToggleDialog.this.setPreferredSize(ToggleDialog.this.getDefaultDetachedSize()); 495 500 pack(); 496 501 setLocationRelativeTo(Main.parent); 497 502 } 498 503 setTitle(titleBar.getTitle()); 499 504 } … … 502 507 Main.pref.put(preferencePrefix+".bounds", detachedDialog.getX()+","+detachedDialog.getY()+","+detachedDialog.getWidth()+","+detachedDialog.getHeight()); 503 508 } 504 509 } 510 511 /** 512 * Change the Geometry of the detached dialog to better fit the content. 513 * Overrride this to make it useful. 514 */ 515 protected Rectangle getDetachedGeometry(Rectangle last) { 516 return last; 517 } 518 519 /** 520 * Default size of the detached dialog. 521 * Override this method to customize the initial dialog size. 522 */ 523 protected Dimension getDefaultDetachedSize() { 524 return new Dimension(TOGGLE_DIALOG_WIDTH, preferredHeight); 525 } 505 526 }
