home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
inprise
/
JSAMPLES.Z
/
IntegerSpinItemEditor.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
8KB
|
251 lines
/*
* Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
*
* This SOURCE CODE FILE, which has been provided by Borland as part
* of a Borland product for use ONLY by licensed users of the product,
* includes CONFIDENTIAL and PROPRIETARY information of Borland.
*
* USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
* OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
* THE PRODUCT.
*
* IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
* COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
* OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
* OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
* OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
* OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
* CODE FILE.
*/
package borland.samples.intl.beans;
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 IntegerSpinItemEditor extends Panel implements ItemEditor, BlackBox {
private Scrollbar scrollbar1 = new Scrollbar();
private MaskableTextItemEditor maskableTextItemEditor1 = new MaskableTextItemEditor();
private int initial;
private int minimum;
private int maximum;
private int increment;
public IntegerSpinItemEditor() {
this(1);
}
public IntegerSpinItemEditor(int initial) {
this(initial, 1, 100);
}
public IntegerSpinItemEditor(int initial, int minimum, int maximum) {
this(initial, minimum, maximum, 1);
}
public IntegerSpinItemEditor(int initial, int minimum, int maximum, int increment) {
this.initial = initial;
this.minimum = minimum;
this.maximum = maximum;
this.increment = increment;
// the vertical scrollbar is arguably upside-down. Pressing the down arrow
// increments its value, and pressing the up arrow decrements it. Thus
// we have to flip the direction of our calculations as follows.
scrollbar1.setValues(-initial, 1, -maximum, -minimum+1);
scrollbar1.setBlockIncrement(-increment);
scrollbar1.addAdjustmentListener(new IntegerSpinItemEditor_scrollbar1_adjustmentAdapter(this));
maskableTextItemEditor1.setText(String.valueOf(initial));
maskableTextItemEditor1.addActionListener(new IntegerSpinItemEditor_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 int getIncrement() {
return 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("IntegerSpinItemEditor.getValue(): " + value);
return variant;
}
public Component getComponent() {
return this;
}
public void startEdit(Object value, Rectangle bounds, ItemEditSite site) {
// System.out.println("IntegerSpinItemEditor.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("IntegerSpinItemEditor.changeBounds()");
maskableTextItemEditor1.changeBounds(bounds);
setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
validate();
}
public boolean canPost() {
// System.out.println("IntegerSpinItemEditor.canPost()");
return maskableTextItemEditor1.canPost();
}
public void endEdit(boolean posted) {
// System.out.println("IntegerSpinItemEditor.endEdit()");
maskableTextItemEditor1.endEdit(posted);
}
public void scrollbar1_adjustmentValueChanged(AdjustmentEvent e) {
// System.out.println("IntegerSpinItemEditor.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("IntegerSpinItemEditor.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("IntegerSpinItemEditor.actionPerformed()");
try {
scrollbar1.setValue(((Variant) maskableTextItemEditor1.getValue()).getInt() * -1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class IntegerSpinItemEditor_scrollbar1_adjustmentAdapter implements java.awt.event.AdjustmentListener {
IntegerSpinItemEditor adaptee;
IntegerSpinItemEditor_scrollbar1_adjustmentAdapter(IntegerSpinItemEditor adaptee) {
this.adaptee = adaptee;
}
public void adjustmentValueChanged(java.awt.event.AdjustmentEvent e) {
adaptee.scrollbar1_adjustmentValueChanged(e);
}
}
class IntegerSpinItemEditor_maskableTextItemEditor1_actionListener implements java.awt.event.ActionListener {
IntegerSpinItemEditor adaptee;
IntegerSpinItemEditor_maskableTextItemEditor1_actionListener(IntegerSpinItemEditor adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.maskableTextItemEditor1_actionPerformed(e);
}
}