com.viaoa.jfc.OACustomComboBox Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oa Show documentation
Show all versions of oa Show documentation
Object Automation library
The newest version!
/* Copyright 1999-2015 Vince Via [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.viaoa.jfc;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
import com.viaoa.hub.*;
import com.viaoa.util.*;
import com.viaoa.jfc.control.*;
import com.viaoa.jfc.table.*;
public abstract class OACustomComboBox extends JComboBox implements OATableComponent, OAJFCComponent {
OACustomComboBoxController control;
OATextField vtf;
OATable table;
String heading = "";
String format;
boolean bTypeEditProperty;
/**
Create an unbound ComboBox.
*/
public OACustomComboBox() {
}
/**
Create a ComboBox that is bound to a property for the active object in a Hub.
@param columns is width of list using character width size.
*/
public OACustomComboBox(Hub hub, String propertyPath, int columns, boolean bTypeEditProperty) {
this(hub,propertyPath,bTypeEditProperty);
setColumns(columns);
}
/**
Create a ComboBox that is bound to a property for the active object in a Hub.
*/
public OACustomComboBox(Hub hub, String propertyPath, boolean bTypeEditProperty) {
control = new OACustomComboBoxController(hub, propertyPath, bTypeEditProperty);
}
/**
Create a ComboBox that is bound to a property for the active object in a Hub.
@param columns is width of list using character width size.
*/
public OACustomComboBox(Object obj, String propertyPath, int columns, boolean bTypeEditProperty) {
this(obj, propertyPath, bTypeEditProperty);
setColumns(columns);
}
/**
Create a ComboBox that is bound to a property for an object.
*/
public OACustomComboBox(Object obj, String propertyPath, boolean bTypeEditProperty) {
control = new OACustomComboBoxController(obj, propertyPath, bTypeEditProperty);
}
/*
* 2006/12/13
* Can be overwritten to clear selected value. Called when clicking the clear button
* on the bottom of the combo popup.
*/
public void onClear() {
// this needs to be overwritten.
}
private boolean bAllowClear;
public void allowClearButton(boolean b) {
this.bAllowClear = b;
}
public boolean getAllowClearButton() {
return bAllowClear;
}
public boolean bSetting;
/**
Directly called by popup to set property value.
This can be overwritten to provide custom features for
setting property.
*/
public void setSelectedItem(Object item) {
if (bSetting) return;
try {
bSetting = true;
// 2006/12/11 commented out, caused exception:
// removeAllItems(); // hides the popup if visible
// if (item != null) addItem(item);
if (control != null) control.updatePropertyValue(item);
super.setSelectedItem(item);
}
finally {
bSetting = false;
}
}
/**
Format string used for displaying value.
*/
public void setFormat(String fmt) {
if (control != null) control.setFormat(fmt);
else {
format = fmt;
}
}
/**
Format string used for displaying value.
*/
public String getFormat() {
if (control != null) control.getFormat();
return format;
}
/**
Property used for image name, that is used to get image to
display with rows.
*/
public void setImageProperty(String prop) {
control.setImageProperty(prop);
}
/**
Property used for image name, that is used to get image to
display with rows.
*/
public String getImageProperty() {
return control.getImageProperty();
}
/**
Root directory path where images are stored.
*/
public void setImagePath(String path) {
control.setImagePath(path);
}
/**
Root directory path where images are stored.
*/
public String getImagePath() {
return control.getImagePath();
}
/**
Returns icon used for active object.
*/
public Icon getIcon() {
return control.getIcon();
}
/**
Returns icon used for an object.
*/
public Icon getIcon(Object obj) {
return control.getIcon(obj);
}
public void setIconColorProperty(String s) {
control.setIconColorProperty(s);
}
public String getIconColorProperty() {
return control.getIconColorProperty();
}
// ----- OATableComponent Interface methods -----------------------zzzzzzz
/**
Hub that this component is bound to.
*/
public Hub getHub() {
if (control == null) return null;
return control.getHub();
}
/**
Hub that this component is bound to.
*/
public void setHub(Hub hub) {
control.setHub(hub);
if (table != null) table.resetColumn(this);
}
/**
Set by OATable when this component is used as a column.
*/
public void setTable(OATable table) {
this.table = table;
}
/**
Set by OATable when this component is used as a column.
*/
public OATable getTable() {
return table;
}
/**
Used to manually disable/enable this component.
*/
public void setDisable(boolean b) {
this.setEnabled(!b);
}
/**
Used to manually disable/enable this component.
*/
public boolean getDisable() {
return this.isEnabled();
}
/* 09/13/99 works with jdk1.1 but this does not work in jdk2
JList list = null;
public void addNotify() {
super.addNotify();
if (list == null) {
list = getUI().getList();
list.setPrototypeCellValue("1234567890");
list.indexToLocation(0); // force update
}
}
*/
/**
Used to determine the default width, based on average character width
of font.
*/
public int getColumns() {
return control.getColumns();
}
/**
Width of ComboBox, based on average width of the font's character.
*/
public void setColumns(int x) {
control.setColumns(x);
int w = OATable.getCharWidth(this, getFont(), x);
Border b = this.getBorder();
if (b != null) {
Insets ins = b.getBorderInsets(this);
if (ins != null) w += ins.left + ins.right;
}
if (table != null) {
table.setColumnWidth(table.getColumnIndex(this), w);
}
String str = "w";
for (int i=1; i 0) vtf.setColumns(c);
else {
c = vtf.getColumns();
if (c > 0) this.setColumns(c);
}
}
}
public void setMaximumColumns(int x) {
control.setMaximumColumns(x);
if (vtf != null) {
int c = this.getMaximumColumns();
if (c > 0) vtf.setMaximumColumns(c);
else {
c = vtf.getMaximumColumns();
if (c > 0) this.setMaximumColumns(c);
}
}
invalidate();
}
public int getMaxColumns() {
return control.getMaximumColumns();
}
public int getMaximumColumns() {
return control.getMaximumColumns();
}
public void setMinimumColumns(int x) {
control.setMinimumColumns(x);
if (vtf != null) {
int c = this.getMinimumColumns();
if (c > 0) vtf.setMinimumColumns(c);
else {
c = vtf.getMinimumColumns();
if (c > 0) this.setMinimumColumns(c);
}
}
invalidate();
}
public int getMinimumColumns() {
return control.getMinimumColumns();
}
public CustomComboBoxController getController() {
return control;
}
/**
Property path used for displaying rows.
*/
public String getPropertyPath() {
return control.getPropertyPath();
}
/**
Property path used for displaying rows.
*/
public void setPropertyPath(String path) {
control.setPropertyPath(path);
if (table != null) table.resetColumn(this);
}
/**
Column heading when used as a column in an OATable.
*/
public String getTableHeading() {
return heading;
}
/**
Column heading when used as a column in an OATable.
*/
public void setTableHeading(String heading) {
this.heading = heading;
if (table != null) table.setColumnHeading(table.getColumnIndex(this),heading);
}
/**
OATextField used to directly edit value.
*/
public void setEditor(OATextField vtf) {
this.vtf = vtf;
if (vtf == null) super.setEditor(null);
else {
int c = this.getColumns();
if (c > 0) vtf.setColumns(c);
else {
c = vtf.getColumns();
if (c > 0) this.setColumns(c);
}
c = this.getMaximumColumns();
if (c > 0) vtf.setMaximumColumns(c);
c = this.getMinimumColumns();
if (c > 0) vtf.setMinimumColumns(c);
OACustomComboBoxEditor ed = new OACustomComboBoxEditor(this, vtf);
super.setEditor(ed);
setEditable(true);
}
focusComp = null;
}
public OATextField getTextEditor() {
return vtf;
}
/*???QQQQQQqqqqqq
public JComponent getComponent() {
return this;
}
*/
protected OAComboBoxTableCellEditor tableCellEditor;
/**
Used by OATable to set this component as an OATable column editor.
*/
public TableCellEditor getTableCellEditor() {
if (tableCellEditor == null) {
tableCellEditor = new OAComboBoxTableCellEditor(this);
}
return tableCellEditor;
}
// 20160402
// hack: JComboBox could be container, so set focus to first good component
JComponent focusComp;
protected JComponent getFocusComponent() {
if (focusComp != null) return focusComp;
if (getEditor() != null) {
focusComp = (JComponent) getEditor().getEditorComponent();
if ( !(focusComp instanceof OATextField) ) focusComp = null; // dont use default editor
}
if (focusComp == null) {
Component[] comps = getComponents();
for (int i=0; i