source: josm/trunk/src/org/openstreetmap/josm/gui/conflict/tags/RelationMemberConflictResolver.java@ 10369

Last change on this file since 10369 was 10369, checked in by stoecker, 8 years ago

see #9995 - patch by strump - improve HIDPI

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