[3850] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io.remotecontrol;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Color;
|
---|
| 7 | import java.awt.Component;
|
---|
| 8 | import java.awt.Font;
|
---|
| 9 | import java.awt.GridBagLayout;
|
---|
| 10 | import java.awt.event.ActionEvent;
|
---|
[5706] | 11 | import java.awt.event.KeyEvent;
|
---|
[5845] | 12 | import java.awt.event.MouseEvent;
|
---|
[3850] | 13 | import java.util.Collection;
|
---|
[5845] | 14 | import java.util.HashMap;
|
---|
[5706] | 15 | import javax.swing.AbstractAction;
|
---|
[3850] | 16 |
|
---|
| 17 | import javax.swing.JPanel;
|
---|
| 18 | import javax.swing.JTable;
|
---|
[5706] | 19 | import javax.swing.KeyStroke;
|
---|
[5845] | 20 | import javax.swing.event.CellEditorListener;
|
---|
| 21 | import javax.swing.event.ChangeEvent;
|
---|
[3850] | 22 | import javax.swing.table.DefaultTableModel;
|
---|
[5844] | 23 | import javax.swing.table.TableCellEditor;
|
---|
[3850] | 24 | import javax.swing.table.TableCellRenderer;
|
---|
| 25 | import javax.swing.table.TableModel;
|
---|
| 26 |
|
---|
| 27 | import org.openstreetmap.josm.Main;
|
---|
| 28 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
| 29 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
| 30 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 31 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 32 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[5784] | 33 | import org.openstreetmap.josm.gui.util.TableHelper;
|
---|
[3850] | 34 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | *
|
---|
| 38 | * @author master
|
---|
| 39 | *
|
---|
| 40 | * Dialog to add tags as part of the remotecontrol
|
---|
| 41 | * Existing Keys get grey color and unchecked selectboxes so they will not overwrite the old Key-Value-Pairs by default.
|
---|
| 42 | * You can choose the tags you want to add by selectboxes. You can edit the tags before you apply them.
|
---|
| 43 | *
|
---|
| 44 | */
|
---|
| 45 | public class AddTagsDialog extends ExtendedDialog implements SelectionChangedListener {
|
---|
| 46 |
|
---|
| 47 |
|
---|
[3851] | 48 | private final JTable propertyTable;
|
---|
| 49 | private Collection<? extends OsmPrimitive> sel;
|
---|
[5706] | 50 | int[] count;
|
---|
[3850] | 51 |
|
---|
[5845] | 52 | /**
|
---|
| 53 | * Class for displaying "delete from ... objects" in the table
|
---|
| 54 | */
|
---|
[5706] | 55 | static class DeleteTagMarker {
|
---|
| 56 | int num;
|
---|
| 57 | public DeleteTagMarker(int num) {
|
---|
| 58 | this.num = num;
|
---|
| 59 | }
|
---|
| 60 | public String toString() {
|
---|
| 61 | return tr("<delete from {0} objects>", num);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[5845] | 65 | /**
|
---|
| 66 | * Class for displaying list of existing tag values in the table
|
---|
| 67 | */
|
---|
| 68 | static class ExistingValues {
|
---|
| 69 | String tag;
|
---|
| 70 | HashMap<String, Integer> valueCount;
|
---|
| 71 | public ExistingValues(String tag) {
|
---|
| 72 | this.tag=tag; valueCount=new HashMap<String, Integer>();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | int addValue(String val) {
|
---|
| 76 | Integer c = valueCount.get(val);
|
---|
| 77 | int r = c==null? 1 : (c.intValue()+1);
|
---|
| 78 | valueCount.put(val, r);
|
---|
| 79 | return r;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | @Override
|
---|
| 83 | public String toString() {
|
---|
| 84 | StringBuilder sb=new StringBuilder();
|
---|
| 85 | for (String k: valueCount.keySet()) {
|
---|
| 86 | if (sb.length()>0) sb.append(", ");
|
---|
| 87 | sb.append(k);
|
---|
| 88 | }
|
---|
| 89 | return sb.toString();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | private String getToolTip() {
|
---|
| 93 | StringBuilder sb=new StringBuilder();
|
---|
| 94 | sb.append("<html>");
|
---|
| 95 | sb.append(tr("Old values of"));
|
---|
| 96 | sb.append(" <b>");
|
---|
| 97 | sb.append(tag);
|
---|
| 98 | sb.append("</b><br/>");
|
---|
| 99 | for (String k: valueCount.keySet()) {
|
---|
| 100 | sb.append("<b>");
|
---|
| 101 | sb.append(valueCount.get(k));
|
---|
| 102 | sb.append(" x </b>");
|
---|
| 103 | sb.append(k);
|
---|
| 104 | sb.append("<br/>");
|
---|
| 105 | }
|
---|
| 106 | sb.append("</html>");
|
---|
| 107 | return sb.toString();
|
---|
[5706] | 108 |
|
---|
[5845] | 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[3851] | 112 | public AddTagsDialog(String[][] tags) {
|
---|
[5706] | 113 | super(Main.parent, tr("Add tags to selected objects"), new String[] { tr("Add selected tags"), tr("Add all tags"), tr("Cancel")},
|
---|
[3851] | 114 | false,
|
---|
| 115 | true);
|
---|
[5706] | 116 | setToolTipTexts(new String[]{tr("Add checked tags to selected objects"), tr("Shift+Enter: Add all tags to selected objects"), ""});
|
---|
| 117 |
|
---|
[3851] | 118 | DataSet.addSelectionListener(this);
|
---|
[3850] | 119 |
|
---|
| 120 |
|
---|
[5845] | 121 | final DefaultTableModel tm = new DefaultTableModel(new String[] {tr("Assume"), tr("Key"), tr("Value"), tr("Existing values")}, tags.length) {
|
---|
| 122 | final Class<?> types[] = {Boolean.class, String.class, Object.class, ExistingValues.class};
|
---|
[3851] | 123 | @Override
|
---|
| 124 | public Class getColumnClass(int c) {
|
---|
[5844] | 125 | return types[c];
|
---|
[3851] | 126 | }
|
---|
[3850] | 127 |
|
---|
[3851] | 128 | };
|
---|
[3850] | 129 |
|
---|
[3851] | 130 | sel = Main.main.getCurrentDataSet().getSelected();
|
---|
[5706] | 131 | count = new int[tags.length];
|
---|
| 132 |
|
---|
[3851] | 133 | for (int i = 0; i<tags.length; i++) {
|
---|
[5706] | 134 | count[i] = 0;
|
---|
[3851] | 135 | String key = tags[i][0];
|
---|
[5845] | 136 | String value = tags[i][1], oldValue;
|
---|
[3851] | 137 | Boolean b = Boolean.TRUE;
|
---|
[5845] | 138 | ExistingValues old = new ExistingValues(key);
|
---|
[3851] | 139 | for (OsmPrimitive osm : sel) {
|
---|
[5845] | 140 | oldValue = osm.get(key);
|
---|
| 141 | if (oldValue!=null) {
|
---|
| 142 | old.addValue(oldValue);
|
---|
| 143 | if (!oldValue.equals(value)) {
|
---|
| 144 | b = Boolean.FALSE;
|
---|
| 145 | count[i]++;
|
---|
| 146 | }
|
---|
[3851] | 147 | }
|
---|
| 148 | }
|
---|
| 149 | tm.setValueAt(b, i, 0);
|
---|
| 150 | tm.setValueAt(tags[i][0], i, 1);
|
---|
[5706] | 151 | tm.setValueAt(tags[i][1].isEmpty() ? new DeleteTagMarker(count[i]) : tags[i][1], i, 2);
|
---|
[5845] | 152 | tm.setValueAt(old , i, 3);
|
---|
[3851] | 153 | }
|
---|
[5845] | 154 |
|
---|
[3851] | 155 | propertyTable = new JTable(tm) {
|
---|
[3850] | 156 |
|
---|
[3851] | 157 | private static final long serialVersionUID = 1L;
|
---|
[5844] | 158 | ///private final DefaultCellEditor textEditor = new DefaultCellEditor( new JTextField() );
|
---|
[3850] | 159 |
|
---|
[3851] | 160 | @Override
|
---|
| 161 | public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
|
---|
| 162 | Component c = super.prepareRenderer(renderer, row, column);
|
---|
[5706] | 163 | if (count[row]>0) {
|
---|
[3851] | 164 | c.setFont(c.getFont().deriveFont(Font.ITALIC));
|
---|
| 165 | c.setForeground(new Color(100, 100, 100));
|
---|
| 166 | } else {
|
---|
| 167 | c.setFont(c.getFont().deriveFont(Font.PLAIN));
|
---|
| 168 | c.setForeground(new Color(0, 0, 0));
|
---|
| 169 | }
|
---|
| 170 | return c;
|
---|
| 171 | }
|
---|
[5844] | 172 |
|
---|
| 173 | @Override
|
---|
| 174 | public TableCellEditor getCellEditor(int row, int column) {
|
---|
| 175 | Object value = getValueAt(row,column);
|
---|
| 176 | if (value instanceof DeleteTagMarker) return null;
|
---|
[5845] | 177 | if (value instanceof ExistingValues) return null;
|
---|
[5844] | 178 | return getDefaultEditor(value.getClass());
|
---|
| 179 | }
|
---|
[5845] | 180 |
|
---|
| 181 | @Override
|
---|
| 182 | public String getToolTipText(MouseEvent event) {
|
---|
| 183 | int r = rowAtPoint(event.getPoint());
|
---|
| 184 | int c = columnAtPoint(event.getPoint());
|
---|
| 185 | Object o = getValueAt(r, c);
|
---|
| 186 | if (c==1 || c==2) return o.toString();
|
---|
| 187 | if (c==3) return ((ExistingValues)o).getToolTip();
|
---|
| 188 | return tr("Enable the checkbox to accept the value");
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[3851] | 191 | };
|
---|
[5706] | 192 |
|
---|
[5784] | 193 | propertyTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
|
---|
[3851] | 194 | // a checkbox has a size of 15 px
|
---|
| 195 | propertyTable.getColumnModel().getColumn(0).setMaxWidth(15);
|
---|
[5845] | 196 | TableHelper.adjustColumnWidth(propertyTable, 1, 150);
|
---|
| 197 | TableHelper.adjustColumnWidth(propertyTable, 2, 400);
|
---|
| 198 | TableHelper.adjustColumnWidth(propertyTable, 3, 300);
|
---|
[3851] | 199 | // get edit results if the table looses the focus, for example if a user clicks "add tags"
|
---|
| 200 | propertyTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
|
---|
[5706] | 201 | propertyTable.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_MASK), "shiftenter");
|
---|
| 202 | propertyTable.getActionMap().put("shiftenter", new AbstractAction() {
|
---|
| 203 | @Override public void actionPerformed(ActionEvent e) {
|
---|
| 204 | buttonAction(1, e); // add all tags on Shift-Enter
|
---|
| 205 | }
|
---|
| 206 | });
|
---|
[5845] | 207 |
|
---|
[3851] | 208 | // set the content of this AddTagsDialog consisting of the tableHeader and the table itself.
|
---|
| 209 | JPanel tablePanel = new JPanel();
|
---|
| 210 | tablePanel.setLayout(new GridBagLayout());
|
---|
| 211 | tablePanel.add(propertyTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
| 212 | tablePanel.add(propertyTable, GBC.eol().fill(GBC.BOTH));
|
---|
| 213 | setContent(tablePanel);
|
---|
[5706] | 214 | setDefaultButton(2);
|
---|
[3851] | 215 | }
|
---|
[3850] | 216 |
|
---|
[3851] | 217 | /**
|
---|
| 218 | * This method looks for existing tags in the current selection and sets the corresponding boolean in the boolean array existing[]
|
---|
| 219 | */
|
---|
| 220 | private void findExistingTags() {
|
---|
| 221 | TableModel tm = propertyTable.getModel();
|
---|
| 222 | for (int i=0; i<tm.getRowCount(); i++) {
|
---|
| 223 | String key = (String)tm.getValueAt(i, 1);
|
---|
[5790] | 224 | String value = (String)tm.getValueAt(i, 1);
|
---|
[5706] | 225 | count[i] = 0;
|
---|
[3851] | 226 | for (OsmPrimitive osm : sel) {
|
---|
[5790] | 227 | if (osm.keySet().contains(key) && !osm.get(key).equals(value)) {
|
---|
[5706] | 228 | count[i]++;
|
---|
[3851] | 229 | break;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | propertyTable.repaint();
|
---|
| 234 | }
|
---|
[3850] | 235 |
|
---|
[3851] | 236 | /**
|
---|
| 237 | * If you click the "Add tags" button build a ChangePropertyCommand for every key that has a checked checkbox to apply the key value pair to all selected osm objects.
|
---|
| 238 | * You get a entry for every key in the command queue.
|
---|
| 239 | */
|
---|
| 240 | @Override
|
---|
| 241 | protected void buttonAction(int buttonIndex, ActionEvent evt) {
|
---|
[5790] | 242 | // if layer all layers were closed, ignore all actions
|
---|
| 243 | if (Main.main.getCurrentDataSet() != null && buttonIndex != 2) {
|
---|
[3851] | 244 | TableModel tm = propertyTable.getModel();
|
---|
| 245 | for (int i=0; i<tm.getRowCount(); i++) {
|
---|
[5706] | 246 | if (buttonIndex==1 || (Boolean)tm.getValueAt(i, 0)) {
|
---|
| 247 | String key =(String)tm.getValueAt(i, 1);
|
---|
| 248 | Object value = tm.getValueAt(i, 2);
|
---|
| 249 | Main.main.undoRedo.add(new ChangePropertyCommand(sel,
|
---|
| 250 | key, value instanceof String ? (String) value : ""));
|
---|
[3851] | 251 | }
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | setVisible(false);
|
---|
| 255 | }
|
---|
[3850] | 256 |
|
---|
[3851] | 257 | @Override
|
---|
| 258 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
| 259 | sel = newSelection;
|
---|
| 260 | findExistingTags();
|
---|
| 261 | }
|
---|
[3850] | 262 |
|
---|
| 263 | }
|
---|