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.pm.dependency;
051:
052: import java.text.FieldPosition;
053: import java.text.MessageFormat;
054: import java.text.NumberFormat;
055: import java.text.ParseException;
056: import java.text.ParsePosition;
057: import java.util.Collection;
058:
059: import com.projity.association.AssociationFormat;
060: import com.projity.association.AssociationFormatParameters;
061: import com.projity.configuration.Settings;
062: import com.projity.datatype.Duration;
063: import com.projity.datatype.DurationFormat;
064: import com.projity.field.FieldParseException;
065: import com.projity.options.GeneralOption;
066: import com.projity.pm.task.Task;
067: import com.projity.strings.Messages;
068:
069: public class DependencyFormat extends AssociationFormat {
070: public static DependencyFormat getInstance(
071: AssociationFormatParameters parameters) {
072: return new DependencyFormat(parameters);
073: }
074:
075: private DependencyFormat(AssociationFormatParameters parameters) {
076: super (parameters);
077: }
078:
079: private String getErrorMessage(String text) {
080: String errorMessage = MessageFormat
081: .format(
082: Messages
083: .getString("Message.invalidDependency.mf"),
084: new Object[] { parameters.isLeftAssociation() ? Messages
085: .getString("Text.predecessor")
086: : Messages.getString("Text.successor") });
087: return errorMessage;
088: }
089:
090: private static NumberFormat integerFormat = NumberFormat
091: .getIntegerInstance();
092:
093: private Object doParse(String string, ParsePosition pos)
094: throws ParseException {
095: Long number = (Long) integerFormat.parseObject(string, pos);
096: if (number == null)
097: throw new ParseException(getErrorMessage(string), pos
098: .getIndex());
099:
100: Object found = null;
101: Collection container = getContainer(parameters
102: .isLeftAssociation());
103: if (container != null)
104: found = parameters.getIdField().find(number, container);
105:
106: if (found == null) { //TODO this should probably be moved to finder
107:
108: if (GeneralOption.getInstance()
109: .isAutomaticallyAddNewResourcesAndTasks()) {
110: found = createNewObject(parameters.isLeftAssociation());
111: try {
112: parameters.getIdField().setText(found,
113: number.toString(), null);
114: } catch (FieldParseException e) {
115: throw new ParseException(e.getMessage(), 0); //TODO don't know about this - can it happen?
116: }
117: } else {
118: throw new ParseException(getErrorMessage(string), pos
119: .getIndex());
120: }
121: }
122: Integer type = (Integer) DependencyType.Format.getInstance()
123: .parseObject(string, pos);
124:
125: if (type == null)
126: throw new ParseException(getErrorMessage(string), pos
127: .getIndex());
128:
129: Duration duration;
130: String durationPart = string.substring(pos.getIndex()).trim();
131:
132: if (durationPart.length() == 0) { // if a duration was entered, use it, otherwise 0
133: duration = Duration.ZERO;
134: } else {
135: duration = (Duration) DurationFormat.getInstance()
136: .parseObject(string, pos);
137: if (duration == null)
138: throw new ParseException(getErrorMessage(string), pos
139: .getIndex());
140: }
141: return Dependency
142: .getInstance(
143: parameters.isLeftAssociation() ? (HasDependencies) found
144: : (HasDependencies) parameters
145: .getThisObject(),
146: parameters.isLeftAssociation() ? (HasDependencies) parameters
147: .getThisObject()
148: : (HasDependencies) found, type
149: .intValue(), duration
150: .getEncodedMillis());
151:
152: }
153:
154: /*
155: * (non-Javadoc)
156: *
157: * @see java.text.Format#parseObject(java.lang.String,
158: * java.text.ParsePosition)
159: */
160: public Object parseObject(String string, ParsePosition pos) {
161: try {
162: return doParse(string, pos);
163: } catch (ParseException e) {
164: parameters.setError(e.getMessage());
165: return null;
166: }
167: }
168:
169: /**
170: * convert to text. The format is either, 123, 123FF, or 123FS-1d
171: */
172: public StringBuffer format(Object dependencyObject,
173: StringBuffer string, FieldPosition fieldPos) {
174: Dependency dependency = (Dependency) dependencyObject;
175: Task task = (Task) ((parameters.isLeftAssociation()) ? dependency
176: .getPredecessor()
177: : dependency.getSuccessor());
178: string.append(parameters.getIdField().getValue(task, null));
179: boolean hasLag = !Duration.isZero(dependency.getLag());
180:
181: StringBuffer details = new StringBuffer();
182: if (!DependencyType.isDefault(dependency.getDependencyType())
183: || hasLag)
184: details.append(DependencyType.mapValueToString(new Integer(
185: dependency.getDependencyType())));
186:
187: Duration duration = new Duration(dependency.getLag()); // use duration format to format duration
188: if (hasLag) {
189: details.append(DurationFormat.getSignedInstance().format(
190: duration));
191: }
192: if (details.length() != 0) {
193: if (parameters.isEncloseInBrackets())
194: string.append(Settings.LEFT_BRACKET);
195: string.append(details);
196: if (parameters.isEncloseInBrackets())
197: string.append(Settings.RIGHT_BRACKET);
198: }
199: return string;
200: }
201:
202: /* (non-Javadoc)
203: * @see com.projity.association.AssociationFormat#getContainerOfLeft(java.lang.Object)
204: */
205: protected Collection getContainer(boolean left) {
206: return ((Task) parameters.getThisObject()).getProject()
207: .getTasks();
208: }
209:
210: protected Object createNewObject(boolean left) {
211: return ((Task) parameters.getThisObject()).getProject()
212: .newNormalTaskInstance(); //TODO this should not search only in current
213: }
214:
215: }
|