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