KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > loaderswitcher > ObjectType


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.loaderswitcher;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.beans.PropertyVetoException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.JPanel JavaDoc;
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 JavaDoc;
38
39 /**
40  *
41  * @author Jaroslav Tulach
42  */

43 final class ObjectType extends JPanel JavaDoc
44 implements DataLoader.RecognizedFiles, java.beans.PropertyChangeListener JavaDoc,
45 ExplorerManager.Provider {
46     /** dd we are included in */
47     private DialogDescriptor dd;
48     /** associated explorer manager */
49     private ExplorerManager em = new ExplorerManager();
50
51
52     private static Logger JavaDoc LOG = Logger.getLogger(ObjectType.class.getName());
53     
54     /** Creates the components to allow choice of a loader for given
55      * object.
56      * @param obj the object to choose data for
57      */

58     private ObjectType(DataObject obj) {
59         setLayout(new BorderLayout JavaDoc());
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 JavaDoc ex) {
68                 nodes[i] = Node.EMPTY.cloneNode ();
69             }
70         }
71         
72         // all loaders
73
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     /** Does conversion of a data object to new values.
86      */

87     public static void convert (DataObject obj) {
88         try {
89             ObjectType reg = new ObjectType (obj);
90
91             javax.swing.JButton JavaDoc def = new javax.swing.JButton JavaDoc (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 JavaDoc[] options = { DialogDescriptor.OK_OPTION, def, DialogDescriptor.CANCEL_OPTION };
98             dd.setOptions (options);
99             dd.setClosingOptions (options);
100             java.awt.Dialog JavaDoc 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                 // clear prefered loader
118
DataLoaderPool.setPreferredLoader (obj.getPrimaryFile (), null);
119                 obj.setValid (false);
120                 return;
121             }
122         } catch (ClassNotFoundException JavaDoc ex) {
123             org.openide.ErrorManager.getDefault().notify(ex);
124         } catch (java.io.IOException JavaDoc ex) {
125             org.openide.ErrorManager.getDefault().notify(ex);
126         } catch (java.beans.PropertyVetoException JavaDoc ex) {
127             org.openide.ErrorManager.getDefault().notify(ex);
128         }
129     }
130
131     static void convertTo(final DataObject obj, final DataLoader pref) throws DataObjectNotFoundException, IOException JavaDoc, PropertyVetoException JavaDoc {
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     /** Computes the list of DataLoaders that are able to recognize given data object.
143      * @param obj the object to check
144      * @return list of loaders (first is the current that recognize the object)
145      */

146     static DataLoader[] findPossibleLoaders (DataObject obj, DataLoader.RecognizedFiles rec) {
147         DataLoaderPool pool = DataLoaderPool.getDefault ();
148         
149         ArrayList JavaDoc recognize = new ArrayList JavaDoc ();
150         recognize.add (obj.getLoader ());
151         
152         DataLoader l = null;
153         Enumeration JavaDoc 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 JavaDoc ("Object created for: " + newobj + " for: " + obj); // NOI18N
164
}
165             } catch (DataObjectExistsException ex) {
166                 recognize.add (l);
167             } catch (java.io.IOException JavaDoc ex) {
168                 // does not recognize
169
}
170         }
171         
172         // the last one is default data loader and it can recognize anything
173
if (l != null && l != obj.getLoader()) {
174             recognize.add (l);
175         }
176
177         return (DataLoader[])recognize.toArray (new DataLoader[0]);
178     }
179     
180     /** Implements DataLoader.RecognizedFiles inteface, but does nothing.
181      */

182     public void markRecognized(FileObject fo) {
183     }
184     
185     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc propertyChangeEvent) {
186         dd.setValid (getExplorerManager ().getSelectedNodes ().length == 1);
187     }
188
189     public ExplorerManager getExplorerManager() {
190         return em;
191     }
192     
193 }
194
Popular Tags