1 | package org.openstreetmap.josm.gui.dialogs;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 | import static org.xnap.commons.i18n.I18n.marktr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.GridLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.MouseAdapter;
|
---|
12 | import java.awt.event.MouseEvent;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.LinkedList;
|
---|
17 |
|
---|
18 | import javax.swing.DefaultListModel;
|
---|
19 | import javax.swing.JButton;
|
---|
20 | import javax.swing.JList;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JScrollPane;
|
---|
24 | import javax.swing.ListSelectionModel;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
28 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
29 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
30 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
31 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
32 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
33 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * A dialog showing all known relations, with buttons to add, edit, and
|
---|
37 | * delete them.
|
---|
38 | *
|
---|
39 | * We don't have such dialogs for nodes, segments, and ways, becaus those
|
---|
40 | * objects are visible on the map and can be selected there. Relations are not.
|
---|
41 | *
|
---|
42 | * @author Frederik Ramm <frederik@remote.org>
|
---|
43 | */
|
---|
44 | public class RelationListDialog extends ToggleDialog {
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * The selection's list data.
|
---|
48 | */
|
---|
49 | private final DefaultListModel list = new DefaultListModel();
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * The display list.
|
---|
53 | */
|
---|
54 | private JList displaylist = new JList(list);
|
---|
55 |
|
---|
56 | public RelationListDialog() {
|
---|
57 | super(tr("Relations"), "relationlist", tr("Open a list of all relations."), KeyEvent.VK_N, 150);
|
---|
58 | displaylist.setCellRenderer(new OsmPrimitivRenderer());
|
---|
59 | displaylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
60 | displaylist.addMouseListener(new MouseAdapter(){
|
---|
61 | @Override public void mouseClicked(MouseEvent e) {
|
---|
62 | if (e.getClickCount() < 2)
|
---|
63 | return;
|
---|
64 | Relation toEdit = (Relation) displaylist.getSelectedValue();
|
---|
65 | if (toEdit != null)
|
---|
66 | new RelationEditor(toEdit).setVisible(true);
|
---|
67 | }
|
---|
68 | });
|
---|
69 |
|
---|
70 | add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
---|
71 |
|
---|
72 | JPanel buttonPanel = new JPanel(new GridLayout(1,3));
|
---|
73 |
|
---|
74 | buttonPanel.add(createButton(marktr("Add Relation"), "addrelation", tr("Create a new relation"), KeyEvent.VK_A, new ActionListener() {
|
---|
75 | public void actionPerformed(ActionEvent e) {
|
---|
76 | // call relation editor with null argument to create new relation
|
---|
77 | new RelationEditor(null).setVisible(true);
|
---|
78 | }
|
---|
79 | }));
|
---|
80 |
|
---|
81 | buttonPanel.add(createButton(marktr("Edit"), "edit", tr( "Open an editor for the selected relation"), KeyEvent.VK_E, new ActionListener() {
|
---|
82 | public void actionPerformed(ActionEvent e) {
|
---|
83 | Relation toEdit = (Relation) displaylist.getSelectedValue();
|
---|
84 | if (toEdit != null)
|
---|
85 | new RelationEditor(toEdit).setVisible(true);
|
---|
86 | }
|
---|
87 | }));
|
---|
88 |
|
---|
89 | buttonPanel.add(createButton(marktr("Delete"), "delete", tr("Delete the selected relation"), KeyEvent.VK_D, new ActionListener() {
|
---|
90 | public void actionPerformed(ActionEvent e) {
|
---|
91 | Relation toDelete = (Relation) displaylist.getSelectedValue();
|
---|
92 | if (toDelete != null) {
|
---|
93 | Main.main.editLayer().add(new DeleteCommand(Collections.singleton(toDelete)));
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }));
|
---|
97 |
|
---|
98 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
99 |
|
---|
100 | /*
|
---|
101 | DataSet.dataListeners.add(new DataChangedListener() {
|
---|
102 | public void dataChanged() {
|
---|
103 | updateList();
|
---|
104 | repaint();
|
---|
105 | }
|
---|
106 | });
|
---|
107 | */
|
---|
108 | }
|
---|
109 |
|
---|
110 | private JButton createButton(String name, String imagename, String tooltip, int mnemonic, ActionListener actionListener) {
|
---|
111 | JButton b = new JButton(tr(name), ImageProvider.get("dialogs", imagename));
|
---|
112 | b.setActionCommand(name);
|
---|
113 | b.addActionListener(actionListener);
|
---|
114 | b.setToolTipText(tooltip);
|
---|
115 | b.setMnemonic(mnemonic);
|
---|
116 | b.putClientProperty("help", "Dialog/Properties/"+name);
|
---|
117 | return b;
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override public void setVisible(boolean b) {
|
---|
121 | super.setVisible(b);
|
---|
122 | if (b) updateList();
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void updateList() {
|
---|
126 | list.setSize(Main.ds.relations.size());
|
---|
127 | int i = 0;
|
---|
128 | for (Relation e : Main.ds.relations) {
|
---|
129 | list.setElementAt(e, i++);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | }
|
---|