001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.awt.Dimension;
030: import java.awt.GridBagConstraints;
031: import java.awt.GridBagLayout;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034:
035: import javax.swing.BorderFactory;
036: import javax.swing.JCheckBox;
037: import javax.swing.JPanel;
038: import javax.swing.JProgressBar;
039:
040: public abstract class MinMaxPanel extends JPanel {
041: private JCheckBox enable = new JCheckBox();
042: private JProgressBar progress = new JProgressBar() {
043: public Dimension getPreferredSize() {
044: return new Dimension(100, super .getPreferredSize().height);
045: }
046: };
047:
048: private Spinner minSpinner = new Spinner(0, 1000, 1000);
049: private Spinner maxSpinner = new Spinner(0, 1000, 1000);
050:
051: private ActionListener minListener = new ActionListener() {
052: public void actionPerformed(ActionEvent e) {
053: int min = minSpinner.getValue();
054: int max = maxSpinner.getValue();
055: if (min > max) {
056: maxSpinner.setValue(min);
057: }
058: newMin(min);
059: }
060: };
061:
062: private ActionListener maxListener = new ActionListener() {
063: public void actionPerformed(ActionEvent e) {
064: int min = minSpinner.getValue();
065: int max = maxSpinner.getValue();
066: if (min > max) {
067: minSpinner.setValue(max);
068: }
069: newMax(max);
070: }
071: };
072:
073: private ActionListener enableListener = new ActionListener() {
074: public void actionPerformed(ActionEvent e) {
075: newEnable(enable.isSelected());
076: }
077: };
078:
079: public MinMaxPanel() {
080: super (new GridBagLayout());
081: GridBagConstraints gbc = new GridBagConstraints();
082: gbc.fill = GridBagConstraints.HORIZONTAL;
083: gbc.weightx = 1.0;
084: add(enable, gbc);
085: gbc.fill = GridBagConstraints.NONE;
086: gbc.weightx = 0.0;
087: add(minSpinner, gbc);
088: add(maxSpinner, gbc);
089: add(progress, gbc);
090: minSpinner.addActionListener(minListener);
091: maxSpinner.addActionListener(maxListener);
092: enable.addActionListener(enableListener);
093: progress.setStringPainted(true);
094: progress.setBorder(BorderFactory.createLoweredBevelBorder());
095: }
096:
097: public void setColumns(int cols) {
098: minSpinner.setColumns(cols);
099: maxSpinner.setColumns(cols);
100: }
101:
102: public void setMin(int min) {
103: minSpinner.setValue(min);
104: }
105:
106: public void setMax(int max) {
107: maxSpinner.setValue(max);
108: }
109:
110: public void setProgressMax(int max) {
111: progress.setMaximum(max);
112: }
113:
114: public void setProgressValue(float completed) {
115: int max = progress.getMaximum();
116: progress.setValue((int) (max * completed));
117: progress.setString(((int) (max * (1.0f - completed))) + "");
118: }
119:
120: public void setEnabled(boolean b) {
121: enable.setSelected(b);
122: }
123:
124: public boolean isEnabled() {
125: return enable.isSelected();
126: }
127:
128: public void setText(String labelText) {
129: enable.setText(labelText);
130: }
131:
132: protected abstract void newMin(int min);
133:
134: protected abstract void newMax(int max);
135:
136: protected abstract void newEnable(boolean enable);
137: }
|