KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > progress > ui > NbProgressBar


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.progress.ui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import javax.swing.JProgressBar JavaDoc;
27 import javax.swing.UIManager JavaDoc;
28 import org.netbeans.progress.spi.ExtractedProgressUIWorker;
29 import org.netbeans.progress.spi.InternalHandle;
30 import org.netbeans.progress.spi.ProgressEvent;
31
32
33 /**
34  * progress component, let just put the UI related issues here, update the state from outside
35
36  * @author mkleint
37  */

38 public class NbProgressBar extends JProgressBar JavaDoc implements ExtractedProgressUIWorker {
39     
40     static final String JavaDoc SLEEPY = "sleepy"; //NOI18N
41
boolean isSetup = false;
42     boolean usedInStatusBar = false;
43     //TODO these two ought to be created only when the the bar is used externally..
44
private JLabel JavaDoc detailLabel = new JLabel JavaDoc();
45     private JLabel JavaDoc mainLabel = new JLabel JavaDoc();
46     
47     /** Creates a new instance of NbProgressBar */
48     public NbProgressBar() {
49         super();
50         setOrientation(JProgressBar.HORIZONTAL);
51         setAlignmentX(0.5f);
52         setAlignmentY(0.5f);
53         Color JavaDoc fg = UIManager.getColor ("nbProgressBar.Foreground");
54         if (fg != null) {
55             setForeground(fg);
56         }
57         Color JavaDoc bg = UIManager.getColor ("nbProgressBar.Background");
58         if (bg != null) {
59             setBackground(bg);
60         }
61     }
62     
63     public void setUseInStatusBar(boolean use) {
64         usedInStatusBar = use;
65     }
66     
67     public Dimension JavaDoc getPreferredSize() {
68         Dimension JavaDoc supers = super.getPreferredSize();
69         if (usedInStatusBar) {
70             supers.width = ListComponent.ITEM_WIDTH / 3;
71         }
72         return supers;
73     }
74
75     
76 //--- these are used only when dealing with extracted component, when in status bar this is not used.
77
//------------------------------------
78

79     public void processProgressEvent(ProgressEvent event) {
80         if (event.getType() == ProgressEvent.TYPE_START || !isSetup || event.isSwitched()) {
81             setupBar(event.getSource(), this);
82             mainLabel.setText(event.getSource().getDisplayName());
83             isSetup = true;
84         }
85         if (event.getType() == ProgressEvent.TYPE_PROGRESS) {
86             if (event.getWorkunitsDone() > 0) {
87                 setValue(event.getWorkunitsDone());
88             }
89             setString(StatusLineComponent.getBarString(event.getPercentageDone(), event.getEstimatedCompletion()));
90             if (event.getDisplayName() != null) {
91                 mainLabel.setText(event.getDisplayName());
92             }
93             if (event.getMessage() != null) {
94                 detailLabel.setText(event.getMessage());
95             }
96             
97         } else if (event.getType() == ProgressEvent.TYPE_FINISH) {
98             boolean wasIndetermenite = isIndeterminate();
99             setIndeterminate(false);
100             setMaximum(event.getSource().getTotalUnits());
101             setValue(event.getSource().getTotalUnits());
102             if (wasIndetermenite) {
103                 setStringPainted(false);
104             } else {
105                 setString(StatusLineComponent.getBarString(100, -1));
106             }
107         }
108     }
109
110     public void processSelectedProgressEvent(ProgressEvent event) {
111         // ignore we'return always processing just one selected component
112
}
113     
114     
115     static void setupBar(InternalHandle handle, NbProgressBar bar) {
116         bar.putClientProperty(SLEEPY, null); //NIO18N
117
int total = handle.getTotalUnits();
118         if (handle.isInSleepMode()) {
119             bar.setStringPainted(true);
120             bar.setIndeterminate(false);
121             bar.setMaximum(1);
122             bar.setMinimum(0);
123             bar.setValue(0);
124             bar.putClientProperty(SLEEPY, new Object JavaDoc()); //NIO18N
125
} else if (total < 1) {
126             // macosx workaround..
127
bar.setValue(bar.getMaximum());
128             bar.setIndeterminate(true);
129             bar.setStringPainted(false);
130         } else {
131             bar.setStringPainted(true);
132             bar.setIndeterminate(false);
133             bar.setMaximum(total);
134             bar.setMinimum(0);
135             bar.setValue(0);
136         }
137         bar.setString(" ");
138     }
139
140     public JComponent JavaDoc getProgressComponent() {
141         return this;
142     }
143
144     public JLabel JavaDoc getMainLabelComponent() {
145         return mainLabel;
146     }
147
148     public JLabel JavaDoc getDetailLabelComponent() {
149         return detailLabel;
150     }
151 }
Popular Tags