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.Graphics;
053: import java.awt.Graphics2D;
054: import java.awt.Rectangle;
055: import java.awt.geom.Line2D;
056: import java.util.Iterator;
057:
058: import javax.swing.JComponent;
059: import javax.swing.table.TableColumn;
060:
061: import com.projity.field.Field;
062: import com.projity.pm.graphic.Renderer;
063: import com.projity.pm.graphic.graph.GraphParams;
064: import com.projity.pm.graphic.model.cache.GraphicNode;
065: import com.projity.pm.graphic.spreadsheet.renderer.OfflineRenderer;
066:
067: public class SpreadSheetRenderer extends Renderer {
068: // protected Stroke cellStroke=new BasicStroke(0.25f);
069: // protected Stroke spreadSheetStroke=new BasicStroke(0.5f);
070: // protected Color cellColor=Color.GRAY;
071: // protected Color spreadSheetColor=Color.BLACK;
072:
073: protected SpreadSheetParams params;
074:
075: public SpreadSheetRenderer(GraphParams graphInfo) {
076: super (graphInfo);
077: params = (SpreadSheetParams) graphInfo;
078: }
079:
080: public SpreadSheetRenderer() {
081: super ();
082: }
083:
084: public void paint(Graphics g) {
085: paint(g, -1, -1);
086: }
087:
088: public void paint(Graphics g, int prow, int pcol) {
089: Graphics2D g2 = (Graphics2D) g;
090: int h = params.getSpreadSheetBounds().y;
091: int rowH = params.getRowHeight();
092: int row0 = 0;
093: int row1 = Integer.MAX_VALUE;
094: int col0 = 0;
095: int col1 = Integer.MAX_VALUE;
096: Rectangle spreadsheetBounds = params.getSpreadSheetBounds();
097: if (prow != -1) {
098: SpreadSheetParamsImpl.PageInfo colInfo = params
099: .getColPageInfo(pcol);
100: SpreadSheetParamsImpl.PageInfo rowInfo = params
101: .getRowPageInfo(prow);
102: col0 = colInfo.getStart();
103: col1 = colInfo.getEnd();
104: row0 = rowInfo.getStart();
105: row1 = rowInfo.getEnd();
106: Rectangle printSpreadsheetBounds = ((SpreadSheetParamsImpl) params)
107: .getSpreadsheetPrintBounds(prow, pcol, false);
108: spreadsheetBounds = new Rectangle(spreadsheetBounds.x,
109: spreadsheetBounds.y, printSpreadsheetBounds.width,
110: printSpreadsheetBounds.height);
111: }
112: // System.out.println("spreadsheetBounds="+spreadsheetBounds);
113: int row = row0;
114: for (Iterator i = graphInfo.getCache().getIterator(row0); i
115: .hasNext()
116: && row <= row1; row++) {
117: GraphicNode gnode = (GraphicNode) i.next();
118: paintRow(g2, row, row0, h, col0, col1, gnode,
119: spreadsheetBounds);
120: h += rowH;
121: }
122: paintColumnHeader(g2, col0, col1, spreadsheetBounds);
123: }
124:
125: private int idColMargin = 2, colMargin = 2, idColumnIndex = 0;
126:
127: protected int getColMargin(int colIndex) {
128: return (idColumnIndex == colIndex) ? idColMargin : colMargin;
129: }
130:
131: protected void paintColumnHeader(Graphics2D g2, int col0, int col1,
132: Rectangle spreadsheetBounds) {
133: TableColumn c;
134: int w = spreadsheetBounds.x;
135: int h = spreadsheetBounds.y
136: - params.getConfiguration().getColumnHeaderHeight();
137: int col = 0;
138: for (Iterator i = params.getColumnIterator(); i.hasNext()
139: && col <= col1; col++) {
140: c = (TableColumn) i.next();
141: if (col < col0)
142: continue;
143:
144: int cwidth = c.getPreferredWidth() + 2 * getColMargin(col);
145:
146: OfflineRenderer renderer = (OfflineRenderer) c
147: .getHeaderRenderer();
148: if (renderer != null) { //rowHeader is null
149: JComponent component = (JComponent) renderer
150: .getComponent(
151: ((Field) params.getFieldArray()
152: .get(col)).getName(),
153: null,
154: (Field) params.getFieldArray().get(col),
155: params);
156: boolean opaque = component.isOpaque();
157: //component.setDoubleBuffered(false);
158: component.setOpaque(false);
159: //component.setForeground(Color.BLACK);
160: component.setSize(cwidth, params.getConfiguration()
161: .getColumnHeaderHeight());
162: g2.translate(w, h);
163: component.doLayout();
164: //g2.setClip(0, 0, cwidth, params.getConfiguration().getColumnHeaderHeight());
165: component.print(g2);
166: //g2.setClip(null);
167: g2.translate(-w, -h);
168: component.setOpaque(opaque);
169: }
170: w += cwidth;
171: // g2.setStroke(spreadSheetStroke);
172: // g2.setColor(spreadSheetColor);
173: g2.draw(new Line2D.Double(w, h, w, spreadsheetBounds
174: .getMaxY()));
175: //g2.drawLine(w,h,w,spreadsheetBounds.y+spreadsheetBounds.height);
176: }
177: }
178:
179: protected void paintRow(Graphics2D g2, int row, int row0, int h,
180: int col0, int col1, GraphicNode node,
181: Rectangle spreadsheetBounds) {
182: TableColumn c;
183: int w = spreadsheetBounds.x;
184: int col = 0;
185: for (Iterator i = params.getColumnIterator(); i.hasNext()
186: && col <= col1; col++) {
187: c = (TableColumn) i.next();
188: if (col < col0)
189: continue;
190: //cell content
191: //GraphicNode node = SpreadSheetUtils.getNodeFromCacheRow(row,1/*rowMultiple*/,params.getCache());
192: Object value = SpreadSheetUtils.getValueAt(node.getNode(),
193: col, params.getCache(), params.getColumnModel(),
194: params.getFieldContext());
195:
196: Field field = (Field) params.getFieldArray().get(col);
197:
198: int compWidth = c.getPreferredWidth();
199: int cwidth = compWidth + 2 * getColMargin(col);
200:
201: OfflineRenderer renderer = (OfflineRenderer) c
202: .getCellRenderer();
203: JComponent component = (JComponent) renderer.getComponent(
204: value, node, field, params);
205: //component.setDoubleBuffered(false);
206: boolean opaque = component.isOpaque();
207: component.setOpaque(false);
208: //component.setForeground(Color.BLACK);
209: component.setSize(compWidth, params.getRowHeight());
210: g2.translate(w + getColMargin(col), h);
211: //g2.setClip(0, 0, compWidth, params.getRowHeight());
212: component.doLayout();
213: component.print(g2);
214: //g2.setClip(null);
215: g2.translate(-w - getColMargin(col), -h);
216: component.setOpaque(opaque);
217: w += cwidth;
218: }
219: if (row != row0)
220: g2.draw(new Line2D.Double(0, h, w, h));
221: }
222:
223: }
|