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