001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.BorderLayout;
008import java.awt.Component;
009import java.awt.event.ActionEvent;
010import java.beans.PropertyChangeEvent;
011import java.beans.PropertyChangeListener;
012
013import javax.swing.AbstractAction;
014import javax.swing.Action;
015import javax.swing.JLabel;
016import javax.swing.JOptionPane;
017import javax.swing.JPanel;
018import javax.swing.SwingConstants;
019
020import org.openstreetmap.josm.data.UndoRedoHandler;
021import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
022import org.openstreetmap.josm.data.osm.OsmPrimitive;
023import org.openstreetmap.josm.gui.ExtendedDialog;
024import org.openstreetmap.josm.gui.MainApplication;
025import org.openstreetmap.josm.gui.conflict.pair.ConflictResolver;
026import org.openstreetmap.josm.gui.help.HelpBrowser;
027import org.openstreetmap.josm.gui.help.HelpUtil;
028import org.openstreetmap.josm.tools.ImageProvider;
029
030/**
031 * This is an extended dialog for resolving conflict between {@link OsmPrimitive}s.
032 * @since 1622
033 */
034public class ConflictResolutionDialog extends ExtendedDialog implements PropertyChangeListener {
035    /** the conflict resolver component */
036    private final ConflictResolver resolver = new ConflictResolver();
037    private final JLabel titleLabel = new JLabel("", null, SwingConstants.CENTER);
038
039    private final ApplyResolutionAction applyResolutionAction = new ApplyResolutionAction();
040
041    private boolean isRegistered;
042
043    /**
044     * Constructs a new {@code ConflictResolutionDialog}.
045     * @param parent parent component
046     */
047    public ConflictResolutionDialog(Component parent) {
048        // We define our own actions, but need to give a hint about number of buttons
049        super(parent, tr("Resolve conflicts"), null, null, null);
050        setDefaultButton(1);
051        setCancelButton(2);
052        build();
053        pack();
054        if (getInsets().top > 0) {
055            titleLabel.setVisible(false);
056        }
057    }
058
059    @Override
060    public void removeNotify() {
061        super.removeNotify();
062        unregisterListeners();
063    }
064
065    @Override
066    public void addNotify() {
067        super.addNotify();
068        registerListeners();
069    }
070
071    private synchronized void registerListeners() {
072        if (!isRegistered) {
073            resolver.addPropertyChangeListener(applyResolutionAction);
074            resolver.registerListeners();
075            isRegistered = true;
076        }
077    }
078
079    private synchronized void unregisterListeners() {
080        // See #13479 - See https://bugs.openjdk.java.net/browse/JDK-4387314
081        // Owner window keep a list of owned windows, and does not remove the references when the child is disposed.
082        // There's no easy way to remove ourselves from this list, so we must keep track of register state
083        if (isRegistered) {
084            resolver.removePropertyChangeListener(applyResolutionAction);
085            resolver.unregisterListeners();
086            isRegistered = false;
087        }
088    }
089
090    /**
091     * builds the GUI
092     */
093    protected void build() {
094        JPanel p = new JPanel(new BorderLayout());
095
096        p.add(titleLabel, BorderLayout.NORTH);
097
098        updateTitle();
099
100        resolver.setName("panel.conflictresolver");
101        p.add(resolver, BorderLayout.CENTER);
102
103        resolver.addPropertyChangeListener(this);
104        HelpUtil.setHelpContext(this.getRootPane(), ht("Dialog/Conflict"));
105
106        setContent(p, false);
107    }
108
109    @Override
110    protected Action createButtonAction(int i) {
111        switch (i) {
112            case 0: return applyResolutionAction;
113            case 1: return new CancelAction();
114            case 2: return new HelpAction();
115            default: return super.createButtonAction(i);
116        }
117    }
118
119    /**
120     * Replies the conflict resolver component.
121     * @return the conflict resolver component
122     */
123    public ConflictResolver getConflictResolver() {
124        return resolver;
125    }
126
127    /**
128     * Action for canceling conflict resolution
129     */
130    class CancelAction extends AbstractAction {
131        CancelAction() {
132            putValue(Action.SHORT_DESCRIPTION, tr("Cancel conflict resolution and close the dialog"));
133            putValue(Action.NAME, tr("Cancel"));
134            new ImageProvider("cancel").getResource().attachImageIcon(this);
135            setEnabled(true);
136        }
137
138        @Override
139        public void actionPerformed(ActionEvent evt) {
140            buttonAction(2, evt);
141        }
142    }
143
144    /**
145     * Action for canceling conflict resolution
146     */
147    static class HelpAction extends AbstractAction {
148        HelpAction() {
149            putValue(Action.SHORT_DESCRIPTION, tr("Show help information"));
150            putValue(Action.NAME, tr("Help"));
151            new ImageProvider("help").getResource().attachImageIcon(this);
152            setEnabled(true);
153        }
154
155        @Override
156        public void actionPerformed(ActionEvent evt) {
157            HelpBrowser.setUrlForHelpTopic(ht("/Dialog/Conflict"));
158        }
159    }
160
161    /**
162     * Action for applying resolved differences in a conflict
163     *
164     */
165    class ApplyResolutionAction extends AbstractAction implements PropertyChangeListener {
166        ApplyResolutionAction() {
167            putValue(Action.SHORT_DESCRIPTION, tr("Apply resolved conflicts and close the dialog"));
168            putValue(Action.NAME, tr("Apply Resolution"));
169            new ImageProvider("dialogs", "conflict").getResource().attachImageIcon(this);
170            updateEnabledState();
171        }
172
173        protected void updateEnabledState() {
174            setEnabled(resolver.isResolvedCompletely());
175        }
176
177        @Override
178        public void actionPerformed(ActionEvent evt) {
179            if (!resolver.isResolvedCompletely()) {
180                Object[] options = {
181                        tr("Close anyway"),
182                        tr("Continue resolving")};
183                int ret = JOptionPane.showOptionDialog(MainApplication.getMainFrame(),
184                        tr("<html>You did not finish to merge the differences in this conflict.<br>"
185                                + "Conflict resolutions will not be applied unless all differences<br>"
186                                + "are resolved.<br>"
187                                + "Click <strong>{0}</strong> to close anyway.<strong> Already<br>"
188                                + "resolved differences will not be applied.</strong><br>"
189                                + "Click <strong>{1}</strong> to return to resolving conflicts.</html>",
190                                options[0].toString(), options[1].toString()
191                        ),
192                        tr("Conflict not resolved completely"),
193                        JOptionPane.YES_NO_OPTION,
194                        JOptionPane.WARNING_MESSAGE,
195                        null,
196                        options,
197                        options[1]
198                );
199                if (ret == JOptionPane.YES_OPTION) {
200                    buttonAction(1, evt);
201                } else {
202                    return;
203                }
204            }
205            UndoRedoHandler.getInstance().add(resolver.buildResolveCommand());
206            buttonAction(1, evt);
207        }
208
209        @Override
210        public void propertyChange(PropertyChangeEvent evt) {
211            if (evt.getPropertyName().equals(ConflictResolver.RESOLVED_COMPLETELY_PROP)) {
212                updateEnabledState();
213            }
214        }
215    }
216
217    protected void updateTitle() {
218        updateTitle(null);
219    }
220
221    protected void updateTitle(OsmPrimitive my) {
222        if (my == null) {
223            setTitle(tr("Resolve conflicts"));
224        } else {
225            setTitle(tr("Resolve conflicts for ''{0}''", my.getDisplayName(DefaultNameFormatter.getInstance())));
226        }
227    }
228
229    @Override
230    public void setTitle(String title) {
231        super.setTitle(title);
232        if (titleLabel != null) {
233            titleLabel.setText(title);
234        }
235    }
236
237    @Override
238    public void propertyChange(PropertyChangeEvent evt) {
239        if (evt.getPropertyName().equals(ConflictResolver.MY_PRIMITIVE_PROP)) {
240            updateTitle((OsmPrimitive) evt.getNewValue());
241        }
242    }
243}