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

Last change on this file since 2252 was 2252, checked in by Gubaer, 15 years ago

Refactored JOSM help system, slightly extended
Fixed problem with new internal representation of nodes as array (in Way)

File size: 8.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Dimension;
9import java.awt.FlowLayout;
10import java.awt.Point;
11import java.awt.event.ActionEvent;
12import java.beans.PropertyChangeEvent;
13import java.beans.PropertyChangeListener;
14import java.util.logging.Logger;
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;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.command.Command;
26import org.openstreetmap.josm.data.osm.OsmPrimitive;
27import org.openstreetmap.josm.gui.DefaultNameFormatter;
28import org.openstreetmap.josm.gui.conflict.pair.ConflictResolver;
29import org.openstreetmap.josm.gui.conflict.pair.properties.OperationCancelledException;
30import org.openstreetmap.josm.gui.help.HelpBuilder;
31import org.openstreetmap.josm.tools.ImageProvider;
32
33/**
34 * This is an extended dialog for resolving conflict between {@see OsmPrimitive}s.
35 *
36 */
37public class ConflictResolutionDialog extends JDialog implements PropertyChangeListener {
38 private static final Logger logger = Logger.getLogger(ConflictResolutionDialog.class.getName());
39 public final static Dimension DEFAULT_SIZE = new Dimension(600,400);
40
41 /** the conflict resolver component */
42 private ConflictResolver resolver;
43
44 /**
45 * restore position and size on screen from preference settings
46 *
47 */
48 protected void restorePositionAndDimension() {
49 Point p = new Point();
50 Dimension d = new Dimension();
51 try {
52 p.x = Integer.parseInt(Main.pref.get("conflictresolutiondialog.x", "0"));
53 p.x = Math.max(0,p.x);
54 } catch(Exception e) {
55 logger.warning("unexpected value for preference conflictresolutiondialog.x, assuming 0");
56 p.x = 0;
57 }
58 try {
59 p.y = Integer.parseInt(Main.pref.get("conflictresolutiondialog.y", "0"));
60 p.y = Math.max(0,p.y);
61 } catch(Exception e) {
62 logger.warning("unexpected value for preference conflictresolutiondialog.x, assuming 0");
63 p.y = 0;
64 }
65 try {
66 d.width = Integer.parseInt(Main.pref.get("conflictresolutiondialog.width", Integer.toString(DEFAULT_SIZE.width)));
67 d.width = Math.max(0,d.width);
68 } catch(Exception e) {
69 logger.warning("unexpected value for preference conflictresolutiondialog.width, assuming " + DEFAULT_SIZE.width);
70 p.y = 0;
71 }
72 try {
73 d.height = Integer.parseInt(Main.pref.get("conflictresolutiondialog.height", Integer.toString(DEFAULT_SIZE.height)));
74 d.height = Math.max(0,d.height);
75 } catch(Exception e) {
76 logger.warning("unexpected value for preference conflictresolutiondialog.height, assuming " + + DEFAULT_SIZE.height);
77 p.y = 0;
78 }
79
80 setLocation(p);
81 setSize(d);
82 }
83
84 /**
85 * remember position and size on screen in the preferences
86 *
87 */
88 protected void rememberPositionAndDimension() {
89 Point p = getLocation();
90 Main.pref.put("conflictresolutiondialog.x", Integer.toString(p.x));
91 Main.pref.put("conflictresolutiondialog.y", Integer.toString(p.y));
92
93 Dimension d = getSize();
94 Main.pref.put("conflictresolutiondialog.width", Integer.toString(d.width));
95 Main.pref.put("conflictresolutiondialog.height", Integer.toString(d.height));
96 }
97
98
99 @Override
100 public void setVisible(boolean isVisible) {
101 if (isVisible){
102 restorePositionAndDimension();
103 toFront();
104 } else {
105 rememberPositionAndDimension();
106 }
107 super.setVisible(isVisible);
108 }
109
110 /**
111 * builds the sub panel with the control buttons
112 *
113 * @return the panel
114 */
115 protected JPanel buildButtonRow() {
116 JPanel pnl = new JPanel();
117 pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
118
119 ApplyResolutionAction applyResolutionAction = new ApplyResolutionAction();
120 resolver.addPropertyChangeListener(applyResolutionAction);
121 JButton btn = new JButton(applyResolutionAction);
122 btn.setName("button.apply");
123 pnl.add(btn);
124
125 btn = new JButton(new CancelAction());
126 btn.setName("button.cancel");
127 pnl.add(btn);
128
129 pnl.setBorder(BorderFactory.createLoweredBevelBorder());
130 return pnl;
131 }
132
133 /**
134 * builds the GUI
135 */
136 protected void build() {
137 updateTitle();
138 getContentPane().setLayout(new BorderLayout());
139
140 resolver = new ConflictResolver();
141 resolver.setName("panel.conflictresolver");
142 getContentPane().add(resolver, BorderLayout.CENTER);
143 getContentPane().add(buildButtonRow(), BorderLayout.SOUTH);
144
145 resolver.addPropertyChangeListener(this);
146 HelpBuilder.setHelpContext(this.getRootPane(), "Dialog/ConflictDialog");
147 }
148
149
150 public ConflictResolutionDialog(Component parent) {
151 super(JOptionPane.getFrameForComponent(parent), true /* modal */);
152 build();
153 }
154
155 public ConflictResolver getConflictResolver() {
156 return resolver;
157 }
158
159 /**
160 * Action for canceling conflict resolution
161 */
162 class CancelAction extends AbstractAction {
163 public CancelAction() {
164 putValue(Action.SHORT_DESCRIPTION, tr("Cancel conflict resolution and close the dialog"));
165 putValue(Action.NAME, tr("Cancel"));
166 putValue(Action.SMALL_ICON, ImageProvider.get("", "cancel"));
167 setEnabled(true);
168 }
169
170
171 public void actionPerformed(ActionEvent arg0) {
172 setVisible(false);
173 }
174 }
175
176 /**
177 * Action for applying resolved differences in a conflict
178 *
179 */
180 class ApplyResolutionAction extends AbstractAction implements PropertyChangeListener {
181 public ApplyResolutionAction() {
182 putValue(Action.SHORT_DESCRIPTION, tr("Apply resolved conflicts and close the dialog"));
183 putValue(Action.NAME, tr("Apply Resolution"));
184 putValue(Action.SMALL_ICON, ImageProvider.get("dialogs", "conflict"));
185 updateEnabledState();
186 }
187
188 protected void updateEnabledState() {
189 setEnabled(resolver.isResolvedCompletely());
190 }
191
192 public void actionPerformed(ActionEvent arg0) {
193 if (! resolver.isResolvedCompletely()) {
194 Object[] options = {
195 tr("Close anyway"),
196 tr("Continue resolving")};
197 int ret = JOptionPane.showOptionDialog(Main.parent,
198 tr("<html>You didn''t finish to merge the differences in this conflict.<br>"
199 + "Conflict resolutions won't be applied unless all differences<br>"
200 + "are resolved."
201 + "Click <strong>{0}</strong> to close anyway.<strong>Already<br>"
202 + "resolved differences won't be applied.</strong><br>"
203 + "Click <strong>{1}</strong> to return to resolving conflicts.</html>"
204 , options[0].toString(), options[1].toString()
205 ),
206 tr("Conflict not resolved completely"),
207 JOptionPane.YES_NO_OPTION,
208 JOptionPane.WARNING_MESSAGE,
209 null,
210 options,
211 options[1]
212 );
213 switch(ret) {
214 case JOptionPane.YES_OPTION:
215 setVisible(false);
216 break;
217 default:
218 return;
219 }
220 }
221 try {
222 Command cmd = resolver.buildResolveCommand();
223 Main.main.undoRedo.add(cmd);
224 setVisible(false);
225 } catch(OperationCancelledException e) {
226 // do nothing. Exception already reported
227 }
228 }
229
230 public void propertyChange(PropertyChangeEvent evt) {
231 if (evt.getPropertyName().equals(ConflictResolver.RESOLVED_COMPLETELY_PROP)) {
232 updateEnabledState();
233 }
234 }
235 }
236
237 protected void updateTitle() {
238 updateTitle(null);
239 }
240
241 protected void updateTitle(OsmPrimitive my) {
242 if (my == null) {
243 setTitle(tr("Resolve conflicts"));
244 } else {
245 setTitle(tr("Resolve conflicts for ''{0}''", my.getDisplayName(DefaultNameFormatter.getInstance())));
246 }
247 }
248
249 public void propertyChange(PropertyChangeEvent evt) {
250 if (evt.getPropertyName().equals(ConflictResolver.MY_PRIMITIVE_PROP)) {
251 updateTitle((OsmPrimitive)evt.getNewValue());
252 }
253 }
254}
Note: See TracBrowser for help on using the repository browser.