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