[2512] | 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;
|
---|
[4619] | 22 | import javax.swing.JComponent;
|
---|
[2512] | 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;
|
---|
[4619] | 29 | import javax.swing.KeyStroke;
|
---|
[2512] | 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;
|
---|
[2947] | 40 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
| 41 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
[10035] | 42 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[2512] | 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 | *
|
---|
[8426] | 49 | * @since 2250
|
---|
[2512] | 50 | */
|
---|
| 51 | public class UploadSelectionDialog extends JDialog {
|
---|
| 52 |
|
---|
[10179] | 53 | private final OsmPrimitiveList lstSelectedPrimitives = new OsmPrimitiveList();
|
---|
| 54 | private final OsmPrimitiveList lstDeletedPrimitives = new OsmPrimitiveList();
|
---|
[2512] | 55 | private JSplitPane spLists;
|
---|
| 56 | private boolean canceled;
|
---|
| 57 | private SideButton btnContinue;
|
---|
| 58 |
|
---|
[8426] | 59 | /**
|
---|
| 60 | * Constructs a new {@code UploadSelectionDialog}.
|
---|
| 61 | */
|
---|
| 62 | public UploadSelectionDialog() {
|
---|
[10035] | 63 | super(GuiHelper.getFrameForComponent(Main.parent), ModalityType.DOCUMENT_MODAL);
|
---|
[8426] | 64 | build();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[2512] | 67 | protected JPanel buildSelectedPrimitivesPanel() {
|
---|
[9543] | 68 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
[8426] | 69 | JLabel lbl = new JLabel(
|
---|
| 70 | tr("<html>Mark modified objects <strong>from the current selection</strong> to be uploaded to the server.</html>"));
|
---|
[8510] | 71 | lbl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
[2512] | 72 | pnl.add(lbl, BorderLayout.NORTH);
|
---|
[10179] | 73 | pnl.add(new JScrollPane(lstSelectedPrimitives), BorderLayout.CENTER);
|
---|
[8426] | 74 | lbl.setLabelFor(lstSelectedPrimitives);
|
---|
[2512] | 75 | return pnl;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | protected JPanel buildDeletedPrimitivesPanel() {
|
---|
[9543] | 79 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
[2512] | 80 | JLabel lbl = new JLabel(tr("<html>Mark <strong>locally deleted objects</strong> to be deleted on the server.</html>"));
|
---|
[8510] | 81 | lbl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
[2512] | 82 | pnl.add(lbl, BorderLayout.NORTH);
|
---|
[10179] | 83 | pnl.add(new JScrollPane(lstDeletedPrimitives), BorderLayout.CENTER);
|
---|
[8426] | 84 | lbl.setLabelFor(lstDeletedPrimitives);
|
---|
[2512] | 85 | return pnl;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | protected JPanel buildButtonPanel() {
|
---|
[9543] | 89 | JPanel pnl = new JPanel(new FlowLayout());
|
---|
[2512] | 90 | ContinueAction continueAction = new ContinueAction();
|
---|
[10179] | 91 | btnContinue = new SideButton(continueAction);
|
---|
| 92 | pnl.add(btnContinue);
|
---|
[2512] | 93 | btnContinue.setFocusable(true);
|
---|
| 94 | lstDeletedPrimitives.getSelectionModel().addListSelectionListener(continueAction);
|
---|
| 95 | lstSelectedPrimitives.getSelectionModel().addListSelectionListener(continueAction);
|
---|
[2947] | 96 |
|
---|
| 97 | pnl.add(new SideButton(new CancelAction()));
|
---|
| 98 | pnl.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/UploadSelection"))));
|
---|
[2512] | 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"));
|
---|
[2947] | 124 | HelpUtil.setHelpContext(getRootPane(), HelpUtil.ht("/Dialog/UploadSelection"));
|
---|
[2512] | 125 | }
|
---|
| 126 |
|
---|
| 127 | public void populate(Collection<OsmPrimitive> selected, Collection<OsmPrimitive> deleted) {
|
---|
| 128 | if (selected != null) {
|
---|
[7005] | 129 | lstSelectedPrimitives.getOsmPrimitiveListModel().setPrimitives(new ArrayList<>(selected));
|
---|
[2512] | 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) {
|
---|
[7005] | 141 | lstDeletedPrimitives.getOsmPrimitiveListModel().setPrimitives(new ArrayList<>(deleted));
|
---|
[2512] | 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() {
|
---|
[7005] | 156 | List<OsmPrimitive> ret = new ArrayList<>();
|
---|
[2512] | 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,
|
---|
[8510] | 169 | new Dimension(200, 400)
|
---|
[2512] | 170 | )
|
---|
[2824] | 171 | ).applySafe(this);
|
---|
[5998] | 172 | } else if (isShowing()) { // Avoid IllegalComponentStateException like in #8775
|
---|
[2512] | 173 | new WindowGeometry(this).remember(getClass().getName() + ".geometry");
|
---|
| 174 | }
|
---|
| 175 | super.setVisible(visible);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[7001] | 178 | static class OsmPrimitiveList extends JList<OsmPrimitive> {
|
---|
[8836] | 179 | OsmPrimitiveList() {
|
---|
[8415] | 180 | this(new OsmPrimitiveListModel());
|
---|
[2512] | 181 | }
|
---|
| 182 |
|
---|
[8836] | 183 | OsmPrimitiveList(OsmPrimitiveListModel model) {
|
---|
[2512] | 184 | super(model);
|
---|
| 185 | init();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[10179] | 188 | protected void init() {
|
---|
| 189 | setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
| 190 | setCellRenderer(new OsmPrimitivRenderer());
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[2512] | 193 | public OsmPrimitiveListModel getOsmPrimitiveListModel() {
|
---|
[8510] | 194 | return (OsmPrimitiveListModel) getModel();
|
---|
[2512] | 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[7001] | 198 | static class OsmPrimitiveListModel extends AbstractListModel<OsmPrimitive> {
|
---|
[8308] | 199 | private transient List<OsmPrimitive> data;
|
---|
[2512] | 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();
|
---|
[6084] | 208 | @Override
|
---|
[2512] | 209 | public int compare(OsmPrimitive o1, OsmPrimitive o2) {
|
---|
| 210 | int ret = OsmPrimitiveType.from(o1).compareTo(OsmPrimitiveType.from(o2));
|
---|
[10179] | 211 | if (ret != 0)
|
---|
| 212 | return ret;
|
---|
[2512] | 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) {
|
---|
[8510] | 223 | fireContentsChanged(this, 0, data.size());
|
---|
[2512] | 224 | } else {
|
---|
[8510] | 225 | fireContentsChanged(this, 0, 0);
|
---|
[2512] | 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[6084] | 229 | @Override
|
---|
[7001] | 230 | public OsmPrimitive getElementAt(int index) {
|
---|
[2512] | 231 | if (data == null)
|
---|
| 232 | return null;
|
---|
| 233 | return data.get(index);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[6084] | 236 | @Override
|
---|
[2512] | 237 | public int getSize() {
|
---|
| 238 | if (data == null)
|
---|
| 239 | return 0;
|
---|
| 240 | return data.size();
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[8444] | 243 | public List<OsmPrimitive> getPrimitives(int[] indices) {
|
---|
[2512] | 244 | if (indices == null || indices.length == 0)
|
---|
| 245 | return Collections.emptyList();
|
---|
[7005] | 246 | List<OsmPrimitive> ret = new ArrayList<>(indices.length);
|
---|
[2512] | 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 {
|
---|
[8836] | 258 | CancelAction() {
|
---|
[2512] | 259 | putValue(Action.SHORT_DESCRIPTION, tr("Cancel uploading"));
|
---|
| 260 | putValue(Action.NAME, tr("Cancel"));
|
---|
| 261 | putValue(Action.SMALL_ICON, ImageProvider.get("", "cancel"));
|
---|
[6070] | 262 | getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
---|
| 263 | .put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
|
---|
[4619] | 264 | getRootPane().getActionMap().put("ESCAPE", this);
|
---|
[2512] | 265 | setEnabled(true);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[6084] | 268 | @Override
|
---|
[2512] | 269 | public void actionPerformed(ActionEvent e) {
|
---|
| 270 | setCanceled(true);
|
---|
| 271 | setVisible(false);
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | class ContinueAction extends AbstractAction implements ListSelectionListener {
|
---|
[8836] | 276 | ContinueAction() {
|
---|
[2512] | 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 |
|
---|
[6084] | 283 | @Override
|
---|
[2512] | 284 | public void actionPerformed(ActionEvent e) {
|
---|
| 285 | setCanceled(false);
|
---|
| 286 | setVisible(false);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | protected void updateEnabledState() {
|
---|
[8510] | 290 | setEnabled(lstSelectedPrimitives.getSelectedIndex() >= 0
|
---|
[2512] | 291 | || lstDeletedPrimitives.getSelectedIndex() >= 0);
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[6084] | 294 | @Override
|
---|
[2512] | 295 | public void valueChanged(ListSelectionEvent e) {
|
---|
| 296 | updateEnabledState();
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 | }
|
---|