Ticket #3473: toggle.patch

File toggle.patch, 10.9 KB (added by bastiK, 17 years ago)

fixes the described bugs + some cosmetics

  • src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java

     
    77import java.awt.Dimension;
    88import java.awt.GridBagLayout;
    99import java.awt.Image;
     10import java.awt.Rectangle;
    1011import java.awt.event.ActionEvent;
    1112import java.awt.event.ActionListener;
    1213import java.awt.event.ComponentAdapter;
     
    4041 *
    4142 */
    4243public 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());
    4445
    4546    /**
    4647     * The action to toggle the visibility state of this toggle dialog.
     
    8081    private  TitleBar titleBar;
    8182    private String title;
    8283
    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     */
    8887    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;
    8998
     99    /** the preferred width of all docked toggle dialogs */
     100    final private int TOGGLE_DIALOG_WIDTH = 330;
    90101    /** the preferred height if the toggle dialog is expanded */
    91102    private int preferredHeight;
    92103    /** the label in the title bar which shows whether the toggle dialog is expanded or collapsed */
     
    94105    /** the JDialog displaying the toggle dialog as undocked dialog */
    95106    private JDialog detachedDialog;
    96107
    97 
    98108    /**
    99109     * Constructor
    100110     *
     
    111121    }
    112122
    113123    /**
    114      * Sets the visibility of all components in this toggle dialog, except the title bar
    115      *
    116      * @param visible true, if the components should be visible; false otherwise
    117      */
    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     /**
    128124     * Initializes the toggle dialog
    129125     *
    130126     * @param name
     
    134130     * @param preferredHeight
    135131     */
    136132    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;
    138135        toggleAction = new ToggleDialogAction(name, "dialogs/"+iconName, tooltip, shortcut, iconName);
    139136        String helpId = "Dialog/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
    140137        toggleAction.putValue("help", helpId.substring(0, helpId.length()-6));
     
    149146        setVisible(false);
    150147        setBorder(BorderFactory.createEtchedBorder());
    151148
    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);
    154151    }
    155152
    156153    /**
     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    /**
    157180     * Collapses the toggle dialog to the title bar only
    158181     *
    159182     */
    160183    protected void collapse() {
    161184        setContentVisible(false);
    162         this.collapsed = true;
     185        isCollapsed = true;
    163186        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));
    166189        lblMinimized.setIcon(ImageProvider.get("misc", "minimized"));
    167190        refreshToggleDialogsView();
    168191    }
     
    172195     */
    173196    protected void expand() {
    174197        setContentVisible(true);
    175         this.collapsed = false;
     198        isCollapsed = false;
    176199        Main.pref.put(preferencePrefix+".minimized", false);
    177         setPreferredSize(new Dimension(330,preferredHeight));
     200        setPreferredSize(new Dimension(TOGGLE_DIALOG_WIDTH,preferredHeight));
    178201        setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
    179202        lblMinimized.setIcon(ImageProvider.get("misc", "normal"));
    180203        refreshToggleDialogsView();
     
    227250            setVisible(false);
    228251        }
    229252        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) {
    232255            collapse();
    233256        } else {
    234257            expand();
    235258        }
    236         docked = true;
    237         Main.pref.put(preferencePrefix+".docked", docked);
     259        isDocked = true;
     260        Main.pref.put(preferencePrefix+".docked", isDocked);
    238261    }
    239262
    240263    /**
     
    244267    protected void detach() {
    245268        setContentVisible(true);
    246269        setVisible(true);
    247 
    248270        // replace the toggle dialog by an invisible place holder. Makes sure
    249271        // we can place the toggle dialog where it was when it becomes docked
    250272        // again.
     
    259281            }
    260282            parent.remove(ToggleDialog.this);
    261283        }
     284       
    262285
    263286        titleBar.setVisible(false);
    264287        detachedDialog = new DetachedDialog();
    265288        detachedDialog.setVisible(true);
    266289        refreshToggleDialogsView();
    267         docked = false;
    268         Main.pref.put(preferencePrefix+".docked", docked);
     290        isDocked = false;
     291        Main.pref.put(preferencePrefix+".docked", isDocked);
    269292    }
    270293
    271294    /**
    272295     * Hides the dialog
    273296     */
    274297    public void hideDialog() {
    275         if (detachedDialog != null) {
    276             detachedDialog.setVisible(false);
    277             detachedDialog.getContentPane().removeAll();
    278             detachedDialog.dispose();
    279         }
     298        closeDetachedDialog();
    280299        setVisible(false);
    281300        isShowing = false;
    282301        Main.pref.put(preferencePrefix+".visible", false);
     
    295314     * Shows the dialog
    296315     */
    297316    public void showDialog() {
    298         if (!docked) {
     317        if (!isDocked) {
    299318            detach();
    300319        } else {
    301320            dock();
    302             if (!collapsed) {
     321            if (!isCollapsed) {
    303322                expand();
    304323                setVisible(true);
    305324                refreshToggleDialogsView();
     
    310329        }
    311330        isShowing = true;
    312331        // toggling the selected value in order to enforce PropertyChangeEvents
    313         //
    314332        toggleAction.putValue("selected", false);
    315333        toggleAction.putValue("selected", true);
    316334        Main.pref.put(preferencePrefix+".visible", true);
    317335    }
    318336
    319337    /**
    320      * Toggles between collapsed and expanded state
    321      *
    322      */
    323     protected void toggleExpandedState() {
    324         if (this.collapsed) {
    325             expand();
    326         } else {
    327             collapse();
    328         }
    329     }
    330 
    331     /**
    332338     * Refreshes the layout of the parent toggle dialog view
    333339     *
    334340     */
     
    346352    public void closeDetachedDialog() {
    347353        if (detachedDialog != null) {
    348354            detachedDialog.setVisible(false);
    349             detachedDialog.removeAll();
     355            detachedDialog.getContentPane().removeAll();
    350356            detachedDialog.dispose();
    351357        }
    352358    }
     
    386392
    387393    /**
    388394     * Replies the name of this toggle dialog
     395     *
    389396     */
    390397    @Override
    391398    public String getName() {
     
    473480     */
    474481    private class DetachedDialog extends JDialog {
    475482        public DetachedDialog() {
    476             super(JOptionPane.getFrameForComponent(Main.parent),false /* not modal*/);
     483            super(JOptionPane.getFrameForComponent(Main.parent));
    477484            getContentPane().add(ToggleDialog.this);
    478485            addWindowListener(new WindowAdapter(){
    479486                @Override public void windowClosing(WindowEvent e) {
     487                    rememberGeometry();
    480488                    getContentPane().removeAll();
    481489                    dispose();
    482490                    dock();
    483491                }
    484492            });
    485             addComponentListener(new ComponentAdapter(){
    486                 @Override public void componentMoved(ComponentEvent e) {
    487                     rememberGeometry();
    488                 }
    489             });
    490493            String bounds = Main.pref.get(preferencePrefix+".bounds",null);
    491494            if (bounds != null) {
    492495                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]))));
    494498            } else {
     499                ToggleDialog.this.setPreferredSize(ToggleDialog.this.getDefaultDetachedSize());
    495500                pack();
    496 
     501                setLocationRelativeTo(Main.parent);
    497502            }
    498503            setTitle(titleBar.getTitle());
    499504        }
     
    502507            Main.pref.put(preferencePrefix+".bounds", detachedDialog.getX()+","+detachedDialog.getY()+","+detachedDialog.getWidth()+","+detachedDialog.getHeight());
    503508        }
    504509    }
     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    }
    505526}