001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright � 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.timescale;
051:
052: import java.util.Calendar;
053:
054: import com.projity.pm.time.HasStartAndEnd;
055: import com.projity.util.DateTime;
056:
057: /**
058: *
059: */
060: public class TimeIterator implements HasStartAndEnd {
061: //protected int calendarField=Calendar.DAY_OF_WEEK;
062: //protected int calendarIncrement=1;
063: protected long startTime;
064: protected long endTime;
065: protected Calendar calendar1;
066: protected Calendar calendar2;
067: protected TimeScale scale;
068: private boolean hasNext = true;
069: private long next2 = -1;
070: private boolean useLargeScale;
071:
072: /**
073: * @param startTime
074: * @param endTime
075: */
076: public TimeIterator(long startTime, long endTime, TimeScale scale,
077: long startReference) {
078: this (startTime, endTime, scale, startReference, false);
079: }
080:
081: public TimeIterator(long startTime, long endTime, TimeScale scale,
082: long startReference, boolean useLargeScale) {
083: //System.out.println("TimeIterator: "+CalendarUtil.toString(startTime)+","+CalendarUtil.toString(endTime)+","+CalendarUtil.toString(startReference));
084: this .useLargeScale = useLargeScale;
085: long s;
086: long e;
087: if (startTime <= endTime) {
088: s = startTime;
089: e = endTime;
090: } else {
091: //this case appears with clip rectangles
092: e = startTime;
093: s = endTime;
094: }
095: long startRef = startReference;
096: this .startTime = s;
097: this .endTime = (s == e) ? (e + 1) : e;
098: this .scale = scale;
099:
100: if (useLargeScale) {
101: calendar2 = DateTime.calendarInstance();
102:
103: calendar2 = DateTime.calendarInstance();
104: calendar2.setTimeInMillis(s);
105: if (startRef == -1)
106: scale.floor2(calendar2);
107: else {
108: scale.floor2(calendar2, startRef);
109: }
110:
111: this .startTime = calendar2.getTimeInMillis();
112: } else {
113: calendar1 = DateTime.calendarInstance();
114:
115: calendar1 = DateTime.calendarInstance();
116: calendar1.setTimeInMillis(s);
117: if (startRef == -1)
118: scale.floor1(calendar1);
119: else {
120: scale.floor1(calendar1, startRef);
121: }
122:
123: calendar2 = DateTime.calendarInstance();
124: calendar2.setTimeInMillis(s);
125:
126: scale.floor2(calendar2, startRef);
127:
128: this .startTime = calendar1.getTimeInMillis();
129: }
130: }
131:
132: public TimeIterator(double startTime, double endTime,
133: TimeScale scale, long startReference) {
134: this (startTime, endTime, scale, startReference, false);
135: }
136:
137: public TimeIterator(double startTime, double endTime,
138: TimeScale scale, long startReference, boolean useLargeScale) {
139: this (CalendarUtil.toLongTime(startTime), CalendarUtil
140: .toLongTime(endTime), scale, startReference,
141: useLargeScale);
142: }
143:
144: public boolean hasNext() {
145: return hasNext;
146: }
147:
148: public TimeInterval next() {
149: if (!hasNext)
150: return null;
151: if (useLargeScale) {
152: long begin2 = calendar2.getTimeInMillis();
153: scale.increment2(calendar2);
154: long end2 = calendar2.getTimeInMillis();
155: if (end2 >= endTime)
156: hasNext = false;
157: //System.out.println("large begin2="+CalendarUtil.toString(begin2));
158: String text2 = scale.getText2(begin2);
159:
160: return new TimeInterval(begin2, end2, text2, -1L, -1L, null);
161: } else {
162: long begin1 = calendar1.getTimeInMillis();
163: scale.increment1(calendar1);
164: long end1 = calendar1.getTimeInMillis();
165: if (end1 >= endTime)
166: hasNext = false;
167: //System.out.println("begin1="+CalendarUtil.toString(begin1));
168: String text1 = scale.getText1(begin1);
169:
170: long begin2 = -1;
171: long end2 = -1;
172: String text2 = null;
173: if (next2 == -1 || begin1 >= next2) {
174: begin2 = calendar2.getTimeInMillis();
175: scale.increment2(calendar2);
176: end2 = calendar2.getTimeInMillis();
177: next2 = end2;
178: //System.out.println("begin2="+CalendarUtil.toString(begin2));
179: text2 = scale.getText2(begin2);
180: }
181: return new TimeInterval(begin1, end1, text1, begin2, end2,
182: text2);
183:
184: }
185: }
186:
187: /**
188: * @return Returns the endTime.
189: */
190: public long getEnd() {
191: return endTime;
192: }
193:
194: /**
195: * @return Returns the startTime.
196: */
197: public long getStart() {
198: return startTime;
199: }
200: }
|