home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
inprise
/
JSAMPLES.Z
/
ResourceablePickListItemEditor.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
12KB
|
366 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.model.*;
import borland.jbcl.view.*;
import borland.jbcl.dataset.*;
/**
* ResourceablePickListItemEditor is a modified version of the JBCL
* PickListItemEditor which optionally treats items from the picklist
* dataset as keys which can be looked up in a specified resource
* bundle. See borland.jbcl.control.PickListItemEditor for more
* info about picklists.
*/
public class ResourceablePickListItemEditor extends java.awt.Panel implements borland.jbcl.model.ItemEditor, ItemListener, KeyListener, BlackBox
{
public ResourceablePickListItemEditor() {
super();
this.setVisible(false);
this.setLayout(new BorderLayout());
cache = true;
value = new Variant();
resourceBundle = null;
choice.addItemListener(this);
choice.addKeyListener(this);
}
/**
* The CachePickList property determines whether or not values from the
* display ("picklist") DataSet are cached. If the column of
* display choices from which to choose is subject to frequent change, setting
* this property false will ensure that the most recent list of choices
* will be displayed when the PickListItemEditor is invoked. For performance,
* this property is true by default.
*/
public final void setCachePickList(boolean cache) {
this.cache = cache;
}
public final boolean isCachePickList() {
return cache;
}
/**
* Sets a RowFilterListener on the picklist dataset.
* By specifying a RowFilterListener and setting Cache false,
* you can dynamically change the list of values displayed
* by PickListItemEditor through the row filter.
*/
public final void addRowFilterListener(RowFilterListener listener)
throws TooManyListenersException
{
if (listener == null) {
throw new IllegalArgumentException();
}
if (this.listener != null) {
throw new TooManyListenersException();
}
this.listener = listener;
if (pickListDataSet != null) {
pickListDataSet.addRowFilterListener(listener);
}
}
public final void removeRowFilterListener(RowFilterListener listener) {
this.listener = null;
if (pickListDataSet != null) {
pickListDataSet.removeRowFilterListener(listener);
}
}
/**
* Sets the resource bundle in which to look up items to display.
*
* @param resourceBundle resourceBundle to use for key lookups
*/
public void setResourceBundle(ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
}
/**
* Returns the resource bundle used to look up resource keys.
*
* @return resource bundle for key lookups.
*/
public ResourceBundle getResourceBundle() {
return resourceBundle;
}
// loadPickList() is called by startEdit() when a user begins
// editing the control. It has the side effect of extracting the
// pickListDescriptor info into class variables.
protected void loadPickList(PickListDescriptor pickList) {
if (pickListDataSet == null || !cache) {
setVisible(true);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
if (pickListDataSet == null) {
pickListDataSet = new DataSetView();
if (listener != null) {
pickListDataSet.addRowFilterListener(listener);
}
// if pickList.getPickListDataSet() returns null, we'll catch
// the NPE below and set pickListDataSet to be null. startEdit()
// checks if this is the case and if so, immediately cancels
// the edit.
pickListDataSet.setStorageDataSet(pickList.getPickListDataSet().getStorageDataSet());
sourceColumns = pickList.getPickListColumns();
targetColumns = pickList.getDestinationColumns();
displayColumns = pickList.getPickListDisplayColumns();
// if any of the columns are null or don't have at least one
// column specified, then setting pickListDataSet null and
// returning will cause startEdit() to immediately cancel
// the edit.
if ((sourceColumns == null) || (sourceColumns.length == 0) ||
(targetColumns == null) || (targetColumns.length == 0) ||
(displayColumns == null) || (displayColumns.length == 0)) {
pickListDataSet = null;
return;
}
}
pickListDataSet.open();
// locateRow = new DataRow(pickListDataSet, sourceColumns[0]);
if (!cache) {
choice.removeAll();
}
int ordinal = pickListDataSet.getColumn(displayColumns[0]).getOrdinal();
if (listener != null) {
pickListDataSet.refilter();
}
pickListDataSet.first();
while(pickListDataSet.inBounds()) {
if (resourceBundle == null) {
choice.add(pickListDataSet.format(ordinal));
} else {
choice.add(resourceBundle.getString(pickListDataSet.format(ordinal)));
}
pickListDataSet.next();
}
// in case cache is false (and loadPickList is called more than
// once), don't add the choice again to the panel
if (getComponentCount() == 0) {
add(choice, BorderLayout.CENTER);
}
}
catch(Exception ex) {
pickListDataSet = null;
DataSetException.handleException(ex);
}
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
public Object getValue() {
if (pickListDataSet != null) {
try {
// Go to the row in the PickListDataSet corresponding to
// the row selected by the user.
pickListDataSet.goToRow(choice.getSelectedIndex());
// Copy the PickList columns to the corresponding destination columns.
ReadRow.copyTo(sourceColumns, pickListDataSet, targetColumns, targetDataSet);
// If the current column is one of the picklist target columns,
// then return that value, otherwise simply return the original
// value.
for (int index = 0; index < targetColumns.length; index++) {
if (targetColumns[index].equalsIgnoreCase(currentColumnName)) {
pickListDataSet.getVariant(sourceColumns[0], value);
break;
}
}
return value;
}
catch(DataSetException ex) {
value.setUnassignedNull();
DataSetException.handleException(ex);
}
}
return value;
}
public Component getComponent() {
return this;
}
public void startEdit(Object data, Rectangle bounds, ItemEditSite editSite) {
Column currentColumn = null;
ColumnVariant currentColumnVariant = null;
setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
this.editSite = editSite;
if (data instanceof ColumnVariant) {
currentColumnVariant = (ColumnVariant)data;
value.setVariant(currentColumnVariant);
currentColumn = currentColumnVariant.getColumn();
currentColumnName = currentColumn.getColumnName();
targetDataSet = currentColumnVariant.getDataSet();
loadPickList(currentColumn.getPickList());
// if there was a problem with information in the PickListDescriptor,
// then quietly cancel the edit.
if (pickListDataSet == null) {
editSite.safeEndEdit(false);
}
}
if (pickListDataSet != null && locateRow != null) {
try {
locateRow = new DataRow(pickListDataSet, sourceColumns[0]);
locateRow.setVariant(sourceColumns[0], currentColumnVariant);
// Doing a locate() on the pickListDataSet should move the
// current row to the target row if it exists.
// We need to select() the corresponding row as an
// index into the choices in the Choice (only if the locate
// succeeds, i.e., the value exists in the dataset).
if (pickListDataSet.locate(locateRow, Locate.FIRST)) {
choice.select(pickListDataSet.getRow());
}
} catch (DataSetException ex) {
DataSetException.handleException(ex);
}
}
choice.invalidate();
if (editSite != null) {
choice.setBackground(editSite.getBackground());
choice.setForeground(editSite.getForeground());
choice.setFont(editSite.getFont());
}
validate();
setVisible(true);
choice.requestFocus();
}
public void changeBounds(Rectangle bounds) {
choice.invalidate();
setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
validate();
}
public boolean canPost() {
return true;
}
public void endEdit(boolean post) {
setBounds(0,0,0,0);
setVisible(false);
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
int sel = choice.getSelectedIndex();
switch (code) {
case e.VK_DOWN:
case e.VK_RIGHT:
if (sel < (choice.getItemCount() - 1)) {
choice.select(sel + 1);
}
e.consume();
break;
case e.VK_LEFT:
case e.VK_UP:
if (sel > 0) {
choice.select(sel - 1);
}
e.consume();
break;
case e.VK_HOME:
choice.select(0);
e.consume();
break;
case e.VK_END:
choice.select(choice.getItemCount() - 1);
e.consume();
break;
case e.VK_PAGE_DOWN:
choice.select(Math.min(sel + 10, choice.getItemCount() - 1));
e.consume();
break;
case e.VK_PAGE_UP:
choice.select(Math.max(sel - 10, 0));
e.consume();
break;
}
}
public void keyReleased(KeyEvent e) {
}
public void addKeyListener(KeyListener l) {
choice.addKeyListener(l);
}
public void removeKeyListener(KeyListener l) {
choice.removeKeyListener(l);
}
public void addFocusListener(FocusListener l) {
choice.addFocusListener(l);
}
public void removeFocusListener(FocusListener l) {
choice.removeFocusListener(l);
}
public void itemStateChanged(ItemEvent e) {
if (e.getID() == ItemEvent.ITEM_STATE_CHANGED) {
editSite.safeEndEdit(true);
}
}
String currentColumnName;
String [] sourceColumns;
String [] targetColumns;
Column [] pickListColumns;
String [] displayColumns;
DataSetView pickListDataSet;
DataSet targetDataSet;
DataRow locateRow;
boolean cache;
Variant value;
Choice choice = new Choice();
RowFilterListener listener;
ItemEditSite editSite;
ResourceBundle resourceBundle;
}