Changeset 1432 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2009-02-20T17:33:15+01:00 (15 years ago)
Author:
stoecker
Message:

close #2211. patch by xeen

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/ExtendedDialog.java

    r1427 r1432  
    2828    private Component parent;
    2929    private final String[] bTexts;
    30    
     30
    3131    // For easy access when inherited
    3232    protected Object contentConstraints = GBC.eol().anchor(GBC.CENTER).insets(5,10,5,0);
    3333    protected ArrayList<JButton> buttons = new ArrayList<JButton>();
    34    
     34
    3535    /**
    3636     * Sets up the dialog. The first button is always the default.
     
    4040     * @param buttonTexts The labels that will be displayed on the buttons
    4141     * @param buttonIcons The path to the icons that will be displayed on the buttons. Path is relative to JOSM's image directory. File extensions need to be included. If a button should not have an icon pass null.
    42      */ 
     42     */
    4343    public ExtendedDialog(Component parent, String title, Component content, String[] buttonTexts, String[] buttonIcons) {
    44         super(JOptionPane.getFrameForComponent(parent), title, true); 
     44        super(JOptionPane.getFrameForComponent(parent), title, true);
    4545        this.parent = parent;
    46         bTexts = buttonTexts;       
     46        bTexts = buttonTexts;
    4747        setupDialog(content, buttonIcons);
    4848        setVisible(true);
    4949    }
    50    
     50
    5151    public ExtendedDialog(Component parent, String title, Component content, String[] buttonTexts) {
    5252        this(parent, title, content, buttonTexts, null);
    5353    }
    54    
     54
    5555    /**
    5656     * Sets up the dialog and displays the given message in a breakable label
     
    5858    public ExtendedDialog(Component parent, String title, String message, String[] buttonTexts, String[] buttonIcons) {
    5959        super(JOptionPane.getFrameForComponent(parent), title, true);
    60        
     60
    6161        JMultilineLabel lbl = new JMultilineLabel(message);
    6262        // Make it not wider than 2/3 of the screen
    6363        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    6464        lbl.setMaxWidth(Math.round(screenSize.width*2/3));
    65        
    66         bTexts = buttonTexts;       
     65
     66        bTexts = buttonTexts;
    6767        setupDialog(lbl, buttonIcons);
    6868        setVisible(true);
    6969    }
    70    
     70
    7171    public ExtendedDialog(Component parent, String title, String message, String[] buttonTexts) {
    7272        this(parent, title, message, buttonTexts, null);
    7373    }
    74    
     74
    7575    /**
    7676     * Constructor that doesn't make the dialog visible immediately. Intended for when inheriting.
    7777     */
    7878    public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal) {
    79         super(JOptionPane.getFrameForComponent(parent), title, modal);     
    80         bTexts = buttonTexts;   
     79        super(JOptionPane.getFrameForComponent(parent), title, modal);
     80        bTexts = buttonTexts;
    8181    }
    82    
     82
    8383    protected void setupDialog(Component content, String[] buttonIcons) {
    8484        setupEscListener();
    85        
     85
    8686        JButton button;
    8787        JPanel buttonsPanel = new JPanel(new GridBagLayout());
    88        
     88
    8989        for(int i=0; i < bTexts.length; i++) {
    9090            Action action = new AbstractAction(bTexts[i]) {
    9191                public void actionPerformed(ActionEvent evt) {
    92                     String a = evt.getActionCommand();
    93                     for(int i=0; i < bTexts.length; i++)
    94                         if(bTexts[i].equals(a)) {
    95                             result = i+1;
    96                             break;
    97                         }
    98                        
    99                     setVisible(false);
     92                    buttonAction(evt);
    10093                }
    10194            };
    102            
    103             button = new JButton(action);           
     95
     96            button = new JButton(action);
    10497            if(buttonIcons != null && buttonIcons[i] != null)
    10598                button.setIcon(ImageProvider.get(buttonIcons[i]));
    106            
    107             if(i == 0) rootPane.setDefaultButton(button);           
     99
     100            if(i == 0) rootPane.setDefaultButton(button);
    108101            buttonsPanel.add(button, GBC.std().insets(2,2,2,2));
    109102            buttons.add(button);
    110103        }
    111        
    112         JPanel cp = new JPanel(new GridBagLayout());       
     104
     105        JPanel cp = new JPanel(new GridBagLayout());
    113106        cp.add(content, contentConstraints);
    114107        cp.add(buttonsPanel, GBC.eol().anchor(GBC.CENTER).insets(5,5,5,5));
    115        
     108
    116109        JScrollPane pane = new JScrollPane(cp);
    117         pane.setBorder(null);       
     110        pane.setBorder(null);
    118111        setContentPane(pane);
    119        
    120         pack(); 
    121        
     112
     113        pack();
     114
    122115        // Try to make it not larger than the parent window or at least not larger than 2/3 of the screen
    123116        Dimension d = getSize();
    124117        Dimension x = findMaxDialogSize();
    125        
     118
    126119        boolean limitedInWidth = d.width > x.width;
    127120        boolean limitedInHeight = d.height > x.height;
     
    129122        if(x.width  > 0 && d.width  > x.width)  d.width  = x.width;
    130123        if(x.height > 0 && d.height > x.height) d.height = x.height;
    131        
     124
    132125        // We have a vertical scrollbar and enough space to prevent a horizontal one
    133126        if(!limitedInWidth && limitedInHeight)
    134127            d.width += new JScrollBar().getPreferredSize().width;
    135        
     128
    136129        setSize(d);
    137130        setLocationRelativeTo(parent);
    138131    }
    139    
     132
    140133    /**
    141      * @return int The selected button. The count starts with 1. 
     134     * @return int The selected button. The count starts with 1.
    142135     *             A return value of 0 means the dialog has been closed otherwise.
    143      */   
     136     */
    144137    public int getValue() {
    145138        return result;
    146139    }
    147    
     140
     141    /**
     142     * This gets performed whenever a button is clicked or activated
     143     * @param evt the button event
     144     */
     145    protected void buttonAction(ActionEvent evt) {
     146        String a = evt.getActionCommand();
     147        for(int i=0; i < bTexts.length; i++)
     148            if(bTexts[i].equals(a)) {
     149                result = i+1;
     150                break;
     151            }
     152
     153        setVisible(false);
     154    }
     155
    148156    /**
    149157     * Tries to find a good value of how large the dialog should be
     
    160168        return x;
    161169    }
    162    
     170
    163171    /**
    164172     * Makes the dialog listen to ESC keypressed
    165173     */
    166174    private void setupEscListener() {
    167         Action actionListener = new AbstractAction() { 
    168             public void actionPerformed(ActionEvent actionEvent) { 
     175        Action actionListener = new AbstractAction() {
     176            public void actionPerformed(ActionEvent actionEvent) {
    169177                setVisible(false);
    170             } 
     178            }
    171179        };
    172        
     180
    173181        rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
    174182            .put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
    175         rootPane.getActionMap().put("ESCAPE", actionListener);       
     183        rootPane.getActionMap().put("ESCAPE", actionListener);
    176184    }
    177185}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/RelationEditor.java

    r1431 r1432  
    175175    // ... until here, and also get rid of the "Collections.sort..." below.
    176176
     177    // We need this twice, so cache result
     178    protected final static String applyChangesText = tr("Apply Changes");
     179
    177180    /**
    178181     * Creates a new relation editor for the given relation. The relation
     
    206209                            : tr("Edit relation #{0}", relation.id)
    207210                       ),
    208                 new String[] { tr("Apply Changes"), tr("Cancel")},
     211                new String[] { applyChangesText, tr("Cancel")},
    209212                false
    210213        );
     
    212215        this.relation = relation;
    213216        ordered = !Main.pref.get("osm-server.version", "0.5").equals("0.5");
    214 ordered = true;
     217
    215218        if (relation == null) {
    216219            // create a new relation
     
    235238
    236239        try { setAlwaysOnTop(true); } catch (SecurityException sx) {}
    237 
    238240        setVisible(true);
    239 
    240         if(getValue() != 1)
    241             return;
    242 
    243         // User clicked "Apply"
    244         applyChanges();
    245     }
     241    }
     242
    246243
    247244    /**
     
    425422
    426423    @Override
     424    protected void buttonAction(ActionEvent evt) {
     425        String a = evt.getActionCommand();
     426        if(applyChangesText.equals(a))
     427            applyChanges();
     428
     429        setVisible(false);
     430    }
     431
     432    @Override
    427433    protected Dimension findMaxDialogSize() {
    428434        // FIXME: Make it remember dialog size
Note: See TracChangeset for help on using the changeset viewer.