1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.tags;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.FlowLayout;
|
---|
9 | import java.awt.GridBagConstraints;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.Insets;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.FocusAdapter;
|
---|
14 | import java.awt.event.FocusEvent;
|
---|
15 | import java.util.Collection;
|
---|
16 |
|
---|
17 | import javax.swing.AbstractAction;
|
---|
18 | import javax.swing.AbstractButton;
|
---|
19 | import javax.swing.BoxLayout;
|
---|
20 | import javax.swing.ButtonModel;
|
---|
21 | import javax.swing.JButton;
|
---|
22 | import javax.swing.JCheckBox;
|
---|
23 | import javax.swing.JLabel;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 | import javax.swing.JScrollPane;
|
---|
26 | import javax.swing.UIManager;
|
---|
27 | import javax.swing.event.ChangeEvent;
|
---|
28 | import javax.swing.event.ChangeListener;
|
---|
29 |
|
---|
30 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
31 | import org.openstreetmap.josm.command.Command;
|
---|
32 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
33 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
34 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
35 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
36 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
37 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
38 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
39 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
40 |
|
---|
41 | public class RelationMemberConflictResolver extends JPanel {
|
---|
42 |
|
---|
43 | private final AutoCompletingTextField tfRole = new AutoCompletingTextField(10);
|
---|
44 | private final AutoCompletingTextField tfKey = new AutoCompletingTextField(10);
|
---|
45 | private final AutoCompletingTextField tfValue = new AutoCompletingTextField(10);
|
---|
46 | private JCheckBox cbTagRelations;
|
---|
47 | private final RelationMemberConflictResolverModel model;
|
---|
48 | private final RelationMemberConflictResolverTable tblResolver;
|
---|
49 | private final JMultilineLabel lblHeader = new JMultilineLabel("");
|
---|
50 |
|
---|
51 | protected final void build() {
|
---|
52 | setLayout(new GridBagLayout());
|
---|
53 | final JPanel pnl = new JPanel(new BorderLayout());
|
---|
54 | pnl.add(lblHeader);
|
---|
55 | GridBagConstraints gc = new GridBagConstraints();
|
---|
56 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
57 | gc.weighty = 0.0;
|
---|
58 | gc.weightx = 1.0;
|
---|
59 | gc.insets = new Insets(5, 5, 5, 5);
|
---|
60 | add(pnl, gc);
|
---|
61 |
|
---|
62 | gc.gridy = 1;
|
---|
63 | gc.weighty = 1.0;
|
---|
64 | gc.fill = GridBagConstraints.BOTH;
|
---|
65 | gc.insets = new Insets(0, 0, 0, 0);
|
---|
66 | add(new JScrollPane(tblResolver), gc);
|
---|
67 |
|
---|
68 | final JPanel pnl2 = new JPanel();
|
---|
69 | pnl2.setLayout(new BoxLayout(pnl2, BoxLayout.Y_AXIS));
|
---|
70 | pnl2.add(buildRoleEditingPanel());
|
---|
71 | pnl2.add(buildTagRelationsPanel());
|
---|
72 | gc.gridy = 2;
|
---|
73 | gc.weighty = 0.0;
|
---|
74 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
75 | add(pnl2, gc);
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected JPanel buildRoleEditingPanel() {
|
---|
79 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
80 | pnl.add(new JLabel(tr("Role:")));
|
---|
81 | pnl.add(tfRole);
|
---|
82 | tfRole.setToolTipText(tr("Enter a role for all relation memberships"));
|
---|
83 | pnl.add(new JButton(new ApplyRoleAction()));
|
---|
84 | tfRole.addActionListener(new ApplyRoleAction());
|
---|
85 | tfRole.addFocusListener(
|
---|
86 | new FocusAdapter() {
|
---|
87 | @Override
|
---|
88 | public void focusGained(FocusEvent e) {
|
---|
89 | tfRole.selectAll();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | );
|
---|
93 | return pnl;
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected JPanel buildTagRelationsPanel() {
|
---|
97 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
98 | cbTagRelations = new JCheckBox(tr("Tag modified relations with "));
|
---|
99 | cbTagRelations.addChangeListener(new ToggleTagRelationsAction());
|
---|
100 | cbTagRelations.setToolTipText(
|
---|
101 | tr("<html>Select to enable entering a tag which will be applied<br>"
|
---|
102 | + "to all modified relations.</html>"));
|
---|
103 | pnl.add(cbTagRelations);
|
---|
104 | pnl.add(new JLabel(trc("tag", "Key:")));
|
---|
105 | pnl.add(tfKey);
|
---|
106 | tfKey.setToolTipText(tr("<html>Enter a tag key, e.g. <strong><tt>fixme</tt></strong></html>"));
|
---|
107 | pnl.add(new JLabel(tr("Value:")));
|
---|
108 | pnl.add(tfValue);
|
---|
109 | tfValue.setToolTipText(tr("<html>Enter a tag value, e.g. <strong><tt>check members</tt></strong></html>"));
|
---|
110 | cbTagRelations.setSelected(false);
|
---|
111 | tfKey.setEnabled(false);
|
---|
112 | tfValue.setEnabled(false);
|
---|
113 | return pnl;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Constructs a new {@code RelationMemberConflictResolver}.
|
---|
118 | * @param model model managing a list of conflicting relation members
|
---|
119 | * @since 7661
|
---|
120 | */
|
---|
121 | public RelationMemberConflictResolver(RelationMemberConflictResolverModel model) {
|
---|
122 | this.model = model;
|
---|
123 | this.tblResolver = new RelationMemberConflictResolverTable(model);
|
---|
124 | build();
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Initializes for way combining.
|
---|
129 | */
|
---|
130 | public void initForWayCombining() {
|
---|
131 | lblHeader.setText(tr("<html>The combined ways are members in one or more relations. "
|
---|
132 | + "Please decide whether you want to <strong>keep</strong> these memberships "
|
---|
133 | + "for the combined way or whether you want to <strong>remove</strong> them.<br>"
|
---|
134 | + "The default is to <strong>keep</strong> the first way and <strong>remove</strong> "
|
---|
135 | + "the other ways that are members of the same relation: the combined way will "
|
---|
136 | + "take the place of the original way in the relation."
|
---|
137 | + "</html>"));
|
---|
138 | invalidate();
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Initializes for node merging.
|
---|
143 | */
|
---|
144 | public void initForNodeMerging() {
|
---|
145 | lblHeader.setText(tr("<html>The merged nodes are members in one or more relations. "
|
---|
146 | + "Please decide whether you want to <strong>keep</strong> these memberships "
|
---|
147 | + "for the target node or whether you want to <strong>remove</strong> them.<br>"
|
---|
148 | + "The default is to <strong>keep</strong> the first node and <strong>remove</strong> "
|
---|
149 | + "the other nodes that are members of the same relation: the target node will "
|
---|
150 | + "take the place of the original node in the relation."
|
---|
151 | + "</html>"));
|
---|
152 | invalidate();
|
---|
153 | }
|
---|
154 |
|
---|
155 | class ApplyRoleAction extends AbstractAction {
|
---|
156 | ApplyRoleAction() {
|
---|
157 | putValue(NAME, tr("Apply"));
|
---|
158 | new ImageProvider("ok").getResource().attachImageIcon(this);
|
---|
159 | putValue(SHORT_DESCRIPTION, tr("Apply this role to all members"));
|
---|
160 | }
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public void actionPerformed(ActionEvent e) {
|
---|
164 | model.applyRole(tfRole.getText());
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | class ToggleTagRelationsAction implements ChangeListener {
|
---|
169 | @Override
|
---|
170 | public void stateChanged(ChangeEvent e) {
|
---|
171 | ButtonModel buttonModel = ((AbstractButton) e.getSource()).getModel();
|
---|
172 | tfKey.setEnabled(buttonModel.isSelected());
|
---|
173 | tfValue.setEnabled(buttonModel.isSelected());
|
---|
174 | tfKey.setBackground(buttonModel.isSelected() ? UIManager.getColor("TextField.background") : UIManager
|
---|
175 | .getColor("Panel.background"));
|
---|
176 | tfValue.setBackground(buttonModel.isSelected() ? UIManager.getColor("TextField.background") : UIManager
|
---|
177 | .getColor("Panel.background"));
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | public RelationMemberConflictResolverModel getModel() {
|
---|
182 | return model;
|
---|
183 | }
|
---|
184 |
|
---|
185 | public Command buildTagApplyCommands(Collection<? extends OsmPrimitive> primitives) {
|
---|
186 | if (!cbTagRelations.isSelected())
|
---|
187 | return null;
|
---|
188 | if (tfKey.getText().trim().isEmpty())
|
---|
189 | return null;
|
---|
190 | if (tfValue.getText().trim().isEmpty())
|
---|
191 | return null;
|
---|
192 | if (primitives == null || primitives.isEmpty())
|
---|
193 | return null;
|
---|
194 | return new ChangePropertyCommand(primitives, Tag.removeWhiteSpaces(tfKey.getText()), Tag.removeWhiteSpaces(tfValue.getText()));
|
---|
195 | }
|
---|
196 |
|
---|
197 | public void prepareForEditing() {
|
---|
198 | AutoCompletionList acList = new AutoCompletionList();
|
---|
199 | OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
|
---|
200 | if (editLayer != null) {
|
---|
201 | editLayer.data.getAutoCompletionManager().populateWithMemberRoles(acList);
|
---|
202 | }
|
---|
203 | tfRole.setAutoCompletionList(acList);
|
---|
204 | AutoCompletingTextField editor = (AutoCompletingTextField) tblResolver.getColumnModel().getColumn(2).getCellEditor();
|
---|
205 | if (editor != null) {
|
---|
206 | editor.setAutoCompletionList(acList);
|
---|
207 | }
|
---|
208 | AutoCompletionList acList2 = new AutoCompletionList();
|
---|
209 | if (editLayer != null) {
|
---|
210 | editLayer.data.getAutoCompletionManager().populateWithKeys(acList2);
|
---|
211 | }
|
---|
212 | tfKey.setAutoCompletionList(acList2);
|
---|
213 | }
|
---|
214 | }
|
---|