001: // EditableSlider.java
002: // $Id: EditableSlider.java,v 1.9 2001/05/28 15:57:37 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.widgets;
007:
008: import java.awt.BorderLayout;
009: import java.awt.FlowLayout;
010:
011: import javax.swing.JSlider;
012: import javax.swing.JPanel;
013: import javax.swing.BorderFactory;
014: import javax.swing.event.ChangeListener;
015: import javax.swing.event.ChangeEvent;
016: import javax.swing.event.DocumentListener;
017: import javax.swing.event.DocumentEvent;
018:
019: /**
020: * A JSlider associated to an IntegerTextField
021: * @version $Revision: 1.9 $
022: * @author Benoît Mahé (bmahe@w3.org)
023: * @see org.w3c.jigadmin.widgets.IntegerTextField
024: */
025: public class EditableSlider extends JPanel {
026:
027: ChangeListener cl = new ChangeListener() {
028: public void stateChanged(ChangeEvent e) {
029: if (e.getSource() == slider) {
030: String newvalue = String.valueOf(slider.getValue());
031: if (!newvalue.equals(textfield.getText()))
032: textfield.setText(newvalue);
033: }
034: }
035: };
036:
037: DocumentListener dl = new DocumentListener() {
038: private void update() {
039: try {
040: int newvalue = Integer.parseInt(textfield.getText());
041: int min, max, step, nbtick, newnbtick, newstep;
042: min = slider.getMinimum();
043: max = slider.getMaximum();
044: if (extensible) {
045: if (newvalue < min) {
046: slider.removeChangeListener(cl);
047: step = slider.getMajorTickSpacing();
048: nbtick = (max - min) / step;
049: newnbtick = (max - newvalue) / step;
050: newstep = (max - newvalue) / nbtick;
051: if (newnbtick != nbtick) {
052: slider.setMajorTickSpacing(newstep);
053: }
054: slider.setMinimum(newvalue);
055: slider.addChangeListener(cl);
056: } else {
057: if (newvalue > max) {
058: slider.removeChangeListener(cl);
059: step = slider.getMajorTickSpacing();
060: nbtick = (max - min) / step;
061: newnbtick = (newvalue - min) / step;
062: newstep = (newvalue - min) / nbtick;
063: if (newnbtick != nbtick) {
064: slider.setMajorTickSpacing(newstep);
065: }
066: slider.setMaximum(newvalue);
067: slider.addChangeListener(cl);
068: }
069: }
070: slider.setValue(newvalue);
071: } else {
072: if ((newvalue >= min) && (newvalue <= max)) {
073: slider.setValue(newvalue);
074: }
075: }
076: } catch (NumberFormatException ex) {
077: //nothing
078: }
079: }
080:
081: public void insertUpdate(DocumentEvent e) {
082: update();
083: }
084:
085: public void changedUpdate(DocumentEvent e) {
086: update();
087: }
088:
089: public void removeUpdate(DocumentEvent e) {
090: update();
091: }
092: };
093:
094: protected JSlider slider = null;
095: protected IntegerTextField textfield = null;
096: private boolean extensible = true;
097:
098: public int getValue() {
099: return slider.getValue();
100: }
101:
102: public void setValue(int value) {
103: slider.setValue(value);
104: }
105:
106: public JSlider getSlider() {
107: return slider;
108: }
109:
110: public EditableSlider(int min, int max, int step, int value) {
111: this (min, max, step, value, true);
112: }
113:
114: public EditableSlider(int min, int max, int step, int value,
115: boolean extensible) {
116: super (new FlowLayout());
117:
118: if (value < min) {
119: slider = new JSlider(value + (value - min), max, value);
120: } else if (value > max) {
121: slider = new JSlider(min, value + (value - max), value);
122: } else {
123: slider = new JSlider(min, max, value);
124: }
125: slider.setMajorTickSpacing(step);
126: slider.setPaintLabels(true);
127: slider.setPaintTicks(true);
128: slider.setPaintTrack(true);
129: slider.setSnapToTicks(false);
130:
131: textfield = new IntegerTextField(8);
132: textfield.setText(String.valueOf(value));
133: textfield.setBorder(BorderFactory.createLoweredBevelBorder());
134:
135: slider.addChangeListener(cl);
136: textfield.getDocument().addDocumentListener(dl);
137:
138: add(textfield, BorderLayout.WEST);
139: add(slider, BorderLayout.CENTER);
140: }
141:
142: }
|