1 22 package org.jrobin.graph; 23 24 import org.jrobin.core.XmlWriter; 25 26 import java.util.*; 27 import java.text.SimpleDateFormat ; 28 29 34 public class TimeAxisUnit 35 { 36 private static final int[] calendarUnit = 40 { 41 Calendar.SECOND, 42 Calendar.MINUTE, 43 Calendar.HOUR_OF_DAY, 44 Calendar.DAY_OF_MONTH, 45 Calendar.WEEK_OF_YEAR, 46 Calendar.MONTH, 47 Calendar.YEAR 48 }; 49 50 public static final int SECOND = 0; 52 public static final int MINUTE = 1; 53 public static final int HOUR = 2; 54 public static final int DAY = 3; 55 public static final int WEEK = 4; 56 public static final int MONTH = 5; 57 public static final int YEAR = 6; 58 59 public static final int MONDAY = Calendar.MONDAY; 61 public static final int TUESDAY = Calendar.TUESDAY; 62 public static final int WEDNESDAY = Calendar.WEDNESDAY; 63 public static final int THURSDAY = Calendar.THURSDAY; 64 public static final int FRIDAY = Calendar.FRIDAY; 65 public static final int SATURDAY = Calendar.SATURDAY; 66 public static final int SUNDAY = Calendar.SUNDAY; 67 68 private static final String [] UNIT_NAMES = { 69 "SECOND", "MINUTE", "HOUR", "DAY", "WEEK", "MONTH", "YEAR" 70 }; 71 72 private static final String [] DAY_NAMES = { 73 "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" 74 }; 75 76 private int minGridTimeUnit = HOUR; private int minGridUnitSteps = 1; 78 private int majGridTimeUnit = HOUR; private int majGridUnitSteps = 6; 80 81 private boolean centerLabels = false; 82 private SimpleDateFormat dateFormat = new SimpleDateFormat ("HH:mm", Locale.ENGLISH ); 83 84 private int firstDayOfWeek = MONDAY; 86 87 107 TimeAxisUnit( int minGridTimeUnit, int minGridUnitSteps, 108 int majGridTimeUnit, int majGridUnitSteps, 109 SimpleDateFormat dateFormat, boolean centerLabels, int firstDayOfWeek ) 110 { 111 this.minGridTimeUnit = minGridTimeUnit; 112 this.minGridUnitSteps = minGridUnitSteps; 113 this.majGridTimeUnit = majGridTimeUnit; 114 this.majGridUnitSteps = majGridUnitSteps; 115 this.dateFormat = new SimpleDateFormat ( dateFormat.toPattern(), Locale.ENGLISH ); 116 this.centerLabels = centerLabels; 117 this.firstDayOfWeek = firstDayOfWeek; 118 } 119 120 121 131 TimeMarker[] getTimeMarkers( long start, long stop ) 132 { 133 start *= 1000; stop *= 1000; 135 136 Calendar cMaj = Calendar.getInstance(); 137 Calendar cMin = Calendar.getInstance(); 138 139 setStartPoint(cMaj, majGridTimeUnit, start); 141 setStartPoint(cMin, minGridTimeUnit, start); 142 143 long minPoint = cMin.getTimeInMillis(); 145 long majPoint = cMaj.getTimeInMillis(); 146 147 while ( majPoint < start ) 148 majPoint = getNextPoint(cMaj, majGridTimeUnit, majGridUnitSteps); 149 while ( minPoint < start ) 150 minPoint = getNextPoint(cMin, minGridTimeUnit, minGridUnitSteps); 151 152 ArrayList markerList = new ArrayList(); 153 154 while ( minPoint <= stop && majPoint <= stop ) 161 { 162 if ( minPoint < majPoint ) 163 { 164 markerList.add( new TimeMarker( minPoint, "", false ) ); 165 minPoint = getNextPoint( cMin, minGridTimeUnit, minGridUnitSteps ); 166 } 167 else if ( minPoint == majPoint ) { 169 markerList.add( new TimeMarker( majPoint, dateFormat.format(cMaj.getTime()), true ) ); 170 majPoint = getNextPoint( cMaj, majGridTimeUnit, majGridUnitSteps ); 171 minPoint = getNextPoint( cMin, minGridTimeUnit, minGridUnitSteps ); 172 } 173 else 174 { 175 markerList.add( new TimeMarker( majPoint, dateFormat.format(cMaj.getTime()), true ) ); 176 majPoint = getNextPoint( cMaj, majGridTimeUnit, majGridUnitSteps ); 177 } 178 } 179 180 while ( minPoint <= stop ) 181 { 182 markerList.add( new TimeMarker( minPoint, "", false ) ); 183 minPoint = getNextPoint( cMin, minGridTimeUnit, minGridUnitSteps ); 184 } 185 186 while ( majPoint <= stop ) 187 { 188 markerList.add( new TimeMarker( majPoint, dateFormat.format(cMaj.getTime()), true ) ); 189 majPoint = getNextPoint( cMaj, majGridTimeUnit, majGridUnitSteps ); 190 } 191 192 return (TimeMarker[]) markerList.toArray( new TimeMarker[0] ); 193 } 194 195 196 200 long getMajorGridWidth() 201 { 202 Calendar c = Calendar.getInstance(); 203 long now = c.getTimeInMillis() / 1000; 204 205 c.add( calendarUnit[majGridTimeUnit], majGridUnitSteps ); 206 207 return (c.getTimeInMillis() / 1000) - now; 208 } 209 210 boolean getCenterLabels() { 211 return centerLabels; 212 } 213 214 int getMinGridTimeUnit() { 215 return minGridTimeUnit; 216 } 217 218 int getMinGridUnitSteps() { 219 return minGridUnitSteps; 220 } 221 222 int getMajGridTimeUnit() { 223 return majGridTimeUnit; 224 } 225 226 int getMajGridUnitSteps() { 227 return majGridUnitSteps; 228 } 229 230 boolean isCenterLabels() { 231 return centerLabels; 232 } 233 234 public SimpleDateFormat getDateFormat() { 235 return dateFormat; 236 } 237 238 239 249 private void setStartPoint( Calendar t, int unit, long exactStart ) 250 { 251 t.setTimeInMillis( exactStart ); 252 t.setFirstDayOfWeek( firstDayOfWeek ); 253 254 for (int i = 0; i < HOUR && i <= unit; i++) 255 t.set( calendarUnit[i], 0 ); 256 257 if ( unit >= HOUR ) 258 t.set( Calendar.HOUR_OF_DAY, 0 ); 259 260 if ( unit == WEEK ) 261 t.set( Calendar.DAY_OF_WEEK, t.getFirstDayOfWeek() ); 262 else if ( unit == MONTH ) 263 t.set( Calendar.DAY_OF_MONTH, 1 ); 264 else if ( unit == YEAR ) 265 { 266 t.set( Calendar.DATE, 1 ); 267 t.set( Calendar.MONTH, 0 ); 268 } 269 } 270 271 278 private long getNextPoint( Calendar t, int unit, int unitSteps ) 279 { 280 t.add( calendarUnit[unit], unitSteps ); 281 282 return t.getTimeInMillis(); 283 } 284 285 static String getUnitName(int unit) { 286 return UNIT_NAMES[unit]; 287 } 288 289 static String getDayName( int dayIndex ) { 290 return DAY_NAMES[dayIndex]; 291 } 292 293 void exportXmlTemplate(XmlWriter xml) { 294 xml.startTag("time_axis"); 295 xml.writeTag("min_grid_time_unit", TimeAxisUnit.getUnitName(getMinGridTimeUnit())); 296 xml.writeTag("min_grid_unit_steps", getMinGridUnitSteps()); 297 xml.writeTag("maj_grid_time_unit", TimeAxisUnit.getUnitName(getMajGridTimeUnit())); 298 xml.writeTag("maj_grid_unit_steps", getMajGridUnitSteps()); 299 xml.writeTag("date_format", getDateFormat().toPattern()); 300 xml.writeTag("center_labels", getCenterLabels()); 301 302 if ( firstDayOfWeek != MONDAY ) 303 xml.writeTag( "first_day_of_week", getDayName(firstDayOfWeek) ); 304 305 xml.closeTag(); } 307 308 } 309 | Popular Tags |