1 19 package org.netbeans.modules.loaderswitcher; 20 21 import java.awt.BorderLayout ; 22 import java.beans.PropertyVetoException ; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 import javax.swing.JComponent ; 28 import javax.swing.JPanel ; 29 30 31 import org.openide.DialogDescriptor; 32 import org.openide.filesystems.*; 33 import org.openide.explorer.*; 34 import org.openide.explorer.view.*; 35 import org.openide.loaders.*; 36 import org.openide.nodes.*; 37 import java.util.ArrayList ; 38 39 43 final class ObjectType extends JPanel 44 implements DataLoader.RecognizedFiles, java.beans.PropertyChangeListener , 45 ExplorerManager.Provider { 46 47 private DialogDescriptor dd; 48 49 private ExplorerManager em = new ExplorerManager(); 50 51 52 private static Logger LOG = Logger.getLogger(ObjectType.class.getName()); 53 54 58 private ObjectType(DataObject obj) { 59 setLayout(new BorderLayout ()); 60 61 DataLoader[] arr = findPossibleLoaders (obj, this); 62 63 Node[] nodes = new Node[arr.length]; 64 for (int i = 0; i < arr.length; i++) { 65 try { 66 nodes[i] = new BeanNode (arr[i]); 67 } catch (java.beans.IntrospectionException ex) { 68 nodes[i] = Node.EMPTY.cloneNode (); 69 } 70 } 71 72 Node root = new AbstractNode (new Children.Array ()); 74 root.getChildren ().add (nodes); 75 76 getExplorerManager ().setRootContext (root); 77 getExplorerManager ().setExploredContext(root, new Node[] { nodes[0] }); 78 79 add (BorderLayout.CENTER, new ListView ()); 80 81 getExplorerManager ().addPropertyChangeListener(this); 82 } 83 84 85 87 public static void convert (DataObject obj) { 88 try { 89 ObjectType reg = new ObjectType (obj); 90 91 javax.swing.JButton def = new javax.swing.JButton (org.openide.util.NbBundle.getMessage(ObjectType.class, "Default")); 92 def.setEnabled (DataLoaderPool.getPreferredLoader (obj.getPrimaryFile ()) != null); 93 94 DialogDescriptor dd = new DialogDescriptor (reg, org.openide.util.NbBundle.getMessage(ObjectType.class, "Choose_a_type_of_this_object")); 95 reg.dd = dd; 96 97 Object [] options = { DialogDescriptor.OK_OPTION, def, DialogDescriptor.CANCEL_OPTION }; 98 dd.setOptions (options); 99 dd.setClosingOptions (options); 100 java.awt.Dialog d = org.openide.DialogDisplayer.getDefault ().createDialog (dd); 101 102 103 d.setVisible(true); 104 105 if (dd.getValue () == DialogDescriptor.OK_OPTION) { 106 Node n = reg.getExplorerManager ().getSelectedNodes ()[0]; 107 org.openide.cookies.InstanceCookie ic = (org.openide.cookies.InstanceCookie)n.getCookie ( 108 org.openide.cookies.InstanceCookie.class 109 ); 110 111 DataLoader pref = (DataLoader)ic.instanceCreate (); 112 LOG.fine("pref: " + pref); 113 convertTo(obj, pref); 114 return; 115 } 116 if (dd.getValue () == def) { 117 DataLoaderPool.setPreferredLoader (obj.getPrimaryFile (), null); 119 obj.setValid (false); 120 return; 121 } 122 } catch (ClassNotFoundException ex) { 123 org.openide.ErrorManager.getDefault().notify(ex); 124 } catch (java.io.IOException ex) { 125 org.openide.ErrorManager.getDefault().notify(ex); 126 } catch (java.beans.PropertyVetoException ex) { 127 org.openide.ErrorManager.getDefault().notify(ex); 128 } 129 } 130 131 static void convertTo(final DataObject obj, final DataLoader pref) throws DataObjectNotFoundException, IOException , PropertyVetoException { 132 DataLoaderPool.setPreferredLoader ( 133 obj.getPrimaryFile (), pref 134 ); 135 obj.setValid (false); 136 if (LOG.isLoggable(Level.FINE)) { 137 LOG.fine("obj: " + obj.isValid ()); 138 LOG.fine("new: " + DataObject.find (obj.getPrimaryFile())); 139 } 140 } 141 142 146 static DataLoader[] findPossibleLoaders (DataObject obj, DataLoader.RecognizedFiles rec) { 147 DataLoaderPool pool = DataLoaderPool.getDefault (); 148 149 ArrayList recognize = new ArrayList (); 150 recognize.add (obj.getLoader ()); 151 152 DataLoader l = null; 153 Enumeration en = pool.allLoaders (); 154 while (en.hasMoreElements ()) { 155 l = (DataLoader)en.nextElement (); 156 try { 157 DataObject newobj = l.findDataObject (obj.getPrimaryFile(), rec); 158 if (newobj == obj) { 159 continue; 160 } 161 162 if (newobj != null) { 163 throw new IllegalStateException ("Object created for: " + newobj + " for: " + obj); } 165 } catch (DataObjectExistsException ex) { 166 recognize.add (l); 167 } catch (java.io.IOException ex) { 168 } 170 } 171 172 if (l != null && l != obj.getLoader()) { 174 recognize.add (l); 175 } 176 177 return (DataLoader[])recognize.toArray (new DataLoader[0]); 178 } 179 180 182 public void markRecognized(FileObject fo) { 183 } 184 185 public void propertyChange(java.beans.PropertyChangeEvent propertyChangeEvent) { 186 dd.setValid (getExplorerManager ().getSelectedNodes ().length == 1); 187 } 188 189 public ExplorerManager getExplorerManager() { 190 return em; 191 } 192 193 } 194 | Popular Tags |