Changeset 26215 in osm for applications/editors/josm
- Timestamp:
- 2011-06-30T13:06:56+02:00 (13 years ago)
- Location:
- applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/OhePlugin.java
r26002 r26215 11 11 import java.util.Collection; 12 12 import java.util.HashMap; 13 import java.util.Locale; 13 14 import java.util.Map; 14 15 import java.util.Map.Entry; … … 20 21 import javax.swing.AbstractAction; 21 22 import javax.swing.ButtonGroup; 23 import javax.swing.JCheckBox; 22 24 import javax.swing.JDialog; 23 25 import javax.swing.JLabel; … … 51 53 * {key, value, key-to-edit} key and value can contain regular expressions 52 54 */ 53 private final String[][] TAG_EDIT_STRINGS = new String[][] { { "opening_hours", ".*", "opening_hours" }, 55 private final String[][] TAG_EDIT_STRINGS = new String[][] { 56 { "opening_hours", ".*", "opening_hours" }, 54 57 { "collection_times", ".*", "collection_times" }, 55 { "collection_times:local", ".*", "collection_times:local" }, { "shop", ".*", "opening_hours" }, 56 { "amenity", "post_box", "collection_times" }, { "amenity", ".*", "opening_hours" }, 57 { "lit", ".*", "lit" }, { "highway", ".*", "lit" } }; 58 { "collection_times:local", ".*", "collection_times:local" }, 59 { "shop", ".*", "opening_hours" }, 60 { "amenity", "post_box", "collection_times" }, 61 { "amenity", "recycling", "collection_times" }, 62 { "amenity", ".*", "opening_hours" }, 63 { "lit", ".*", "lit" }, 64 { "highway", ".*", "lit" } }; 58 65 59 66 /** … … 216 223 preSelectedKey = pattern[2]; 217 224 break searchLoop; 218 } else if (value instanceof Map<?, ?>) 225 } else if (value instanceof Map<?, ?>) { 219 226 for (String v : ((Map<String, Integer>) value).keySet()) 220 227 if (valuePattern.matcher(v).matches()) { … … 222 229 break searchLoop; 223 230 } 231 } 224 232 } 225 233 } … … 227 235 int preSelectedRow = -1; 228 236 for (int i = 0; i < propertyData.getRowCount(); ++i) 229 if (preSelectedKey.equals(propertyData.getValueAt(i, 0))) 237 if (preSelectedKey.equals(propertyData.getValueAt(i, 0))) { 230 238 preSelectedRow = i; 239 } 231 240 if (preSelectedRow != -1) { 232 241 propertyTable.setEnabled(true); … … 241 250 } 242 251 252 JCheckBox useTwelveHourClock = new JCheckBox(tr("Display clock in 12h mode."), 253 ClockSystem.getClockSystem(Locale.getDefault()) == ClockSystem.TWELVE_HOURS); 254 243 255 JPanel dlgPanel = new JPanel(new GridBagLayout()); 244 dlgPanel.add(editButton, GBC.std().anchor(GBC. CENTER));256 dlgPanel.add(editButton, GBC.std().anchor(GBC.WEST)); 245 257 dlgPanel.add(sp, GBC.eol().fill(GBC.BOTH)); 246 dlgPanel.add(newButton, GBC.std().anchor(GBC. CENTER));258 dlgPanel.add(newButton, GBC.std().anchor(GBC.WEST)); 247 259 dlgPanel.add(newTagField, GBC.eol().fill(GBC.HORIZONTAL)); 260 dlgPanel.add(useTwelveHourClock, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 5)); 248 261 249 262 JOptionPane optionPane = new JOptionPane(dlgPanel, JOptionPane.QUESTION_MESSAGE, … … 262 275 keyToEdit = (String) propertyData.getValueAt(propertyTable.getSelectedRow(), 0); 263 276 valuesToEdit = propertyData.getValueAt(propertyTable.getSelectedRow(), 1); 264 } else if (newButton.isSelected()) 277 } else if (newButton.isSelected()) { 265 278 keyToEdit = newTagField.getText(); 279 } 266 280 if (keyToEdit == null) 267 281 return; 268 282 269 OheDialogPanel panel = new OheDialogPanel(OhePlugin.this, keyToEdit, valuesToEdit); 283 OheDialogPanel panel = new OheDialogPanel(OhePlugin.this, keyToEdit, valuesToEdit, 284 useTwelveHourClock.isSelected() ? ClockSystem.TWELVE_HOURS : ClockSystem.TWENTYFOUR_HOURS); 270 285 271 286 optionPane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION); … … 276 291 String[] changedKeyValuePair = null; 277 292 answer = optionPane.getValue(); 278 if (!(answer == null || answer == JOptionPane.UNINITIALIZED_VALUE || (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))) 293 if (!(answer == null || answer == JOptionPane.UNINITIALIZED_VALUE || (answer instanceof Integer && (Integer) answer != JOptionPane.OK_OPTION))) { 279 294 changedKeyValuePair = panel.getChangedKeyValuePair(); 295 } 280 296 if (changedKeyValuePair == null) 281 297 return; -
applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/OpeningTimeUtils.java
r23192 r26215 17 17 18 18 // test if the given entry is a single dayspan 19 if (dateTime.daySpans.size() == 1 20 && dateTime.daySpans.get(0).isSpan()) { 19 if (dateTime.daySpans.size() == 1 && dateTime.daySpans.get(0).isSpan()) { 21 20 ArrayList<DaySpan> partDaySpans = new ArrayList<DaySpan>(); 22 21 int start_day = dateTime.daySpans.get(0).startDay; … … 25 24 while (i + 1 < dateTimes.size()) { 26 25 ArrayList<DaySpan> following = dateTimes.get(i + 1).daySpans; 27 if (following.size() == 1 28 && following.get(0).startDay > dateTime.daySpans 29 .get(0).startDay 30 && following.get(0).endDay < dateTime.daySpans 31 .get(0).endDay) { 32 partDaySpans.add(new DaySpan(start_day, following 33 .get(0).startDay - 1)); 26 if (following.size() == 1 && following.get(0).startDay > dateTime.daySpans.get(0).startDay 27 && following.get(0).endDay < dateTime.daySpans.get(0).endDay) { 28 partDaySpans.add(new DaySpan(start_day, following.get(0).startDay - 1)); 34 29 start_day = following.get(0).endDay + 1; 35 30 newDateTimes.add(dateTimes.get(i + 1)); 36 31 i++; 37 } else 32 } else { 38 33 break; 39 } 40 41 partDaySpans.add(new DaySpan(start_day, dateTime.daySpans 42 .get(0).endDay)); 43 newDateTimes.add(new DateTime(partDaySpans, 44 dateTime.daytimeSpans)); 45 } 46 if (newDateTimes.isEmpty()) 34 } 35 } 36 37 partDaySpans.add(new DaySpan(start_day, dateTime.daySpans.get(0).endDay)); 38 newDateTimes.add(new DateTime(partDaySpans, dateTime.daytimeSpans)); 39 } 40 if (newDateTimes.isEmpty()) { 47 41 newDateTimes.add(dateTime); 42 } 48 43 49 44 // create the int-array … … 52 47 for (DaySpan dayspan : dateTime2.daySpans) { 53 48 for (DaytimeSpan timespan : dateTime2.daytimeSpans) { 54 if (!timespan.isOff()) 55 ret.add(new int[] { dayspan.startDay, 56 dayspan.endDay, timespan.startMinute, 49 if (!timespan.isOff()) { 50 ret.add(new int[] { dayspan.startDay, dayspan.endDay, timespan.startMinute, 57 51 timespan.endMinute }); 52 } 58 53 } 59 54 } … … 103 98 public ArrayList<DaytimeSpan> daytimeSpans; 104 99 105 public DateTime(ArrayList<DaySpan> daySpans, 106 ArrayList<DaytimeSpan> daytimeSpans) { 100 public DateTime(ArrayList<DaySpan> daySpans, ArrayList<DaytimeSpan> daytimeSpans) { 107 101 this.daySpans = daySpans; 108 102 this.daytimeSpans = daytimeSpans; … … 116 110 // in a week 117 111 boolean[][] minuteArray = new boolean[7][24 * 60 + 2]; 118 for (int day = 0; day < 7; ++day) 119 for (int minute = 0; minute < 24 * 60 + 2; ++minute) 112 for (int day = 0; day < 7; ++day) { 113 for (int minute = 0; minute < 24 * 60 + 2; ++minute) { 120 114 minuteArray[day][minute] = false; 121 for (TimeRect timeRect : givenTimeRects) 122 for (int day = timeRect.getDayStart(); day <= timeRect.getDayEnd(); ++day) 123 for (int minute = timeRect.getMinuteStart(); minute <= timeRect 124 .getMinuteEnd(); ++minute) 115 } 116 } 117 for (TimeRect timeRect : givenTimeRects) { 118 for (int day = timeRect.getDayStart(); day <= timeRect.getDayEnd(); ++day) { 119 for (int minute = timeRect.getMinuteStart(); minute <= timeRect.getMinuteEnd(); ++minute) { 125 120 minuteArray[day][minute] = true; 121 } 122 } 123 } 126 124 127 125 String ret = ""; … … 153 151 if (sameDayCount == 1) { 154 152 // a single Day with this special opening_hours 155 add = OpeningTimeCompiler.WEEKDAYS[i] + " " 156 + makeStringFromMinuteArray(minuteArray[i]); 153 add = OpeningTimeCompiler.WEEKDAYS[i] + " " + makeStringFromMinuteArray(minuteArray[i]); 157 154 } else if (sameDayCount == 2) { 158 155 // exactly two Days with this special opening_hours 159 add = OpeningTimeCompiler.WEEKDAYS[i] + "," 160 + OpeningTimeCompiler.WEEKDAYS[lastSameDay] + " " 161 + makeStringFromMinuteArray(minuteArray[i]); 156 add = OpeningTimeCompiler.WEEKDAYS[i] + "," + OpeningTimeCompiler.WEEKDAYS[lastSameDay] + " " 157 + makeStringFromMinuteArray(minuteArray[i]); 162 158 } else if (sameDayCount > 2) { 163 159 // more than two Days with this special opening_hours 164 add = OpeningTimeCompiler.WEEKDAYS[i] + "-" 165 + OpeningTimeCompiler.WEEKDAYS[lastSameDay] + " " 166 + makeStringFromMinuteArray(minuteArray[i]); 160 add = OpeningTimeCompiler.WEEKDAYS[i] + "-" + OpeningTimeCompiler.WEEKDAYS[lastSameDay] + " " 161 + makeStringFromMinuteArray(minuteArray[i]); 167 162 for (int j = i + 1; j < lastSameDay; ++j) { 168 if (days[j] == 0) 163 if (days[j] == 0) { 169 164 days[j] = -i - 1; 165 } 170 166 } 171 167 } … … 173 169 174 170 if (!add.isEmpty()) { 175 if (!ret.isEmpty()) 171 if (!ret.isEmpty()) { 176 172 ret += "; "; 173 } 177 174 ret += add; 178 175 } … … 188 185 if (minutes[i]) { 189 186 int start = i; 190 while (i < minutes.length && minutes[i]) 187 while (i < minutes.length && minutes[i]) { 191 188 ++i; 189 } 192 190 String addString = timeString(start); 193 if (i - 1 == 24 * 60 + 1) // open end191 if (i - 1 == 24 * 60 + 1) { 194 192 addString += "+"; 195 else if (start != i - 1) // closing time193 } else if (start != i - 1) { 196 194 addString += "-" + timeString(i - 1); 197 if (!ret.isEmpty()) 195 } 196 if (!ret.isEmpty()) { 198 197 ret += ","; 198 } 199 199 ret += addString; 200 200 } … … 204 204 205 205 public static String timeString(int minutes) { 206 return timeString(minutes, ClockSystem.TWENTYFOUR_HOURS); 207 } 208 209 public static String timeString(int minutes, ClockSystem hourMode) { 210 return timeString(minutes, hourMode, false); 211 } 212 213 public static String timeString(int minutes, ClockSystem hourMode, boolean showPeriod) { 206 214 int h = minutes / 60; 215 String period = ""; 216 if (hourMode == ClockSystem.TWELVE_HOURS) { 217 if (showPeriod) { 218 period = h <= 12 ? " AM" : " PM"; 219 } 220 h %= 12; 221 if (h == 0) { 222 h = 12; 223 } 224 } 207 225 int m = minutes % 60; 208 return (h < 10 ? "0" : "") + h + ":" + (m < 10 ? "0" : "") + m ;226 return (h < 10 ? "0" : "") + h + ":" + (m < 10 ? "0" : "") + m + period; 209 227 } 210 228 … … 218 236 private static boolean arraysEqual(boolean[] bs, boolean[] bs2) { 219 237 boolean ret = true; 220 for (int i = 0; i < bs.length; i++) 238 for (int i = 0; i < bs.length; i++) { 221 239 ret &= bs[i] == bs2[i]; 240 } 222 241 return ret; 223 242 } -
applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/gui/OheDialogPanel.java
r26002 r26215 16 16 import javax.swing.JPanel; 17 17 import javax.swing.JTextField; 18 import javax.swing.SwingUtilities;19 18 19 import org.openstreetmap.josm.plugins.ohe.ClockSystem; 20 20 import org.openstreetmap.josm.plugins.ohe.OhePlugin; 21 21 import org.openstreetmap.josm.plugins.ohe.OpeningTimeUtils; … … 41 41 private final String oldkey; 42 42 43 private ClockSystem clockSystem; 44 43 45 /** 44 46 * The Panel for editing the time-values. … … 50 52 * multiple values and their number of occurences 51 53 */ 52 public OheDialogPanel(OhePlugin plugin, String key, Object valuesToEdit) { 54 public OheDialogPanel(OhePlugin plugin, String key, Object valuesToEdit, ClockSystem clockSystem) { 55 this.clockSystem = clockSystem; 56 53 57 oldkey = key; 54 58 keyField = new JTextField(key); … … 177 181 actualPostionLabel.setText(positionText); 178 182 } 183 184 /** 185 * @return the hourMode 186 */ 187 public ClockSystem getHourMode() { 188 return clockSystem; 189 } 179 190 } -
applications/editors/josm/plugins/OpeningHoursEditor/src/org/openstreetmap/josm/plugins/ohe/gui/OheEditor.java
r26002 r26215 16 16 17 17 import org.openstreetmap.josm.Main; 18 import org.openstreetmap.josm.plugins.ohe.ClockSystem; 18 19 import org.openstreetmap.josm.plugins.ohe.OpeningTimeUtils; 19 20 import org.openstreetmap.josm.plugins.ohe.parser.OpeningTimeCompiler; 20 21 21 public class OheEditor extends JPanel implements MouseListener, 22 MouseMotionListener { 22 public class OheEditor extends JPanel implements MouseListener, 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, 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); 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 if (dialog.getHourMode() == ClockSystem.TWELVE_HOURS) { 50 g.setColor(new Color(255, 255, 218)); 51 g.fillRect(0, getMinutePosition(12 * 60), getWidth(), getHeight() - getMinutePosition(12 * 60)); 52 } 53 54 // horizontal Lines 55 for (int i = 1; i < 24; ++i) { 56 if (i % 3 == 0) { 57 g.setColor(Color.BLACK); 58 } else { 59 g.setColor(Color.LIGHT_GRAY); 60 } 61 62 g.drawLine(0, getMinutePosition(i * 60), getWidth(), getMinutePosition(i * 60)); 63 } 64 65 // vertical Lines 66 g.setColor(Color.BLACK); 67 for (int i = 1; i < 7; ++i) { 68 g.drawLine(getDayPosition(i), 0, getDayPosition(i), getHeight()); 69 } 70 71 // if a new Rect is dragged draw it 72 if (day0 >= 0) { 73 Graphics2D g2D = (Graphics2D) g; 74 75 int day2 = Math.min(day0, day1); 76 int day3 = Math.max(day0, day1); 77 int minute2 = Math.min(minute0, minute1); 78 int minute3 = Math.max(minute0, minute1); 79 Rectangle bounds = getPanelBoundsForTimeinterval(day2, day3 + 1, minute2, minute3); 80 81 TimeRect.drawTimeRect(g2D, bounds, minute2 == minute3, false); 82 } 83 } else { 84 g.setColor(Color.LIGHT_GRAY); 85 g.fillRect(0, 0, getWidth(), getHeight()); 86 } 87 } 88 }; 89 contentPanel.addMouseListener(this); 90 contentPanel.addMouseMotionListener(this); 91 contentPanel.setLayout(null); 92 contentPanel.setPreferredSize(new Dimension(180, 384)); 93 94 initTimeRects(); 95 96 scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 97 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 98 scrollPane.setViewportView(contentPanel); 99 100 // the upper Panel for showing Weekdays 101 scrollPane.setColumnHeaderView(new JPanel() { 102 @Override 103 public Dimension getPreferredSize() { 104 return new Dimension(contentPanel.getWidth(), dayAxisHeight); 105 } 106 107 @Override 108 public void paintComponent(Graphics g) { 109 g.setColor(Color.WHITE); 110 g.fillRect(0, 0, getWidth(), getHeight()); 111 112 g.setColor(Color.BLACK); 113 for (int i = 0; i < 7; ++i) { 114 if (i > 0) { 115 g.drawLine(getDayPosition(i) + 1, 0, getDayPosition(i) + 1, getHeight()); 116 } 117 118 String text = OpeningTimeCompiler.WEEKDAYS[i]; 119 g.drawString(text, (int) (getDayPosition(i + 0.5) - g.getFontMetrics().stringWidth(text) * 0.5), 120 (int) (dayAxisHeight * 0.5 + g.getFontMetrics().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 if (dialog.getHourMode() == ClockSystem.TWELVE_HOURS) { 137 g.setColor(new Color(255, 255, 218)); 138 g.fillRect(0, getMinutePosition(12 * 60), getWidth(), getHeight() - getMinutePosition(12 * 60)); 139 } 140 141 for (int i = 1; i < 24; ++i) { 142 if (i % 3 == 0) { 143 g.setColor(Color.BLACK); 144 String text = OpeningTimeUtils.timeString(i * 60, dialog.getHourMode(), false); 145 g.drawString(text, (timeAxisWidth - g.getFontMetrics().stringWidth(text)) / 2, 146 getMinutePosition(i * 60) + (int) (g.getFontMetrics().getHeight() * 0.35)); 147 } else { 148 g.setColor(Color.LIGHT_GRAY); 149 } 150 151 g.drawLine(getWidth() - 4, getMinutePosition(i * 60), getWidth(), getMinutePosition(i * 60)); 152 } 153 154 g.setColor(Color.BLACK); 155 String text = OpeningTimeUtils.timeString(0, dialog.getHourMode(), false); 156 g.drawString(text, (timeAxisWidth - g.getFontMetrics().stringWidth(text)) / 2, getMinutePosition(0) 157 + (int) (g.getFontMetrics().getHeight() * 1.0)); 158 if (dialog.getHourMode() == ClockSystem.TWELVE_HOURS) { 159 text = "AM"; 160 g.drawString(text, (timeAxisWidth - g.getFontMetrics().stringWidth(text)) / 2, getMinutePosition(0) 161 + (int) (g.getFontMetrics().getHeight() * 1.85)); 162 text = "PM"; 163 g.drawString(text, (timeAxisWidth - g.getFontMetrics().stringWidth(text)) / 2, 164 getMinutePosition(12 * 60) + (int) (g.getFontMetrics().getHeight() * 1.2)); 165 } 166 } 167 }); 168 169 setLayout(new BorderLayout()); 170 add(scrollPane, BorderLayout.CENTER); 158 171 } 159 172 160 173 // update all the TimeRects with new Data 161 174 public void initTimeRects() { 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(); 175 contentPanel.removeAll(); 176 177 ArrayList<int[]> time; 178 try { 179 time = dialog.getTime(); 180 } catch (Exception exc) { 181 setEnabled(false); 182 return; 183 } 184 185 setEnabled(true); 186 timeRects = new ArrayList<TimeRect>(); 187 if (time != null) { 188 for (int[] timeRectValues : time) { 189 int day0 = timeRectValues[0]; 190 int day1 = timeRectValues[1]; 191 int minute0 = timeRectValues[2]; 192 int minute1 = timeRectValues[3]; 193 TimeRect timeRect = new TimeRect(OheEditor.this, day0, day1, minute0, minute1); 194 timeRects.add(timeRect); 195 contentPanel.add(timeRect); 196 } 197 } 198 199 repositionTimeRects(); 200 repaint(); 189 201 } 190 202 191 203 protected void repositionTimeRects() { 192 if (timeRects != null) 193 for (TimeRect timeRect : timeRects) 194 timeRect.reposition(); 204 if (timeRects != null) { 205 for (TimeRect timeRect : timeRects) { 206 timeRect.reposition(); 207 } 208 } 195 209 } 196 210 197 211 // returns the physical Borders of the TimeRect on the mainPanel 198 public Rectangle getPanelBoundsForTimeinterval(int dayStart, int dayEnd, 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); 212 public Rectangle getPanelBoundsForTimeinterval(int dayStart, int dayEnd, int minutesStart, int minutesEnd) { 213 int x = getDayPosition(dayStart); 214 int y = getMinutePosition(minutesStart); 215 int width = getDayPosition(dayEnd) - getDayPosition(dayStart); 216 int height = getMinutePosition(minutesEnd) - getMinutePosition(minutesStart); 217 218 // work around openjdk bug 219 if (Main.isOpenjdk) { 220 x++; 221 y++; 222 } 223 224 if (minutesStart == minutesEnd) 225 return new Rectangle(x, y - 2 - TimeRect.verticalNonDrawedPixels, width, height + 5 + 2 226 * TimeRect.verticalNonDrawedPixels); 227 228 return new Rectangle(x, y, width, height + 1); 217 229 } 218 230 219 231 public double getDayWidth() { 220 232 return (contentPanel.getWidth() - 1) / 7.0; 221 233 } 222 234 223 235 public int getDayPosition(double d) { 224 236 return (int) (d * getDayWidth()); 225 237 } 226 238 227 239 public double getMinuteHeight() { 228 240 return (contentPanel.getHeight() - 1) / (24.0 * 60); 229 241 } 230 242 231 243 public int getMinutePosition(int minute) { 232 244 return (int) (minute * getMinuteHeight()); 233 245 } 234 246 235 247 // removes the given timerect from the panel and from the arraylist 236 248 public void removeTimeRect(TimeRect timeRectToRemove) { 237 238 239 240 249 timeRects.remove(timeRectToRemove); 250 contentPanel.remove(timeRectToRemove); 251 dialog.updateValueField(timeRects); 252 repaint(); 241 253 } 242 254 … … 255 267 @Override 256 268 public void mouseEntered(MouseEvent evt) { 257 269 mousePositionChanged(0, 0, true); 258 270 } 259 271 260 272 @Override 261 273 public void mouseExited(MouseEvent evt) { 262 274 mousePositionChanged(0, 0, false); 263 275 } 264 276 265 277 @Override 266 278 public void mousePressed(MouseEvent evt) { 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(); 279 day0 = (int) Math.floor(evt.getX() / getDayWidth()); 280 minute0 = (int) Math.floor(evt.getY() / (getMinuteHeight() * TimeRect.minuteResterize)) 281 * TimeRect.minuteResterize; 282 day1 = day0; 283 minute1 = minute0; 284 xDragStart = evt.getX(); 285 yDragStart = evt.getY(); 275 286 } 276 287 277 288 @Override 278 289 public void mouseReleased(MouseEvent evt) { 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 } 290 // mouse must be moved 5px before creating a rect 291 if (xDragStart == -1 || Math.abs(evt.getX() - xDragStart) + Math.abs(evt.getY() - yDragStart) > 5) { 292 int day2 = Math.min(day0, day1); 293 int day3 = Math.max(day0, day1); 294 int minute2 = Math.min(minute0, minute1); 295 int minute3 = Math.max(minute0, minute1); 296 297 TimeRect timeRect = new TimeRect(OheEditor.this, day2, day3, minute2, minute3); 298 timeRects.add(timeRect); 299 contentPanel.add(timeRect); 300 timeRect.reposition(); 301 dialog.updateValueField(timeRects); 302 303 day0 = -1; 304 repaint(); 305 } 298 306 } 299 307 300 308 @Override 301 309 public void mouseDragged(MouseEvent evt) { 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); 310 // mouse must be moved 5px before drawing a rect 311 if (xDragStart == -1 || Math.abs(evt.getX() - xDragStart) + Math.abs(evt.getY() - yDragStart) > 5) { 312 xDragStart = -1; 313 day1 = (int) Math.floor(evt.getX() / getDayWidth()); 314 minute1 = (int) Math.floor(evt.getY() / (getMinuteHeight() * TimeRect.minuteResterize)) 315 * TimeRect.minuteResterize; 316 317 // ensure that the new time is in a valid range 318 day1 = Math.max(day1, 0); 319 day1 = Math.min(day1, 6); 320 minute1 = Math.max(minute1, 0); 321 minute1 = Math.min(minute1, 24 * 60); 322 323 repaint(); 324 } 325 mousePositionChanged(evt.getX(), evt.getY(), true); 321 326 } 322 327 323 328 @Override 324 329 public void mouseMoved(MouseEvent evt) { 325 326 } 327 328 public void mousePositionChanged(int x, int y, 329 330 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("-"); 330 mousePositionChanged(evt.getX(), evt.getY(), true); 331 } 332 333 public void mousePositionChanged(int x, int y, boolean mouseInside) { 334 if (mouseInside) { 335 int actualDay = (int) Math.floor(x / getDayWidth()); 336 int minutes = (int) Math.floor(y / (getMinuteHeight() * TimeRect.minuteResterize)) 337 * TimeRect.minuteResterize; 338 actualDay = Math.max(0, Math.min(6, actualDay)); 339 minutes = Math.max(0, Math.min(24 * 60, minutes)); 340 dialog.setMousePositionText(OpeningTimeCompiler.WEEKDAYS[actualDay] + " " 341 + OpeningTimeUtils.timeString(minutes, dialog.getHourMode(), true)); 342 } else { 343 dialog.setMousePositionText("-"); 344 } 340 345 } 341 346 }
Note:
See TracChangeset
for help on using the changeset viewer.