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.dialog.calendar;
051:
052: import java.util.Calendar;
053: import java.util.Collection;
054: import java.util.Comparator;
055: import java.util.Iterator;
056: import java.util.SortedSet;
057: import java.util.TreeSet;
058:
059: import org.jdesktop.swing.calendar.DateSpan;
060:
061: import com.projity.pm.time.HasStartAndEnd;
062: import com.projity.util.DateTime;
063:
064: /**
065: *
066: */
067: public class Intervals extends TreeSet implements HasStartAndEnd {
068:
069: /**
070: *
071: */
072: public Intervals() {
073: super (new Comparator() {
074: public int compare(Object o1, Object o2) {
075: HasStartAndEnd d1 = (HasStartAndEnd) o1; //Only want to compare DateSpan no need to use instanceof
076: HasStartAndEnd d2 = (HasStartAndEnd) o2;
077: if (d1.getStart() < d2.getStart()
078: || (d1.getStart() == d2.getStart() && d1
079: .getEnd() < d2.getEnd()))
080: return -1;
081: else if (d1.getStart() > d2.getStart()
082: || (d1.getStart() == d2.getStart() && d1
083: .getEnd() > d2.getEnd()))
084: return 1;
085: else
086: return 0;
087: }
088: });
089: }
090:
091: protected HasStartAndEnd createInterval(long start, long end) {
092: return new DateSpan(start, end);
093: }
094:
095: protected HasStartAndEnd mergeIntervals(HasStartAndEnd i1,
096: HasStartAndEnd i2) {
097: return new DateSpan(Math.min(i1.getStart(), i2.getStart()),
098: Math.max(i1.getEnd(), i2.getEnd()));
099: }
100:
101: public boolean add(Object o) {
102: HasStartAndEnd toAdd = (HasStartAndEnd) o;
103: SortedSet set = headSet(o);
104: if (set.size() > 0) {
105: HasStartAndEnd interval = (HasStartAndEnd) set.last();
106: if (interval.getEnd() >= toAdd.getStart())
107: toAdd = mergeIntervals(toAdd, interval);
108: }
109:
110: set = tailSet(o);
111: if (set.size() > 0) {
112: HasStartAndEnd interval = (HasStartAndEnd) set.first();
113: if (toAdd.getEnd() >= interval.getStart())
114: toAdd = mergeIntervals(toAdd, interval);
115: }
116: return super .add(toAdd);
117: }
118:
119: public boolean addAll(Collection c) {
120: if (c == null)
121: return false;
122: boolean added = false;
123: for (Iterator i = c.iterator(); i.hasNext();) {
124: if (super .add(i.next()))
125: added = true;
126: }
127: return added;
128: }
129:
130: public long getEnd() {
131: return (size() == 0) ? -1 : ((HasStartAndEnd) last()).getEnd();
132: }
133:
134: public long getStart() {
135: return (size() == 0) ? -1 : ((HasStartAndEnd) first())
136: .getStart();
137: }
138:
139: public boolean containsDate(long date) {
140: for (Iterator i = iterator(); i.hasNext();) { //a more optimized version can be found
141: HasStartAndEnd interval = (HasStartAndEnd) i.next();
142: if (interval.getStart() <= date
143: && date <= interval.getEnd())
144: return true;
145: }
146: return false;
147: }
148:
149: void eliminateWeekdayDuplicates(boolean weekDays[]) {
150: Calendar cal = DateTime.calendarInstance();
151: for (Iterator i = iterator(); i.hasNext();) { //a more optimized version can be found
152: HasStartAndEnd interval = (HasStartAndEnd) i.next();
153: cal.setTimeInMillis(interval.getStart());
154: int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
155:
156: // remove any days which correspond to a selected week day
157: for (int d = 0; d < 7; d++) {
158: if (weekDays[d] && d == dayOfWeek) {
159: i.remove();
160: }
161: }
162: }
163: }
164:
165: /*public void xorAdd(HasStartAndEnd o,Closure removeFunctor,Closure addFunctor){
166: HasStartAndEnd toAdd=o;
167: SortedSet set=headSet(o);
168: if (set.size()>0){
169: HasStartAndEnd previous=(HasStartAndEnd)set.last();
170: if (previous.getEnd()>=toAdd.getStart()){
171: remove(previous);
172: if (previous.getEnd()==toAdd.getStart()){
173: toAdd=createInterval(previous.getStart(),toAdd.getEnd());
174: }else{
175: if (previous.getStart()<toAdd.getStart())
176: super.add(createInterval(previous.getStart(),toAdd.getStart()));
177: removeFunctor.execute(createInterval(toAdd.getStart(),previous.getEnd()));
178: toAdd=createInterval(previous.getEnd(),toAdd.getEnd());
179: }
180: }
181: }
182:
183: set=tailSet(o);
184: if (set.size()>0){
185: HasStartAndEnd next=(HasStartAndEnd)set.first();
186: if (next.getStart()<=toAdd.getEnd()){
187: remove(next);
188: if (next.getStart()==toAdd.getEnd()){
189: createInterval(toAdd.getStart(),next.getEnd());
190: }else{
191: if (next.getEnd()>toAdd.getEnd())
192: super.add(createInterval(toAdd.getEnd(),next.getEnd()));
193: removeFunctor.execute(createInterval(next.getStart(),toAdd.getEnd()));
194: toAdd=createInterval(toAdd.getStart(),next.getStart());
195: }
196: }
197: }
198:
199: if (toAdd.getStart()<=toAdd.getEnd()){
200: super.add(toAdd);
201: addFunctor.execute(toAdd);
202: }
203:
204: }*/
205: }
|