You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by pr...@apache.org on 2002/07/31 22:41:42 UTC
cvs commit: jakarta-avalon-apps/phyre/src/java/org/apache/avalon/phyre/panels/grid ConfigurableTableModel.java GridColumn.java GridPanel.java GridPanelMouseAdaptor.java
proyal 2002/07/31 13:41:42
Added: phyre/src/java/org/apache/avalon/phyre/panels/grid
ConfigurableTableModel.java GridColumn.java
GridPanel.java GridPanelMouseAdaptor.java
Log:
Panel that displays a grid
Revision Changes Path
1.1 jakarta-avalon-apps/phyre/src/java/org/apache/avalon/phyre/panels/grid/ConfigurableTableModel.java
Index: ConfigurableTableModel.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.avalon.phyre.panels.grid;
import javax.swing.table.AbstractTableModel;
import org.apache.avalon.excalibur.property.PropertyUtil;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.container.ContainerUtil;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.phyre.PhyreRuntimeException;
import org.apache.avalon.phyre.mbean.MBeanAccessor;
/**
* @author <a href="mailto:proyal@apache.org">Peter Royal</a>
*/
public class ConfigurableTableModel extends AbstractTableModel
implements LogEnabled, Serviceable, Configurable, Initializable, Contextualizable
{
private Context m_context;
private Logger m_logger;
private ServiceManager m_manager;
private Configuration m_config;
private String[] m_rowKeys;
private GridColumn[] m_columns;
public ConfigurableTableModel()
{
}
public void enableLogging( final Logger logger )
{
m_logger = logger;
}
protected final Logger getLogger()
{
return m_logger;
}
public void contextualize( Context context )
throws ContextException
{
m_context = context;
}
public void service( ServiceManager manager )
throws ServiceException
{
m_manager = manager;
}
public void configure( Configuration configuration )
throws ConfigurationException
{
m_config = configuration;
}
public void initialize()
throws Exception
{
m_rowKeys = getKeys( m_config.getChild( "source" ) );
m_columns = createColumns( m_config.getChildren( "column" ) );
}
private String[] getKeys( final Configuration config )
throws Exception
{
final String objectName =
( String ) PropertyUtil.resolveProperty( config.getChild( "object-name" ).getValue(),
m_context,
false );
final MBeanAccessor mbean =
( MBeanAccessor ) m_manager.lookup( objectName );
return ( String[] ) mbean.get( config.getChild( "attribute" ).getValue() );
}
private GridColumn[] createColumns( final Configuration[] cols )
throws Exception
{
final GridColumn[] columns = new GridColumn[cols.length];
final DefaultContext columnContext = new DefaultContext( m_context );
columnContext.put( "row-keys", m_rowKeys );
columnContext.makeReadOnly();
for( int i = 0; i < cols.length; i++ )
{
final GridColumn column = new GridColumn();
ContainerUtil.enableLogging( column, getLogger() );
ContainerUtil.contextualize( column, columnContext );
ContainerUtil.service( column, m_manager );
ContainerUtil.configure( column, cols[i] );
ContainerUtil.initialize( column );
columns[i] = column;
}
return columns;
}
public int getColumnCount()
{
return m_columns.length;
}
public int getRowCount()
{
return m_rowKeys.length;
}
public synchronized Object getValueAt( int rowIndex, int columnIndex )
{
try
{
return m_columns[columnIndex].getValue( rowIndex );
}
catch( Exception e )
{
throw new PhyreRuntimeException( "Unable to load value [row: " + rowIndex
+ ", col: " + columnIndex + "]", e );
}
}
public Class getColumnClass( int columnIndex )
{
return m_columns[columnIndex].getColumnClass();
}
public String getColumnName( int column )
{
return m_columns[column].getCaption();
}
public String getRowKey( int rowIndex )
{
return m_rowKeys[rowIndex];
}
}
1.1 jakarta-avalon-apps/phyre/src/java/org/apache/avalon/phyre/panels/grid/GridColumn.java
Index: GridColumn.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.avalon.phyre.panels.grid;
import org.apache.avalon.excalibur.property.PropertyException;
import org.apache.avalon.excalibur.property.PropertyUtil;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.phyre.mbean.MBeanAccessor;
/**
* @author <a href="mailto:proyal@apache.org">Peter Royal</a>
*/
class GridColumn extends AbstractLogEnabled
implements Contextualizable, Configurable, Initializable, Serviceable
{
private ServiceManager m_manager;
private Context m_context;
private Configuration m_sourceConfig;
private Class m_columnClass = String.class;
private String m_caption;
private String m_valueString;
private String[] m_rowKeys;
private MBeanAccessor[] m_MBeans;
private String m_attribute;
private String m_operation;
private String[] m_operationParameters;
public GridColumn()
{
}
public void contextualize( Context context )
throws ContextException
{
m_context = context;
m_rowKeys = ( String[] ) context.get( "row-keys" );
}
public void service( ServiceManager manager )
throws ServiceException
{
m_manager = manager;
}
public void configure( Configuration configuration )
throws ConfigurationException
{
final Configuration value = configuration.getChild( "value" );
m_caption = configuration.getChild( "caption" ).getValue();
m_sourceConfig = value.getChild( "source", false );
if( null == m_sourceConfig )
{
m_valueString = value.getValue();
}
}
public void initialize()
throws Exception
{
if( null != m_sourceConfig )
{
m_MBeans = getMBeans( m_sourceConfig.getChild( "object-name" ).getValue() );
m_attribute = m_sourceConfig.getChild( "attribute" ).getValue( null );
if( null == m_attribute )
{
final Configuration op = m_sourceConfig.getChild( "operation", false );
if( null == op )
{
throw new ConfigurationException( "source element must declare an attribute "
+ "or operation" );
}
m_operation = op.getAttribute( "name" );
m_columnClass =
Class.forName( op.getChild( "datatype" ).getValue( "java.lang.String" ) );
final Configuration[] params = op.getChildren( "parameter" );
m_operationParameters = new String[params.length];
for( int i = 0; i < params.length; i++ )
{
m_operationParameters[i] = params[i].getValue();
}
}
else
{
// m_MBeans[0].info().
}
m_sourceConfig = null;
}
}
private MBeanAccessor[] getMBeans( final String objectName )
throws ServiceException, PropertyException
{
final MBeanAccessor[] accessors = new MBeanAccessor[m_rowKeys.length];
for( int i = 0; i < m_rowKeys.length; i++ )
{
final String rname =
( String ) PropertyUtil.resolveProperty( objectName,
createContext( i ),
false );
accessors[i] = ( MBeanAccessor ) m_manager.lookup( rname );
}
return accessors;
}
public String getCaption()
{
return m_caption;
}
public Object getValue( int rowIndex ) throws Exception
{
if( null != m_valueString )
{
return PropertyUtil.resolveProperty( m_valueString, createContext( rowIndex ), false );
}
else if( null != m_attribute )
{
return m_MBeans[rowIndex].get( m_attribute );
}
else if( null != m_operation )
{
return m_MBeans[rowIndex].invoke( m_operation, createParameters( rowIndex ) );
}
else
{
throw new IllegalStateException( "grid column not configured properly" );
}
}
private String paramsToString( final Object[] params )
{
final StringBuffer sb = new StringBuffer( 20 );
for( int i = 0; i < params.length; i++ )
{
if( i > 0 )
{
sb.append( ',' );
}
sb.append( params[i] );
}
return sb.toString();
}
private Object[] createParameters( final int rowIndex ) throws PropertyException
{
final Object[] params = new Object[m_operationParameters.length];
final Context ctx = createContext( rowIndex );
for( int i = 0; i < params.length; i++ )
{
params[i] = PropertyUtil.resolveProperty( m_operationParameters[i], ctx, false );
}
return params;
}
private Context createContext( final int rowIndex )
{
DefaultContext ctx = new DefaultContext( m_context );
ctx.put( "row-key", m_rowKeys[rowIndex] );
return ctx;
}
public Class getColumnClass()
{
return m_columnClass;
}
}
1.1 jakarta-avalon-apps/phyre/src/java/org/apache/avalon/phyre/panels/grid/GridPanel.java
Index: GridPanel.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.avalon.phyre.panels.grid;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.container.ContainerUtil;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.phyre.actions.Action;
import org.apache.avalon.phyre.actions.ActionFactory;
import org.apache.avalon.phyre.panels.ButtonFactory;
import org.apache.avalon.phyre.panels.LogEnabledJPanel;
/**
* @author <a href="mailto:proyal@apache.org">Peter Royal</a>
*/
public class GridPanel extends LogEnabledJPanel
implements Configurable, Initializable, Serviceable, Contextualizable
{
private ServiceManager m_manager;
private ActionFactory m_listenerFactory;
private Configuration m_config;
private Context m_context;
public GridPanel()
{
super( new GridBagLayout() );
}
public void contextualize( Context context )
throws ContextException
{
m_context = context;
}
public void service( ServiceManager manager )
throws ServiceException
{
m_manager = manager;
m_listenerFactory = ( ActionFactory ) manager.lookup( ActionFactory.ROLE );
}
public void configure( Configuration configuration )
throws ConfigurationException
{
m_config = configuration;
}
public void initialize()
throws Exception
{
final GridBagConstraints constraints = new GridBagConstraints();
final String title = m_config.getChild( "title" ).getValue( null );
if( null != title )
{
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 2.0;
constraints.gridwidth = GridBagConstraints.REMAINDER;
add( new JLabel( title, JLabel.CENTER ), constraints );
}
final Configuration[] buttons = m_config.getChildren( "button" );
for( int i = 0; i < buttons.length; i++ )
{
constraints.weightx = 1.0;
constraints.gridwidth = 1;
if( i == ( buttons.length - 1 ) )
{
constraints.gridwidth = GridBagConstraints.REMAINDER;
}
add( ButtonFactory.createButton( m_listenerFactory, m_context, buttons[i] ), constraints );
}
constraints.weightx = 2.0;
constraints.weighty = 1.0;
constraints.gridwidth = GridBagConstraints.REMAINDER;
final JTable table = new JTable( createTableModel( m_config.getChild( "grid" ) ) );
final Configuration action = m_config.getChild( "action", false );
if( null != action )
{
final Action a = m_listenerFactory.create( action.getAttribute( "type" ), action );
table.addMouseListener( new GridPanelMouseAdaptor( a, m_context ) );
}
add( new JScrollPane( table ), constraints );
}
private TableModel createTableModel( final Configuration config ) throws Exception
{
final ConfigurableTableModel model = new ConfigurableTableModel();
ContainerUtil.enableLogging( model, getLogger() );
ContainerUtil.contextualize( model, m_context );
ContainerUtil.service( model, m_manager );
ContainerUtil.configure( model, config );
ContainerUtil.initialize( model );
return model;
}
}
1.1 jakarta-avalon-apps/phyre/src/java/org/apache/avalon/phyre/panels/grid/GridPanelMouseAdaptor.java
Index: GridPanelMouseAdaptor.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.avalon.phyre.panels.grid;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.phyre.actions.Action;
/**
* @author <a href="mailto:proyal@apache.org">Peter Royal</a>
*/
public class GridPanelMouseAdaptor extends MouseAdapter
{
private final Action m_action;
private final Context m_context;
public GridPanelMouseAdaptor( Action action, Context ctx )
{
m_action = action;
m_context = ctx;
}
public void mouseClicked( MouseEvent e )
{
if( e.getClickCount() > 1 )
{
final JTable source = ( JTable ) e.getSource();
final ConfigurableTableModel model = ( ConfigurableTableModel ) source.getModel();
final DefaultContext ctx = new DefaultContext( m_context );
ctx.put( "row-key", model.getRowKey( source.getSelectedRow() ) );
ctx.makeReadOnly();
m_action.perform( ctx );
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>