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.JLabel;
|
---|
15 | import javax.swing.JList;
|
---|
16 | import javax.swing.JPanel;
|
---|
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(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(new JLabel(tr("including immediate children of parent relations")));
|
---|
64 | pnl.add(cbReadFull = new JCheckBox());
|
---|
65 |
|
---|
66 | editAction = new EditAction();
|
---|
67 | referrers.getSelectionModel().addListSelectionListener(editAction);
|
---|
68 | pnl.add(new SideButton(editAction));
|
---|
69 | add(pnl, BorderLayout.SOUTH);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public ReferringRelationsBrowser(OsmDataLayer layer, ReferringRelationsBrowserModel model, GenericRelationEditor relationEditor) {
|
---|
73 | this.relationEditor = relationEditor;
|
---|
74 | this.model = model;
|
---|
75 | this.layer = layer;
|
---|
76 | build();
|
---|
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 | public ReloadAction() {
|
---|
93 | putValue(SHORT_DESCRIPTION, tr("Load parent relations"));
|
---|
94 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "refresh"));
|
---|
95 | putValue(NAME, tr("Reload"));
|
---|
96 | refreshEnabled();
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected void refreshEnabled() {
|
---|
100 | setEnabled(model.canReload());
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void actionPerformed(ActionEvent e) {
|
---|
104 | boolean full = cbReadFull.isSelected();
|
---|
105 | final ParentRelationLoadingTask task = new ParentRelationLoadingTask(
|
---|
106 | model.getRelation(),
|
---|
107 | getLayer(),
|
---|
108 | full,
|
---|
109 | new PleaseWaitProgressMonitor(tr("Loading parent relations"))
|
---|
110 | );
|
---|
111 | task.setContinuation(
|
---|
112 | new Runnable() {
|
---|
113 | public void run() {
|
---|
114 | if (task.isCancelled() || task.hasError())
|
---|
115 | return;
|
---|
116 | model.populate(task.getParents());
|
---|
117 | }
|
---|
118 | }
|
---|
119 | );
|
---|
120 | Main.worker.submit(task);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void contentsChanged(ListDataEvent e) {
|
---|
124 | refreshEnabled();
|
---|
125 | }
|
---|
126 |
|
---|
127 | public void intervalAdded(ListDataEvent e) {
|
---|
128 | refreshEnabled();
|
---|
129 | }
|
---|
130 |
|
---|
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 | public EditAction() {
|
---|
142 | putValue(SHORT_DESCRIPTION, tr("Edit the currently selected relation"));
|
---|
143 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
|
---|
144 | putValue(NAME, tr("Edit"));
|
---|
145 | refreshEnabled();
|
---|
146 | }
|
---|
147 |
|
---|
148 | protected void refreshEnabled() {
|
---|
149 | setEnabled(referrers.getSelectionModel().getMinSelectionIndex() >=0);
|
---|
150 | }
|
---|
151 |
|
---|
152 | public void actionPerformed(ActionEvent e) {
|
---|
153 | run();
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void run() {
|
---|
157 | int idx = referrers.getSelectedIndex();
|
---|
158 | if (idx < 0) return;
|
---|
159 | Relation r = model.get(idx);
|
---|
160 | if (r == null) return;
|
---|
161 | RelationEditor editor = RelationEditor.getEditor(getLayer(), r, null);
|
---|
162 | editor.setVisible(true);
|
---|
163 | }
|
---|
164 |
|
---|
165 | public void valueChanged(ListSelectionEvent e) {
|
---|
166 | refreshEnabled();
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | class DblClickMouseAdapter extends MouseAdapter {
|
---|
171 | @Override
|
---|
172 | public void mouseClicked(MouseEvent e) {
|
---|
173 | if (e.getClickCount() == 2) {
|
---|
174 | editAction.run();
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|