PropertyEditorManager.findEditor() and getPropertyEditorClass() disagree?!?!

View: New views
3 Messages — Rating Filter:   Alert me  

PropertyEditorManager.findEditor() and getPropertyEditorClass() disagree?!?!

by Rhizomorph :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am trying to register a property editor for a particular class type by using the PropertyEditorManager.registerEditor(...) function, but Netbeans doesn't seem to pay attention to it and never uses the editor. I should say two things right off the bat:

        The Property Editor that I am trying to use DOES work - I know this for a fact, and I can successfully use this exact same editor class IF I specify it in the BeanInfo file.


        The call to registerEditor() in the static block IS getting executed. And when I later call PropertyEditorManager.findEditor(MyEnum.class), it returns a property editor of the correct class.


However, despite the fact that MyEnum is obviously being linked to the correct editor, Netbeans doesn't actually use that editor. Moreover, when I try to ask Netbeans what the property editor is by referring to a particular property via the descriptor, it returns NULL. Here's an example...




Code:
import java.beans.*;

import java.io.Serializable;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import toolbox.swing.editors.EnumPropertyEditor;



public class BrokenBean2 implements Serializable {



        public static enum MyEnum {

                VALUE1,

                VALUE2;

                static {

                        PropertyEditorManager.registerEditor(MyEnum.class, Editor.class);

                }

                public static class Editor extends EnumPropertyEditor<MyEnum> {

                        public Editor() {

                                super(MyEnum.class);

                        }

                }

        }



        public static final String PROP_EXAMPLE = "example";

       

        private MyEnum example;

        private PropertyChangeSupport pcs;



        public BrokenBean2() {

                pcs = new PropertyChangeSupport(this);

        }



        public MyEnum getExample() {

                return example;

        }



        public void setExample(MyEnum value) {

                MyEnum oldValue = example;

                example = value;

                pcs.firePropertyChange(PROP_EXAMPLE, oldValue, example);



                try {

                        String summary = "<html>";

                        PropertyDescriptor property = new PropertyDescriptor("example", BrokenBean2.class);

                        PropertyEditor editor1 = PropertyEditorManager.findEditor(example.getClass());

                        PropertyEditor editor2 = PropertyEditorManager.findEditor(property.getPropertyType());

                        PropertyEditor editor3 = PropertyEditorManager.findEditor(MyEnum.class);

                        Class clazz = property.getPropertyEditorClass();

                        property.setPropertyEditorClass(MyEnum.Editor.class);

                        summary = summary + "editor1: " + (editor1 == null ? "null" : editor1.getClass().getName()) + "<br>";

                        summary = summary + "editor2: " + (editor2 == null ? "null" : editor2.getClass().getName()) + "<br>";

                        summary = summary + "editor3: " + (editor3 == null ? "null" : editor3.getClass().getName()) + "<br>";

                        summary = summary + "clazz: " + (clazz == null ? "null" : clazz.getName()) + "<br>";

                        summary = summary + "</html>";

                        JOptionPane.showMessageDialog(null, new JLabel(summary));

                } catch (IntrospectionException ex) {

                }

        }



        public void addPropertyChangeListener(PropertyChangeListener listener) {

                pcs.addPropertyChangeListener(listener);

        }



        public void removePropertyChangeListener(PropertyChangeListener listener) {

                pcs.removePropertyChangeListener(listener);

        }

}







With the above bean, Netbeans uses the default Enum editor, not mine, for the property "example". However, when you change the value of "example", the popup message indicates that "example" is of type MyEnum and that the editor for MyEnum is MyEnum.Editor.... but it doesn't use it?!?!?! Are these methods deprecated and they just forgot to document that, or am I doing something wrong?



My goal is to be able to specify the editor for any class WITHIN THAT CLASS, not in multiple BeanInfo files spread throughout a library.





Re: PropertyEditorManager.findEditor() and getPropertyEditorClass() disagree?!?!

by David Strupl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

rhizomorph napsal(a):
> The call to registerEditor() in the static block IS getting executed.

And is it executed "soon enough"? I mean whether it is not executed *after* the
call to findEditor by NB code. You should find out using the debugger I guess
since my comment is just a wild guess ;-)

> And
> when I later call PropertyEditorManager.findEditor(MyEnum.class), it returns
> a property editor of the correct class.

Br,

--
David Strupl
http://dstrupl.blogspot.com

Re: PropertyEditorManager.findEditor() and getPropertyEditorClass() disagree?!?!

by Tim Boudreau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Rhizomorph wrote:
I am trying to register a property editor for a particular class type by using the PropertyEditorManager.registerEditor(...) function, but Netbeans doesn't seem to pay attention to it and never uses the editor. I should say two things right off the bat:

        The Property Editor that I am trying to use DOES work - I know this for a fact, and I can successfully use this exact same editor class IF I specify it in the BeanInfo file.


        The call to registerEditor() in the static block IS getting executed. And when I later call PropertyEditorManager.findEditor(MyEnum.class), it returns a property editor of the correct class.


However, despite the fact that MyEnum is obviously being linked to the correct editor, Netbeans doesn't actually use that editor. Moreover, when I try to ask Netbeans what the property editor is by referring to a particular property via the descriptor, it returns NULL. Here's an example...
Agree with David - try putting your registration code in a ModuleInstall, to be sure it really gets registered before anybody asks for a property sheet from a node for your bean.

I'd also suggest perhaps not using BeanNode (which I'm guessing you are);  then you have complete control over the property editors for your property objects...

-Tim