BookNBPlatformCookbookCH0208

Contents

NetBeans Platform Cookbook Chapter 02 08

Properties

Properties window is standardized way to display properties and values of any object in the synoptical table and enable edit them. Properties can be arranged into groups and you can provide a custom editor for wished property. You know it from GUI editors where properties of GUI components can be changed.

Preparation

The Properties Window listens changes of action global context lookup (Lookup where currently selected/active objects are listed - see Lookup chapter 3.). It looks up selected node and asks it for a Sheet by getSheet() method. If it is not null properties are listed in the Properties window.

We will illustrate it by example. We show properties of the object Book.

How to

To provide property sheet of the BookNode override the createSheet() method. First create a Sheet then Sheet.Set for each group of properties and put properties description into each.

protected Sheet createSheet() {
    Sheet sheet = new Sheet();

    Sheet.Set s1 = new Sheet.Set();
    s1.setName("Basic");
    s1.setDisplayName( NbBundle.getMessage(
                       BookNode.class, "PROP_SET.BASIC") );

    Sheet.Set s2 = new Sheet.Set();
    s2.setName("Publish");
    s2.setDisplayName( NbBundle.getMessage(
                       BookNode.class, "PROP_SET.PUBLISH") );

    Sheet.Set s3 = new Sheet.Set();
    s3.setName("Other");
    s3.setDisplayName( NbBundle.getMessage(
                       BookNode.class, "PROP_SET.OTHER") );
    s3.setValue("tabName", NbBundle.getMessage(
                       BookNode.class, "PROP_SET.OTHER") );

    sheet.put(s1);
    sheet.put(s2);
    sheet.put(s3);

    Book book = getLookup().lookup(Book.class);
    try {

        PropertySupport.Reflection<String> propTitle =
               new PropertySupport.Reflection<String>(
                    book, String.class, Book.PROP_TITLE);
        propTitle.setDisplayName(NbBundle.getMessage(
                        BookNode.class, "TITLE"));
        propTitle.setName("title");  // do not forget

        PropertySupport.Reflection<String> propAuthor =
                new PropertySupport.Reflection<String>(
                      book, String.class, Book.PROP_AUTHOR);
        propAuthor.setDisplayName(NbBundle.getMessage(
                            BookNode.class, "AUTHOR"));
        propAuthor.setName("author");

        PropertySupport.Reflection<String> propPublisher =
                new PropertySupport.Reflection<String>(
                    book, String.class, Book.PROP_PUBLISHER);
        propPublisher.setDisplayName(NbBundle.getMessage(
                            BookNode.class, "PUBLISHER"));
        propPublisher.setName("publisher");

        PropertySupport.Reflection<String> propIsbn =
                new PropertySupport.Reflection<String>(
                      book, String.class, Book.PROP_ISBN);
        propIsbn.setDisplayName(NbBundle.getMessage(
                            BookNode.class, "ISBN"));
        propIsbn.setPropertyEditorClass( 
                            IsbnPropertyEditor.class );
        propIsbn.setName("isbn");

        PropertySupport.Reflection<Integer> propYear =
                new PropertySupport.Reflection<Integer>(
                            book, int.class, Book.PROP_YEAR);
        propYear.setDisplayName(NbBundle.getMessage(
                            BookNode.class, "YEAR"));
        propYear.setName("year");

        PropertySupport.Reflection<String> propDescription =
                new PropertySupport.Reflection<String>(
                  book, String.class, Book.PROP_DESCRIPTION);
        propDescription.setDisplayName(NbBundle.getMessage(
                         BookNode.class, "DESCRIPTION"));
        propDescription.setName("description");

        s1.put( propTitle );
        s1.put( propAuthor );
        s2.put( propPublisher );
        s2.put( propIsbn );
        s2.put( propYear );
        s3.put( propDescription );
    }
    catch (NoSuchMethodException ex) {
        Exceptions.printStackTrace(ex);
    }
    return sheet;
}    

For creating property description use org.openide.nodes.PropertySupport.Reflection class from Nodes API. Minimally provide the instance, class type and name of the property to constructor parameters if set and get method are by JavaBeans standard. If you want forbid editing pass null to setter method. Set localized display name and name of the property - name given into the constructor is for creating get/set methods and property changes listening.

The isbn property has own IsbnPropertyEditor. Implement the InplaceEditor.Factory method and create your inplace editor. Next implement ExPropertyEditor interface with attachEnv(PropertyEnv env) method. It gets PropertyEnv instance. You register your inplace editor by it.


env.registerInplaceEditorFactory(this);

image:nbpcook_02_14_properties.png

Figure 2.14 Properties of the selected Book.


Notes/Tips

Text and sources were created under NB 6.8, 6.9, 7.0, 7.1, 7.2.

Navigation

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo