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