Changeset 26002 in osm for applications
- Timestamp:
- 2011-05-16T20:18:28+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/OhePlugin.java
r23192 r26002 2 2 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 import static org.openstreetmap.josm.tools.I18n.trn; 5 6 import java.awt.Component; 7 import java.awt.Font; 5 8 import java.awt.GridBagLayout; 6 9 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener;8 10 import java.awt.event.KeyEvent; 9 11 import java.util.Collection; 12 import java.util.HashMap; 10 13 import java.util.Map; 14 import java.util.Map.Entry; 15 import java.util.TreeMap; 11 16 import java.util.Vector; 12 17 import java.util.regex.Matcher; 13 18 import java.util.regex.Pattern; 14 19 20 import javax.swing.AbstractAction; 15 21 import javax.swing.ButtonGroup; 16 22 import javax.swing.JDialog; 23 import javax.swing.JLabel; 17 24 import javax.swing.JOptionPane; 18 25 import javax.swing.JPanel; … … 22 29 import javax.swing.JTextField; 23 30 import javax.swing.ListSelectionModel; 31 import javax.swing.table.DefaultTableCellRenderer; 32 import javax.swing.table.DefaultTableModel; 24 33 25 34 import org.openstreetmap.josm.Main; … … 37 46 38 47 public class OhePlugin extends Plugin { 39 40 // Strings for choosing which key of an object with given tags should be 41 // edited 42 // the order is referencing the preference of the keys 43 // String[] -> {key, value, to-editing-key} key and value can contain regexp 44 private final String[][] TAG_EDIT_STRINGS = new String[][] { 45 { "opening_hours", ".*", "opening_hours" }, 48 /** 49 * Strings for choosing which key of an object with given tags should be 50 * edited, the order is referencing the preference of the keys, String[] -> 51 * {key, value, key-to-edit} key and value can contain regular expressions 52 */ 53 private final String[][] TAG_EDIT_STRINGS = new String[][] { { "opening_hours", ".*", "opening_hours" }, 46 54 { "collection_times", ".*", "collection_times" }, 47 { "collection_times:local", ".*", "collection_times:local" }, 48 { "lit", ".*", "lit" }, 49 { "amenity", "post_box", "collection_times" }, 50 { "amenity", ".*", "opening_hours" }, 51 { "shop", ".*", "opening_hours" }, { "highway", ".*", "lit" } }; 55 { "collection_times:local", ".*", "collection_times:local" }, { "shop", ".*", "opening_hours" }, 56 { "amenity", "post_box", "collection_times" }, { "amenity", ".*", "opening_hours" }, 57 { "lit", ".*", "lit" }, { "highway", ".*", "lit" } }; 52 58 53 59 /** 54 60 * Will be invoked by JOSM to bootstrap the plugin 55 * 61 * 56 62 * @param info 57 63 * information about the plugin and its local installation … … 62 68 } 63 69 70 /** 71 * this Action is used for calling the OpeningsHourEditor, the selected 72 * objects in the active datalayer are edited 73 * 74 * @author boman 75 */ 64 76 class OheMenuAction extends JosmAction { 77 private static final long serialVersionUID = 1456257438391417756L; 78 65 79 public OheMenuAction() { 66 super( 67 tr("Edit opening hours"), 68 "opening_hours.png", 69 tr("Edit time-tag of selected element in a graphical interface"), 70 Shortcut.registerShortcut("tools:opening_hourseditor", tr( 71 "Tool: {0}", tr("Edit opening hours")), 72 KeyEvent.VK_T, Shortcut.GROUP_MENU), false); 80 super(tr("Edit opening hours"), "opening_hours.png", 81 tr("Edit time-tag of selected element in a graphical interface"), Shortcut.registerShortcut( 82 "tools:opening_hourseditor", tr("Tool: {0}", tr("Edit opening hours")), KeyEvent.VK_T, 83 Shortcut.GROUP_MENU), false); 73 84 } 74 85 … … 76 87 protected void updateEnabledState() { 77 88 if (getCurrentDataSet() == null) { 89 // if there is no current dataset, then the action is disabled 78 90 setEnabled(false); 79 91 } else { … … 83 95 84 96 @Override 85 protected void updateEnabledState( 86 Collection<? extends OsmPrimitive> selection) {97 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { 98 // only enable the action if something is selected 87 99 setEnabled(selection != null && !selection.isEmpty()); 88 100 } … … 91 103 // fetch active Layer 92 104 OsmDataLayer osmlayer = Main.main.getEditLayer(); 93 if (osmlayer != null) { 94 Collection<OsmPrimitive> selection = osmlayer.data 95 .getSelected(); 96 if (selection.size() == 1) { // one object selected 97 OsmPrimitive object = selection.iterator().next(); 98 String[] keyValuePair = editTimeTags(object.getKeys()); 99 if (keyValuePair != null) { 100 String key = keyValuePair[0].trim(); 101 String newkey = keyValuePair[1].trim(); 102 String value = keyValuePair[2].trim(); 103 104 if (value.equals("")) { 105 value = null; // delete the key 105 if (osmlayer == null) 106 return; 107 Collection<OsmPrimitive> selection = osmlayer.data.getSelected(); 108 109 // handling of multiple objects and their tags 110 // copied from 111 // org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog[rev4079][line802] 112 Map<String, Integer> keyCount = new HashMap<String, Integer>(); 113 Map<String, Map<String, Integer>> valueCount = new TreeMap<String, Map<String, Integer>>(); 114 for (OsmPrimitive osm : selection) { 115 for (String key : osm.keySet()) { 116 String value = osm.get(key); 117 keyCount.put(key, keyCount.containsKey(key) ? keyCount.get(key) + 1 : 1); 118 if (valueCount.containsKey(key)) { 119 Map<String, Integer> v = valueCount.get(key); 120 v.put(value, v.containsKey(value) ? v.get(value) + 1 : 1); 121 } else { 122 TreeMap<String, Integer> v = new TreeMap<String, Integer>(); 123 v.put(value, 1); 124 valueCount.put(key, v); 125 } 126 } 127 } 128 129 DefaultTableModel propertyData = new DefaultTableModel() { 130 @Override 131 public boolean isCellEditable(int row, int column) { 132 return false; 133 } 134 135 @Override 136 public Class<?> getColumnClass(int columnIndex) { 137 return String.class; 138 } 139 }; 140 propertyData.setColumnIdentifiers(new String[] { tr("Key"), tr("Value") }); 141 for (Entry<String, Map<String, Integer>> e : valueCount.entrySet()) { 142 int count = 0; 143 for (Entry<String, Integer> e1 : e.getValue().entrySet()) { 144 count += e1.getValue(); 145 } 146 if (count < selection.size()) { 147 e.getValue().put("", selection.size() - count); 148 } 149 propertyData.addRow(new Object[] { e.getKey(), e.getValue() }); 150 } 151 final JTable propertyTable = new JTable(propertyData); 152 propertyTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer() { 153 @Override 154 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 155 boolean hasFocus, int row, int column) { 156 Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column); 157 if (value == null) 158 return this; 159 if (c instanceof JLabel) { 160 String str = null; 161 if (value instanceof String) { 162 str = (String) value; 163 } else if (value instanceof Map<?, ?>) { 164 Map<?, ?> v = (Map<?, ?>) value; 165 if (v.size() != 1) { 166 str = tr("<different>"); 167 c.setFont(c.getFont().deriveFont(Font.ITALIC)); 168 } else { 169 final Map.Entry<?, ?> entry = v.entrySet().iterator().next(); 170 str = (String) entry.getKey(); 171 } 106 172 } 107 if (newkey.equals("")) { 108 newkey = key; 109 value = null; // delete the key instead 173 ((JLabel) c).setText(str); 174 } 175 return c; 176 } 177 }); 178 // end copy 179 180 // showing the tags in a dialog 181 propertyTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 182 JScrollPane sp = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 183 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 184 sp.setViewportView(propertyTable); 185 186 final JTextField newTagField = new JTextField(); 187 188 JRadioButton editButton = new JRadioButton(new AbstractAction(tr("edit existing tag")) { 189 @Override 190 public void actionPerformed(ActionEvent e) { 191 propertyTable.setEnabled(true); 192 newTagField.setEnabled(false); 193 } 194 }); 195 JRadioButton newButton = new JRadioButton(new AbstractAction(tr("edit new tag")) { 196 @Override 197 public void actionPerformed(ActionEvent e) { 198 propertyTable.setEnabled(false); 199 newTagField.setEnabled(true); 200 } 201 }); 202 ButtonGroup group = new ButtonGroup(); 203 group.add(newButton); 204 group.add(editButton); 205 206 // search through the tags and choose which one should be selected 207 String preSelectedKey = ""; 208 searchLoop: for (String[] pattern : TAG_EDIT_STRINGS) { 209 Pattern keyPattern = Pattern.compile(pattern[0]); 210 Pattern valuePattern = Pattern.compile(pattern[1]); 211 for (int i = 0; i < propertyData.getRowCount(); ++i) { 212 Matcher keyMatcher = keyPattern.matcher((String) propertyData.getValueAt(i, 0)); 213 if (keyMatcher.matches()) { 214 Object value = propertyData.getValueAt(i, 1); 215 if (value instanceof String && valuePattern.matcher((String) value).matches()) { 216 preSelectedKey = pattern[2]; 217 break searchLoop; 218 } else if (value instanceof Map<?, ?>) 219 for (String v : ((Map<String, Integer>) value).keySet()) 220 if (valuePattern.matcher(v).matches()) { 221 preSelectedKey = pattern[2]; 222 break searchLoop; 223 } 224 } 225 } 226 } 227 int preSelectedRow = -1; 228 for (int i = 0; i < propertyData.getRowCount(); ++i) 229 if (preSelectedKey.equals(propertyData.getValueAt(i, 0))) 230 preSelectedRow = i; 231 if (preSelectedRow != -1) { 232 propertyTable.setEnabled(true); 233 newTagField.setEnabled(false); 234 propertyTable.setRowSelectionInterval(preSelectedRow, preSelectedRow); 235 editButton.setSelected(true); 236 } else { 237 propertyTable.setEnabled(false); 238 newTagField.setEnabled(true); 239 newTagField.setText(preSelectedKey); 240 newButton.setSelected(true); 241 } 242 243 JPanel dlgPanel = new JPanel(new GridBagLayout()); 244 dlgPanel.add(editButton, GBC.std().anchor(GBC.CENTER)); 245 dlgPanel.add(sp, GBC.eol().fill(GBC.BOTH)); 246 dlgPanel.add(newButton, GBC.std().anchor(GBC.CENTER)); 247 dlgPanel.add(newTagField, GBC.eol().fill(GBC.HORIZONTAL)); 248 249 JOptionPane optionPane = new JOptionPane(dlgPanel, JOptionPane.QUESTION_MESSAGE, 250 JOptionPane.OK_CANCEL_OPTION); 251 JDialog dlg = optionPane.createDialog(Main.parent, tr("Choose key")); 252 dlg.pack(); 253 dlg.setResizable(true); 254 dlg.setVisible(true); 255 256 Object answer = optionPane.getValue(); 257 String keyToEdit = null; 258 Object valuesToEdit = ""; 259 if (answer != null && answer != JOptionPane.UNINITIALIZED_VALUE 260 && (answer instanceof Integer && (Integer) answer == JOptionPane.OK_OPTION)) 261 if (editButton.isSelected() && propertyTable.getSelectedRow() != -1) { 262 keyToEdit = (String) propertyData.getValueAt(propertyTable.getSelectedRow(), 0); 263 valuesToEdit = propertyData.getValueAt(propertyTable.getSelectedRow(), 1); 264 } else if (newButton.isSelected()) 265 keyToEdit = newTagField.getText(); 266 if (keyToEdit == null) 267 return; 268 269 OheDialogPanel panel = new OheDialogPanel(OhePlugin.this, keyToEdit, valuesToEdit); 270 271 optionPane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION); 272 dlg = optionPane.createDialog(Main.parent, tr("Edit")); 273 dlg.setResizable(true); 274 dlg.setVisible(true); 275 276 String[] changedKeyValuePair = null; 277 answer = optionPane.getValue(); 278 if (!(answer == null || answer == JOptionPane.UNINITIALIZED_VALUE || (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))) 279 changedKeyValuePair = panel.getChangedKeyValuePair(); 280 if (changedKeyValuePair == null) 281 return; 282 String key = changedKeyValuePair[0].trim(); 283 String newkey = changedKeyValuePair[1].trim(); 284 String value = changedKeyValuePair[2].trim(); 285 286 if (value.equals("")) { 287 value = null; // delete the key 288 } 289 if (newkey.equals("")) { 290 newkey = key; 291 value = null; // delete the key instead 292 } 293 if (key.equals(newkey) && tr("<different>").equals(value)) 294 return; 295 if (key.equals(newkey) || value == null) { 296 Main.main.undoRedo.add(new ChangePropertyCommand(selection, newkey, value)); 297 } else { 298 Collection<Command> commands = new Vector<Command>(); 299 commands.add(new ChangePropertyCommand(selection, key, null)); 300 if (value.equals(tr("<different>"))) { 301 HashMap<String, Vector<OsmPrimitive>> map = new HashMap<String, Vector<OsmPrimitive>>(); 302 for (OsmPrimitive osm : selection) { 303 String val = osm.get(key); 304 if (val != null) { 305 if (map.containsKey(val)) { 306 map.get(val).add(osm); 307 } else { 308 Vector<OsmPrimitive> v = new Vector<OsmPrimitive>(); 309 v.add(osm); 310 map.put(val, v); 311 } 110 312 } 111 if (key.equals(newkey) 112 && tr("<different>").equals(value)) 113 return; 114 if (key.equals(newkey) || value == null) { 115 Main.main.undoRedo.add(new ChangePropertyCommand( 116 object, newkey, value)); 117 } else { 118 Collection<Command> commands = new Vector<Command>(); 119 commands.add(new ChangePropertyCommand(object, key, 120 null)); 121 commands.add(new ChangePropertyCommand(object, 122 newkey, value)); 123 Main.main.undoRedo.add(new SequenceCommand( 124 tr("Change properties of 1 object"), 125 commands)); 126 } 127 } 128 } else { // Not possible to edit 0, 2 or more objects 129 JOptionPane 130 .showMessageDialog( 131 Main.parent, 132 tr( 133 "You have {0} Elements selected. But you can edit only one element!", 134 selection.size()), 135 "openingHoursEditor Warning", 136 JOptionPane.ERROR_MESSAGE); 137 } 313 } 314 for (Entry<String, Vector<OsmPrimitive>> e : map.entrySet()) { 315 commands.add(new ChangePropertyCommand(e.getValue(), newkey, e.getKey())); 316 } 317 } else { 318 commands.add(new ChangePropertyCommand(selection, newkey, value)); 319 } 320 Main.main.undoRedo.add(new SequenceCommand(trn("Change properties of up to {0} object", 321 "Change properties of up to {0} objects", selection.size(), selection.size()), commands)); 138 322 } 139 323 } 140 324 } 141 142 // opens up dialogs to change one of the key-value-pairs and returns the143 // changed pair144 private String[] editTimeTags(Map<String, String> keyValueMap) {145 String selectedKey = "";146 147 if ((selectedKey = tagChooseDialog(keyValueMap)) == null)148 return null;149 150 final String value = (keyValueMap.containsKey(selectedKey)) ? keyValueMap151 .get(selectedKey)152 : "";153 OheDialogPanel panel = new OheDialogPanel(this, selectedKey, value);154 155 final JOptionPane optionPane = new JOptionPane(panel,156 JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);157 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Edit"));158 159 dlg.setResizable(true);160 dlg.setVisible(true);161 162 Object answer = optionPane.getValue();163 if (!(answer == null || answer == JOptionPane.UNINITIALIZED_VALUE || (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION)))164 return panel.getChangedKeyValuePair();165 166 return null;167 }168 169 // opens a dialog for choosing from a set of tags which can be edited170 // the chosen one is returned171 private String tagChooseDialog(Map<String, String> keyValueMap) {172 String preSelectedKey = getPreSelectedKey(keyValueMap);173 int preSelectedRow = -1;174 175 String[][] rowData = new String[keyValueMap.size()][2];176 int cnt = 0;177 for (Object key : keyValueMap.keySet().toArray()) {178 rowData[cnt][0] = key.toString();179 rowData[cnt][1] = keyValueMap.get(key);180 if (key.toString().equals(preSelectedKey))181 preSelectedRow = cnt;182 cnt++;183 }184 185 final JTable table = new JTable(rowData,186 new String[] { "key", "value" }) {187 public boolean isCellEditable(int rowIndex, int colIndex) {188 return false; // Disallow the editing of any cell189 }190 };191 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);192 JScrollPane sp = new JScrollPane(193 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,194 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);195 sp.setViewportView(table);196 197 final JTextField tf = new JTextField();198 199 ActionListener al = new ActionListener() {200 @Override201 public void actionPerformed(ActionEvent e) {202 if (e.getActionCommand().equals("edit")) {203 table.setEnabled(true);204 tf.setEnabled(false);205 } else if (e.getActionCommand().equals("new")) {206 table.setEnabled(false);207 tf.setEnabled(true);208 }209 }210 };211 212 JRadioButton editButton = new JRadioButton("edit existing tag");213 editButton.setActionCommand("edit");214 editButton.addActionListener(al);215 JRadioButton newButton = new JRadioButton("edit new tag");216 newButton.setActionCommand("new");217 newButton.addActionListener(al);218 ButtonGroup group = new ButtonGroup();219 group.add(newButton);220 group.add(editButton);221 222 if (preSelectedRow != -1) {223 table.setEnabled(true);224 tf.setEnabled(false);225 table.setRowSelectionInterval(preSelectedRow, preSelectedRow);226 editButton.setSelected(true);227 } else {228 table.setEnabled(false);229 tf.setEnabled(true);230 tf.setText(preSelectedKey);231 newButton.setSelected(true);232 }233 234 JPanel dlgPanel = new JPanel(new GridBagLayout());235 dlgPanel.add(editButton, GBC.std().anchor(GBC.CENTER));236 dlgPanel.add(sp, GBC.eol().fill(GBC.BOTH));237 dlgPanel.add(newButton, GBC.std().anchor(GBC.CENTER));238 dlgPanel.add(tf, GBC.eol().fill(GBC.HORIZONTAL));239 240 JOptionPane optionPane = new JOptionPane(dlgPanel,241 JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);242 JDialog dlg = optionPane.createDialog(Main.parent, tr("Choose key"));243 244 dlg.pack();245 dlg.setResizable(true);246 dlg.setVisible(true);247 248 Object answer = optionPane.getValue();249 if (answer != null250 && answer != JOptionPane.UNINITIALIZED_VALUE251 && (answer instanceof Integer && (Integer) answer == JOptionPane.OK_OPTION))252 if (editButton.isSelected() && table.getSelectedRow() != -1)253 return rowData[table.getSelectedRow()][0];254 else if (newButton.isSelected())255 return tf.getText();256 257 return null;258 }259 260 private String getPreSelectedKey(Map<String, String> keyValueMap) {261 for (String[] pattern : TAG_EDIT_STRINGS) {262 Pattern keyPattern = Pattern.compile(pattern[0]);263 Pattern valuePattern = Pattern.compile(pattern[1]);264 for (Object key : keyValueMap.keySet().toArray()) {265 Matcher keyMatcher = keyPattern.matcher(key.toString());266 if (keyMatcher.matches()) {267 Matcher valueMatcher = valuePattern.matcher(keyValueMap268 .get(key));269 if (valueMatcher.matches()) {270 return pattern[2];271 }272 }273 }274 }275 return "";276 }277 325 } -
applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/gui/OheDialogPanel.java
r23192 r26002 8 8 import java.awt.event.ActionListener; 9 9 import java.util.ArrayList; 10 import java.util.Map; 10 11 11 12 import javax.swing.Box; … … 15 16 import javax.swing.JPanel; 16 17 import javax.swing.JTextField; 18 import javax.swing.SwingUtilities; 17 19 18 20 import org.openstreetmap.josm.plugins.ohe.OhePlugin; … … 39 41 private final String oldkey; 40 42 41 public OheDialogPanel(OhePlugin plugin, String key, String value) { 43 /** 44 * The Panel for editing the time-values. 45 * 46 * @param plugin 47 * @param key 48 * @param valuesToEdit 49 * can be a String or a Map<String, Integer> which contains 50 * multiple values and their number of occurences 51 */ 52 public OheDialogPanel(OhePlugin plugin, String key, Object valuesToEdit) { 42 53 oldkey = key; 43 54 keyField = new JTextField(key); 44 55 56 String value = ""; 57 if (valuesToEdit instanceof String) 58 value = (String) valuesToEdit; 59 else if (valuesToEdit instanceof Map<?, ?>) { 60 Map<String, Integer> valuesMap = (Map<String, Integer>) valuesToEdit; 61 if (valuesMap.size() == 1) 62 value = valuesMap.keySet().iterator().next(); 63 else if (valuesMap.size() > 1) { 64 // TODO let the user choose which value he wants to edit (e.g. 65 // with a combobox) 66 int mostOccurences = 0; 67 for (String v : valuesMap.keySet()) 68 if (valuesMap.get(v) > mostOccurences) { 69 value = v; 70 mostOccurences = valuesMap.get(v); 71 } 72 } 73 } 45 74 valueField = new JTextField(value); 46 75 valueField.addActionListener(new ActionListener() { … … 61 90 }); 62 91 63 actualPostionLabel = new JLabel(" Mo 00:00");92 actualPostionLabel = new JLabel("-"); 64 93 JPanel toolsPanel = new JPanel(new GridBagLayout()); 65 94 toolsPanel.add(twentyfourSevenButton, GBC.std()); … … 80 109 add(editorPanel, GBC.eol().fill()); 81 110 82 valueField.requestFocus();83 111 setPreferredSize(new Dimension(480, 520)); 84 112 } … … 102 130 if (t instanceof ParseException) { 103 131 ParseException parserExc = (ParseException) t; 104 tColumns = new int[] { 105 parserExc.currentToken.beginColumn - 1, 106 parserExc.currentToken.endColumn + 1 }; 132 tColumns = new int[] { parserExc.currentToken.beginColumn - 1, parserExc.currentToken.endColumn + 1 }; 107 133 } else if (t instanceof SyntaxException) { 108 134 SyntaxException syntaxError = (SyntaxException) t; 109 tColumns = new int[] { syntaxError.getStartColumn(), 110 syntaxError.getEndColumn() }; 135 tColumns = new int[] { syntaxError.getStartColumn(), syntaxError.getEndColumn() }; 111 136 info = syntaxError.getInfo(); 112 137 } else if (t instanceof TokenMgrError) { 113 138 TokenMgrError tokenMgrError = (TokenMgrError) t; 114 tColumns = new int[] { tokenMgrError.errorColumn - 1, 115 tokenMgrError.errorColumn + 1 }; 139 tColumns = new int[] { tokenMgrError.errorColumn - 1, tokenMgrError.errorColumn + 1 }; 116 140 } else { 117 141 t.printStackTrace(); … … 125 149 String middle = value.substring(first, last); 126 150 String end = value.substring(last); 127 String message = "<html>" 128 + tr("There is something wrong in the value near:") 129 + "<br>" + begin 130 + "<span style='background-color:red;'>" + middle 131 + "</span>" + end; 151 valueField.setCaretPosition(first); 152 // TODO focus on the valueField 153 String message = "<html>" + tr("There is something wrong in the value near:") + "<br>" + begin 154 + "<span style='background-color:red;'>" + middle + "</span>" + end; 132 155 if (info != null) 133 156 message += "<br>" + tr("Info: {0}", tr(info)); 134 message += "<br>" 135 + tr("Correct the value manually and than press Enter."); 157 message += "<br>" + tr("Correct the value manually and than press Enter."); 136 158 message += "</html>"; 137 JOptionPane.showMessageDialog(this, message, 138 tr("Error in timeformat"), 159 JOptionPane.showMessageDialog(this, message, tr("Error in timeformat"), 139 160 JOptionPane.INFORMATION_MESSAGE); 140 161 } -
applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/gui/OheEditor.java
r24290 r26002 20 20 21 21 public class OheEditor extends JPanel implements MouseListener, 22 22 MouseMotionListener { 23 23 final OheDialogPanel dialog; 24 24 … … 32 32 33 33 public OheEditor(OheDialogPanel oheDialogPanel) { 34 dialog = oheDialogPanel; 35 36 // the MainPanel for showing the TimeRects 37 contentPanel = new JPanel() { 38 @Override 39 public void setSize(Dimension d) { 40 super.setSize(d); 41 repositionTimeRects(); 42 } 43 44 @Override 45 public void paintComponent(Graphics g) { 46 if (OheEditor.this.isEnabled()) { 47 g.setColor(Color.WHITE); 48 g.fillRect(0, 0, getWidth(), getHeight()); 49 50 // horizontal Lines 51 for (int i = 1; i < 24; ++i) { 52 if (i % 3 == 0) 53 g.setColor(Color.BLACK); 54 else 55 g.setColor(Color.LIGHT_GRAY); 56 57 g.drawLine(0, getMinutePosition(i * 60), getWidth(), 58 getMinutePosition(i * 60)); 59 } 60 61 // vertical Lines 62 g.setColor(Color.BLACK); 63 for (int i = 1; i < 7; ++i) 64 g.drawLine(getDayPosition(i), 0, getDayPosition(i), 65 getHeight()); 66 67 // if a new Rect is dragged draw it 68 if (day0 >= 0) { 69 Graphics2D g2D = (Graphics2D) g; 70 71 int day2 = Math.min(day0, day1); 72 int day3 = Math.max(day0, day1); 73 int minute2 = Math.min(minute0, minute1); 74 int minute3 = Math.max(minute0, minute1); 75 Rectangle bounds = getPanelBoundsForTimeinterval(day2, 76 day3 + 1, minute2, minute3); 77 78 TimeRect.drawTimeRect(g2D, bounds, minute2 == minute3, false); 79 } 80 } else { 81 g.setColor(Color.LIGHT_GRAY); 82 g.fillRect(0, 0, getWidth(), getHeight()); 83 } 84 } 85 }; 86 contentPanel.addMouseListener(this); 87 contentPanel.addMouseMotionListener(this); 88 contentPanel.setLayout(null); 89 contentPanel.setPreferredSize(new Dimension(180, 384)); 90 91 initTimeRects(); 92 93 scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 94 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 95 scrollPane.setViewportView(contentPanel); 96 97 // the upper Panel for showing Weekdays 98 scrollPane.setColumnHeaderView(new JPanel() { 99 @Override 100 public Dimension getPreferredSize() { 101 return new Dimension(contentPanel.getWidth(), dayAxisHeight); 102 } 103 104 @Override 105 public void paintComponent(Graphics g) { 106 g.setColor(Color.WHITE); 107 g.fillRect(0, 0, getWidth(), getHeight()); 108 109 g.setColor(Color.BLACK); 110 for (int i = 0; i < 7; ++i) { 111 if (i > 0) 112 g.drawLine(getDayPosition(i) + 1, 0, 113 getDayPosition(i) + 1, getHeight()); 114 115 String text = OpeningTimeCompiler.WEEKDAYS[i]; 116 g.drawString(text, (int) (getDayPosition(i + 0.5) - g 117 .getFontMetrics().stringWidth(text) * 0.5), 118 (int) (dayAxisHeight * 0.5 + g.getFontMetrics() 119 .getHeight() * 0.35)); 120 } 121 } 122 }); 123 124 // the left Panel for showing the hours 125 scrollPane.setRowHeaderView(new JPanel() { 126 @Override 127 public Dimension getPreferredSize() { 128 return new Dimension(timeAxisWidth, contentPanel.getHeight()); 129 } 130 131 @Override 132 public void paintComponent(Graphics g) { 133 g.setColor(Color.WHITE); 134 g.fillRect(0, 0, getWidth(), getHeight()); 135 136 for (int i = 1; i < 24; ++i) { 137 if (i % 3 == 0) { 138 g.setColor(Color.BLACK); 139 String text = ((i < 10) ? "0" + i : i) + ":00"; 140 g 141 .drawString(text, timeAxisWidth - 10 142 - g.getFontMetrics().stringWidth(text), 143 getMinutePosition(i * 60) 144 + (int) (g.getFontMetrics() 145 .getHeight() * 0.35)); 146 } else 147 g.setColor(Color.LIGHT_GRAY); 148 149 g.drawLine(getWidth() - 4, getMinutePosition(i * 60) + 1, 150 getWidth(), getMinutePosition(i * 60) + 1); 151 } 152 } 153 }); 154 155 setLayout(new BorderLayout()); 156 add(scrollPane, BorderLayout.CENTER); 34 dialog = oheDialogPanel; 35 36 // the MainPanel for showing the TimeRects 37 contentPanel = new JPanel() { 38 @Override 39 public void setSize(Dimension d) { 40 super.setSize(d); 41 repositionTimeRects(); 42 } 43 44 @Override 45 public void paintComponent(Graphics g) { 46 if (OheEditor.this.isEnabled()) { 47 g.setColor(Color.WHITE); 48 g.fillRect(0, 0, getWidth(), getHeight()); 49 50 // horizontal Lines 51 for (int i = 1; i < 24; ++i) { 52 if (i % 3 == 0) 53 g.setColor(Color.BLACK); 54 else 55 g.setColor(Color.LIGHT_GRAY); 56 57 g.drawLine(0, getMinutePosition(i * 60), getWidth(), 58 getMinutePosition(i * 60)); 59 } 60 61 // vertical Lines 62 g.setColor(Color.BLACK); 63 for (int i = 1; i < 7; ++i) 64 g.drawLine(getDayPosition(i), 0, getDayPosition(i), 65 getHeight()); 66 67 // if a new Rect is dragged draw it 68 if (day0 >= 0) { 69 Graphics2D g2D = (Graphics2D) g; 70 71 int day2 = Math.min(day0, day1); 72 int day3 = Math.max(day0, day1); 73 int minute2 = Math.min(minute0, minute1); 74 int minute3 = Math.max(minute0, minute1); 75 Rectangle bounds = getPanelBoundsForTimeinterval(day2, 76 day3 + 1, minute2, minute3); 77 78 TimeRect.drawTimeRect(g2D, bounds, minute2 == minute3, 79 false); 80 } 81 } else { 82 g.setColor(Color.LIGHT_GRAY); 83 g.fillRect(0, 0, getWidth(), getHeight()); 84 } 85 } 86 }; 87 contentPanel.addMouseListener(this); 88 contentPanel.addMouseMotionListener(this); 89 contentPanel.setLayout(null); 90 contentPanel.setPreferredSize(new Dimension(180, 384)); 91 92 initTimeRects(); 93 94 scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 95 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 96 scrollPane.setViewportView(contentPanel); 97 98 // the upper Panel for showing Weekdays 99 scrollPane.setColumnHeaderView(new JPanel() { 100 @Override 101 public Dimension getPreferredSize() { 102 return new Dimension(contentPanel.getWidth(), dayAxisHeight); 103 } 104 105 @Override 106 public void paintComponent(Graphics g) { 107 g.setColor(Color.WHITE); 108 g.fillRect(0, 0, getWidth(), getHeight()); 109 110 g.setColor(Color.BLACK); 111 for (int i = 0; i < 7; ++i) { 112 if (i > 0) 113 g.drawLine(getDayPosition(i) + 1, 0, 114 getDayPosition(i) + 1, getHeight()); 115 116 String text = OpeningTimeCompiler.WEEKDAYS[i]; 117 g.drawString(text, (int) (getDayPosition(i + 0.5) - g 118 .getFontMetrics().stringWidth(text) * 0.5), 119 (int) (dayAxisHeight * 0.5 + g.getFontMetrics() 120 .getHeight() * 0.35)); 121 } 122 } 123 }); 124 125 // the left Panel for showing the hours 126 scrollPane.setRowHeaderView(new JPanel() { 127 @Override 128 public Dimension getPreferredSize() { 129 return new Dimension(timeAxisWidth, contentPanel.getHeight()); 130 } 131 132 @Override 133 public void paintComponent(Graphics g) { 134 g.setColor(Color.WHITE); 135 g.fillRect(0, 0, getWidth(), getHeight()); 136 137 for (int i = 1; i < 24; ++i) { 138 if (i % 3 == 0) { 139 g.setColor(Color.BLACK); 140 String text = ((i < 10) ? "0" + i : i) + ":00"; 141 g.drawString( 142 text, 143 timeAxisWidth - 10 144 - g.getFontMetrics().stringWidth(text), 145 getMinutePosition(i * 60) 146 + (int) (g.getFontMetrics().getHeight() * 0.35)); 147 } else 148 g.setColor(Color.LIGHT_GRAY); 149 150 g.drawLine(getWidth() - 4, getMinutePosition(i * 60) + 1, 151 getWidth(), getMinutePosition(i * 60) + 1); 152 } 153 } 154 }); 155 156 setLayout(new BorderLayout()); 157 add(scrollPane, BorderLayout.CENTER); 157 158 } 158 159 159 160 // update all the TimeRects with new Data 160 161 public void initTimeRects() { 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 162 contentPanel.removeAll(); 163 164 ArrayList<int[]> time; 165 try { 166 time = dialog.getTime(); 167 } catch (Exception exc) { 168 setEnabled(false); 169 return; 170 } 171 172 setEnabled(true); 173 timeRects = new ArrayList<TimeRect>(); 174 if (time != null) { 175 for (int[] timeRectValues : time) { 176 int day0 = timeRectValues[0]; 177 int day1 = timeRectValues[1]; 178 int minute0 = timeRectValues[2]; 179 int minute1 = timeRectValues[3]; 180 TimeRect timeRect = new TimeRect(OheEditor.this, day0, day1, 181 minute0, minute1); 182 timeRects.add(timeRect); 183 contentPanel.add(timeRect); 184 } 185 } 186 187 repositionTimeRects(); 188 repaint(); 188 189 } 189 190 190 191 protected void repositionTimeRects() { 191 192 193 192 if (timeRects != null) 193 for (TimeRect timeRect : timeRects) 194 timeRect.reposition(); 194 195 } 195 196 196 197 // returns the physical Borders of the TimeRect on the mainPanel 197 198 public Rectangle getPanelBoundsForTimeinterval(int dayStart, int dayEnd, 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 199 int minutesStart, int minutesEnd) { 200 int x = getDayPosition(dayStart); 201 int y = getMinutePosition(minutesStart); 202 int width = getDayPosition(dayEnd) - getDayPosition(dayStart); 203 int height = getMinutePosition(minutesEnd) 204 - getMinutePosition(minutesStart); 205 206 // work around openjdk bug 207 if (Main.isOpenjdk) { 208 x++; 209 y++; 210 } 211 212 if (minutesStart == minutesEnd) 213 return new Rectangle(x, y - 2 - TimeRect.verticalNonDrawedPixels, 214 width, height + 5 + 2 * TimeRect.verticalNonDrawedPixels); 215 216 return new Rectangle(x, y, width, height + 1); 216 217 } 217 218 218 219 public double getDayWidth() { 219 220 return (contentPanel.getWidth() - 1) / 7.0; 220 221 } 221 222 222 223 public int getDayPosition(double d) { 223 224 return (int) (d * getDayWidth()); 224 225 } 225 226 226 227 public double getMinuteHeight() { 227 228 return (contentPanel.getHeight() - 1) / (24.0 * 60); 228 229 } 229 230 230 231 public int getMinutePosition(int minute) { 231 232 return (int) (minute * getMinuteHeight()); 232 233 } 233 234 234 235 // removes the given timerect from the panel and from the arraylist 235 236 public void removeTimeRect(TimeRect timeRectToRemove) { 236 237 238 239 237 timeRects.remove(timeRectToRemove); 238 contentPanel.remove(timeRectToRemove); 239 dialog.updateValueField(timeRects); 240 repaint(); 240 241 } 241 242 … … 254 255 @Override 255 256 public void mouseEntered(MouseEvent evt) { 257 mousePositionChanged(0, 0, true); 256 258 } 257 259 258 260 @Override 259 261 public void mouseExited(MouseEvent evt) { 262 mousePositionChanged(0, 0, false); 260 263 } 261 264 262 265 @Override 263 266 public void mousePressed(MouseEvent evt) { 264 265 266 267 268 269 270 271 267 day0 = (int) Math.floor(evt.getX() / getDayWidth()); 268 minute0 = (int) Math.floor(evt.getY() 269 / (getMinuteHeight() * TimeRect.minuteResterize)) 270 * TimeRect.minuteResterize; 271 day1 = day0; 272 minute1 = minute0; 273 xDragStart = evt.getX(); 274 yDragStart = evt.getY(); 272 275 } 273 276 274 277 @Override 275 278 public void mouseReleased(MouseEvent evt) { 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 279 // mouse must be moved 5px before creating a rect 280 if (xDragStart == -1 281 || Math.abs(evt.getX() - xDragStart) 282 + Math.abs(evt.getY() - yDragStart) > 5) { 283 int day2 = Math.min(day0, day1); 284 int day3 = Math.max(day0, day1); 285 int minute2 = Math.min(minute0, minute1); 286 int minute3 = Math.max(minute0, minute1); 287 288 TimeRect timeRect = new TimeRect(OheEditor.this, day2, day3, 289 minute2, minute3); 290 timeRects.add(timeRect); 291 contentPanel.add(timeRect); 292 timeRect.reposition(); 293 dialog.updateValueField(timeRects); 294 295 day0 = -1; 296 repaint(); 297 } 295 298 } 296 299 297 300 @Override 298 301 public void mouseDragged(MouseEvent evt) { 299 // mouse must be moved 5px before drawing a rect 300 if (xDragStart == -1 301 || Math.abs(evt.getX() - xDragStart) 302 + Math.abs(evt.getY() - yDragStart) > 5) { 303 xDragStart = -1; 304 day1 = (int) Math.floor(evt.getX() / getDayWidth()); 305 minute1 = (int) Math.floor(evt.getY() 306 / (getMinuteHeight() * TimeRect.minuteResterize)) 307 * TimeRect.minuteResterize; 308 309 // ensure that the new time is in a valid range 310 day1 = Math.max(day1, 0); 311 day1 = Math.min(day1, 6); 312 minute1 = Math.max(minute1, 0); 313 minute1 = Math.min(minute1, 24 * 60); 314 315 repaint(); 316 } 302 // mouse must be moved 5px before drawing a rect 303 if (xDragStart == -1 304 || Math.abs(evt.getX() - xDragStart) 305 + Math.abs(evt.getY() - yDragStart) > 5) { 306 xDragStart = -1; 307 day1 = (int) Math.floor(evt.getX() / getDayWidth()); 308 minute1 = (int) Math.floor(evt.getY() 309 / (getMinuteHeight() * TimeRect.minuteResterize)) 310 * TimeRect.minuteResterize; 311 312 // ensure that the new time is in a valid range 313 day1 = Math.max(day1, 0); 314 day1 = Math.min(day1, 6); 315 minute1 = Math.max(minute1, 0); 316 minute1 = Math.min(minute1, 24 * 60); 317 318 repaint(); 319 } 320 mousePositionChanged(evt.getX(), evt.getY(), true); 317 321 } 318 322 319 323 @Override 320 324 public void mouseMoved(MouseEvent evt) { 321 mousePositionChanged(evt.getX(), evt.getY()); 322 } 323 324 public void mousePositionChanged(int x, int y) { 325 int actualDay = (int) Math.floor(x / getDayWidth()); 326 int minutes = (int) Math.floor(y 327 / (getMinuteHeight() * TimeRect.minuteResterize)) 328 * TimeRect.minuteResterize; 329 actualDay = Math.max(0, Math.min(6, actualDay)); 330 minutes = Math.max(0, Math.min(24 * 60, minutes)); 331 dialog.setMousePositionText(OpeningTimeCompiler.WEEKDAYS[actualDay] 332 + " " + OpeningTimeUtils.timeString(minutes)); 325 mousePositionChanged(evt.getX(), evt.getY(), true); 326 } 327 328 public void mousePositionChanged(int x, int y, boolean mouseInside) { 329 if (mouseInside) { 330 int actualDay = (int) Math.floor(x / getDayWidth()); 331 int minutes = (int) Math.floor(y 332 / (getMinuteHeight() * TimeRect.minuteResterize)) 333 * TimeRect.minuteResterize; 334 actualDay = Math.max(0, Math.min(6, actualDay)); 335 minutes = Math.max(0, Math.min(24 * 60, minutes)); 336 dialog.setMousePositionText(OpeningTimeCompiler.WEEKDAYS[actualDay] 337 + " " + OpeningTimeUtils.timeString(minutes)); 338 } else 339 dialog.setMousePositionText("-"); 333 340 } 334 341 } -
applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/gui/TimeRect.java
r23192 r26002 285 285 } 286 286 } 287 editor.mousePositionChanged(evt.getX() + getX(), evt.getY() + getY() );287 editor.mousePositionChanged(evt.getX() + getX(), evt.getY() + getY(), true); 288 288 } 289 289 … … 292 292 if (transformType < 0) 293 293 setCursor(new Cursor(transformCursorTypes[getTransformType(evt)])); 294 editor.mousePositionChanged(evt.getX() + getX(), evt.getY() + getY() );294 editor.mousePositionChanged(evt.getX() + getX(), evt.getY() + getY(), true); 295 295 } 296 296 }
Note:
See TracChangeset
for help on using the changeset viewer.