1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Dialog;
|
---|
8 | import java.awt.FlowLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.MouseAdapter;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.util.ArrayList;
|
---|
14 |
|
---|
15 | import javax.swing.AbstractAction;
|
---|
16 | import javax.swing.JCheckBox;
|
---|
17 | import javax.swing.JDialog;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JList;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.ListSelectionModel;
|
---|
23 | import javax.swing.SwingUtilities;
|
---|
24 | import javax.swing.event.ListDataEvent;
|
---|
25 | import javax.swing.event.ListDataListener;
|
---|
26 | import javax.swing.event.ListSelectionEvent;
|
---|
27 | import javax.swing.event.ListSelectionListener;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.Main;
|
---|
30 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
31 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
32 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
33 | import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
|
---|
34 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
35 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
36 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
37 | import org.openstreetmap.josm.gui.SideButton;
|
---|
38 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
39 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
40 | import org.openstreetmap.josm.io.OsmApi;
|
---|
41 | import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
|
---|
42 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
43 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
44 | import org.xml.sax.SAXException;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * This is browser for a list of relations which refer to another relations
|
---|
48 | *
|
---|
49 | *
|
---|
50 | */
|
---|
51 | public class ReferringRelationsBrowser extends JPanel {
|
---|
52 |
|
---|
53 | /** the list of relations */
|
---|
54 | private JList referrers;
|
---|
55 | private ReferringRelationsBrowserModel model;
|
---|
56 | private OsmDataLayer layer;
|
---|
57 | private JCheckBox cbReadFull;
|
---|
58 | private EditAction editAction;
|
---|
59 | private final GenericRelationEditor relationEditor;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * build the GUI
|
---|
63 | */
|
---|
64 | protected void build() {
|
---|
65 | setLayout(new BorderLayout());
|
---|
66 | referrers = new JList(model);
|
---|
67 | referrers.setCellRenderer(new OsmPrimitivRenderer());
|
---|
68 | add(referrers, BorderLayout.CENTER);
|
---|
69 | referrers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
70 | referrers.addMouseListener(new DblClickMouseAdapter());
|
---|
71 |
|
---|
72 | JPanel pnl = new JPanel();
|
---|
73 | pnl.setLayout(new FlowLayout(FlowLayout.LEFT));
|
---|
74 |
|
---|
75 | ReloadAction reloadAction = new ReloadAction();
|
---|
76 | referrers.getModel().addListDataListener(reloadAction);
|
---|
77 | pnl.add(new SideButton(reloadAction));
|
---|
78 | pnl.add(new JLabel(tr("including immediate children of parent relations")));
|
---|
79 | pnl.add(cbReadFull = new JCheckBox());
|
---|
80 |
|
---|
81 | editAction = new EditAction();
|
---|
82 | referrers.getSelectionModel().addListSelectionListener(editAction);
|
---|
83 | pnl.add(new SideButton(editAction));
|
---|
84 | add(pnl, BorderLayout.SOUTH);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public ReferringRelationsBrowser(OsmDataLayer layer, ReferringRelationsBrowserModel model, GenericRelationEditor relationEditor) {
|
---|
88 | this.relationEditor = relationEditor;
|
---|
89 | this.model = model;
|
---|
90 | this.layer = layer;
|
---|
91 | build();
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void init() {
|
---|
95 | model.populate(getLayer().data);
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected OsmDataLayer getLayer() {
|
---|
99 | return layer;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Action for loading the parent relations of a relation
|
---|
104 | *
|
---|
105 | */
|
---|
106 | class ReloadAction extends AbstractAction implements ListDataListener {
|
---|
107 | public ReloadAction() {
|
---|
108 | putValue(SHORT_DESCRIPTION, tr("Load parent relations"));
|
---|
109 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "refresh"));
|
---|
110 | putValue(NAME, tr("Reload"));
|
---|
111 | refreshEnabled();
|
---|
112 | }
|
---|
113 |
|
---|
114 | protected void refreshEnabled() {
|
---|
115 | setEnabled(model.canReload());
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void actionPerformed(ActionEvent e) {
|
---|
119 | boolean full = cbReadFull.isSelected();
|
---|
120 | final ParentRelationLoadingTask task = new ParentRelationLoadingTask(
|
---|
121 | model.getRelation(),
|
---|
122 | getLayer(),
|
---|
123 | full,
|
---|
124 | new PleaseWaitProgressMonitor()
|
---|
125 | );
|
---|
126 | task.setContinuation(
|
---|
127 | new Runnable() {
|
---|
128 | public void run() {
|
---|
129 | if (task.isCancelled() || task.hasError())
|
---|
130 | return;
|
---|
131 | model.populate(task.getParents());
|
---|
132 | }
|
---|
133 | }
|
---|
134 | );
|
---|
135 | Main.worker.submit(task);
|
---|
136 | }
|
---|
137 |
|
---|
138 | public void contentsChanged(ListDataEvent e) {
|
---|
139 | refreshEnabled();
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void intervalAdded(ListDataEvent e) {
|
---|
143 | refreshEnabled();
|
---|
144 | }
|
---|
145 |
|
---|
146 | public void intervalRemoved(ListDataEvent e) {
|
---|
147 | refreshEnabled();
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Action for editing the currently selected relation
|
---|
153 | *
|
---|
154 | */
|
---|
155 | class EditAction extends AbstractAction implements ListSelectionListener {
|
---|
156 | public EditAction() {
|
---|
157 | putValue(SHORT_DESCRIPTION, tr("Edit the currently selected relation"));
|
---|
158 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
|
---|
159 | putValue(NAME, tr("Edit"));
|
---|
160 | refreshEnabled();
|
---|
161 | }
|
---|
162 |
|
---|
163 | protected void refreshEnabled() {
|
---|
164 | setEnabled(referrers.getSelectionModel().getMinSelectionIndex() >=0);
|
---|
165 | }
|
---|
166 |
|
---|
167 | public void actionPerformed(ActionEvent e) {
|
---|
168 | run();
|
---|
169 | }
|
---|
170 |
|
---|
171 | public void run() {
|
---|
172 | int idx = referrers.getSelectedIndex();
|
---|
173 | if (idx < 0) return;
|
---|
174 | Relation r = model.get(idx);
|
---|
175 | if (r == null) return;
|
---|
176 | RelationEditor editor = RelationEditor.getEditor(getLayer(), r, null);
|
---|
177 | editor.setVisible(true);
|
---|
178 | }
|
---|
179 |
|
---|
180 | public void valueChanged(ListSelectionEvent e) {
|
---|
181 | refreshEnabled();
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | class DblClickMouseAdapter extends MouseAdapter {
|
---|
186 | @Override
|
---|
187 | public void mouseClicked(MouseEvent e) {
|
---|
188 | if (e.getClickCount() == 2) {
|
---|
189 | editAction.run();
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|