001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Graphics;
023: import java.io.Serializable;
024:
025: import javax.accessibility.Accessible;
026: import javax.accessibility.AccessibleContext;
027: import javax.accessibility.AccessibleRole;
028: import javax.accessibility.AccessibleState;
029: import javax.accessibility.AccessibleStateSet;
030: import javax.accessibility.AccessibleValue;
031: import javax.swing.event.ChangeEvent;
032: import javax.swing.event.ChangeListener;
033: import javax.swing.plaf.ProgressBarUI;
034:
035: import org.apache.harmony.x.swing.StringConstants;
036:
037: import org.apache.harmony.x.swing.internal.nls.Messages;
038:
039: public class JProgressBar extends JComponent implements SwingConstants,
040: Accessible {
041: protected int orientation = JProgressBar.HORIZONTAL;
042: protected boolean paintBorder = true;;
043: protected BoundedRangeModel model;
044: protected String progressString;
045: protected boolean paintString;
046: protected transient ChangeEvent changeEvent;
047: protected ChangeListener changeListener = createChangeListener();
048:
049: private boolean indeterminate;
050: private static final String UI_CLASS_ID = "ProgressBarUI";
051: private static final String BORDER_PAINTED_PROPERTY = "borderPainted";
052: private static final String PROGRESS_STRING_PROPERTY = "string";
053:
054: protected class AccessibleJProgressBar extends AccessibleJComponent
055: implements AccessibleValue {
056: public AccessibleRole getAccessibleRole() {
057: return AccessibleRole.PROGRESS_BAR;
058: }
059:
060: public Number getCurrentAccessibleValue() {
061: return new Integer(model.getValue());
062: }
063:
064: public boolean setCurrentAccessibleValue(final Number n) {
065: setValue(n.intValue());
066: return true;
067: }
068:
069: public Number getMinimumAccessibleValue() {
070: return new Integer(model.getMinimum());
071: }
072:
073: public Number getMaximumAccessibleValue() {
074: return new Integer(model.getMaximum());
075: }
076:
077: public AccessibleStateSet getAccessibleStateSet() {
078: AccessibleStateSet stateSet = super .getAccessibleStateSet();
079: if (orientation == JProgressBar.HORIZONTAL) {
080: stateSet.add(AccessibleState.HORIZONTAL);
081: } else {
082: stateSet.add(AccessibleState.VERTICAL);
083: }
084: return stateSet;
085: }
086:
087: public AccessibleValue getAccessibleValue() {
088: return this ;
089: }
090: }
091:
092: private class ModelChangeListener implements ChangeListener,
093: Serializable {
094:
095: public void stateChanged(final ChangeEvent e) {
096: fireStateChanged();
097: }
098:
099: }
100:
101: public JProgressBar() {
102: this (new DefaultBoundedRangeModel());
103: }
104:
105: public JProgressBar(final int orient) {
106: this (new DefaultBoundedRangeModel());
107: setOrientation(orient);
108: }
109:
110: public JProgressBar(final int min, final int max) {
111: this (new DefaultBoundedRangeModel(min, 0, min, max));
112: }
113:
114: public JProgressBar(final int orient, final int min, final int max) {
115: this (new DefaultBoundedRangeModel(min, 0, min, max));
116: setOrientation(orient);
117: }
118:
119: public JProgressBar(final BoundedRangeModel newModel) {
120: model = newModel;
121: model.addChangeListener(changeListener);
122: updateUI();
123: }
124:
125: public int getOrientation() {
126: return orientation;
127: }
128:
129: public void setOrientation(final int orientation) {
130: if (orientation != JProgressBar.HORIZONTAL
131: && orientation != JProgressBar.VERTICAL) {
132:
133: throw new IllegalArgumentException(Messages.getString(
134: "swing.4A", orientation)); //$NON-NLS-1$
135: }
136: int oldValue = this .orientation;
137: this .orientation = orientation;
138: firePropertyChange(StringConstants.ORIENTATION, oldValue,
139: orientation);
140: }
141:
142: public boolean isStringPainted() {
143: return paintString;
144: }
145:
146: public void setStringPainted(final boolean painted) {
147: boolean oldValue = paintString;
148: paintString = painted;
149: firePropertyChange(
150: StringConstants.PROGRESS_STRING_PAINTED_PROPERTY,
151: oldValue, painted);
152: }
153:
154: public String getString() {
155: return progressString;
156: }
157:
158: public void setString(final String progressString) {
159: String oldValue = this .progressString;
160: this .progressString = progressString;
161: firePropertyChange(JProgressBar.PROGRESS_STRING_PROPERTY,
162: oldValue, progressString);
163: }
164:
165: public double getPercentComplete() {
166: int min = getMinimum();
167: return 1. * (getValue() - min) / (getMaximum() - min);
168: }
169:
170: public boolean isBorderPainted() {
171: return paintBorder;
172: }
173:
174: public void setBorderPainted(final boolean painted) {
175: boolean oldValue = paintBorder;
176: paintBorder = painted;
177: firePropertyChange(JProgressBar.BORDER_PAINTED_PROPERTY,
178: oldValue, painted);
179: }
180:
181: public ProgressBarUI getUI() {
182: return (ProgressBarUI) ui;
183: }
184:
185: public void setUI(final ProgressBarUI ui) {
186: super .setUI(ui);
187: }
188:
189: public void updateUI() {
190: setUI((ProgressBarUI) UIManager.getUI(this ));
191: }
192:
193: public String getUIClassID() {
194: return JProgressBar.UI_CLASS_ID;
195: }
196:
197: protected ChangeListener createChangeListener() {
198: return new ModelChangeListener();
199: }
200:
201: public void addChangeListener(final ChangeListener l) {
202: listenerList.add(ChangeListener.class, l);
203: }
204:
205: public void removeChangeListener(final ChangeListener l) {
206: listenerList.remove(ChangeListener.class, l);
207: }
208:
209: public ChangeListener[] getChangeListeners() {
210: return (ChangeListener[]) getListeners(ChangeListener.class);
211: }
212:
213: protected void fireStateChanged() {
214: if (changeEvent == null) {
215: changeEvent = new ChangeEvent(this );
216: }
217: ChangeListener[] listeners = getChangeListeners();
218: for (int i = 0; i < listeners.length; i++) {
219: listeners[i].stateChanged(changeEvent);
220: }
221: }
222:
223: public BoundedRangeModel getModel() {
224: return model;
225: }
226:
227: public void setModel(final BoundedRangeModel model) {
228: if (this .model != null) {
229: this .model.removeChangeListener(changeListener);
230: }
231: this .model = model;
232: if (model != null) {
233: model.addChangeListener(changeListener);
234: }
235: }
236:
237: public void setValue(final int value) {
238: model.setValue(value);
239: }
240:
241: public int getValue() {
242: return model.getValue();
243: }
244:
245: public void setMinimum(final int min) {
246: model.setMinimum(min);
247: }
248:
249: public int getMinimum() {
250: return model.getMinimum();
251: }
252:
253: public void setMaximum(final int max) {
254: model.setMaximum(max);
255: }
256:
257: public int getMaximum() {
258: return model.getMaximum();
259: }
260:
261: public void setIndeterminate(final boolean indeterminate) {
262: boolean oldValue = this .indeterminate;
263: this .indeterminate = indeterminate;
264: firePropertyChange(StringConstants.INDETERMINATE_PROPERTY,
265: oldValue, indeterminate);
266: }
267:
268: public boolean isIndeterminate() {
269: return indeterminate;
270: }
271:
272: public AccessibleContext getAccessibleContext() {
273: if (accessibleContext == null) {
274: accessibleContext = new AccessibleJProgressBar();
275: }
276: return accessibleContext;
277: }
278:
279: protected void paintBorder(final Graphics g) {
280: if (isBorderPainted()) {
281: super.paintBorder(g);
282: }
283: }
284: }
|