1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.FlowLayout;
|
---|
10 | import java.awt.Point;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.util.logging.Logger;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.Action;
|
---|
16 | import javax.swing.BorderFactory;
|
---|
17 | import javax.swing.JButton;
|
---|
18 | import javax.swing.JDialog;
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.command.Command;
|
---|
24 | import org.openstreetmap.josm.gui.conflict.ConflictResolver;
|
---|
25 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * This is an extended dialog for resolving conflict between {@see OsmPrimitive}.
|
---|
29 | *
|
---|
30 | *
|
---|
31 | */
|
---|
32 | public class ConflictResolutionDialog extends JDialog {
|
---|
33 | private static final Logger logger = Logger.getLogger(ConflictResolutionDialog.class.getName());
|
---|
34 | public final static Dimension DEFAULT_SIZE = new Dimension(600,400);
|
---|
35 |
|
---|
36 | /** the conflict resolver component */
|
---|
37 | private ConflictResolver resolver;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * restore position and size on screen from preference settings
|
---|
41 | *
|
---|
42 | */
|
---|
43 | protected void restorePositionAndDimension() {
|
---|
44 | Point p = new Point();
|
---|
45 | Dimension d = new Dimension();
|
---|
46 | try {
|
---|
47 | p.x = Integer.parseInt(Main.pref.get("conflictresolutiondialog.x", "0"));
|
---|
48 | p.x = Math.max(0,p.x);
|
---|
49 | } catch(Exception e) {
|
---|
50 | logger.warning("unexpected value for preference conflictresolutiondialog.x, assuming 0");
|
---|
51 | p.x = 0;
|
---|
52 | }
|
---|
53 | try {
|
---|
54 | p.y = Integer.parseInt(Main.pref.get("conflictresolutiondialog.y", "0"));
|
---|
55 | p.y = Math.max(0,p.y);
|
---|
56 | } catch(Exception e) {
|
---|
57 | logger.warning("unexpected value for preference conflictresolutiondialog.x, assuming 0");
|
---|
58 | p.y = 0;
|
---|
59 | }
|
---|
60 | try {
|
---|
61 | d.width = Integer.parseInt(Main.pref.get("conflictresolutiondialog.width", Integer.toString(DEFAULT_SIZE.width)));
|
---|
62 | d.width = Math.max(0,d.width);
|
---|
63 | } catch(Exception e) {
|
---|
64 | logger.warning("unexpected value for preference conflictresolutiondialog.width, assuming " + DEFAULT_SIZE.width);
|
---|
65 | p.y = 0;
|
---|
66 | }
|
---|
67 | try {
|
---|
68 | d.height = Integer.parseInt(Main.pref.get("conflictresolutiondialog.height", Integer.toString(DEFAULT_SIZE.height)));
|
---|
69 | d.height = Math.max(0,d.height);
|
---|
70 | } catch(Exception e) {
|
---|
71 | logger.warning("unexpected value for preference conflictresolutiondialog.height, assuming " + + DEFAULT_SIZE.height);
|
---|
72 | p.y = 0;
|
---|
73 | }
|
---|
74 |
|
---|
75 | setLocation(p);
|
---|
76 | setSize(d);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * remember position and size on screen in the preferences
|
---|
81 | *
|
---|
82 | */
|
---|
83 | protected void rememberPositionAndDimension() {
|
---|
84 | Point p = getLocation();
|
---|
85 | Main.pref.put("conflictresolutiondialog.x", Integer.toString(p.x));
|
---|
86 | Main.pref.put("conflictresolutiondialog.y", Integer.toString(p.y));
|
---|
87 |
|
---|
88 | Dimension d = getSize();
|
---|
89 | Main.pref.put("conflictresolutiondialog.width", Integer.toString(d.width));
|
---|
90 | Main.pref.put("conflictresolutiondialog.height", Integer.toString(d.height));
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public void setVisible(boolean isVisible) {
|
---|
96 | if (isVisible){
|
---|
97 | restorePositionAndDimension();
|
---|
98 | } else {
|
---|
99 | rememberPositionAndDimension();
|
---|
100 | }
|
---|
101 | super.setVisible(isVisible);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * builds the sub panel with the control buttons
|
---|
106 | *
|
---|
107 | * @return the panel
|
---|
108 | */
|
---|
109 | protected JPanel buildButtonRow() {
|
---|
110 | JPanel pnl = new JPanel();
|
---|
111 | pnl.setLayout(new FlowLayout(FlowLayout.RIGHT));
|
---|
112 |
|
---|
113 | JButton btn = new JButton(new CancelAction());
|
---|
114 | btn.setName("button.cancel");
|
---|
115 | pnl.add(btn);
|
---|
116 |
|
---|
117 | btn = new JButton(new ApplyResolutionAction());
|
---|
118 | btn.setName("button.apply");
|
---|
119 | pnl.add(btn);
|
---|
120 |
|
---|
121 | pnl.setBorder(BorderFactory.createLoweredBevelBorder());
|
---|
122 | return pnl;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * builds the GUI
|
---|
127 | */
|
---|
128 | protected void build() {
|
---|
129 | setTitle(tr("Resolve conflicts"));
|
---|
130 | getContentPane().setLayout(new BorderLayout());
|
---|
131 |
|
---|
132 | resolver = new ConflictResolver();
|
---|
133 | resolver.setName("panel.conflictresolver");
|
---|
134 | getContentPane().add(resolver, BorderLayout.CENTER);
|
---|
135 | getContentPane().add(buildButtonRow(), BorderLayout.SOUTH);
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | public ConflictResolutionDialog(Component parent) {
|
---|
140 | super(JOptionPane.getFrameForComponent(parent), true /* modal */);
|
---|
141 | build();
|
---|
142 | }
|
---|
143 |
|
---|
144 | public ConflictResolver getConflictResolver() {
|
---|
145 | return resolver;
|
---|
146 | }
|
---|
147 |
|
---|
148 | class CancelAction extends AbstractAction {
|
---|
149 | public CancelAction() {
|
---|
150 | putValue(Action.SHORT_DESCRIPTION, tr("Cancel conflict resolution and close the dialog"));
|
---|
151 | putValue(Action.NAME, tr("Cancel"));
|
---|
152 | putValue(Action.SMALL_ICON, ImageProvider.get("", "cancel"));
|
---|
153 | setEnabled(true);
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | public void actionPerformed(ActionEvent arg0) {
|
---|
158 | setVisible(false);
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | class ApplyResolutionAction extends AbstractAction {
|
---|
163 | public ApplyResolutionAction() {
|
---|
164 | putValue(Action.SHORT_DESCRIPTION, tr("Apply resolved conflicts and close the dialog"));
|
---|
165 | putValue(Action.NAME, tr("Apply Resolution"));
|
---|
166 | putValue(Action.SMALL_ICON, ImageProvider.get("dialogs", "conflict"));
|
---|
167 | setEnabled(true);
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void actionPerformed(ActionEvent arg0) {
|
---|
171 | Command cmd = resolver.buildResolveCommand();
|
---|
172 | Main.main.undoRedo.add(cmd);
|
---|
173 | setVisible(false);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|