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