|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| BeanContextListView.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* EJTools, the Enterprise Java Tools
|
|
| 3 |
*
|
|
| 4 |
* Distributable under LGPL license.
|
|
| 5 |
* See terms of license at www.gnu.org.
|
|
| 6 |
*/
|
|
| 7 |
package org.ejtools.adwt;
|
|
| 8 |
|
|
| 9 |
import java.awt.Component;
|
|
| 10 |
import java.beans.BeanInfo;
|
|
| 11 |
import java.beans.Introspector;
|
|
| 12 |
import java.beans.beancontext.BeanContext;
|
|
| 13 |
import java.beans.beancontext.BeanContextChildComponentProxy;
|
|
| 14 |
import java.beans.beancontext.BeanContextContainerProxy;
|
|
| 15 |
import java.beans.beancontext.BeanContextMembershipEvent;
|
|
| 16 |
import java.beans.beancontext.BeanContextMembershipListener;
|
|
| 17 |
import java.lang.reflect.Method;
|
|
| 18 |
import java.util.Iterator;
|
|
| 19 |
import java.util.Vector;
|
|
| 20 |
|
|
| 21 |
import javax.swing.DefaultListCellRenderer;
|
|
| 22 |
import javax.swing.ImageIcon;
|
|
| 23 |
import javax.swing.JLabel;
|
|
| 24 |
import javax.swing.JList;
|
|
| 25 |
import javax.swing.ListModel;
|
|
| 26 |
import javax.swing.event.ListDataEvent;
|
|
| 27 |
import javax.swing.event.ListDataListener;
|
|
| 28 |
|
|
| 29 |
/**
|
|
| 30 |
* Description of the Class
|
|
| 31 |
*
|
|
| 32 |
* @author Laurent Etiemble
|
|
| 33 |
* @version $Revision: 1.5 $
|
|
| 34 |
*/
|
|
| 35 |
public class BeanContextListView extends JList |
|
| 36 |
{
|
|
| 37 |
/**
|
|
| 38 |
* Constructor for the BeanContextTreeView object
|
|
| 39 |
*
|
|
| 40 |
* @param beancontext Description of Parameter
|
|
| 41 |
*/
|
|
| 42 | 0 |
public BeanContextListView(BeanContext beancontext)
|
| 43 |
{
|
|
| 44 | 0 |
this.setModel(new BeanContextListModel(beancontext)); |
| 45 | 0 |
this.setCellRenderer(
|
| 46 |
new DefaultListCellRenderer()
|
|
| 47 |
{
|
|
| 48 | 0 |
public Component getListCellRendererComponent(JList list, Object obj, int index, boolean isSelected, boolean cellHasFocus) |
| 49 |
{
|
|
| 50 | 0 |
try
|
| 51 |
{
|
|
| 52 | 0 |
ContextElement contextelement = (ContextElement) obj; |
| 53 | 0 |
Object obj1 = contextelement.getUserObject(); |
| 54 | 0 |
super.getListCellRendererComponent(list, obj1, index, isSelected, cellHasFocus);
|
| 55 | 0 |
if (contextelement.icon != null) |
| 56 |
{
|
|
| 57 | 0 |
setIcon(contextelement.icon); |
| 58 |
} |
|
| 59 | 0 |
return this; |
| 60 |
} |
|
| 61 |
catch (Exception _ex)
|
|
| 62 |
{
|
|
| 63 | 0 |
System.err.println(_ex.getMessage()); |
| 64 | 0 |
return super.getListCellRendererComponent(list, obj, index, isSelected, cellHasFocus); |
| 65 |
} |
|
| 66 |
} |
|
| 67 |
}); |
|
| 68 |
|
|
| 69 |
} |
|
| 70 |
|
|
| 71 |
|
|
| 72 |
/**
|
|
| 73 |
* Description of the Class
|
|
| 74 |
*
|
|
| 75 |
* @author Administrator
|
|
| 76 |
* @version $Revision: 1.5 $
|
|
| 77 |
*/
|
|
| 78 |
protected class BeanContextListModel implements ListModel, BeanContextMembershipListener |
|
| 79 |
{
|
|
| 80 |
/** Description of the Field */
|
|
| 81 |
protected Vector content = new Vector(); |
|
| 82 |
/** Description of the Field */
|
|
| 83 |
protected BeanContext context;
|
|
| 84 |
/** Description of the Field */
|
|
| 85 |
protected Vector listeners = new Vector(); |
|
| 86 |
|
|
| 87 |
|
|
| 88 |
/**
|
|
| 89 |
* Constructor for the BeanContextListModel object
|
|
| 90 |
*
|
|
| 91 |
* @param beancontext Description of Parameter
|
|
| 92 |
*/
|
|
| 93 | 0 |
public BeanContextListModel(BeanContext beancontext)
|
| 94 |
{
|
|
| 95 | 0 |
this.context = beancontext;
|
| 96 | 0 |
this.context.addBeanContextMembershipListener(this); |
| 97 | 0 |
this.updateModel();
|
| 98 |
} |
|
| 99 |
|
|
| 100 |
|
|
| 101 |
/**
|
|
| 102 |
* Adds a feature to the ListDataListener attribute of the BeanContextListModel object
|
|
| 103 |
*
|
|
| 104 |
* @param l The feature to be added to the ListDataListener attribute
|
|
| 105 |
*/
|
|
| 106 | 0 |
public void addListDataListener(ListDataListener l) |
| 107 |
{
|
|
| 108 | 0 |
this.listeners.add(l);
|
| 109 |
} |
|
| 110 |
|
|
| 111 |
|
|
| 112 |
/**
|
|
| 113 |
* Description of the Method
|
|
| 114 |
*
|
|
| 115 |
* @param beancontextmembershipevent Description of Parameter
|
|
| 116 |
*/
|
|
| 117 | 0 |
public void childrenAdded(BeanContextMembershipEvent beancontextmembershipevent) |
| 118 |
{
|
|
| 119 | 0 |
this.updateModel();
|
| 120 | 0 |
this.notifyListener();
|
| 121 |
} |
|
| 122 |
|
|
| 123 |
|
|
| 124 |
/**
|
|
| 125 |
* Description of the Method
|
|
| 126 |
*
|
|
| 127 |
* @param beancontextmembershipevent Description of Parameter
|
|
| 128 |
*/
|
|
| 129 | 0 |
public void childrenRemoved(BeanContextMembershipEvent beancontextmembershipevent) |
| 130 |
{
|
|
| 131 | 0 |
this.updateModel();
|
| 132 | 0 |
this.notifyListener();
|
| 133 |
} |
|
| 134 |
|
|
| 135 |
|
|
| 136 |
/**
|
|
| 137 |
* Description of the Method
|
|
| 138 |
*
|
|
| 139 |
* @param index Description of Parameter
|
|
| 140 |
* @return Description of the Returned Value
|
|
| 141 |
*/
|
|
| 142 | 0 |
public Object getElementAt(int index) |
| 143 |
{
|
|
| 144 | 0 |
return this.content.elementAt(index); |
| 145 |
} |
|
| 146 |
|
|
| 147 |
|
|
| 148 |
/**
|
|
| 149 |
* Gets the size attribute of the BeanContextListModel object
|
|
| 150 |
*
|
|
| 151 |
* @return The size value
|
|
| 152 |
*/
|
|
| 153 | 0 |
public int getSize() |
| 154 |
{
|
|
| 155 | 0 |
return this.context.size(); |
| 156 |
} |
|
| 157 |
|
|
| 158 |
|
|
| 159 |
/**
|
|
| 160 |
* Description of the Method
|
|
| 161 |
*
|
|
| 162 |
* @param l Description of Parameter
|
|
| 163 |
*/
|
|
| 164 | 0 |
public void removeListDataListener(ListDataListener l) |
| 165 |
{
|
|
| 166 | 0 |
this.listeners.remove(l);
|
| 167 |
} |
|
| 168 |
|
|
| 169 |
|
|
| 170 |
/** Description of the Method */
|
|
| 171 | 0 |
private void notifyListener() |
| 172 |
{
|
|
| 173 | 0 |
ListDataEvent event = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.content.size()); |
| 174 | 0 |
for (int i = 0; i < this.listeners.size(); i++) |
| 175 |
{
|
|
| 176 | 0 |
ListDataListener listener = (ListDataListener) this.listeners.elementAt(i);
|
| 177 | 0 |
listener.contentsChanged(event); |
| 178 |
} |
|
| 179 |
} |
|
| 180 |
|
|
| 181 |
|
|
| 182 |
/** Description of the Method */
|
|
| 183 | 0 |
private void updateModel() |
| 184 |
{
|
|
| 185 | 0 |
this.content.clear();
|
| 186 | 0 |
Iterator iterator = this.context.iterator();
|
| 187 | 0 |
while (iterator.hasNext())
|
| 188 |
{
|
|
| 189 | 0 |
this.content.addElement(new ContextElement(iterator.next())); |
| 190 |
} |
|
| 191 |
} |
|
| 192 |
} |
|
| 193 |
|
|
| 194 |
|
|
| 195 |
/**
|
|
| 196 |
* Description of the Class
|
|
| 197 |
*
|
|
| 198 |
* @author Administrator
|
|
| 199 |
* @version $Revision: 1.5 $
|
|
| 200 |
*/
|
|
| 201 |
protected class ContextElement implements BeanContextChildComponentProxy |
|
| 202 |
{
|
|
| 203 |
/** Description of the Field */
|
|
| 204 |
protected ImageIcon icon = null; |
|
| 205 |
/** Description of the Field */
|
|
| 206 |
protected Object obj;
|
|
| 207 |
/** Description of the Field */
|
|
| 208 |
protected String txt;
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
/**
|
|
| 212 |
* Constructor for the ContextNode object
|
|
| 213 |
*
|
|
| 214 |
* @param obj1 Description of Parameter
|
|
| 215 |
*/
|
|
| 216 | 0 |
public ContextElement(Object obj1)
|
| 217 |
{
|
|
| 218 | 0 |
obj = obj1; |
| 219 | 0 |
try
|
| 220 |
{
|
|
| 221 | 0 |
BeanInfo beaninfo = Introspector.getBeanInfo(obj1.getClass()); |
| 222 | 0 |
if (beaninfo.getIcon(1) != null) |
| 223 |
{
|
|
| 224 | 0 |
icon = new ImageIcon(beaninfo.getIcon(1));
|
| 225 |
} |
|
| 226 | 0 |
Method method = obj1.getClass().getMethod("toString", new Class[0]); |
| 227 | 0 |
if (method.getDeclaringClass().equals(Object.class)) |
| 228 |
{
|
|
| 229 | 0 |
txt = beaninfo.getBeanDescriptor().getDisplayName(); |
| 230 |
} |
|
| 231 |
} |
|
| 232 |
catch (Exception exception)
|
|
| 233 |
{
|
|
| 234 | 0 |
exception.printStackTrace(); |
| 235 |
} |
|
| 236 |
} |
|
| 237 |
|
|
| 238 |
|
|
| 239 |
/**
|
|
| 240 |
* Gets the component attribute of the ContextNode object
|
|
| 241 |
*
|
|
| 242 |
* @return The component value
|
|
| 243 |
*/
|
|
| 244 | 0 |
public Component getComponent()
|
| 245 |
{
|
|
| 246 | 0 |
if (obj instanceof Component) |
| 247 |
{
|
|
| 248 | 0 |
return (Component) obj;
|
| 249 |
} |
|
| 250 | 0 |
if (obj instanceof BeanContextContainerProxy) |
| 251 |
{
|
|
| 252 | 0 |
return ((BeanContextContainerProxy) obj).getContainer();
|
| 253 |
} |
|
| 254 | 0 |
if (obj instanceof BeanContextChildComponentProxy) |
| 255 |
{
|
|
| 256 | 0 |
return ((BeanContextChildComponentProxy) obj).getComponent();
|
| 257 |
} |
|
| 258 |
else
|
|
| 259 |
{
|
|
| 260 | 0 |
return new JLabel("No GUI available", 0); |
| 261 |
} |
|
| 262 |
} |
|
| 263 |
|
|
| 264 |
|
|
| 265 |
/**
|
|
| 266 |
* Gets the userObject attribute of the ContextNode object
|
|
| 267 |
*
|
|
| 268 |
* @return The userObject value
|
|
| 269 |
*/
|
|
| 270 | 0 |
public Object getUserObject()
|
| 271 |
{
|
|
| 272 | 0 |
return obj;
|
| 273 |
} |
|
| 274 |
|
|
| 275 |
|
|
| 276 |
/**
|
|
| 277 |
* Description of the Method
|
|
| 278 |
*
|
|
| 279 |
* @return Description of the Returned Value
|
|
| 280 |
*/
|
|
| 281 | 0 |
public String toString()
|
| 282 |
{
|
|
| 283 | 0 |
return txt != null ? txt : obj.toString(); |
| 284 |
} |
|
| 285 |
} |
|
| 286 |
} |
|
| 287 |
|
|
| 288 |
|
|
||||||||||