1 19 20 package org.netbeans.modules.progress.ui; 21 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import javax.swing.JComponent ; 25 import javax.swing.JLabel ; 26 import javax.swing.JProgressBar ; 27 import javax.swing.UIManager ; 28 import org.netbeans.progress.spi.ExtractedProgressUIWorker; 29 import org.netbeans.progress.spi.InternalHandle; 30 import org.netbeans.progress.spi.ProgressEvent; 31 32 33 38 public class NbProgressBar extends JProgressBar implements ExtractedProgressUIWorker { 39 40 static final String SLEEPY = "sleepy"; boolean isSetup = false; 42 boolean usedInStatusBar = false; 43 private JLabel detailLabel = new JLabel (); 45 private JLabel mainLabel = new JLabel (); 46 47 48 public NbProgressBar() { 49 super(); 50 setOrientation(JProgressBar.HORIZONTAL); 51 setAlignmentX(0.5f); 52 setAlignmentY(0.5f); 53 Color fg = UIManager.getColor ("nbProgressBar.Foreground"); 54 if (fg != null) { 55 setForeground(fg); 56 } 57 Color 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 getPreferredSize() { 68 Dimension supers = super.getPreferredSize(); 69 if (usedInStatusBar) { 70 supers.width = ListComponent.ITEM_WIDTH / 3; 71 } 72 return supers; 73 } 74 75 76 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 } 113 114 115 static void setupBar(InternalHandle handle, NbProgressBar bar) { 116 bar.putClientProperty(SLEEPY, null); 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 ()); } else if (total < 1) { 126 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 getProgressComponent() { 141 return this; 142 } 143 144 public JLabel getMainLabelComponent() { 145 return mainLabel; 146 } 147 148 public JLabel getDetailLabelComponent() { 149 return detailLabel; 150 } 151 } | Popular Tags |