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.graphic.configuration;
051:
052: import java.awt.Point;
053: import java.io.IOException;
054: import java.io.ObjectInputStream;
055: import java.io.ObjectOutputStream;
056: import java.io.Serializable;
057: import java.util.ArrayList;
058: import java.util.Collection;
059: import java.util.Iterator;
060: import java.util.LinkedHashMap;
061: import java.util.Map;
062:
063: import org.apache.commons.digester.Digester;
064:
065: import com.projity.configuration.Configuration;
066: import com.projity.configuration.Dictionary;
067:
068: import com.projity.configuration.NamedItem;
069: import com.projity.field.Field;
070: import com.projity.pm.assignment.TimeDistributedHelper;
071: import com.projity.strings.Messages;
072: import com.projity.workspace.SavableToWorkspace;
073: import com.projity.workspace.WorkspaceSetting;
074:
075: /**
076: *
077: */
078: public class SpreadSheetFieldArray extends ArrayList implements
079: NamedItem, Cloneable, WorkspaceSetting {
080: private static final long serialVersionUID = 6310711336308730391L;
081: transient Map map = new LinkedHashMap();
082: transient boolean userCreated = false;
083: ArrayList<Integer> widths = new ArrayList<Integer>();
084:
085: public Object clone() {
086: return super .clone();
087: }
088:
089: public SpreadSheetFieldArray() {
090:
091: }
092:
093: public SpreadSheetFieldArray makeUserDefinedCopy() {
094: SpreadSheetFieldArray newOne = (SpreadSheetFieldArray) clone();
095: newOne.setId(null); // it's user defined
096: newOne.setName(Dictionary.generateUniqueName(this ));
097: newOne.userCreated = true;
098: return newOne;
099: }
100:
101: public SpreadSheetFieldArray makeEditableVersion() {
102: SpreadSheetFieldArray f = this ;
103: if (!f.isUserDefined()) {
104: f = f.makeUserDefinedCopy();
105: Dictionary.add(f);
106: }
107: return f;
108: }
109:
110: public SpreadSheetFieldArray insertField(int position, Field field) {
111: SpreadSheetFieldArray f = makeEditableVersion();
112: f.add(position, field);
113: f.widths.add(field.getColumnWidth());
114: return f;
115: }
116:
117: public SpreadSheetFieldArray removeField(int position) {
118: SpreadSheetFieldArray f = makeEditableVersion();
119: f.remove(position);
120: f.widths.remove(position);
121: return f;
122: }
123:
124: public SpreadSheetFieldArray move(int oldPosition, int newPosition) {
125: SpreadSheetFieldArray f = makeEditableVersion();
126: Field field = (Field) f.remove(oldPosition);
127: Integer w = f.widths.remove(oldPosition);
128: SpreadSheetFieldArray result = f
129: .insertField(newPosition, field);
130: result.widths.set(newPosition, w);
131: return result;
132:
133: }
134:
135: public void setWidth(int column, int width) {
136: widths.set(column, width);
137: }
138:
139: /**
140: * Equality is based on name, not on contents
141: */
142: public boolean equals(Object arg0) {
143: if (!(arg0 instanceof SpreadSheetFieldArray))
144: return false;
145: return name == ((SpreadSheetFieldArray) arg0).getName();
146: }
147:
148: private String name = null;
149: private String category;
150: private String cellStyleId;
151: private String actionListId;
152: private String id = null;
153:
154: public String getCellStyleId() {
155: return cellStyleId;
156: }
157:
158: public void setCellStyleId(String cellStyleId) {
159: this .cellStyleId = cellStyleId;
160: }
161:
162: public CellStyle getCellStyle() {
163: CellStyles cellStyles = CellStyles.getInstance();
164: if (cellStyleId == null || cellStyleId.length() == 0)
165: return cellStyles.getDefaultStyle();
166: CellStyle style = cellStyles.getStyle(cellStyleId);
167: if (style == null)
168: style = cellStyles.getDefaultStyle();
169: return style;
170: }
171:
172: public String getActionListId() {
173: return actionListId;
174: }
175:
176: public void setActionListId(String actionListId) {
177: this .actionListId = actionListId;
178: }
179:
180: public ActionList getActionList() {
181: ActionLists actionLists = ActionLists.getInstance();
182: if (actionListId == null || actionListId.length() == 0)
183: return actionLists.getDefaultActionList();
184: ActionList actionList = actionLists.getActionList(actionListId);
185: if (actionList == null)
186: actionList = actionLists.getDefaultActionList();
187: return actionList;
188: }
189:
190: /**
191: * @return Returns the name.
192: */
193: public String getName() {
194: return name;
195: }
196:
197: public void setId(String messageId) {
198: this .id = messageId;
199: if (name == null)
200: setName(Messages.getString(messageId));
201: }
202:
203: /**
204: * @param name The name to set.
205: */
206: public void setName(String name) {
207: this .name = name;
208: }
209:
210: public String toString() {
211: return getName();
212: }
213:
214: /**
215: * @return Returns the category.
216: */
217: public String getCategory() {
218: return category;
219: }
220:
221: /**
222: * @param category The category to set.
223: */
224: public void setCategory(String category) {
225: this .category = category;
226: }
227:
228: public boolean isUserDefined() {
229: return id == null;
230: }
231:
232: public void addField(String fieldId) {
233: Field field = Configuration.getFieldFromId(fieldId);
234: if (field != null) {
235: if (mapFieldTo != null) {
236: map.put(fieldId, mapFieldTo);
237: mapFieldTo = null;
238: }
239:
240: add(field);
241: widths.add(field.getColumnWidth());
242: } else {
243: // System.out.println("field is null in SpreadSheetFieldArray addField : ");
244: }
245: }
246:
247: public void removeField(String fieldId) {
248: if (fieldId == null)
249: return;
250: map.remove(fieldId);
251: for (int i = 0; i < size(); i++) {
252: Field field = (Field) get(i);
253: if (fieldId.equals(field.getId())) {
254: remove(i);
255: widths.remove(i);
256: }
257:
258: }
259: }
260:
261: public String mapFieldTo;
262:
263: //root node needs to be Dictionary
264: public static void addDigesterEvents(Digester digester) {
265: digester
266: .addObjectCreate("*/spreadsheet",
267: "com.projity.graphic.configuration.SpreadSheetFieldArray");
268: digester.addSetProperties("*/spreadsheet");
269: digester.addSetNext("*/spreadsheet", "add",
270: "com.projity.configuration.NamedItem");
271: digester.addSetProperties("*/spreadsheet/columns/column");
272: digester.addCallMethod("*/spreadsheet/columns/column",
273: "addField", 0);
274:
275: }
276:
277: /**
278: * @return
279: */
280: public Object next() {
281: // TODO Auto-generated method stub
282: return null;
283: }
284:
285: public static final SpreadSheetFieldArray getFromId(
286: String category, String id) {
287: SpreadSheetFieldArray result = (SpreadSheetFieldArray) Dictionary
288: .get(category, Messages.getString(id));
289: if (result == null)
290: result = (SpreadSheetFieldArray) Dictionary.get(category,
291: id);
292: return result;
293: }
294:
295: public final String getMapFieldTo() {
296: return mapFieldTo;
297: }
298:
299: public final void setMapFieldTo(String mapFieldTo) {
300: this .mapFieldTo = mapFieldTo;
301: }
302:
303: public final String getMappedValue(String key) {
304: return (String) map.get(key);
305: }
306:
307: public static Object[] toIdArray(Object[] fieldArray) {
308: Object[] result = new Object[fieldArray.length];
309: for (int i = 0; i < fieldArray.length; i++)
310: result[i] = TimeDistributedHelper
311: .getIdForObject(fieldArray[i]);
312: return result;
313: }
314:
315: public static Object[] fromIdArray(Object[] fieldArray) {
316: Object[] result = new Object[fieldArray.length];
317: for (int i = 0; i < fieldArray.length; i++)
318: result[i] = TimeDistributedHelper
319: .getObjectFromId((String) fieldArray[i]);
320: return result;
321: }
322:
323: public static Collection toIdArray(Collection fieldArray) {
324: ArrayList result = new ArrayList(fieldArray.size());
325: Iterator i = fieldArray.iterator();
326: while (i.hasNext()) {
327: result.add(TimeDistributedHelper.getIdForObject(i.next()));
328: }
329: return result;
330: }
331:
332: public static Collection fromIdArray(Collection fieldArray) {
333: ArrayList result = new ArrayList(fieldArray.size());
334: Iterator i = fieldArray.iterator();
335: while (i.hasNext()) {
336: result.add(TimeDistributedHelper.getObjectFromId((String) i
337: .next()));
338: }
339: return result;
340: }
341:
342: public boolean isUserCreated() {
343: return userCreated;
344: }
345:
346: public void setUserCreated(boolean userCreated) {
347: this .userCreated = userCreated;
348: }
349:
350: public int getWidth(int column) {
351: return widths.get(column);
352: }
353:
354: public WorkspaceSetting createWorkspace(int context) {
355: Workspace ws = new Workspace();
356: ws.fields.addAll(toIdArray(this ));
357: ws.widths.addAll(widths);
358: return ws;
359: }
360:
361: public void restoreWorkspace(WorkspaceSetting w, int context) {
362: Workspace ws = (Workspace) w;
363: addAll(fromIdArray(ws.fields));
364: widths.addAll(ws.widths);
365: }
366:
367: public static class Workspace implements WorkspaceSetting {
368: private static final long serialVersionUID = -4517935309304612237L;
369: ArrayList<Integer> widths = new ArrayList<Integer>();
370: ArrayList fields = new ArrayList();
371: }
372:
373: }
|