[ Back | Previous | Next ]

How create resource driven ComboBox?

Package:
javax.swing.*
Product:
Swing
Release:
1.0.3
Related Links:
ComboBox
JDialog
JFileChooser
JFrame
JOptionPane
JProgressBar
JScrollPane
JTable
General
JTree
JWindow
KeyStroke
LayeredPane
UIDefaults
Comment:
package nl.rotterdam.port.swing;

import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.table.*;
import java.beans.*;
import java.util.*;
import java.lang.reflect.*;
import nl.rotterdam.port.util.*;
/**
 * This type was created in VisualAge.
 */
public class ViewResourceTableModel extends AbstractTableModel implements TableModel {
	protected transient PropertyChangeSupport propertyChange;
	private AbstractViewTableResource fieldResource = null;
	private Vector fieldData = new Vector();
/**
 * ViewResourceTableModel constructor comment.
 */
public ViewResourceTableModel() {
	super();
}
/**
 * The addPropertyChangeListener method was generated to support the propertyChange field.
 */
public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
	getPropertyChange().addPropertyChangeListener(listener);
}
/**
 * This method was created in VisualAge.
 * @param str java.lang.String
 */
private void dbg(String str) {
	System.out.println( str );
}
/**
 * The firePropertyChange method was generated to support the propertyChange field.
 */
public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
	getPropertyChange().firePropertyChange(propertyName, oldValue, newValue);
}
/**
 * getColumnCount method comment.
 */
public int getColumnCount() {
	if (getResource() != null) {
		dbg("" + getResource().getContents().length );
		return getResource().getContents().length;
	} else
		dbg("columnCount: " + getResource());
		return 0;
}
public String getColumnName(int column) {
	if (getResource() == null) {
		return "NoResource";
	}
	try {
		dbg("name: #" + column + "=" + getResource().findObjectList("c" + column)[2]);
		Object[] obj =  getResource().findObjectList("c" + column);
		if (obj[2] != null)
			return (String) obj[2];
		else
			return "NoNameInResource";
	} catch (Exception cce) {
		System.out.println( cce );
		return "NoNameInResource";
	}
}
/**
 * Gets the data property (java.util.Vector) value.
 * @return The data property value.
 * @see #setData
 */
public Vector getData() {
	return fieldData;
}
/**
 * Accessor for the propertyChange field.
 */
protected PropertyChangeSupport getPropertyChange() {
	if (propertyChange == null) {
		propertyChange = new java.beans.PropertyChangeSupport(this);
	};
	return propertyChange;
}
/**
 * Gets the resource property (scheepsinfo.AbstractViewTableResource) value.
 * @return The resource property value.
 * @see #setResource
 */
public AbstractViewTableResource getResource() {
	return fieldResource;
}
/**
 * getRowCount method comment.
 */
public int getRowCount() {
	return (getData()!=null? getData().size() : 0);
}
/**
 * getValueAt method comment.
 */
public Object getValueAt(int arg1, int arg2) {
	if (getResource() == null) {
		System.out.println("No resource defined...");
	}
	Object aRow = (Object) getData().elementAt(arg1);
	//
	if (aRow != null) {

		Field[] f = aRow.getClass().getDeclaredFields();
		String fieldName = null;
		try {
			fieldName = (String) ((Object[]) getResource().findObjectList("c" + arg2))[1];
		} catch (Exception e) {
			return "NoFieldName";
		}
		//for (int j = 0; j < f.length; j++) {
		//	dbg("fields: " + f[j] + ": " + f[j].getType());
		//	dbg
		//}
		//dbg("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
		//
		try {
			Class c = aRow.getClass();
			Object[] args_value = null;
			Class[] args_class = null;
			try {
				// this fails!
				Method m = c.getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), args_class);
				try {
					return m.invoke(aRow, args_value);
				} catch (Exception e) {
					System.err.println("Failed to invoke: " + e.toString());
				}
			} catch (Exception e) {
				System.err.println("Failed to get method: " + e.toString());
			}
		} catch (Exception e) {
			System.err.println("Error: " + e.toString());
		}
		return getData().elementAt(arg2);
	}
	return null;
}
public boolean isCellEditable(int row, int col) {
	return false;
}
/**
 * The removePropertyChangeListener method was generated to support the propertyChange field.
 */
public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
	getPropertyChange().removePropertyChangeListener(listener);
}
/**
 * Sets the data property (java.util.Vector) value.
 * @param data The new value for the property.
 * @see #getData
 */
public void setData(Vector data) {
	Vector oldValue = fieldData;
	fieldData = data;
	firePropertyChange("data", oldValue, data);
	fireTableDataChanged();
}
/**
 * Sets the resource property (scheepsinfo.AbstractViewTableResource) value.
 * @param resource The new value for the property.
 * @see #getResource
 */
public void setResource(AbstractViewTableResource resource) {
	AbstractViewTableResource oldValue = fieldResource;
	dbg("setResource");
	fieldResource = resource;
	firePropertyChange("resource", oldValue, resource);
	fireTableStructureChanged();
}
public void setValueAt(Object aValue, int row, int column)  {
	if ( getResource() == null) {
		System.out.println("No resource defined...");
	}
	System.out.println("Setting value to: " + aValue);
	
	Object aRow = (Object) getData().elementAt(row);
	/*
	if (aRow != null && aRow instanceof Schip) {
		Melding m = (Melding) aRow;
		switch (column) {
			case 0 : // meldingsoort
				m.meldingsoort = (String) aValue;
				break;
			case 1 : // volgnr
				m.volgnr = (String) aValue;
				break;
			case 2 : // berichtsoort
				m.berichtsoort = (String) aValue;
				break;
			default :
				break;
		}
	}*/
}
}
1