source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/ConflictResolutionDialog.java @ 5241

Revision 4189, 7.8 KB checked in by stoecker, 11 months ago (diff)

unify geometry code

  • Property svn:eol-style set to native
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.BorderLayout;
8import java.awt.Component;
9import java.awt.Dimension;
10import java.awt.FlowLayout;
11import java.awt.Point;
12import java.awt.event.ActionEvent;
13import java.beans.PropertyChangeEvent;
14import java.beans.PropertyChangeListener;
15
16import javax.swing.AbstractAction;
17import javax.swing.Action;
18import javax.swing.BorderFactory;
19import javax.swing.JButton;
20import javax.swing.JDialog;
21import javax.swing.JOptionPane;
22import javax.swing.JPanel;
23import javax.swing.WindowConstants;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.command.Command;
27import org.openstreetmap.josm.data.osm.OsmPrimitive;
28import org.openstreetmap.josm.gui.DefaultNameFormatter;
29import org.openstreetmap.josm.gui.conflict.pair.ConflictResolver;
30import org.openstreetmap.josm.gui.help.HelpBrowser;
31import org.openstreetmap.josm.gui.help.HelpUtil;
32import org.openstreetmap.josm.tools.ImageProvider;
33import org.openstreetmap.josm.tools.WindowGeometry;
34
35/**
36 * This is an extended dialog for resolving conflict between {@see OsmPrimitive}s.
37 *
38 */
39public class ConflictResolutionDialog extends JDialog implements PropertyChangeListener {
40    /** the conflict resolver component */
41    private ConflictResolver resolver;
42
43    private ApplyResolutionAction applyResolutionAction;
44
45    @Override
46    public void removeNotify() {
47        super.removeNotify();
48        unregisterListeners();
49    }
50
51    @Override
52    public void setVisible(boolean isVisible) {
53        String geom = getClass().getName() + ".geometry";
54        if (isVisible){
55            toFront();
56            new WindowGeometry(geom, WindowGeometry.centerInWindow(Main.parent,
57                new Dimension(600, 400))).applySafe(this);
58        } else {
59            new WindowGeometry(this).remember(geom);
60            unregisterListeners();
61        }
62        super.setVisible(isVisible);
63    }
64
65    private void closeDialog() {
66        setVisible(false);
67        dispose();
68    }
69
70    /**
71     * builds the sub panel with the control buttons
72     *
73     * @return the panel
74     */
75    protected JPanel buildButtonRow() {
76        JPanel pnl = new JPanel();
77        pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
78
79        applyResolutionAction = new ApplyResolutionAction();
80        JButton btn = new JButton(applyResolutionAction);
81        btn.setName("button.apply");
82        pnl.add(btn);
83
84        btn = new JButton(new CancelAction());
85        btn.setName("button.cancel");
86        pnl.add(btn);
87
88        btn = new JButton(new HelpAction());
89        btn.setName("button.help");
90        pnl.add(btn);
91
92        pnl.setBorder(BorderFactory.createLoweredBevelBorder());
93        return pnl;
94    }
95
96    private void registerListeners() {
97        resolver.addPropertyChangeListener(applyResolutionAction);
98    }
99
100    private void unregisterListeners() {
101        resolver.removePropertyChangeListener(applyResolutionAction);
102    }
103
104    /**
105     * builds the GUI
106     */
107    protected void build() {
108        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
109        updateTitle();
110        getContentPane().setLayout(new BorderLayout());
111
112        resolver = new ConflictResolver();
113        resolver.setName("panel.conflictresolver");
114        getContentPane().add(resolver, BorderLayout.CENTER);
115        getContentPane().add(buildButtonRow(), BorderLayout.SOUTH);
116
117        resolver.addPropertyChangeListener(this);
118        HelpUtil.setHelpContext(this.getRootPane(), ht("Dialog/Conflict"));
119
120        registerListeners();
121    }
122
123    public ConflictResolutionDialog(Component parent) {
124        super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
125        build();
126    }
127
128    public ConflictResolver getConflictResolver() {
129        return resolver;
130    }
131
132    /**
133     * Action for canceling conflict resolution
134     */
135    class CancelAction extends AbstractAction {
136        public CancelAction() {
137            putValue(Action.SHORT_DESCRIPTION, tr("Cancel conflict resolution and close the dialog"));
138            putValue(Action.NAME, tr("Cancel"));
139            putValue(Action.SMALL_ICON, ImageProvider.get("", "cancel"));
140            setEnabled(true);
141        }
142
143        public void actionPerformed(ActionEvent arg0) {
144            closeDialog();
145        }
146    }
147
148    /**
149     * Action for canceling conflict resolution
150     */
151    static class HelpAction extends AbstractAction {
152        public HelpAction() {
153            putValue(Action.SHORT_DESCRIPTION, tr("Show help information"));
154            putValue(Action.NAME, tr("Help"));
155            putValue(Action.SMALL_ICON, ImageProvider.get("help"));
156            setEnabled(true);
157        }
158
159        public void actionPerformed(ActionEvent arg0) {
160            HelpBrowser.setUrlForHelpTopic(ht("/Dialog/Conflict"));
161        }
162    }
163
164    /**
165     * Action for applying resolved differences in a conflict
166     *
167     */
168    class ApplyResolutionAction extends AbstractAction implements PropertyChangeListener {
169        public ApplyResolutionAction() {
170            putValue(Action.SHORT_DESCRIPTION, tr("Apply resolved conflicts and close the dialog"));
171            putValue(Action.NAME, tr("Apply Resolution"));
172            putValue(Action.SMALL_ICON, ImageProvider.get("dialogs", "conflict"));
173            updateEnabledState();
174        }
175
176        protected void updateEnabledState() {
177            setEnabled(resolver.isResolvedCompletely());
178        }
179
180        public void actionPerformed(ActionEvent arg0) {
181            if (! resolver.isResolvedCompletely()) {
182                Object[] options = {
183                        tr("Close anyway"),
184                        tr("Continue resolving")};
185                int ret = JOptionPane.showOptionDialog(Main.parent,
186                        tr("<html>You did not finish to merge the differences in this conflict.<br>"
187                                + "Conflict resolutions will not be applied unless all differences<br>"
188                                + "are resolved.<br>"
189                                + "Click <strong>{0}</strong> to close anyway.<strong> Already<br>"
190                                + "resolved differences will not be applied.</strong><br>"
191                                + "Click <strong>{1}</strong> to return to resolving conflicts.</html>"
192                                , options[0].toString(), options[1].toString()
193                        ),
194                        tr("Conflict not resolved completely"),
195                        JOptionPane.YES_NO_OPTION,
196                        JOptionPane.WARNING_MESSAGE,
197                        null,
198                        options,
199                        options[1]
200                );
201                switch(ret) {
202                case JOptionPane.YES_OPTION:
203                    closeDialog();
204                    break;
205                default:
206                    return;
207                }
208            }
209            Command cmd = resolver.buildResolveCommand();
210            Main.main.undoRedo.add(cmd);
211            closeDialog();
212        }
213
214        public void propertyChange(PropertyChangeEvent evt) {
215            if (evt.getPropertyName().equals(ConflictResolver.RESOLVED_COMPLETELY_PROP)) {
216                updateEnabledState();
217            }
218        }
219    }
220
221    protected void updateTitle() {
222        updateTitle(null);
223    }
224
225    protected void updateTitle(OsmPrimitive my) {
226        if (my == null) {
227            setTitle(tr("Resolve conflicts"));
228        } else {
229            setTitle(tr("Resolve conflicts for ''{0}''", my.getDisplayName(DefaultNameFormatter.getInstance())));
230        }
231    }
232
233    public void propertyChange(PropertyChangeEvent evt) {
234        if (evt.getPropertyName().equals(ConflictResolver.MY_PRIMITIVE_PROP)) {
235            updateTitle((OsmPrimitive)evt.getNewValue());
236        }
237    }
238}
Note: See TracBrowser for help on using the repository browser.