001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.BorderLayout;
007import java.awt.Dimension;
008import java.awt.FlowLayout;
009import java.awt.event.ActionEvent;
010import java.awt.event.WindowAdapter;
011import java.awt.event.WindowEvent;
012import java.util.ArrayList;
013import java.util.Collection;
014import java.util.Collections;
015import java.util.Comparator;
016import java.util.List;
017
018import javax.swing.AbstractAction;
019import javax.swing.AbstractListModel;
020import javax.swing.Action;
021import javax.swing.BorderFactory;
022import javax.swing.JComponent;
023import javax.swing.JDialog;
024import javax.swing.JLabel;
025import javax.swing.JList;
026import javax.swing.JPanel;
027import javax.swing.JScrollPane;
028import javax.swing.JSplitPane;
029import javax.swing.KeyStroke;
030import javax.swing.ListSelectionModel;
031import javax.swing.event.ListSelectionEvent;
032import javax.swing.event.ListSelectionListener;
033
034import org.openstreetmap.josm.Main;
035import org.openstreetmap.josm.data.osm.OsmPrimitive;
036import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
037import org.openstreetmap.josm.gui.DefaultNameFormatter;
038import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
039import org.openstreetmap.josm.gui.SideButton;
040import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
041import org.openstreetmap.josm.gui.help.HelpUtil;
042import org.openstreetmap.josm.gui.util.GuiHelper;
043import org.openstreetmap.josm.tools.ImageProvider;
044import org.openstreetmap.josm.tools.WindowGeometry;
045
046/**
047 * This dialog can be used to select individual object for uploading.
048 *
049 * @since 2250
050 */
051public class UploadSelectionDialog extends JDialog {
052
053    private final OsmPrimitiveList lstSelectedPrimitives = new OsmPrimitiveList();
054    private final OsmPrimitiveList lstDeletedPrimitives = new OsmPrimitiveList();
055    private JSplitPane spLists;
056    private boolean canceled;
057    private SideButton btnContinue;
058
059    /**
060     * Constructs a new {@code UploadSelectionDialog}.
061     */
062    public UploadSelectionDialog() {
063        super(GuiHelper.getFrameForComponent(Main.parent), ModalityType.DOCUMENT_MODAL);
064        build();
065    }
066
067    protected JPanel buildSelectedPrimitivesPanel() {
068        JPanel pnl = new JPanel(new BorderLayout());
069        JLabel lbl = new JLabel(
070                tr("<html>Mark modified objects <strong>from the current selection</strong> to be uploaded to the server.</html>"));
071        lbl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
072        pnl.add(lbl, BorderLayout.NORTH);
073        pnl.add(new JScrollPane(lstSelectedPrimitives), BorderLayout.CENTER);
074        lbl.setLabelFor(lstSelectedPrimitives);
075        return pnl;
076    }
077
078    protected JPanel buildDeletedPrimitivesPanel() {
079        JPanel pnl = new JPanel(new BorderLayout());
080        JLabel lbl = new JLabel(tr("<html>Mark <strong>locally deleted objects</strong> to be deleted on the server.</html>"));
081        lbl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
082        pnl.add(lbl, BorderLayout.NORTH);
083        pnl.add(new JScrollPane(lstDeletedPrimitives), BorderLayout.CENTER);
084        lbl.setLabelFor(lstDeletedPrimitives);
085        return pnl;
086    }
087
088    protected JPanel buildButtonPanel() {
089        JPanel pnl = new JPanel(new FlowLayout());
090        ContinueAction continueAction = new ContinueAction();
091        btnContinue = new SideButton(continueAction);
092        pnl.add(btnContinue);
093        btnContinue.setFocusable(true);
094        lstDeletedPrimitives.getSelectionModel().addListSelectionListener(continueAction);
095        lstSelectedPrimitives.getSelectionModel().addListSelectionListener(continueAction);
096
097        pnl.add(new SideButton(new CancelAction()));
098        pnl.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/UploadSelection"))));
099        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            new ImageProvider("cancel").getResource().attachImageIcon(this);
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            new ImageProvider("upload").getResource().attachImageIcon(this);
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}