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

Last change on this file since 2512 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

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