1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.FlowLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.WindowAdapter;
|
---|
11 | import java.awt.event.WindowEvent;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.Collections;
|
---|
15 | import java.util.Comparator;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | import javax.swing.AbstractAction;
|
---|
19 | import javax.swing.AbstractListModel;
|
---|
20 | import javax.swing.Action;
|
---|
21 | import javax.swing.BorderFactory;
|
---|
22 | import javax.swing.JComponent;
|
---|
23 | import javax.swing.JDialog;
|
---|
24 | import javax.swing.JLabel;
|
---|
25 | import javax.swing.JList;
|
---|
26 | import javax.swing.JOptionPane;
|
---|
27 | import javax.swing.JPanel;
|
---|
28 | import javax.swing.JScrollPane;
|
---|
29 | import javax.swing.JSplitPane;
|
---|
30 | import javax.swing.KeyStroke;
|
---|
31 | import javax.swing.ListSelectionModel;
|
---|
32 | import javax.swing.event.ListSelectionEvent;
|
---|
33 | import javax.swing.event.ListSelectionListener;
|
---|
34 |
|
---|
35 | import org.openstreetmap.josm.Main;
|
---|
36 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
37 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
38 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
39 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
40 | import org.openstreetmap.josm.gui.SideButton;
|
---|
41 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
42 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
43 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
44 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * This dialog can be used to select individual object for uploading.
|
---|
48 | *
|
---|
49 | *
|
---|
50 | */
|
---|
51 | public class UploadSelectionDialog extends JDialog {
|
---|
52 |
|
---|
53 | private OsmPrimitiveList lstSelectedPrimitives;
|
---|
54 | private OsmPrimitiveList lstDeletedPrimitives;
|
---|
55 | private JSplitPane spLists;
|
---|
56 | private boolean canceled;
|
---|
57 | private SideButton btnContinue;
|
---|
58 |
|
---|
59 | protected JPanel buildSelectedPrimitivesPanel() {
|
---|
60 | JPanel pnl = new JPanel();
|
---|
61 | pnl.setLayout(new BorderLayout());
|
---|
62 | JLabel lbl = new JLabel(tr("<html>Mark modified objects <strong>from the current selection</strong> to be uploaded to the server.</html>"));
|
---|
63 | lbl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
64 | pnl.add(lbl, BorderLayout.NORTH);
|
---|
65 | pnl.add(new JScrollPane(lstSelectedPrimitives = new OsmPrimitiveList()), BorderLayout.CENTER);
|
---|
66 | return pnl;
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected JPanel buildDeletedPrimitivesPanel() {
|
---|
70 | JPanel pnl = new JPanel();
|
---|
71 | pnl.setLayout(new BorderLayout());
|
---|
72 | JLabel lbl = new JLabel(tr("<html>Mark <strong>locally deleted objects</strong> to be deleted on the server.</html>"));
|
---|
73 | lbl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
74 | pnl.add(lbl, BorderLayout.NORTH);
|
---|
75 | pnl.add(new JScrollPane(lstDeletedPrimitives = new OsmPrimitiveList()), BorderLayout.CENTER);
|
---|
76 | return pnl;
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected JPanel buildButtonPanel() {
|
---|
80 | JPanel pnl = new JPanel();
|
---|
81 | pnl.setLayout(new FlowLayout());
|
---|
82 | ContinueAction continueAction = new ContinueAction();
|
---|
83 | pnl.add(btnContinue = new SideButton(continueAction));
|
---|
84 | btnContinue.setFocusable(true);
|
---|
85 | lstDeletedPrimitives.getSelectionModel().addListSelectionListener(continueAction);
|
---|
86 | lstSelectedPrimitives.getSelectionModel().addListSelectionListener(continueAction);
|
---|
87 |
|
---|
88 | pnl.add(new SideButton(new CancelAction()));
|
---|
89 | pnl.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/UploadSelection"))));
|
---|
90 | return pnl;
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected void build() {
|
---|
94 | setLayout(new BorderLayout());
|
---|
95 | spLists = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
|
---|
96 | spLists.setTopComponent(buildSelectedPrimitivesPanel());
|
---|
97 | spLists.setBottomComponent(buildDeletedPrimitivesPanel());
|
---|
98 | add(spLists, BorderLayout.CENTER);
|
---|
99 | add(buildButtonPanel(), BorderLayout.SOUTH);
|
---|
100 | addWindowListener(
|
---|
101 | new WindowAdapter() {
|
---|
102 | @Override
|
---|
103 | public void windowOpened(WindowEvent e) {
|
---|
104 | spLists.setDividerLocation(0.5);
|
---|
105 | btnContinue.requestFocusInWindow();
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public void windowClosing(WindowEvent e) {
|
---|
110 | setCanceled(true);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | );
|
---|
114 | setTitle(tr("Select objects to upload"));
|
---|
115 | HelpUtil.setHelpContext(getRootPane(), HelpUtil.ht("/Dialog/UploadSelection"));
|
---|
116 | }
|
---|
117 |
|
---|
118 | public UploadSelectionDialog() {
|
---|
119 | super(JOptionPane.getFrameForComponent(Main.parent), ModalityType.DOCUMENT_MODAL);
|
---|
120 | build();
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void populate(Collection<OsmPrimitive> selected, Collection<OsmPrimitive> deleted) {
|
---|
124 | if (selected != null) {
|
---|
125 | lstSelectedPrimitives.getOsmPrimitiveListModel().setPrimitives(new ArrayList<>(selected));
|
---|
126 | if (!selected.isEmpty()) {
|
---|
127 | lstSelectedPrimitives.getSelectionModel().setSelectionInterval(0, selected.size()-1);
|
---|
128 | } else {
|
---|
129 | lstSelectedPrimitives.getSelectionModel().clearSelection();
|
---|
130 | }
|
---|
131 | } else {
|
---|
132 | lstSelectedPrimitives.getOsmPrimitiveListModel().setPrimitives(null);
|
---|
133 | lstSelectedPrimitives.getSelectionModel().clearSelection();
|
---|
134 | }
|
---|
135 |
|
---|
136 | if (deleted != null) {
|
---|
137 | lstDeletedPrimitives.getOsmPrimitiveListModel().setPrimitives(new ArrayList<>(deleted));
|
---|
138 | } else {
|
---|
139 | lstDeletedPrimitives.getOsmPrimitiveListModel().setPrimitives(null);
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public boolean isCanceled() {
|
---|
144 | return canceled;
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected void setCanceled(boolean canceled) {
|
---|
148 | this.canceled = canceled;
|
---|
149 | }
|
---|
150 |
|
---|
151 | public List<OsmPrimitive> getSelectedPrimitives() {
|
---|
152 | List<OsmPrimitive> ret = new ArrayList<>();
|
---|
153 | ret.addAll(lstSelectedPrimitives.getOsmPrimitiveListModel().getPrimitives(lstSelectedPrimitives.getSelectedIndices()));
|
---|
154 | ret.addAll(lstDeletedPrimitives.getOsmPrimitiveListModel().getPrimitives(lstDeletedPrimitives.getSelectedIndices()));
|
---|
155 | return ret;
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public void setVisible(boolean visible) {
|
---|
160 | if (visible) {
|
---|
161 | new WindowGeometry(
|
---|
162 | getClass().getName() + ".geometry",
|
---|
163 | WindowGeometry.centerInWindow(
|
---|
164 | Main.parent,
|
---|
165 | new Dimension(200,400)
|
---|
166 | )
|
---|
167 | ).applySafe(this);
|
---|
168 | } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
|
---|
169 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
170 | }
|
---|
171 | super.setVisible(visible);
|
---|
172 | }
|
---|
173 |
|
---|
174 | static class OsmPrimitiveList extends JList<OsmPrimitive> {
|
---|
175 | protected void init() {
|
---|
176 | setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
177 | setCellRenderer(new OsmPrimitivRenderer());
|
---|
178 | }
|
---|
179 |
|
---|
180 | public OsmPrimitiveList() {
|
---|
181 | super(new OsmPrimitiveListModel());
|
---|
182 | init();
|
---|
183 | }
|
---|
184 |
|
---|
185 | public OsmPrimitiveList(OsmPrimitiveListModel model) {
|
---|
186 | super(model);
|
---|
187 | init();
|
---|
188 | }
|
---|
189 |
|
---|
190 | public OsmPrimitiveListModel getOsmPrimitiveListModel() {
|
---|
191 | return (OsmPrimitiveListModel)getModel();
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | static class OsmPrimitiveListModel extends AbstractListModel<OsmPrimitive> {
|
---|
196 | private List<OsmPrimitive> data;
|
---|
197 |
|
---|
198 | public OsmPrimitiveListModel() {
|
---|
199 | }
|
---|
200 |
|
---|
201 | protected void sort() {
|
---|
202 | if (data == null)
|
---|
203 | return;
|
---|
204 | Collections.sort(
|
---|
205 | data,
|
---|
206 | new Comparator<OsmPrimitive>() {
|
---|
207 | private DefaultNameFormatter formatter = DefaultNameFormatter.getInstance();
|
---|
208 | @Override
|
---|
209 | public int compare(OsmPrimitive o1, OsmPrimitive o2) {
|
---|
210 | int ret = OsmPrimitiveType.from(o1).compareTo(OsmPrimitiveType.from(o2));
|
---|
211 | if (ret != 0) return ret;
|
---|
212 | return o1.getDisplayName(formatter).compareTo(o1.getDisplayName(formatter));
|
---|
213 | }
|
---|
214 | }
|
---|
215 | );
|
---|
216 | }
|
---|
217 |
|
---|
218 | public OsmPrimitiveListModel(List<OsmPrimitive> data) {
|
---|
219 | setPrimitives(data);
|
---|
220 | }
|
---|
221 |
|
---|
222 | public void setPrimitives(List<OsmPrimitive> data) {
|
---|
223 | this.data = data;
|
---|
224 | sort();
|
---|
225 | if (data != null) {
|
---|
226 | fireContentsChanged(this,0, data.size());
|
---|
227 | } else {
|
---|
228 | fireContentsChanged(this, 0,0);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | @Override
|
---|
233 | public OsmPrimitive getElementAt(int index) {
|
---|
234 | if (data == null)
|
---|
235 | return null;
|
---|
236 | return data.get(index);
|
---|
237 | }
|
---|
238 |
|
---|
239 | @Override
|
---|
240 | public int getSize() {
|
---|
241 | if (data == null)
|
---|
242 | return 0;
|
---|
243 | return data.size();
|
---|
244 | }
|
---|
245 |
|
---|
246 | public List<OsmPrimitive> getPrimitives(int [] indices) {
|
---|
247 | if (indices == null || indices.length == 0)
|
---|
248 | return Collections.emptyList();
|
---|
249 | List<OsmPrimitive> ret = new ArrayList<>(indices.length);
|
---|
250 | for (int i: indices) {
|
---|
251 | if (i < 0) {
|
---|
252 | continue;
|
---|
253 | }
|
---|
254 | ret.add(data.get(i));
|
---|
255 | }
|
---|
256 | return ret;
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | class CancelAction extends AbstractAction {
|
---|
261 | public CancelAction() {
|
---|
262 | putValue(Action.SHORT_DESCRIPTION, tr("Cancel uploading"));
|
---|
263 | putValue(Action.NAME, tr("Cancel"));
|
---|
264 | putValue(Action.SMALL_ICON, ImageProvider.get("", "cancel"));
|
---|
265 | getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
---|
266 | .put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
|
---|
267 | getRootPane().getActionMap().put("ESCAPE", this);
|
---|
268 | setEnabled(true);
|
---|
269 | }
|
---|
270 |
|
---|
271 | @Override
|
---|
272 | public void actionPerformed(ActionEvent e) {
|
---|
273 | setCanceled(true);
|
---|
274 | setVisible(false);
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | class ContinueAction extends AbstractAction implements ListSelectionListener {
|
---|
279 | public ContinueAction() {
|
---|
280 | putValue(Action.SHORT_DESCRIPTION, tr("Continue uploading"));
|
---|
281 | putValue(Action.NAME, tr("Continue"));
|
---|
282 | putValue(Action.SMALL_ICON, ImageProvider.get("", "upload"));
|
---|
283 | updateEnabledState();
|
---|
284 | }
|
---|
285 |
|
---|
286 | @Override
|
---|
287 | public void actionPerformed(ActionEvent e) {
|
---|
288 | setCanceled(false);
|
---|
289 | setVisible(false);
|
---|
290 | }
|
---|
291 |
|
---|
292 | protected void updateEnabledState() {
|
---|
293 | setEnabled(lstSelectedPrimitives.getSelectedIndex() >=0
|
---|
294 | || lstDeletedPrimitives.getSelectedIndex() >= 0);
|
---|
295 | }
|
---|
296 |
|
---|
297 | @Override
|
---|
298 | public void valueChanged(ListSelectionEvent e) {
|
---|
299 | updateEnabledState();
|
---|
300 | }
|
---|
301 | }
|
---|
302 | }
|
---|