home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BUG 15
/
BUGCD1998_06.ISO
/
aplic
/
jbuilder
/
jsamples.z
/
IntegerSpinEditor.java
< prev
next >
Wrap
Text File
|
1997-07-30
|
7KB
|
225 lines
package borland.samples.intl.util;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import borland.jbcl.util.*;
import borland.jbcl.view.*;
import borland.jbcl.control.*;
import borland.jbcl.model.*;
public class IntegerSpinEditor extends Panel implements ItemEditor {
private Scrollbar scrollbar1 = new Scrollbar();
private MaskableTextItemEditor maskableTextItemEditor1 = new MaskableTextItemEditor();
private int initial;
private int minimum;
private int maximum;
private int increment;
public IntegerSpinEditor() {
this(1);
}
public IntegerSpinEditor(int initial) {
this(initial, 1, 100);
}
public IntegerSpinEditor(int initial, int minimum, int maximum) {
this(initial, minimum, maximum, 1);
}
public IntegerSpinEditor(int initial, int minimum, int maximum, int increment) {
this.initial = initial;
this.minimum = minimum;
this.maximum = maximum;
this.increment = increment;
scrollbar1.setValues(-initial, 1, -maximum, -minimum+1);
scrollbar1.setBlockIncrement(-increment);
scrollbar1.addAdjustmentListener(new IntegerSpinEditor_scrollbar1_adjustmentAdapter(this));
maskableTextItemEditor1.setText(String.valueOf(initial));
maskableTextItemEditor1.addActionListener(new IntegerSpinEditor_maskableTextItemEditor1_actionListener(this));
setLayout(new BorderLayout());
add(maskableTextItemEditor1, BorderLayout.CENTER);
add(scrollbar1, BorderLayout.EAST);
maskableTextItemEditor1.setEditMask("######", Variant.INT, Locale.getDefault());
}
public void setMinimum(int minimum) {
scrollbar1.setMinimum(-minimum+1);
this.minimum = minimum;
}
public int getMinimum() {
return minimum;
}
public void setMaximum(int maximum) {
scrollbar1.setMaximum(-maximum);
this.maximum = maximum;
}
public int getMaximum() {
return maximum;
}
public void setIncrement(int increment) {
scrollbar1.setBlockIncrement(-increment);
this.increment = increment;
}
public void addKeyListener(KeyListener l) {
super.addKeyListener(l);
maskableTextItemEditor1.addKeyListener(l);
scrollbar1.addKeyListener(l);
}
public void removeKeyListener(KeyListener l) {
super.removeKeyListener(l);
maskableTextItemEditor1.removeKeyListener(l);
scrollbar1.removeKeyListener(l);
}
public Object getValue() {
Variant variant = (Variant) maskableTextItemEditor1.getValue();
int value = variant.getInt();
if (value < minimum) {
value = minimum;
} else if (value > maximum) {
value = maximum;
}
variant.setInt(value);
// System.out.println("IntegerSpinEditor.getValue(): " + value);
return variant;
}
public Component getComponent() {
return this;
}
public void startEdit(Object value, Rectangle bounds, ItemEditSite site) {
// System.out.println("IntegerSpinEditor.startEdit(" + value.toString() + ")");
// do bounds needs to be adjusted? Possibly not, because we layout again below.
maskableTextItemEditor1.startEdit(value, bounds, site);
Integer integer;
try {
integer = new Integer(((Variant) maskableTextItemEditor1.getValue()).getInt());
} catch (NumberFormatException ex) {
integer = new Integer(minimum);
}
int intVal = integer.intValue();
scrollbar1.setValue(-intVal);
if (site != null) {
setBackground(site.getBackground());
setForeground(site.getForeground());
setFont(site.getFont());
}
setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
validate();
setVisible(true);
maskableTextItemEditor1.requestFocus();
}
public void changeBounds(Rectangle bounds) {
// System.out.println("IntegerSpinEditor.changeBounds()");
maskableTextItemEditor1.changeBounds(bounds);
setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
validate();
}
public boolean canPost() {
// System.out.println("IntegerSpinEditor.canPost()");
return maskableTextItemEditor1.canPost();
}
public void endEdit(boolean posted) {
// System.out.println("IntegerSpinEditor.endEdit()");
maskableTextItemEditor1.endEdit(posted);
}
public void scrollbar1_adjustmentValueChanged(AdjustmentEvent e) {
// System.out.println("IntegerSpinEditor.adjustmentValueChanged(), scrollbar1.getValue(): " + scrollbar1.getValue());
Integer integer;
Variant variant = (Variant) maskableTextItemEditor1.getValue();
maskableTextItemEditor1.endEdit(false);
// System.out.println("original value: " + variant);
int value = variant.getInt();
int type = e.getAdjustmentType();
switch (type) {
case AdjustmentEvent.UNIT_INCREMENT:
// System.out.println("UNIT_INCREMENT");
value--;
break;
case AdjustmentEvent.UNIT_DECREMENT:
// System.out.println("UNIT_DECREMENT");
value++;
break;
case AdjustmentEvent.BLOCK_INCREMENT:
// System.out.println("BLOCK_INCREMENT");
value -= increment;
break;
case AdjustmentEvent.BLOCK_DECREMENT:
// System.out.println("BLOCK_DECREMENT");
value += increment;
break;
}
if (value < minimum) {
value = minimum;
} else if (value > maximum) {
value = maximum;
}
variant.setInt(value);
scrollbar1.setValue(-value);
// System.out.println("IntegerSpinEditor.adjustmentValueChanged(), scrollbar1.getValue(): " + scrollbar1.getValue());
// System.out.println("new value: " + value);
maskableTextItemEditor1.startEdit(variant, getBounds(), null);
validate();
setVisible(true);
requestFocus();
}
void maskableTextItemEditor1_actionPerformed(ActionEvent e) {
// System.out.println("IntegerSpinEditor.actionPerformed()");
try {
scrollbar1.setValue(((Variant) maskableTextItemEditor1.getValue()).getInt() * -1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class IntegerSpinEditor_scrollbar1_adjustmentAdapter implements java.awt.event.AdjustmentListener {
IntegerSpinEditor adaptee;
IntegerSpinEditor_scrollbar1_adjustmentAdapter(IntegerSpinEditor adaptee) {
this.adaptee = adaptee;
}
public void adjustmentValueChanged(java.awt.event.AdjustmentEvent e) {
adaptee.scrollbar1_adjustmentValueChanged(e);
}
}
class IntegerSpinEditor_maskableTextItemEditor1_actionListener implements java.awt.event.ActionListener {
IntegerSpinEditor adaptee;
IntegerSpinEditor_maskableTextItemEditor1_actionListener(IntegerSpinEditor adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.maskableTextItemEditor1_actionPerformed(e);
}
}