Ignore:
Timestamp:
2016-09-04T16:32:05+02:00 (8 years ago)
Author:
Don-vip
Message:

see #13479 - make ConflictResolutionDialog inherit from ExtendedDialog

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

Legend:

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

    r10791 r10957  
    146146    }
    147147
     148    /**
     149     * Same as above but lets you define if the dialog should be disposed on close.
     150     * @param parent The parent element that will be used for position and maximum size
     151     * @param title The text that will be shown in the window titlebar
     152     * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one.
     153     * @param modal Set it to {@code true} if you want the dialog to be modal
     154     * @param disposeOnClose whether to call {@link #dispose} when closing the dialog
     155     */
    148156    public ExtendedDialog(Component parent, String title, String[] buttonTexts, boolean modal, boolean disposeOnClose) {
    149157        super(searchRealParent(parent), title, modal ? ModalityType.DOCUMENT_MODAL : ModalityType.MODELESS);
     
    329337
    330338        for (int i = 0; i < bTexts.length; i++) {
    331             final int finalI = i;
    332             Action action = new AbstractAction(bTexts[i]) {
    333                 @Override
    334                 public void actionPerformed(ActionEvent evt) {
    335                     buttonAction(finalI, evt);
    336                 }
    337             };
    338 
    339             button = new JButton(action);
     339            button = new JButton(createButtonAction(i));
    340340            if (i == defaultButtonIdx-1) {
    341341                defaultButton = button;
     
    430430        setSize(d);
    431431        setLocationRelativeTo(parent);
     432    }
     433
     434    protected Action createButtonAction(final int i) {
     435        return new AbstractAction(bTexts[i]) {
     436            @Override
     437            public void actionPerformed(ActionEvent evt) {
     438                buttonAction(i, evt);
     439            }
     440        };
    432441    }
    433442
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ConflictDialog.java

    r10853 r10957  
    222222        ConflictResolutionDialog dialog = new ConflictResolutionDialog(Main.parent);
    223223        dialog.getConflictResolver().populate(c);
    224         dialog.setVisible(true);
     224        dialog.showDialog();
    225225
    226226        lstConflicts.setSelectedIndex(index);
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ConflictResolutionDialog.java

    r10454 r10957  
    77import java.awt.BorderLayout;
    88import java.awt.Component;
    9 import java.awt.Dimension;
    10 import java.awt.FlowLayout;
    119import java.awt.event.ActionEvent;
    1210import java.beans.PropertyChangeEvent;
     
    1513import javax.swing.AbstractAction;
    1614import javax.swing.Action;
    17 import javax.swing.BorderFactory;
    18 import javax.swing.JButton;
    19 import javax.swing.JDialog;
    2015import javax.swing.JLabel;
    2116import javax.swing.JOptionPane;
    2217import javax.swing.JPanel;
    23 import javax.swing.WindowConstants;
    2418
    2519import org.openstreetmap.josm.Main;
    26 import org.openstreetmap.josm.command.Command;
    2720import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2821import org.openstreetmap.josm.gui.DefaultNameFormatter;
     22import org.openstreetmap.josm.gui.ExtendedDialog;
    2923import org.openstreetmap.josm.gui.conflict.pair.ConflictResolver;
    3024import org.openstreetmap.josm.gui.help.HelpBrowser;
    3125import org.openstreetmap.josm.gui.help.HelpUtil;
    32 import org.openstreetmap.josm.gui.util.GuiHelper;
    3326import org.openstreetmap.josm.tools.ImageProvider;
    34 import org.openstreetmap.josm.tools.WindowGeometry;
    3527
    3628/**
     
    3830 * @since 1622
    3931 */
    40 public class ConflictResolutionDialog extends JDialog implements PropertyChangeListener {
     32public class ConflictResolutionDialog extends ExtendedDialog implements PropertyChangeListener {
    4133    /** the conflict resolver component */
    42     private ConflictResolver resolver;
    43     private JLabel titleLabel;
    44 
    45     private ApplyResolutionAction applyResolutionAction;
    46 
    47     @Override
    48     public void removeNotify() {
    49         super.removeNotify();
    50         unregisterListeners();
    51     }
    52 
    53     @Override
    54     public void addNotify() {
    55         super.addNotify();
    56         registerListeners();
    57     }
    58 
    59     @Override
    60     public void setVisible(boolean isVisible) {
    61         String geom = getClass().getName() + ".geometry";
    62         if (isVisible) {
    63             toFront();
    64             new WindowGeometry(geom, WindowGeometry.centerInWindow(Main.parent,
    65                 new Dimension(600, 400))).applySafe(this);
    66         } else {
    67             if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
    68                 new WindowGeometry(this).remember(geom);
    69             }
    70         }
    71         super.setVisible(isVisible);
    72     }
    73 
    74     private void closeDialog() {
    75         setVisible(false);
    76         dispose();
    77     }
    78 
    79     /**
    80      * builds the sub panel with the control buttons
    81      *
    82      * @return the panel
    83      */
    84     protected JPanel buildButtonRow() {
    85         JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
    86 
    87         applyResolutionAction = new ApplyResolutionAction();
    88         JButton btn = new JButton(applyResolutionAction);
    89         btn.setName("button.apply");
    90         pnl.add(btn);
    91 
    92         btn = new JButton(new CancelAction());
    93         btn.setName("button.cancel");
    94         pnl.add(btn);
    95 
    96         btn = new JButton(new HelpAction());
    97         btn.setName("button.help");
    98         pnl.add(btn);
    99 
    100         pnl.setBorder(BorderFactory.createLoweredBevelBorder());
    101         return pnl;
    102     }
    103 
    104     private void registerListeners() {
    105         resolver.addPropertyChangeListener(applyResolutionAction);
    106         resolver.registerListeners();
    107     }
    108 
    109     private void unregisterListeners() {
    110         resolver.removePropertyChangeListener(applyResolutionAction);
    111         resolver.unregisterListeners();
    112     }
    113 
    114     /**
    115      * builds the GUI
    116      */
    117     protected void build() {
    118         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    119         getContentPane().setLayout(new BorderLayout());
    120 
    121         titleLabel = new JLabel();
    122         titleLabel.setHorizontalAlignment(JLabel.CENTER);
    123         getContentPane().add(titleLabel, BorderLayout.NORTH);
    124 
    125         updateTitle();
    126 
    127         resolver = new ConflictResolver();
    128         resolver.setName("panel.conflictresolver");
    129         getContentPane().add(resolver, BorderLayout.CENTER);
    130         getContentPane().add(buildButtonRow(), BorderLayout.SOUTH);
    131 
    132         resolver.addPropertyChangeListener(this);
    133         HelpUtil.setHelpContext(this.getRootPane(), ht("Dialog/Conflict"));
    134     }
     34    private final ConflictResolver resolver = new ConflictResolver();
     35    private final JLabel titleLabel = new JLabel("", null, JLabel.CENTER);
     36
     37    private final ApplyResolutionAction applyResolutionAction = new ApplyResolutionAction();
    13538
    13639    /**
     
    13942     */
    14043    public ConflictResolutionDialog(Component parent) {
    141         super(GuiHelper.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
     44        // We define our own actions, but need to give a hint about number of buttons
     45        super(parent, tr("Resolve conflicts"), new String[] {null, null, null});
     46        setDefaultButton(1);
     47        setCancelButton(2);
    14248        build();
    14349        pack();
    14450        if (getInsets().top > 0) {
    14551            titleLabel.setVisible(false);
     52        }
     53    }
     54
     55    @Override
     56    public void removeNotify() {
     57        super.removeNotify();
     58        unregisterListeners();
     59    }
     60
     61    @Override
     62    public void addNotify() {
     63        super.addNotify();
     64        registerListeners();
     65    }
     66
     67    private void registerListeners() {
     68        resolver.addPropertyChangeListener(applyResolutionAction);
     69        resolver.registerListeners();
     70    }
     71
     72    private void unregisterListeners() {
     73        resolver.removePropertyChangeListener(applyResolutionAction);
     74        resolver.unregisterListeners();
     75    }
     76
     77    /**
     78     * builds the GUI
     79     */
     80    protected void build() {
     81        JPanel p = new JPanel(new BorderLayout());
     82
     83        p.add(titleLabel, BorderLayout.NORTH);
     84
     85        updateTitle();
     86
     87        resolver.setName("panel.conflictresolver");
     88        p.add(resolver, BorderLayout.CENTER);
     89
     90        resolver.addPropertyChangeListener(this);
     91        HelpUtil.setHelpContext(this.getRootPane(), ht("Dialog/Conflict"));
     92
     93        setContent(p);
     94    }
     95
     96    @Override
     97    protected Action createButtonAction(int i) {
     98        switch (i) {
     99            case 0: return applyResolutionAction;
     100            case 1: return new CancelAction();
     101            case 2: return new HelpAction();
     102            default: return super.createButtonAction(i);
    146103        }
    147104    }
     
    167124
    168125        @Override
    169         public void actionPerformed(ActionEvent arg0) {
    170             closeDialog();
     126        public void actionPerformed(ActionEvent evt) {
     127            buttonAction(2, evt);
    171128        }
    172129    }
     
    184141
    185142        @Override
    186         public void actionPerformed(ActionEvent arg0) {
     143        public void actionPerformed(ActionEvent evt) {
    187144            HelpBrowser.setUrlForHelpTopic(ht("/Dialog/Conflict"));
    188145        }
     
    206163
    207164        @Override
    208         public void actionPerformed(ActionEvent arg0) {
     165        public void actionPerformed(ActionEvent evt) {
    209166            if (!resolver.isResolvedCompletely()) {
    210167                Object[] options = {
     
    229186                switch(ret) {
    230187                case JOptionPane.YES_OPTION:
    231                     closeDialog();
     188                    buttonAction(1, evt);
    232189                    break;
    233190                default:
     
    235192                }
    236193            }
    237             Command cmd = resolver.buildResolveCommand();
    238             Main.main.undoRedo.add(cmd);
    239             closeDialog();
     194            Main.main.undoRedo.add(resolver.buildResolveCommand());
     195            buttonAction(1, evt);
    240196        }
    241197
Note: See TracChangeset for help on using the changeset viewer.