This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 22422
Collapse All | Expand All

(-)core/src/org/netbeans/core/resources/mf-layer.xml (-1 / +22 lines)
Lines 71-79 Link Here
71
                <attr name="instanceClass" stringvalue="org.openide.xml.EntityCatalog" />
71
                <attr name="instanceClass" stringvalue="org.openide.xml.EntityCatalog" />
72
                <attr name="instanceCreate" methodvalue="org.openide.loaders.XMLDataObject.createEntityCatalog"/>
72
                <attr name="instanceCreate" methodvalue="org.openide.loaders.XMLDataObject.createEntityCatalog"/>
73
            </file>
73
            </file>
74
74
            
75
            <!--New XML property editor manager registration-->
76
            <file name="org-netbeans-core-NbPropertyEditorManagerImpl.instance">
77
                <attr name="instanceClass" stringvalue="org.netbeans.core.NbPropertyEditorManagerImpl"/>
78
                <attr name="instanceOf" stringvalue="org.openide.util.NbPropertyEditorManager"/>
79
                <attr name="instanceCreate" methodvalue="org.netbeans.core.NbPropertyEditorManagerImpl.getInstance"/>
80
            </file>
75
        </folder>
81
        </folder>
76
    </folder> 
82
    </folder> 
83
    
84
    <folder name="propertyeditors">
85
        <file name="String">
86
            <attr name="editorFor" stringvalue="java.lang.String"/>
87
            <attr name="editorClass" stringvalue="org.netbeans.beaninfo.editors.StringEditor"/>
88
        </file>
89
        <file name="Insets">
90
            <attr name="editorFor" stringvalue="java.awt.Insets"/>
91
            <attr name="editorClass" stringvalue="org.netbeans.beaninfo.editors.InsetsEditor"/>
92
        </file>
93
        <file name="Color">
94
            <attr name="editorFor" stringvalue="java.awt.Color"/>
95
            <attr name="editorClass" stringvalue="org.netbeans.beaninfo.editors.ColorEditor"/>
96
        </file>
97
    </folder>
77
98
78
    <folder name="Mount">
99
    <folder name="Mount">
79
        <attr name="SystemFileSystem.localizingBundle" stringvalue="org.netbeans.core.Bundle" />
100
        <attr name="SystemFileSystem.localizingBundle" stringvalue="org.netbeans.core.Bundle" />
(-)core/src/org/netbeans/core/NbPropertyEditorManagerImpl.java (+239 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
/*
14
 * NbPropertyEditorManager.java
15
 *
16
 * Created on January 12, 2003, 8:06 PM
17
 */
18
19
package org.netbeans.core;
20
import java.beans.*;
21
import java.util.*;
22
import org.openide.filesystems.*;
23
import org.openide.ErrorManager;
24
import org.openide.util.NbPropertyEditorManager;
25
/** Provides property editors registered via XML, falling
26
 *  back to <code>java.beans.PropertyEditorManager</code>.
27
 * @author  Tim Boudreau
28
 */
29
final class NbPropertyEditorManagerImpl extends NbPropertyEditorManager {
30
    private final HashMap map = new HashMap();
31
    private String folder;
32
    public static final String ATTR_EDITORFOR = "editorFor"; //NOI18N
33
    public static final String ATTR_EDITORCLASS = "editorClass"; //NOI18N
34
    
35
    public NbPropertyEditorManagerImpl () {
36
        this ("/propertyeditors");
37
    }
38
    
39
    private NbPropertyEditorManagerImpl (String editorFolder) {
40
        //XXX may want to expose this constructor later, for e.g. form 
41
        //editor which wants its own private property editor registry
42
        folder = editorFolder;
43
    }
44
    
45
    public PropertyEditor findEditor (Class clazz) {
46
        return findEditor (clazz, true);
47
    }
48
    
49
    private static NbPropertyEditorManager instance=null;
50
    public static NbPropertyEditorManager getInstance() {
51
        if (instance == null) {
52
            instance = new NbPropertyEditorManagerImpl(); //NOI18N
53
        }
54
        return instance;
55
    }
56
    
57
    public PropertyEditor findEditor (Class clazz, boolean newInstance) {
58
        synchronized (map) {
59
            if (map.isEmpty()) buildMap();
60
        }
61
        System.out.println("Looking for editor for " + clazz.getName());
62
        String cname = clazz.getName();
63
        
64
        System.out.println("Map lookup for " + cname);
65
        System.out.println("Map contents:");
66
        Iterator i = map.keySet().iterator();
67
        while (i.hasNext()) 
68
            System.out.println(" -" + i.next());
69
        
70
        Info in = (Info) map.get(cname);
71
        PropertyEditor result = null;
72
        if (in != null) 
73
            result = in.get (newInstance);
74
        //Result will only be null if there was a cnfe fetching the editor
75
        //class, or if no editor is registered via XML
76
        if (result != null) return result;
77
        System.out.println("Falling back to property editor manager");
78
        //Fall back to PropertyEditorManager
79
        return PropertyEditorManager.findEditor (clazz);
80
    }
81
    
82
    private boolean listening=false;
83
    private void buildMap() {
84
        synchronized (map) {
85
            FileObject fo = org.openide.filesystems.Repository.getDefault().getDefaultFileSystem().findResource(folder); 
86
            FileObject[] kids = fo.getChildren();
87
            synchronized (map) {
88
                for (int i=0; i < kids.length; i++) {
89
                    String editorFor = (String)kids[i].getAttribute ("editorFor"); //NOI18N
90
                    map.put (editorFor, new InfoImpl (kids[i]));
91
                }
92
            }
93
            if (listening == false) {
94
                fo.addFileChangeListener(new FileChangeListener () {
95
                    public void fileFolderCreated (FileEvent fe){
96
                        //meaningless
97
                    }
98
                    public void fileDataCreated (FileEvent fe){
99
                        synchronized (map) {
100
                            map.clear();
101
                        }
102
                    }
103
                    public void fileChanged (FileEvent fe) {
104
                        synchronized (map) {
105
                            map.clear();
106
                        }
107
                    }
108
                    public void fileDeleted (FileEvent fe) {
109
                        synchronized (map) {
110
                            map.clear();
111
                        }
112
                    }
113
114
                    public void fileRenamed (FileRenameEvent fe) {
115
                        //no effect
116
                    }
117
118
                    public void fileAttributeChanged (FileAttributeEvent fe) {
119
                        //in the case that editors are added or removed,
120
                        //dump the cache - the next call will rebuild it
121
                        synchronized (map) {
122
                            map.clear();
123
                        }
124
                    }
125
126
                });
127
                listening = true;
128
                }
129
            }
130
        }
131
    
132
    public boolean hasEditor (Class clazz) {
133
        synchronized (map) {
134
            if (map.isEmpty()) buildMap();
135
        }
136
        String cname = clazz.getName();
137
        return map.keySet().contains (cname);
138
    }
139
140
    public Info findInfo(Class clazz) {
141
        if (map.isEmpty()) buildMap();
142
        return (Info) map.get (clazz.getName());
143
    }
144
    
145
    class InfoImpl implements NbPropertyEditorManager.Info {
146
        private String editorClassName;
147
        private String editorFor;
148
        private Class editorClass;
149
        private Class edForClass;
150
        private boolean badClassInfo=false;
151
        private PropertyEditor defaultInstance=null;  //make weak/soft ref?
152
        private boolean initFromPe=false;
153
        public InfoImpl (FileObject fo) {
154
            editorClassName = (String) fo.getAttribute (ATTR_EDITORCLASS); //NOI18N
155
            editorFor = (String) fo.getAttribute (ATTR_EDITORFOR); //NOI18N
156
            System.out.println("Registered " + editorClassName + " for " + editorFor); //XXX debug code
157
        }
158
        
159
        public PropertyEditor get(boolean newInstance) {
160
            PropertyEditor result = null;
161
            if (newInstance) {
162
                try {
163
                    result = (PropertyEditor) getEditorClass().newInstance();
164
                    System.out.println("Created new instance of " + result.getClass().getName());
165
                    return result;
166
                } catch (Exception e) {
167
                    e.printStackTrace();
168
                    return null;
169
                }
170
            } else if (defaultInstance == null) {
171
                try {
172
                    result = (PropertyEditor) getEditorClass().newInstance();
173
                    defaultInstance = result;
174
                    System.out.println("Created default instance of " + result.getClass().getName());
175
                } catch (Exception e) {
176
                    e.printStackTrace();
177
                }
178
            }
179
            return result;
180
        }
181
182
        private Class getEditorClass () {
183
            //If we know we have a bad registration, throwing the
184
            //exception once is enough.
185
            if (badClassInfo) return null;
186
            if (editorClass == null) {
187
                try {
188
                    editorClass = Class.forName(editorClassName);
189
                } catch (ClassNotFoundException cnfe) {
190
                    ErrorManager.getDefault().log (ErrorManager.WARNING, 
191
                    "The class \"" + editorClassName + "\" which is a property editor class for \"" //NOI18N 
192
                    + editorFor + "\" is registered cannot be loaded."); //NOI18N
193
                    ErrorManager.getDefault().notify(ErrorManager.WARNING, cnfe);
194
                    badClassInfo = true;
195
                }
196
                if (editorClass != null) editorClassName = null;
197
            }
198
            return editorClass;
199
        }
200
        
201
        private Class getEdForClass () {
202
            //If we know we have a bad registration, throwing the
203
            //exception once is enough.
204
            if (badClassInfo) {
205
                return null;
206
            }
207
            if (edForClass == null) {
208
                try {
209
                    edForClass = Class.forName(editorFor);
210
                } catch (ClassNotFoundException cnfe) {
211
                    ErrorManager.getDefault().log (ErrorManager.WARNING, 
212
                    "The class \"" + editorFor + "\" for which the property editor class \"" //NOI18N 
213
                    + editorClassName + "\" is registered cannot be loaded."); //NOI18N
214
                    ErrorManager.getDefault().notify(ErrorManager.WARNING, cnfe);
215
                    badClassInfo = true;
216
                }
217
                if (edForClass != null) editorFor = null;
218
            }
219
            return edForClass;
220
        }
221
        
222
        public int hashCode () {
223
            if (badClassInfo) return super.hashCode();
224
            return getEditorClass().getName().hashCode() ^ 31;
225
        }
226
        
227
        public boolean equals (Object o) {
228
            if (!(o instanceof NbPropertyEditorManager.Info))
229
                return false;
230
            return o.hashCode() == hashCode();
231
        }
232
        
233
        public Object getAttribute(Object key) {
234
            //do nothing for now, maybe return fo's attributes
235
            return null;
236
        }
237
    }
238
    
239
}
(-)openide/src/org/openide/util/NbPropertyEditorManager.java (+164 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
/*
14
 * NbPropertyEditorManager.java
15
 *
16
 * Created on January 12, 2003, 10:49 PM
17
 */
18
19
package org.openide.util;
20
import java.beans.PropertyEditor;
21
import java.beans.PropertyEditorManager;
22
import org.openide.ErrorManager;
23
/** NetBeans property editor manager.  The default implementation
24
 * in the NetBeans IDE allows registration of property editors
25
 * via XML. <P>  The preferred method of fetching property editors
26
 * in NetBeans is by using this class -
27
 * <code>java.beans.PropertyEditorManager</code> will not contain
28
 * property editors registered using the XML registration
29
 * technique.  The advantage of registration via XML is that
30
 * the classes do not need to be loaded on IDE startup, and
31
 * many modules that currently use ModuleInstall classes to
32
 * do such registration may eliminated them by using layer-based
33
 * registration instead.
34
 * @author Tim Boudreau
35
 */
36
public abstract class NbPropertyEditorManager {
37
    
38
    private static NbPropertyEditorManager defaultInstance=null;
39
    /** Get the default implementation of NbPropertyEditorManager
40
     * @return The implementation of NbPropertyEditorManager.
41
     */    
42
    public static NbPropertyEditorManager getDefault() {
43
        if (defaultInstance == null) {
44
            try {
45
                defaultInstance = (NbPropertyEditorManager) Lookup.getDefault().lookup (NbPropertyEditorManager.class);
46
            } catch (Exception e) {
47
                ErrorManager.getDefault().log (ErrorManager.INFORMATIONAL, 
48
                "No instance of NbPropertyEditorManager registered in lookup.  Using proxy for java.beans.PropertyEditorManager instead."); //NOI18N
49
                ErrorManager.getDefault().notify (ErrorManager.INFORMATIONAL, e);
50
            } finally {
51
                if (defaultInstance == null) defaultInstance = new BeansWrapper();
52
                return defaultInstance;
53
            }
54
        }
55
        return defaultInstance;
56
    }
57
    /*
58
     //could optionally implement as static methods, changing names of instance methods
59
    public static final PropertyEditor findEditor (Class clazz, boolean newInstance) {
60
        return getDefault().findEditor (clazz, newInstance);
61
    }
62
    
63
    public static final PropertyEditor findEditor (Class clazz) {
64
        return getDefault().findEditor (clazz);
65
    }
66
    
67
    public static final boolean hasEditor (Class clazz) {
68
        return getDefault().hasEditor (clazz);
69
    }
70
    
71
    public static final Info findInfo (Class clazz) {
72
        return getDefault().findInfo (clazz);
73
    }
74
     */
75
    
76
    /** Find a property editor for the given class, specifying whether a new
77
     * or shared instance should be used.  Looks first for
78
     * editors registered with the NetBeans property editor
79
     * infrastructure, and if it finds none, falls back to
80
     * java.beans.PropertyEditorManager.<P>
81
     * <strong>Do not attach listeners to instances returned when
82
     * <code>newInstance</code> is false!  Such returned property
83
     * editors' values are liable to change without notice, as
84
     * property displaying infrastructure may reconfigure them
85
     * without notice!</STRONG>
86
     * @param clazz The class an editor is sought for.
87
     * @param newInstance If true, method will return a new instance of the class in question.
88
     * @return A property editor instance.
89
     */    
90
    public abstract PropertyEditor findEditor (Class clazz, boolean newInstance);
91
     /** Find an editor for the given class. There are two use cases for
92
      * property editors - as renderers, which are used merely for
93
      * display purposes, and to actually edit properties.  <P>Looks first for
94
      * editors registered with the NetBeans property editor
95
      * infrastructure, and if it finds none, falls back to
96
      * java.beans.PropertyEditorManager.
97
      * @param clazz The class an editor is sought for
98
      * @return A new property editor instance
99
      */    
100
    public abstract PropertyEditor findEditor (Class clazz);
101
    /** Returns true if a property editor has been registered with
102
     * this implementation (if using the default NetBeans implementation,
103
     * this means if an editor has been registered using XML).  Note
104
     * that <code>findEditor()</code> may return true even if this
105
     * method returns false, in the case that an editor was registered
106
     * via <code>java.beans.PropertyEditorManager</code>.
107
     * @param clazz The class sought
108
     * @return True if an editor has been registered
109
     */    
110
    public abstract boolean hasEditor (Class clazz);
111
    /** Get an object representing the extended information that is part
112
     * of property editor registration.  This information is primarily
113
     * used by the property sheet infrastructure for performance
114
     * optimizations.
115
     * @param clazz The class an Info object is sought for
116
     * @return An Info instance containing the extended registration
117
     * information, or null if no editor was registered using
118
     * the default registration mechanism (this does not mean
119
     * that no editor was registered with <code>
120
     * java.beans.PropertyEditorManager</code>.
121
     */    
122
    public abstract Info findInfo (Class clazz);
123
    
124
    /** Extended information pertaining to a property editor. */    
125
    public interface Info {
126
        /** Get an instance of the property editor class represented by
127
         * this Info object.  If <code>newInstance</code> is false, do
128
         * <strong>not</strong> attach listeners to the result!
129
         * @param newInstance True if a new instance of this property editor is desired, rather than a shared
130
         * instance
131
         * @return The property editor instance
132
         */        
133
        public PropertyEditor get (boolean newInstance);
134
        /**Get an arbitrary keyed attribute pertaining to the property editor.
135
         * This method is here for future flexibility - there is the 
136
         * potential for optimizations if property editors register
137
         * additional information about themselves.  */
138
        public Object getAttribute (Object key);
139
    }
140
    
141
    /**A fallback default implementation in the case that no
142
     * instance of NbPropertyEditorManager is registered via
143
     * a layer.*/
144
    private static class BeansWrapper extends NbPropertyEditorManager {
145
        
146
        public PropertyEditor findEditor(Class clazz) {
147
            return PropertyEditorManager.findEditor (clazz);
148
        }
149
        
150
        public PropertyEditor findEditor(Class clazz, boolean newInstance) {
151
            return PropertyEditorManager.findEditor (clazz);
152
        }
153
        
154
        public Info findInfo(Class clazz) {
155
            return null;
156
        }
157
        
158
        public boolean hasEditor(Class clazz) {
159
            return false;
160
        }
161
        
162
    }
163
    
164
}
(-)openide/src/org/openide/nodes/Node.java (-4 / +12 lines)
Lines 23-28 Link Here
23
import java.lang.ref.WeakReference;
23
import java.lang.ref.WeakReference;
24
24
25
import java.lang.reflect.InvocationTargetException;
25
import java.lang.reflect.InvocationTargetException;
26
import java.lang.ref.SoftReference;
26
import java.util.WeakHashMap;
27
import java.util.WeakHashMap;
27
import javax.swing.Action;
28
import javax.swing.Action;
28
import javax.swing.JPopupMenu;
29
import javax.swing.JPopupMenu;
Lines 36-41 Link Here
36
import org.openide.util.LookupListener;
37
import org.openide.util.LookupListener;
37
import org.openide.util.NbBundle;
38
import org.openide.util.NbBundle;
38
import org.openide.util.actions.SystemAction;
39
import org.openide.util.actions.SystemAction;
40
import org.openide.util.NbPropertyEditorManager;
39
41
40
42
41
/** A <em>node</em> represents one element in a hierarchy of objects (beans).
43
/** A <em>node</em> represents one element in a hierarchy of objects (beans).
Lines 1057-1069 Link Here
1057
            return true;
1059
            return true;
1058
        }
1060
        }
1059
1061
1062
        SoftReference ped = null;
1060
        /** Get a property editor for this property.
1063
        /** Get a property editor for this property.
1061
        * The default implementation tries to use {@link java.beans.PropertyEditorManager}.
1064
        * The default implementation tries to use {@link java.beans.PropertyEditorManager}.
1062
        * @return the property editor, or <CODE>null</CODE> if there is no editor
1065
        * @return the property editor, or <CODE>null</CODE> if there is no editor  */
1063
        */
1064
        public PropertyEditor getPropertyEditor () {
1066
        public PropertyEditor getPropertyEditor () {
1065
            if (type == null) return null;
1067
            if (type == null) return null;
1066
            return java.beans.PropertyEditorManager.findEditor(type);
1068
            PropertyEditor result=null;
1069
            if (ped != null) {
1070
                result = (PropertyEditor) ped.get();
1071
                if (result != null) return result;
1072
            }
1073
            result = NbPropertyEditorManager.getDefault().findEditor (type);
1074
            ped = new SoftReference (result);
1075
            return result;
1067
        }
1076
        }
1068
1077
1069
        /* Standard equals implementation for all property
1078
        /* Standard equals implementation for all property
Lines 1259-1265 Link Here
1259
            if (result == null) {
1268
            if (result == null) {
1260
                result = lookup.lookup (TEMPL_COOKIE);
1269
                result = lookup.lookup (TEMPL_COOKIE);
1261
                result.addLookupListener (this);
1270
                result.addLookupListener (this);
1262
                result.allItems();
1263
            }
1271
            }
1264
        }
1272
        }
1265
        
1273
        

Return to bug 22422