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.graphic.spreadsheet;
051:
052: import java.awt.Rectangle;
053: import java.util.ArrayList;
054: import java.util.Enumeration;
055: import java.util.Iterator;
056: import java.util.List;
057:
058: import javax.swing.table.TableColumn;
059: import javax.swing.table.TableColumnModel;
060:
061: import com.projity.configuration.Dictionary;
062: import com.projity.field.FieldContext;
063: import com.projity.graphic.configuration.SpreadSheetCategories;
064: import com.projity.graphic.configuration.SpreadSheetFieldArray;
065: import com.projity.pm.graphic.gantt.GanttParamsImpl;
066: import com.projity.pm.graphic.spreadsheet.common.SpreadSheetRowHeaderColumnModel;
067: import com.projity.strings.Messages;
068:
069: public class SpreadSheetParamsImpl extends GanttParamsImpl implements
070: SpreadSheetParams {
071: protected String spreadsheetCategory;
072: protected SpreadSheetFieldArray fieldArray;
073: protected List<Integer> colWidth;
074: protected FieldContext fieldContext;
075: protected SpreadSheetColumnModel columnModel;
076: protected SpreadSheetRowHeaderColumnModel headerColumnModel;
077: protected int spreadSheetWidth = -1, idColMargin = 2,
078: colMargin = 2;
079:
080: //protected boolean printGantt;
081: // public SpreadSheetParamsImpl(){
082: // super();
083: // headerColumnModel=new SpreadSheetRowHeaderColumnModel();
084: // spreadsheetCategory=SpreadSheetCategories.taskSpreadsheetCategory;
085: // setFieldArray((SpreadSheetFieldArray) Dictionary.get(spreadsheetCategory,Messages.getString("Spreadsheet.Task.entry")));
086: // }
087: public SpreadSheetParamsImpl(SpreadSheetFieldArray fieldArray,
088: List<Integer> colWidth, boolean printGantt) {
089: super ();
090: headerColumnModel = new SpreadSheetRowHeaderColumnModel();
091: spreadsheetCategory = SpreadSheetCategories.taskSpreadsheetCategory;
092: setFieldArray(
093: fieldArray == null ? ((SpreadSheetFieldArray) Dictionary
094: .get(spreadsheetCategory, Messages
095: .getString("Spreadsheet.Task.entry")))
096: : fieldArray, fieldArray == null ? null
097: : colWidth);
098: setRightPartVisible(printGantt);
099:
100: }
101:
102: public SpreadSheetFieldArray getFieldArray() {
103: return fieldArray;
104: }
105:
106: public void setFieldArray(SpreadSheetFieldArray fieldArray,
107: List<Integer> colWidth) {
108: this .fieldArray = fieldArray;
109: this .colWidth = colWidth;
110: columnModel = new SpreadSheetColumnModel(fieldArray, colWidth);
111: columnModel.setSvg(true);
112: initColumns(columnModel, fieldArray.size());
113: initColumns(headerColumnModel, 1);
114:
115: fieldContext = new FieldContext();
116: fieldContext.setLeftAssociation(true);
117:
118: updateWidth();
119: }
120:
121: public String getSpreadsheetCategory() {
122: return spreadsheetCategory;
123: }
124:
125: public FieldContext getFieldContext() {
126: return fieldContext;
127: }
128:
129: public void initColumns(TableColumnModel cm, int columnCount) {
130: while (cm.getColumnCount() > 0) {
131: cm.removeColumn(cm.getColumn(0));
132: }
133:
134: // int index=0;
135: // for (Iterator i=fieldArray.iterator();i.hasNext();index++) {
136: // Field field=(Field)i.next();
137: // TableColumn c = new TableColumn(index);
138: // c.setHeaderValue(field.getName());
139: // cm.addColumn(c);
140: // }
141: for (int col = 0; col < columnCount; col++) {
142: TableColumn c = new TableColumn(col);
143: c.setHeaderValue("" + col);
144: cm.addColumn(c);
145: }
146: }
147:
148: public Rectangle getSpreadSheetBounds() {
149: //return isLeftPartVisible()?(new Rectangle(0,configuration.getColumnHeaderHeight(),spreadSheetWidth,configuration.getRowHeight()*cache.getSize())):null;
150: return new Rectangle(0, configuration.getColumnHeaderHeight(),
151: spreadSheetWidth, configuration.getRowHeight()
152: * cache.getSize());
153: }
154:
155: public Rectangle getDrawingBounds() {
156: return new Rectangle(
157: 0,
158: configuration.getColumnHeaderHeight(),
159: (isLeftPartVisible() ? (getSpreadSheetBounds().width)
160: : 0)
161: + (isRightPartVisible() ? (getGanttBounds().width)
162: : 0), getSpreadSheetBounds().height
163: + configuration.getColumnHeaderHeight());
164: }
165:
166: public int getSpreadSheetWidth() {
167: //return isLeftPartVisible()?spreadSheetWidth:0;
168: return spreadSheetWidth;
169: }
170:
171: public SpreadSheetColumnModel getColumnModel() {
172: return columnModel;
173: }
174:
175: public SpreadSheetRowHeaderColumnModel getHeaderColumnModel() {
176: return headerColumnModel;
177: }
178:
179: public Iterator getColumnIterator() {
180: return new Iterator() {
181: protected Enumeration headerE = headerColumnModel
182: .getColumns();
183: protected Enumeration e = columnModel.getColumns();
184:
185: public boolean hasNext() {
186: return headerE.hasMoreElements() || e.hasMoreElements();
187: }
188:
189: public Object next() {
190: if (headerE.hasMoreElements())
191: return headerE.nextElement();
192: else
193: return e.nextElement();
194: }
195:
196: public void remove() {
197: throw new UnsupportedOperationException();
198: }
199: };
200: }
201:
202: public void updateDrawingBounds() {
203: updateWidth();
204: }
205:
206: private void updateWidth() {
207: //if (isLeftPartVisible()){
208: spreadSheetWidth = getConfiguration().getRowHeaderWidth() + 2
209: * idColMargin;
210: TableColumn c;
211: for (Enumeration e = columnModel.getColumns(); e
212: .hasMoreElements();) {
213: c = (TableColumn) e.nextElement();
214: int cwidth = c.getPreferredWidth() + 2 * colMargin;
215: spreadSheetWidth += cwidth;
216: }
217: //} else spreadSheetWidth=0;
218: }
219:
220: public void setPrintBounds(Rectangle printBounds) {
221: super .setPrintBounds(printBounds);
222: updatePages();
223: }
224:
225: private void updatePages() {
226: if (printBounds == null)
227: return;
228: if (colPageInfo == null)
229: colPageInfo = new ArrayList();
230: else
231: colPageInfo.clear();
232: if (rowPageInfo == null)
233: rowPageInfo = new ArrayList();
234: else
235: rowPageInfo.clear();
236:
237: int w = getConfiguration().getRowHeaderWidth() + 2
238: * idColMargin;
239: TableColumn c;
240: int start = 0;
241: int current = 1;
242: int x = 0;
243: for (Enumeration e = columnModel.getColumns(); e
244: .hasMoreElements(); current++) {
245: c = (TableColumn) e.nextElement();
246: if (w + c.getPreferredWidth() + 2 * colMargin > printBounds.width) {
247: colPageInfo.add(new PageInfo(start, current - 1, x, w));
248: start = current;
249: x += w;
250: w = 0;
251: }
252: w += c.getPreferredWidth() + 2 * colMargin;
253: }
254: colPageInfo.add(new PageInfo(start, current - 1, x, w));
255:
256: int rowsPerPage = getRowsPerPage();
257: int count = cache.getSize();
258: int pageCount = count / rowsPerPage;
259: for (current = 0; current < pageCount; current++)
260: rowPageInfo.add(new PageInfo(current * rowsPerPage,
261: (current + 1) * rowsPerPage - 1, current
262: * rowsPerPage
263: * configuration.getRowHeight(), rowsPerPage
264: * configuration.getRowHeight()));
265: int lastRows = count % rowsPerPage;
266: if (lastRows != 0)
267: rowPageInfo.add(new PageInfo(current * rowsPerPage,
268: count - 1, current * rowsPerPage
269: * configuration.getRowHeight(), lastRows
270: * configuration.getRowHeight()));
271: }
272:
273: public class PageInfo {
274: protected int start, end, x, width;
275:
276: public PageInfo(int start, int end, int x, int width) {
277: super ();
278: this .start = start;
279: this .end = end;
280: this .x = x;
281: this .width = width;
282: }
283:
284: public int getEnd() {
285: return end;
286: }
287:
288: public void setEnd(int end) {
289: this .end = end;
290: }
291:
292: public int getStart() {
293: return start;
294: }
295:
296: public void setStart(int start) {
297: this .start = start;
298: }
299:
300: public int getWidth() {
301: return width;
302: }
303:
304: public void setWidth(int width) {
305: this .width = width;
306: }
307:
308: public int getX() {
309: return x;
310: }
311:
312: public void setX(int x) {
313: this .x = x;
314: }
315: }
316:
317: protected List colPageInfo, rowPageInfo;
318:
319: public PageInfo getRowPageInfo(int row) {
320: return (row >= rowPageInfo.size()) ? null
321: : (PageInfo) rowPageInfo.get(row);
322: }
323:
324: public PageInfo getColPageInfo(int col) {
325: return (col >= colPageInfo.size()) ? null
326: : (PageInfo) colPageInfo.get(col);
327: }
328:
329: public int getRowsPerPage() {
330: return (printBounds.height
331: - configuration.getColumnHeaderHeight() - configuration
332: .getPrintFooterHeight())
333: / configuration.getRowHeight();
334: }
335:
336: public Rectangle getSpreadsheetPrintBounds(int row, int col,
337: boolean ifVisibleOnly) {
338: if (ifVisibleOnly && !isLeftPartVisible())
339: return null;
340: PageInfo colInfo = getColPageInfo(col);
341: PageInfo rowInfo = getRowPageInfo(row);
342: if (colInfo == null || rowInfo == null)
343: return null;
344: return new Rectangle(colInfo.getX(), rowInfo.getX()
345: + configuration.getColumnHeaderHeight(), colInfo
346: .getWidth(), rowInfo.getWidth());
347: }
348:
349: public Rectangle getGanttPrintBounds(int row, int col) {
350: if (!isRightPartVisible())
351: return null;
352: int spLastCol = colPageInfo.size() - 1;
353: if (col < spLastCol && isLeftPartVisible())
354: return null;
355: int totalWidth = getGanttBounds().width;
356: Rectangle spreadsheetBounds = getSpreadsheetPrintBounds(row,
357: spLastCol, true);
358: if ((col == spLastCol || !isLeftPartVisible())
359: && spreadsheetBounds != null) {
360: int x = spreadsheetBounds.width;
361: int width = getPrintBounds().width
362: - spreadsheetBounds.width;
363: //if (x+width>totalWidth) width=totalWidth-x;
364: if (width > totalWidth)
365: width = totalWidth;
366: Rectangle ganttBounds = new Rectangle(0,
367: spreadsheetBounds.y
368: - configuration.getColumnHeaderHeight(),
369: width, spreadsheetBounds.height);
370: if (ganttBounds.width == 0)
371: return null;
372: else
373: return ganttBounds;
374: } else {
375: spreadsheetBounds = getSpreadsheetPrintBounds(row,
376: spLastCol, false);
377: int x = -getGanttDeltaX(row, col);
378: int width = getPrintBounds().width;
379: if (x + width > totalWidth)
380: width = totalWidth - x;
381: Rectangle ganttBounds = new Rectangle(x,
382: spreadsheetBounds == null ? 0 : spreadsheetBounds.y
383: - configuration.getColumnHeaderHeight(),
384: width, spreadsheetBounds == null ? 0
385: : spreadsheetBounds.height);
386: return ganttBounds;
387: }
388: }
389:
390: public int getGanttDeltaX(int row, int col) {
391: int spLastCol = colPageInfo.size() - 1;
392: if (col < spLastCol && isLeftPartVisible())
393: return -1;
394: Rectangle spreadsheetBounds = getSpreadsheetPrintBounds(row,
395: spLastCol, true);
396: if (spreadsheetBounds == null)
397: return -col * getPrintBounds().width;
398: else
399: return spreadsheetBounds.width - (col - spLastCol)
400: * getPrintBounds().width;
401: }
402:
403: public int getPrintCols() {
404: int spColCount = colPageInfo.size();
405: PageInfo colInfo = getColPageInfo(spColCount - 1);
406: int width = (isLeftPartVisible() ? colInfo.width : 0)
407: + (isRightPartVisible() ? getGanttBounds().width : 0);
408: return (isLeftPartVisible() ? (spColCount - 1) : 0)
409: + (int) Math.ceil(width / getPrintBounds().getWidth());
410: }
411:
412: public int getPrintRows() {
413: return rowPageInfo.size();
414: }
415:
416: }
|