1 19 20 package org.netbeans.modules.java.ui; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.util.Collection ; 24 import java.util.Arrays ; 25 import java.util.LinkedList ; 26 27 import java.beans.PropertyChangeListener ; 28 import java.beans.PropertyChangeEvent ; 29 30 import org.openide.explorer.propertysheet.DefaultPropertyModel; 31 import org.openide.explorer.propertysheet.PropertyModel; 32 import org.openide.nodes.Index; 33 34 35 41 public abstract class IndexedPropertyChildren extends Index.KeysChildren { 42 44 private PropertyModel model; 45 46 48 private PropertyChangeListener beanListener; 49 50 53 boolean ignoreBeanChanges; 54 55 59 public IndexedPropertyChildren(Object bean, String propertyName) { 60 this(new DefaultPropertyModel(bean, propertyName)); 61 } 62 63 66 public IndexedPropertyChildren(PropertyModel m) { 67 super(new LinkedList ()); 68 this.model = m; 69 70 if (!m.getPropertyType().isArray()) { 71 throw new IllegalArgumentException ("Property of array type expected."); } 73 } 74 75 78 protected void addNotify() { 79 model.addPropertyChangeListener(getBeanListener()); 80 refreshData(); 81 } 82 83 87 protected void removeNotify() { 88 model.removePropertyChangeListener(getBeanListener()); 89 } 90 91 protected final PropertyChangeListener getBeanListener() { 92 if (beanListener != null) { 93 return beanListener; 94 } 95 return beanListener = createBeanListener(); 96 } 97 98 103 protected PropertyChangeListener createBeanListener() { 104 return new SourceListener(); 105 } 106 107 111 protected final void refreshData() { 112 Collection c; 113 try { 114 c = Arrays.asList((Object [])model.getValue()); 115 } catch (InvocationTargetException e) { 116 return; 118 } 119 120 this.list.clear(); 121 this.list.addAll(c); 122 update(); 123 } 124 125 130 protected Object [] createValue(Collection value) { 131 Class compType = model.getPropertyType().getComponentType(); 132 Object [] val = (Object [])java.lang.reflect.Array.newInstance(compType, value.size()); 133 return value.toArray(val); 134 } 135 136 142 protected void reorder(int []permutation) { 143 super.reorder(permutation); 144 try { 145 try { 146 ignoreBeanChanges = true; 147 Object [] value = createValue(list); 148 model.setValue(value); 149 } finally { 150 ignoreBeanChanges = false; 151 } 152 } catch (InvocationTargetException e) { 153 handleReorderException(e); 154 } 155 } 156 157 162 protected void handleReorderException(InvocationTargetException e) { 163 refreshData(); 164 } 165 166 167 private class SourceListener implements PropertyChangeListener { 168 public void propertyChange(PropertyChangeEvent evt) { 169 if (PropertyModel.PROP_VALUE.equals(evt.getPropertyName())) { 170 IndexedPropertyChildren.this.refreshData(); 172 } 173 } 174 } 175 } 176 | Popular Tags |