Thread: [Ejtools-cvs] CVS: libraries/adwt/src/main/net/sourceforge/ejtools/awt BeanContextListPanel.java,NON
Brought to you by:
letiemble
From: Laurent E. <let...@us...> - 2002-04-28 19:38:22
|
Update of /cvsroot/ejtools/libraries/adwt/src/main/net/sourceforge/ejtools/awt In directory usw-pr-cvs1:/tmp/cvs-serv15372/libraries/adwt/src/main/net/sourceforge/ejtools/awt Added Files: BeanContextListPanel.java BeanContextListView.java Log Message: Add List View of BeanContext (List and Panel) --- NEW FILE: BeanContextListPanel.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package net.sourceforge.ejtools.awt; import java.awt.Container; import java.awt.Dimension; import java.beans.beancontext.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*; /** *Description of the Class * * @author Administrator * @created 16 octobre 2001 */ public class BeanContextListPanel extends JSplitPane { JList list; /** *Constructor for the BeanContextListPanel object * * @param beancontext Description of Parameter */ public BeanContextListPanel(BeanContext beancontext) { this(beancontext, null); } /** *Constructor for the BeanContextListPanel object * * @param beancontext Description of Parameter * @param s Description of Parameter */ public BeanContextListPanel(BeanContext beancontext, String s) { super(1); if (s != null) { setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), s)); } list = new BeanContextListView(beancontext); JScrollPane jscrollpane = new JScrollPane(list); jscrollpane.setMinimumSize(new Dimension(200, 300)); jscrollpane.setPreferredSize(new Dimension(200, 300)); final JPanel dummyMsg = new JPanel(); dummyMsg.add(new JLabel("", 0)); setLeftComponent(jscrollpane); setRightComponent(dummyMsg); list.addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting()) { return; } final int index = list.getSelectedIndex(); final ListModel model = list.getModel(); SwingUtilities.invokeLater( new Runnable() { public void run() { try { Object obj = ((BeanContextListView.ContextElement) model.getElementAt(index)).getUserObject(); if (obj instanceof BeanContextChildComponentProxy) { BeanContextChildComponentProxy beancontextchildcomponentproxy = (BeanContextChildComponentProxy) obj; setRightComponent(beancontextchildcomponentproxy.getComponent()); } else if (obj instanceof BeanContextContainerProxy) { BeanContextContainerProxy beancontextcontainerproxy = (BeanContextContainerProxy) obj; setRightComponent(beancontextcontainerproxy.getContainer()); } validate(); } catch (Exception _ex) { } } }); } }); list.getModel().addListDataListener( new ListDataListener() { public void intervalAdded(ListDataEvent e) { list.clearSelection(); setRightComponent(dummyMsg); validate(); } public void intervalRemoved(ListDataEvent e) { list.clearSelection(); setRightComponent(dummyMsg); validate(); } public void contentsChanged(ListDataEvent e) { list.clearSelection(); setRightComponent(dummyMsg); validate(); } }); } } --- NEW FILE: BeanContextListView.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package net.sourceforge.ejtools.awt; import java.awt.*; import java.awt.event.*; import java.beans.*; import java.beans.beancontext.*; import java.lang.reflect.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; /** * Description of the Class * * @author letiembl * @created 24 septembre 2001 */ public class BeanContextListView extends JList { static Class class$java$lang$Object; /** * Constructor for the BeanContextTreeView object * * @param beancontext Description of Parameter */ public BeanContextListView(BeanContext beancontext) { setModel(new BeanContextListModel(beancontext)); setCellRenderer( new DefaultListCellRenderer() { public Component getListCellRendererComponent(JList list, Object obj, int index, boolean isSelected, boolean cellHasFocus) { try { ContextElement contextelement = (ContextElement) obj; Object obj1 = contextelement.getUserObject(); super.getListCellRendererComponent(list, obj1, index, isSelected, cellHasFocus); if (contextelement.icon != null) { setIcon(contextelement.icon); } return this; } catch (Exception _ex) { System.err.println(_ex.getMessage()); return super.getListCellRendererComponent(list, obj, index, isSelected, cellHasFocus); } } }); } /** *Description of the Method * * @param s Description of Parameter * @return Description of the Returned Value */ static Class _mthclass$(String s) { try { return Class.forName(s); } catch (ClassNotFoundException classnotfoundexception) { throw new NoClassDefFoundError(classnotfoundexception.getMessage()); } } /** *Description of the Class * * @author Administrator * @created 16 octobre 2001 */ class BeanContextListModel implements ListModel, BeanContextMembershipListener { BeanContext context; Vector listeners = new Vector(); Vector content = new Vector(); /** *Constructor for the BeanContextListModel object * * @param beancontext Description of Parameter */ BeanContextListModel(BeanContext beancontext) { context = beancontext; context.addBeanContextMembershipListener(this); updateModel(); } /** *Gets the size attribute of the BeanContextListModel object * * @return The size value */ public int getSize() { return context.size(); } /** *Description of the Method * * @param index Description of Parameter * @return Description of the Returned Value */ public Object getElementAt(int index) { return content.elementAt(index); } /** *Adds a feature to the ListDataListener attribute of the BeanContextListModel object * * @param l The feature to be added to the ListDataListener attribute */ public void addListDataListener(ListDataListener l) { listeners.add(l); } /** *Description of the Method * * @param l Description of Parameter */ public void removeListDataListener(ListDataListener l) { listeners.remove(l); } /** *Description of the Method * * @param beancontextmembershipevent Description of Parameter */ public void childrenAdded(BeanContextMembershipEvent beancontextmembershipevent) { updateModel(); notifyListener(); } /** *Description of the Method * * @param beancontextmembershipevent Description of Parameter */ public void childrenRemoved(BeanContextMembershipEvent beancontextmembershipevent) { updateModel(); notifyListener(); } /** *Description of the Method */ private void notifyListener() { ListDataEvent event = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, content.size()); for (int i = 0; i < listeners.size(); i++) { ListDataListener listener = (ListDataListener) listeners.elementAt(i); listener.contentsChanged(event); } } /** *Description of the Method */ private void updateModel() { content.clear(); Iterator enum = context.iterator(); while (enum.hasNext()) { content.addElement(new ContextElement(enum.next())); } } } /** *Description of the Class * * @author Administrator * @created 16 octobre 2001 */ class ContextElement implements BeanContextChildComponentProxy { ImageIcon icon = null; String txt; Object obj; /** *Constructor for the ContextNode object * * @param obj1 Description of Parameter */ ContextElement(Object obj1) { obj = obj1; try { BeanInfo beaninfo = Introspector.getBeanInfo(obj1.getClass()); if (beaninfo.getIcon(1) != null) { icon = new ImageIcon(beaninfo.getIcon(1)); } Method method = obj1.getClass().getMethod("toString", new Class[0]); if (method.getDeclaringClass().equals(BeanContextListView.class$java$lang$Object == null ? ((Object) (BeanContextListView.class$java$lang$Object = BeanContextListView._mthclass$("java.lang.Object"))) : ((Object) (BeanContextListView.class$java$lang$Object)))) { txt = beaninfo.getBeanDescriptor().getDisplayName(); } } catch (Exception exception) { exception.printStackTrace(); } } /** *Gets the component attribute of the ContextNode object * * @return The component value */ public Component getComponent() { if (obj instanceof Component) { return (Component) obj; } if (obj instanceof BeanContextContainerProxy) { return ((BeanContextContainerProxy) obj).getContainer(); } if (obj instanceof BeanContextChildComponentProxy) { return ((BeanContextChildComponentProxy) obj).getComponent(); } else { return new JLabel("No GUI available", 0); } } /** *Gets the userObject attribute of the ContextNode object * * @return The userObject value */ public Object getUserObject() { return obj; } /** *Description of the Method * * @return Description of the Returned Value */ public String toString() { return txt != null ? txt : obj.toString(); } } } |