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