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

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

replaced JOptionDialog.show... by OptionPaneUtil.show....
improved relation editor

File size: 7.3 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.util.logging.Logger;
13
14import javax.swing.AbstractAction;
15import javax.swing.Action;
16import javax.swing.BorderFactory;
17import javax.swing.JButton;
18import javax.swing.JDialog;
19import javax.swing.JOptionPane;
20import javax.swing.JPanel;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.command.Command;
24import org.openstreetmap.josm.gui.OptionPaneUtil;
25import org.openstreetmap.josm.gui.conflict.ConflictResolver;
26import org.openstreetmap.josm.gui.conflict.properties.OperationCancelledException;
27import org.openstreetmap.josm.tools.ImageProvider;
28
29/**
30 * This is an extended dialog for resolving conflict between {@see OsmPrimitive}.
31 *
32 *
33 */
34public class ConflictResolutionDialog extends JDialog {
35 private static final Logger logger = Logger.getLogger(ConflictResolutionDialog.class.getName());
36 public final static Dimension DEFAULT_SIZE = new Dimension(600,400);
37
38 /** the conflict resolver component */
39 private ConflictResolver resolver;
40
41 /**
42 * restore position and size on screen from preference settings
43 *
44 */
45 protected void restorePositionAndDimension() {
46 Point p = new Point();
47 Dimension d = new Dimension();
48 try {
49 p.x = Integer.parseInt(Main.pref.get("conflictresolutiondialog.x", "0"));
50 p.x = Math.max(0,p.x);
51 } catch(Exception e) {
52 logger.warning("unexpected value for preference conflictresolutiondialog.x, assuming 0");
53 p.x = 0;
54 }
55 try {
56 p.y = Integer.parseInt(Main.pref.get("conflictresolutiondialog.y", "0"));
57 p.y = Math.max(0,p.y);
58 } catch(Exception e) {
59 logger.warning("unexpected value for preference conflictresolutiondialog.x, assuming 0");
60 p.y = 0;
61 }
62 try {
63 d.width = Integer.parseInt(Main.pref.get("conflictresolutiondialog.width", Integer.toString(DEFAULT_SIZE.width)));
64 d.width = Math.max(0,d.width);
65 } catch(Exception e) {
66 logger.warning("unexpected value for preference conflictresolutiondialog.width, assuming " + DEFAULT_SIZE.width);
67 p.y = 0;
68 }
69 try {
70 d.height = Integer.parseInt(Main.pref.get("conflictresolutiondialog.height", Integer.toString(DEFAULT_SIZE.height)));
71 d.height = Math.max(0,d.height);
72 } catch(Exception e) {
73 logger.warning("unexpected value for preference conflictresolutiondialog.height, assuming " + + DEFAULT_SIZE.height);
74 p.y = 0;
75 }
76
77 setLocation(p);
78 setSize(d);
79 }
80
81 /**
82 * remember position and size on screen in the preferences
83 *
84 */
85 protected void rememberPositionAndDimension() {
86 Point p = getLocation();
87 Main.pref.put("conflictresolutiondialog.x", Integer.toString(p.x));
88 Main.pref.put("conflictresolutiondialog.y", Integer.toString(p.y));
89
90 Dimension d = getSize();
91 Main.pref.put("conflictresolutiondialog.width", Integer.toString(d.width));
92 Main.pref.put("conflictresolutiondialog.height", Integer.toString(d.height));
93 }
94
95
96 @Override
97 public void setVisible(boolean isVisible) {
98 if (isVisible){
99 restorePositionAndDimension();
100 toFront();
101 } else {
102 rememberPositionAndDimension();
103 }
104 super.setVisible(isVisible);
105 }
106
107 /**
108 * builds the sub panel with the control buttons
109 *
110 * @return the panel
111 */
112 protected JPanel buildButtonRow() {
113 JPanel pnl = new JPanel();
114 pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
115
116 JButton btn = new JButton(new ApplyResolutionAction());
117 btn.setName("button.apply");
118 pnl.add(btn);
119
120 btn = new JButton(new CancelAction());
121 btn.setName("button.cancel");
122 pnl.add(btn);
123
124 pnl.setBorder(BorderFactory.createLoweredBevelBorder());
125 return pnl;
126 }
127
128 /**
129 * builds the GUI
130 */
131 protected void build() {
132 setTitle(tr("Resolve conflicts"));
133 try {
134 setAlwaysOnTop(true);
135 } catch(SecurityException e) {
136 System.out.println(tr("Warning: couldn't setAlwaysOnTop(true) for ConflictResolution Dialog. Exception: {0}", e.toString()));
137 }
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
146
147 public ConflictResolutionDialog(Component parent) {
148 super(JOptionPane.getFrameForComponent(parent), true /* modal */);
149 build();
150 }
151
152 public ConflictResolver getConflictResolver() {
153 return resolver;
154 }
155
156 class CancelAction extends AbstractAction {
157 public CancelAction() {
158 putValue(Action.SHORT_DESCRIPTION, tr("Cancel conflict resolution and close the dialog"));
159 putValue(Action.NAME, tr("Cancel"));
160 putValue(Action.SMALL_ICON, ImageProvider.get("", "cancel"));
161 setEnabled(true);
162 }
163
164
165 public void actionPerformed(ActionEvent arg0) {
166 setVisible(false);
167 }
168 }
169
170 class ApplyResolutionAction extends AbstractAction {
171 public ApplyResolutionAction() {
172 putValue(Action.SHORT_DESCRIPTION, tr("Apply resolved conflicts and close the dialog"));
173 putValue(Action.NAME, tr("Apply Resolution"));
174 putValue(Action.SMALL_ICON, ImageProvider.get("dialogs", "conflict"));
175 setEnabled(true);
176 }
177
178 public void actionPerformed(ActionEvent arg0) {
179 if (! resolver.isResolvedCompletely()) {
180 Object[] options = {
181 tr("Apply partial resolutions"),
182 tr("Continue resolving")};
183 int n = OptionPaneUtil.showOptionDialog(null,
184 tr("<html>You didn''t finish to resolve all conflicts.<br>"
185 + "Click <strong>{0}</strong> to apply already resolved conflicts anyway.<br>"
186 + "You can resolve the remaining conflicts later.<br>"
187 + "Click <strong>{1}</strong> to return to resolving conflicts.</html>"
188 , options[0].toString(), options[1].toString()
189 ),
190 tr("Warning"),
191 JOptionPane.YES_NO_OPTION,
192 JOptionPane.WARNING_MESSAGE,
193 options,
194 options[1]
195 );
196 if (n == JOptionPane.NO_OPTION || n == JOptionPane.CLOSED_OPTION)
197 return;
198 }
199 try {
200 Command cmd = resolver.buildResolveCommand();
201 Main.main.undoRedo.add(cmd);
202 } catch(OperationCancelledException e) {
203 // do nothing. Exception already reported
204 }
205 setVisible(false);
206 }
207 }
208}
Note: See TracBrowser for help on using the repository browser.