1 19 20 21 package org.netbeans.modules.search.types; 22 23 24 import java.awt.event.HierarchyEvent ; 25 import java.awt.event.HierarchyListener ; 26 import java.beans.Customizer ; 27 import java.beans.PropertyChangeListener ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.TreeSet ; 32 import java.util.Enumeration ; 33 import java.util.List ; 34 import java.util.ResourceBundle ; 35 import javax.swing.border.CompoundBorder ; 36 import javax.swing.border.TitledBorder ; 37 import javax.swing.JPanel ; 38 39 import org.openide.ErrorManager; 40 import org.openide.awt.Mnemonics; 41 import org.openide.loaders.DataLoader; 42 import org.openide.loaders.DataLoaderPool; 43 import org.openide.util.Lookup; 44 import org.openide.util.NbBundle; 45 46 47 53 public class ObjectTypeCustomizer extends JPanel implements Customizer , 54 HierarchyListener { 55 56 61 private static final String FOLDER_LOADER_NAME 62 = "org.openide.loaders.DataFolder"; 64 65 private ObjectTypeType peer; 66 67 68 private boolean setting = false; 69 73 private boolean discovered = false; 74 75 80 private transient Collection loaders; 81 82 83 84 public ObjectTypeCustomizer() { 85 initComponents(); 86 initAccessibility(); 87 addHierarchyListener(this); 88 TitledBorder tb = new TitledBorder (NbBundle.getBundle(ObjectTypeCustomizer.class).getString("TEXT_LABEL_OBJECT_TYPE")); 89 90 tb.setBorder(new CompoundBorder ()); 91 setBorder (tb); 92 } 93 94 private void initAccessibility() { 95 ResourceBundle bundle = NbBundle.getBundle(ObjectTypeCustomizer.class); 96 this.getAccessibleContext().setAccessibleDescription(bundle.getString("TEXT_LABEL_OBJECT_TYPE")); 97 typeList.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_TypeList")); 98 typeList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_TypeList")); 99 jScrollPane1.getHorizontalScrollBar().getAccessibleContext() 100 .setAccessibleName(bundle.getString("ACSN_HScrollBar")); jScrollPane1.getVerticalScrollBar().getAccessibleContext() 102 .setAccessibleName(bundle.getString("ACSN_VScrollBar")); } 104 105 110 private void initComponents() { lblType = new javax.swing.JLabel (); 112 jScrollPane1 = new javax.swing.JScrollPane (); 113 typeList = new javax.swing.JList (); 114 115 setLayout(new java.awt.BorderLayout (0, 6)); 116 117 lblType.setLabelFor(typeList); 118 Mnemonics.setLocalizedText(lblType, NbBundle.getMessage(ObjectTypeCustomizer.class, "LBL_Type")); 119 add(lblType, java.awt.BorderLayout.NORTH); 120 121 typeList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 122 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 123 typeListValueChanged(evt); 124 } 125 }); 126 127 jScrollPane1.setViewportView(typeList); 128 129 add(jScrollPane1, java.awt.BorderLayout.CENTER); 130 131 } 133 private void typeListValueChanged (javax.swing.event.ListSelectionEvent evt) { if (setting) { 135 return; 137 } 138 if (loaders == null) { 139 peer.setMask(new Class [0]); 140 return; 141 } 142 143 List toret = new ArrayList (); 144 Object [] sel = typeList.getSelectedValues(); 145 146 for (Iterator i = loaders.iterator(); i.hasNext(); ) { 147 DataLoader nextLoader = (DataLoader) i.next(); 148 String id = nextLoader.getDisplayName(); 149 150 if (id == null) { 151 continue; } 153 for (int j = 0; j < sel.length; j++) { 154 if (sel[j] == null) { 155 continue; } 157 if (id.equals((String ) sel[j])) { 158 toret.add(nextLoader); 159 break; 160 } 161 } 162 } 163 164 Class [] ret = new Class [toret.size()]; 165 166 Iterator it = toret.iterator(); 167 int k = 0; 168 while (it.hasNext()) { 169 ret[k++] = it.next().getClass(); 170 } 171 172 peer.setMask(ret); 173 } 175 176 private javax.swing.JScrollPane jScrollPane1; 178 private javax.swing.JLabel lblType; 179 private javax.swing.JList typeList; 180 182 public void setObject(Object obj) { 184 peer = (ObjectTypeType) obj; 185 } 186 187 190 private void loadObjectTypes() { 191 assert loaders == null; 192 193 setting = true; 194 try { 195 196 Enumeration en = ((DataLoaderPool) Lookup.getDefault().lookup(DataLoaderPool.class)) 197 .allLoaders(); 198 199 TreeSet types = new TreeSet (); 201 202 ArrayList selected = new ArrayList (); 203 204 boolean folderLoaderFound = false; 205 206 Class [] mask = peer.getMask(); 207 if ((mask != null) && (mask.length == 0)) { 208 mask = null; 209 } 210 int loadersToFind = (mask != null) ? mask.length : 0; 211 212 loaders = new ArrayList (); 213 while (en.hasMoreElements()) { 214 DataLoader nextLoader = (DataLoader) en.nextElement(); 215 loaders.add(nextLoader); 216 217 221 if (!folderLoaderFound) { 222 if (FOLDER_LOADER_NAME 223 .equals(nextLoader.getRepresentationClassName())) { 224 folderLoaderFound = true; 225 continue; 226 } 227 } 228 229 String displayName; 230 try { 231 displayName = nextLoader.getDisplayName(); 232 } catch (Exception ex) { 233 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 234 continue; 235 } 236 if (displayName == null) { 237 continue; 238 } 239 types.add(displayName); 240 241 if (loadersToFind > 0) { 243 Class repreClass = nextLoader.getRepresentationClass(); 244 if (repreClass != null) { 245 for (int j = 0; j < mask.length; j++) { 246 if (mask[j] == null) { 247 continue; 248 } 249 DataLoader loader = DataLoader.getLoader(mask[j]); 250 if (loader != null && repreClass.equals(loader.getRepresentationClass())) { 251 selected.add(displayName); 252 loadersToFind--; 253 mask[j] = null; 254 break; 255 } 256 } 257 } 258 } 259 } 260 ((ArrayList ) loaders).trimToSize(); 261 262 typeList.setListData((String []) types.toArray(new String [types.size()])); 264 265 if (selected.size() > 0) { 266 int[] indexes = new int[selected.size()]; 267 268 ArrayList typesList = new ArrayList (types); 269 for (int i = 0; i < indexes.length; i++) { 270 indexes[i] = typesList.indexOf(selected.get(i)); 271 } 272 typeList.setSelectedIndices(indexes); 273 } 274 } catch (Exception e) { 275 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 276 } finally { 277 setting = false; 278 } 279 280 } 281 282 283 public void addPropertyChangeListener(PropertyChangeListener p1) {} 284 285 286 public void removePropertyChangeListener(PropertyChangeListener p1) {} 287 288 292 public void hierarchyChanged(HierarchyEvent e) { 293 if (discovered) { 294 return; 295 } 296 297 if (((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) 298 && isShowing()) { 299 discovered = true; 300 loadObjectTypes(); 301 } 302 } 303 304 305 public void requestFocus() { 306 typeList.requestFocus(); 307 } 308 309 310 } 311 | Popular Tags |