001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * ReportProgressBar.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.commonswing;
030:
031: import javax.swing.JProgressBar;
032: import javax.swing.SwingUtilities;
033:
034: import org.jfree.report.event.ReportProgressEvent;
035: import org.jfree.report.event.ReportProgressListener;
036:
037: public class ReportProgressBar extends JProgressBar implements
038: ReportProgressListener {
039: private class ScreenUpdateRunnable implements Runnable {
040: /**
041: * This is the event upon which we will update the report progress information
042: */
043: private ReportProgressEvent reportProgressEvent;
044:
045: /**
046: * Constructor for the screen updatable thread
047: */
048: protected ScreenUpdateRunnable() {
049: }
050:
051: public ReportProgressEvent getReportProgressEvent() {
052: return reportProgressEvent;
053: }
054:
055: public void setReportProgressEvent(
056: final ReportProgressEvent reportProgressEvent) {
057: this .reportProgressEvent = reportProgressEvent;
058: }
059:
060: /**
061: * Performs the process of actually updaing the UI
062: */
063: public synchronized void run() {
064: if (reportProgressEvent == null) {
065: return;
066: }
067:
068: setValue((int) computePercentageComplete(reportProgressEvent));
069: reportProgressEvent = null;
070: }
071:
072: public boolean update(final ReportProgressEvent event) {
073: final boolean retval = (reportProgressEvent == null);
074: this .reportProgressEvent = event;
075: return retval;
076: }
077: }
078:
079: /**
080: * Indicates if this process is only for pagination
081: */
082: private boolean onlyPagination;
083: private final ScreenUpdateRunnable runnable;
084:
085: /**
086: * Creates a horizontal progress bar that displays a border but no progress string. The
087: * initial and minimum values are 0, and the maximum is 100.
088: *
089: * @see #setOrientation
090: * @see #setBorderPainted
091: * @see #setStringPainted
092: * @see #setString
093: * @see #setIndeterminate
094: */
095: public ReportProgressBar() {
096: super (JProgressBar.HORIZONTAL, 0, 100);
097: this .runnable = new ScreenUpdateRunnable();
098: }
099:
100: public boolean isOnlyPagination() {
101: return onlyPagination;
102: }
103:
104: public void setOnlyPagination(final boolean onlyPagination) {
105: this .onlyPagination = onlyPagination;
106: }
107:
108: public void reportProcessingStarted(final ReportProgressEvent event) {
109: synchronized (runnable) {
110: if (runnable.update(event)) {
111: if (SwingUtilities.isEventDispatchThread()) {
112: runnable.run();
113: } else {
114: SwingUtilities.invokeLater(runnable);
115: }
116: }
117: }
118: }
119:
120: public void reportProcessingUpdate(final ReportProgressEvent event) {
121: synchronized (runnable) {
122: if (runnable.update(event)) {
123: if (SwingUtilities.isEventDispatchThread()) {
124: runnable.run();
125: } else {
126: SwingUtilities.invokeLater(runnable);
127: }
128: }
129: }
130: }
131:
132: public void reportProcessingFinished(final ReportProgressEvent event) {
133: synchronized (runnable) {
134: if (runnable.update(event)) {
135: if (SwingUtilities.isEventDispatchThread()) {
136: runnable.run();
137: } else {
138: SwingUtilities.invokeLater(runnable);
139: }
140: }
141: }
142: }
143:
144: /**
145: * Computes the percentage complete (on a scale from 0.0 to 100.0) based on the
146: * information found in the report progress event.
147: * @param event the data used to calculate the percentage complete
148: * @return the calculated percentage complete
149: */
150: protected double computePercentageComplete(
151: final ReportProgressEvent event) {
152: final double maximumLevel;
153: final double level;
154: if (isOnlyPagination()) {
155: maximumLevel = event.getMaximumLevel();
156: level = event.getLevel();
157: } else {
158: maximumLevel = event.getMaximumLevel() + 1;
159: if (event.getActivity() == ReportProgressEvent.GENERATING_CONTENT) {
160: level = event.getLevel() + 1;
161: } else {
162: level = event.getLevel();
163: }
164: }
165: final double levelPercentage = level / maximumLevel;
166: final double levelSizePercentage = 1.0 / maximumLevel;
167: final double subPercentage = levelSizePercentage
168: * (event.getRow() / (double) event.getMaximumRow());
169: final double percentage = 100.0 * (levelPercentage + subPercentage);
170: return Math.max(0.0, Math.min(100.0, percentage));
171: }
172: }
|