1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import javax.swing.*; 27 import javax.swing.border.*; 28 import javax.swing.tree.*; 29 import org.openide.explorer.*; 30 import org.openide.explorer.propertysheet.PropertyEnv; 31 import org.openide.explorer.view.*; 32 import org.openide.loaders.*; 33 import org.openide.nodes.*; 34 import org.openide.util.*; 35 import org.netbeans.beaninfo.ExplorerPanel; 36 import org.netbeans.beaninfo.editors.DataObjectPanel.FilteredChildren; 37 38 45 public class DataObjectTreeView extends DataObjectPanel { 46 47 final static int DEFAULT_INSET = 10; 48 49 private ExplorerPanel expPanel; 50 private TreeView reposTree; 51 52 public DataObjectTreeView (PropertyEditorSupport my, PropertyEnv env) { 53 super(my, env); 54 initComponent(); 55 56 reposTree.getAccessibleContext().setAccessibleName( NbBundle.getMessage(DataObjectTreeView.class, "ACSN_DataObjectPanel")); 57 setDescription( NbBundle.getMessage(DataObjectTreeView.class, "ACSD_DataObjectPanel")); 58 } 59 60 public void addNotify() { 61 completeInitialization(); 62 super.addNotify(); 63 } 64 65 66 private void initComponent() { 67 expPanel = new ExplorerPanel(); 68 expPanel.setLayout(new BorderLayout()); 69 reposTree = new BeanTreeView(); 70 reposTree.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 71 reposTree.setPopupAllowed(false); 72 reposTree.setDefaultActionAllowed(false); 73 expPanel.add(reposTree, "Center"); } 75 76 private boolean initialized=false; 77 78 private void completeInitialization() { 79 if (initialized) { 80 return; 84 } 85 if (insets != null) { 86 setBorder(new EmptyBorder(insets)); 87 } else { 88 setBorder(new EmptyBorder(12, 12, 0, 11)); 89 } 90 setLayout(new BorderLayout(0, 2)); 91 92 if (subTitle != null) { 93 JLabel l = new JLabel(subTitle); 94 l.setLabelFor(reposTree); 95 add(l, BorderLayout.NORTH); 96 } 97 98 if (rootNode == null) { 99 if (dataFilter != null) { 100 if (folderFilter != null) { 101 DataFilter dFilter = new DataFilter() { 102 public boolean acceptDataObject(DataObject obj) { 103 if (folderFilter.acceptDataObject(obj)) { 104 return true; 105 } 106 return dataFilter.acceptDataObject(obj); 107 } 108 }; 109 rootNode = RepositoryNodeFactory.getDefault().repository(dFilter); 110 } else { 111 rootNode = RepositoryNodeFactory.getDefault().repository(dataFilter); 112 } 113 } else { 114 if (folderFilter != null) { 115 rootNode = RepositoryNodeFactory.getDefault().repository(folderFilter); 116 } else { 117 rootNode = RepositoryNodeFactory.getDefault().repository(DataFilter.ALL); 118 } 119 } 120 } 121 122 if (nodeFilter != null) { 123 FilteredChildren children = 124 new FilteredChildren(rootNode, nodeFilter, dataFilter); 125 FilterNode n = new FilterNode(rootNode, children); 126 rootNode = n; 127 } 128 129 Node rNode = rootNode; 130 if (rootObject != null) { 131 Node n = findNodeForObj(rootNode, rootObject); 132 if (n != null) { 133 NodeAcceptor naccep = nodeFilter; 134 if (naccep == null) { 135 naccep = new NodeAcceptor() { 136 public boolean acceptNodes(Node [] nodes) { 137 return false; 138 } 139 }; 140 } 141 FilteredChildren children = 142 new FilteredChildren(n, naccep, dataFilter); 143 FilterNode filtNode = new FilterNode(n, children); 144 rNode = filtNode; 145 } 146 } 147 148 expPanel.getExplorerManager().setRootContext(rNode); 149 150 Node theNode = null; 151 if (dObj != null) { 152 theNode = findNodeForObj(rNode, dObj); 153 } 154 if (theNode != null) { 155 try { 156 expPanel.getExplorerManager().setSelectedNodes 157 (new Node [] { theNode }); 158 } catch (PropertyVetoException pve) { 159 Logger.getLogger(DataObjectTreeView.class.getName()).log(Level.WARNING, null, pve); 160 } catch (IllegalArgumentException iae) { 161 Logger.getLogger(DataObjectTreeView.class.getName()).log(Level.WARNING, null, iae); 162 } 163 } 164 165 expPanel.getExplorerManager().addPropertyChangeListener( 166 new PropertyChangeListener() { 167 public void propertyChange(PropertyChangeEvent evt) { 168 if (ExplorerManager.PROP_SELECTED_NODES.equals 169 (evt.getPropertyName())) { 170 Node [] nodes = (Node []) evt.getNewValue(); 171 DataObject d = getDataObject(); 172 boolean enableOK = false; 173 if ((nodes != null) && (nodes.length > 0) && 174 (dataFilter != null) && (d != null)) { 175 enableOK = dataFilter.acceptDataObject( d ); 176 } else { 177 enableOK = ( d != null ); 178 } 179 if ( enableOK ) 180 myEditor.setValue( d ); 181 setOkButtonEnabled( enableOK ); 182 } 183 } 184 }); 185 186 add(expPanel, BorderLayout.CENTER); 187 188 if ((dataFilter != null) && (getDataObject() != null)) { 189 setOkButtonEnabled( 190 dataFilter.acceptDataObject(getDataObject())); 191 } else { 192 setOkButtonEnabled(getDataObject() != null); 193 } 194 initialized=true; 195 } 196 197 202 public void setDescription(String desc) { 203 getAccessibleContext().setAccessibleDescription(desc); 204 reposTree.getAccessibleContext().setAccessibleDescription(desc); 205 } 206 207 211 public DataObject getDataObject() { 212 DataObject retValue = null; 213 Node[] na = expPanel.getExplorerManager().getSelectedNodes(); 214 if ((na != null) && (na.length>0)) { 215 retValue = (DataObject)na[0].getCookie(DataObject.class); 216 } 217 return retValue; 218 } 219 220 224 public Node getNode() { 225 Node retValue = null; 226 Node[] na = expPanel.getExplorerManager().getSelectedNodes(); 227 if ((na != null) && (na.length>0)) { 228 retValue = na[0]; 229 } 230 return retValue; 231 } 232 233 238 public Object getPropertyValue() throws IllegalStateException { 239 return getDataObject(); 240 } 241 } 242 | Popular Tags |