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.chart;
051:
052: import java.awt.Color;
053: import java.awt.Dimension;
054: import java.awt.Paint;
055: import java.awt.Rectangle;
056: import java.text.FieldPosition;
057: import java.text.Format;
058: import java.text.NumberFormat;
059: import java.text.ParsePosition;
060:
061: import javax.swing.JComponent;
062: import javax.swing.JMenuItem;
063: import javax.swing.JPopupMenu;
064: import javax.swing.JScrollPane;
065: import javax.swing.JViewport;
066: import javax.swing.Scrollable;
067: import javax.swing.SwingConstants;
068: import javax.swing.UIManager;
069: import javax.swing.border.Border;
070: import javax.swing.event.ChangeEvent;
071: import javax.swing.event.ChangeListener;
072: import javax.swing.plaf.UIResource;
073:
074: import org.jfree.chart.ChartPanel;
075: import org.jfree.chart.ChartRenderingInfo;
076: import org.jfree.chart.JFreeChart;
077: import org.jfree.chart.axis.NumberAxis;
078: import org.jfree.chart.labels.CustomXYToolTipGenerator;
079: import org.jfree.data.xy.XYDataset;
080:
081: import com.projity.datatype.Money;
082: import com.projity.field.Field;
083: import com.projity.graphic.configuration.shape.PredefinedStroke;
084: import com.projity.pm.assignment.HasTimeDistributedData;
085: import com.projity.pm.graphic.timescale.CoordinatesConverter;
086: import com.projity.pm.graphic.timescale.ScaledComponent;
087:
088: public class TimeChartPanel extends ChartPanel implements Scrollable,
089: ScaledComponent {
090: private static final long serialVersionUID = 2034704461047717965L;
091:
092: ChartInfo chartInfo;
093:
094: JViewport viewport;
095:
096: /**
097: * @param chart
098: */
099: public TimeChartPanel(ChartInfo chartInfo) {
100: super (chartInfo.setChart(buildChart(chartInfo.getModel())),
101: true);
102: this .chartInfo = chartInfo;
103: setMaximumDrawWidth(4000);
104: setMaximumDrawHeight(1000);
105: }
106:
107: // protected JScrollPane scrollPane;
108: public void configureScrollPaneHeaders(JScrollPane scrollPane,
109: JComponent rowHeader) {
110: viewport = scrollPane.getViewport();
111: if (viewport == null || viewport.getView() != this )
112: return;
113:
114: JViewport vp = new JViewport();
115: vp.setView(rowHeader);
116: vp.setPreferredSize(rowHeader.getPreferredSize());
117: scrollPane.setRowHeader(vp);
118:
119: scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER,
120: new ChartCorner(this ));
121:
122: Border border = scrollPane.getBorder();
123: if (border == null || border instanceof UIResource) {
124: scrollPane.setBorder(UIManager
125: .getBorder("Table.scrollPaneBorder"));
126: }
127:
128: // left scale synchro
129: viewport.addChangeListener(new ChangeListener() {
130: public void stateChanged(ChangeEvent e) {
131: updateTimeScaleComponentSize();
132: }
133: });
134:
135: }
136:
137: // left scale synchro
138: private Dimension olddmain = null;
139:
140: public void updateTimeScaleComponentSize() {
141: Dimension dmain = viewport.getViewSize();
142:
143: if (dmain.equals(olddmain))
144: return;
145: olddmain = dmain;
146: Dimension d = chartInfo.getAxisPanel().getPreferredSize();
147: d.setSize(d.getWidth(), dmain.getHeight());
148: chartInfo.getAxisPanel().revalidate();
149: }
150:
151: protected JFreeChart buildChart() {
152: JFreeChart newChart = ChartHelper.createChart(chartInfo
153: .getModel().getDataset(), chartInfo.isHistogram(),
154: chartInfo.getModel().getSecondDataset());
155: NumberFormat numberFormat = NumberFormat.getPercentInstance(); // default
156: Object[] traces = chartInfo.getTraces();
157: // chartInfo.getModel().dumpDataset(traces);
158: if (!chartInfo.isSimple()
159: && (traces.length > 0 && traces[0] instanceof Field)) {
160: Field field = (Field) traces[0];
161: if (field.isMoney()) {
162: numberFormat = new NumberFormat() {
163:
164: @Override
165: public StringBuffer format(double number,
166: StringBuffer toAppendTo, FieldPosition pos) {
167: return toAppendTo.append(Money.formatCurrency(
168: number, true));
169: }
170:
171: @Override
172: public StringBuffer format(long number,
173: StringBuffer toAppendTo, FieldPosition pos) {
174: // TODO Auto-generated method stub
175: return null;
176: }
177:
178: @Override
179: public Number parse(String source,
180: ParsePosition parsePosition) {
181: // TODO Auto-generated method stub
182: return null;
183: }
184: };
185: } else {
186: Format format = field.getFormat();
187: if (format instanceof NumberFormat)
188: numberFormat = (NumberFormat) format;
189: else
190: numberFormat = NumberFormat.getNumberInstance();
191: }
192: }
193:
194: ((NumberAxis) newChart.getXYPlot().getRangeAxis())
195: .setNumberFormatOverride(numberFormat);
196: return newChart;
197: }
198:
199: public static JFreeChart buildChart(ChartModel model) {
200: return ChartHelper.createChart(model.getDataset(), true, model
201: .getSecondDataset());
202: }
203:
204: public void updateChart() {
205: JFreeChart chart = chartInfo.getChart();
206: final Object[] traces = chartInfo.getTraces();
207: setChart(chart);
208:
209: Color color;
210: Paint paint;
211: int series = 0;
212: for (int i = 0; i < traces.length; i++) {
213: color = ChartHelper.getColorForField(traces[i]);
214:
215: if (traces[i] == HasTimeDistributedData.AVAILABILITY) {
216: chart.getXYPlot().getRenderer(1).setSeriesPaint(0,
217: color);
218: chart.getXYPlot().getRenderer(1).setSeriesStroke(0,
219: PredefinedStroke.LARGE_FRAMED);
220: continue; // do not increment series
221: }
222:
223: chart.getXYPlot().getRenderer().setSeriesPaint(series,
224: color);
225:
226: chart.getXYPlot().getRenderer().setToolTipGenerator(
227: new CustomXYToolTipGenerator() {
228: public String generateToolTip(XYDataset data,
229: int series, int item) {
230: return traces[0] + " ";
231: }
232:
233: });
234: series++; // excludes availability from count
235: }
236: // chart.getXYPlot().addRangeMarker(new ValueMarker(1.0));
237:
238: chart.getXYPlot().getDomainAxis().setLowerBound(
239: chartInfo.getCoord().getOrigin());
240: chart.getXYPlot().getDomainAxis().setUpperBound(
241: Math.max(chartInfo.getCoord().getEnd(), chartInfo
242: .getCoord().toTime(viewport.getWidth())));
243: }
244:
245: /**
246: * Gets space used by legend and headers if any
247: *
248: * @return
249: */
250: public double getNonPlotHeight() {
251: ChartRenderingInfo info = getChartRenderingInfo();
252: return info.getChartArea().getHeight()
253: - info.getPlotInfo().getDataArea().getHeight();
254: }
255:
256: /*
257: * (non-Javadoc)
258: *
259: * @see com.projity.pm.graphic.timescale.ScaledComponent#setCoord(com.projity.pm.graphic.timescale.CoordinatesConverter)
260: */
261: public void setCoord(CoordinatesConverter coord) {
262: chartInfo.setCoord(coord);
263: }
264:
265: /*
266: * (non-Javadoc)
267: *
268: * @see com.projity.pm.graphic.timescale.ScaledComponent#getCoord()
269: */
270: public CoordinatesConverter getCoord() {
271: return chartInfo.getCoord();
272: }
273:
274: protected boolean verticalScrolling = false;
275:
276: /**
277: * @return Returns the verticalScrolling.
278: */
279: public boolean isVerticalScrolling() {
280: return verticalScrolling;
281: }
282:
283: /**
284: * @param verticalScrolling
285: * The verticalScrolling to set.
286: */
287: public void setVerticalScrolling(boolean verticalScrolling) {
288: this .verticalScrolling = verticalScrolling;
289: }
290:
291: public Dimension getPreferredScrollableViewportSize() {
292: return getPreferredSize();
293: }
294:
295: public int getScrollableBlockIncrement(Rectangle visibleRect,
296: int orientation, int direction) {
297: return (orientation == SwingConstants.VERTICAL) ? visibleRect.height
298: : visibleRect.width;
299: }
300:
301: public boolean getScrollableTracksViewportHeight() {
302: if (getParent() instanceof JViewport) {
303: if (((JViewport) getParent()).getHeight() > getPreferredSize().height)
304: return true;
305: else
306: return !verticalScrolling;
307: }
308: return false;
309: }
310:
311: public boolean getScrollableTracksViewportWidth() {
312: if (getParent() instanceof JViewport) {
313: return (((JViewport) getParent()).getWidth() > getPreferredSize().width);
314: }
315: return false;
316: }
317:
318: public int getScrollableUnitIncrement(Rectangle visibleRect,
319: int orientation, int direction) {
320: if (orientation == SwingConstants.VERTICAL) {
321: return 2;
322: }
323: return 4;
324: }
325:
326: JMenuItem verticalScrollingItem;
327:
328: protected JPopupMenu createPopupMenu(boolean arg0, boolean arg1,
329: boolean arg2, boolean arg3) {
330: JPopupMenu menu = super .createPopupMenu(false, arg1, arg2,
331: false); // hide
332: // properties
333: // and
334: // zoom
335: // menu.add(new JSeparator());
336: // menu.add(verticalScrollingItem = TimeChartPopupMenu.buildVerticalScrollingItem(this));
337: return menu;
338: }
339:
340: }
|