001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.progress.ui;
043:
044: import java.awt.Color;
045: import java.awt.Dimension;
046: import javax.swing.JComponent;
047: import javax.swing.JLabel;
048: import javax.swing.JProgressBar;
049: import javax.swing.UIManager;
050: import org.netbeans.progress.spi.ExtractedProgressUIWorker;
051: import org.netbeans.progress.spi.InternalHandle;
052: import org.netbeans.progress.spi.ProgressEvent;
053:
054: /**
055: * progress component, let just put the UI related issues here, update the state from outside
056:
057: * @author mkleint
058: */
059: public class NbProgressBar extends JProgressBar implements
060: ExtractedProgressUIWorker {
061:
062: static final String SLEEPY = "sleepy"; //NOI18N
063: boolean isSetup = false;
064: boolean usedInStatusBar = false;
065: //TODO these two ought to be created only when the the bar is used externally..
066: private JLabel detailLabel = new JLabel();
067: private JLabel mainLabel = new JLabel();
068:
069: /** Creates a new instance of NbProgressBar */
070: public NbProgressBar() {
071: super ();
072: setOrientation(JProgressBar.HORIZONTAL);
073: setAlignmentX(0.5f);
074: setAlignmentY(0.5f);
075: Color fg = UIManager.getColor("nbProgressBar.Foreground");
076: if (fg != null) {
077: setForeground(fg);
078: }
079: Color bg = UIManager.getColor("nbProgressBar.Background");
080: if (bg != null) {
081: setBackground(bg);
082: }
083: }
084:
085: public void setUseInStatusBar(boolean use) {
086: usedInStatusBar = use;
087: }
088:
089: public Dimension getPreferredSize() {
090: Dimension super s = super .getPreferredSize();
091: if (usedInStatusBar) {
092: super s.width = ListComponent.ITEM_WIDTH / 3;
093: }
094: return super s;
095: }
096:
097: //--- these are used only when dealing with extracted component, when in status bar this is not used.
098: //------------------------------------
099:
100: public void processProgressEvent(ProgressEvent event) {
101: if (event.getType() == ProgressEvent.TYPE_START || !isSetup
102: || event.isSwitched()) {
103: setupBar(event.getSource(), this );
104: mainLabel.setText(event.getSource().getDisplayName());
105: isSetup = true;
106: }
107: if (event.getType() == ProgressEvent.TYPE_PROGRESS) {
108: if (event.getWorkunitsDone() > 0) {
109: setValue(event.getWorkunitsDone());
110: }
111: setString(StatusLineComponent.getBarString(event
112: .getPercentageDone(), event
113: .getEstimatedCompletion()));
114: if (event.getDisplayName() != null) {
115: mainLabel.setText(event.getDisplayName());
116: }
117: if (event.getMessage() != null) {
118: detailLabel.setText(event.getMessage());
119: }
120:
121: } else if (event.getType() == ProgressEvent.TYPE_FINISH) {
122: boolean wasIndetermenite = isIndeterminate();
123: setIndeterminate(false);
124: setMaximum(event.getSource().getTotalUnits());
125: setValue(event.getSource().getTotalUnits());
126: if (wasIndetermenite) {
127: setStringPainted(false);
128: } else {
129: setString(StatusLineComponent.getBarString(100, -1));
130: }
131: }
132: }
133:
134: public void processSelectedProgressEvent(ProgressEvent event) {
135: // ignore we'return always processing just one selected component
136: }
137:
138: static void setupBar(InternalHandle handle, NbProgressBar bar) {
139: bar.putClientProperty(SLEEPY, null); //NIO18N
140: int total = handle.getTotalUnits();
141: if (handle.isInSleepMode()) {
142: bar.setStringPainted(true);
143: bar.setIndeterminate(false);
144: bar.setMaximum(1);
145: bar.setMinimum(0);
146: bar.setValue(0);
147: bar.putClientProperty(SLEEPY, new Object()); //NIO18N
148: } else if (total < 1) {
149: // macosx workaround..
150: bar.setValue(bar.getMaximum());
151: bar.setIndeterminate(true);
152: bar.setStringPainted(false);
153: } else {
154: bar.setStringPainted(true);
155: bar.setIndeterminate(false);
156: bar.setMaximum(total);
157: bar.setMinimum(0);
158: bar.setValue(0);
159: }
160: bar.setString(" ");
161: }
162:
163: public JComponent getProgressComponent() {
164: return this ;
165: }
166:
167: public JLabel getMainLabelComponent() {
168: return mainLabel;
169: }
170:
171: public JLabel getDetailLabelComponent() {
172: return detailLabel;
173: }
174: }
|