alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

package org.netbeans.modules.url;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import org.openide.awt.Actions;
import org.openide.loaders.DataNode;
import org.openide.loaders.DataObject;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.nodes.PropertySupport;
import org.openide.nodes.Sheet;
import org.openide.util.NbBundle;

/**
 * URL node representing URLDataObject.
 * It has no children, the default action opens an editor or instantiates
 * a template.
 *
 * @author  Ian Formanek
 * @author  Marian Petras
 */
final class URLNode extends DataNode {

    /** Default constructor, constructs node */
    public URLNode(final DataObject dataObject) {
        super(dataObject, Children.LEAF);
        setIconBase("org/netbeans/modules/url/urlObject");              //NOI18N

        // Trick: To have a node name localized but remain the object name 
        // the same until renamed explictly. -> due to be able show
        // node names without ampersands for shortcuts and be able to show
        // shortcuts in bookmarks menu.
        // Note: see getDisplayName method bellow.
        setName(super.getDisplayName(), false);
    }

    /**
     * Gets the display name.
     * Cuts ampersand from the original display name.
     *
     * @return  URLDataObject's display name without the first
     *          ampersand (&), if there was any
     */
    public String getDisplayName() {
        return Actions.cutAmpersand(super.getDisplayName());
    }

    /** */
    protected Sheet createSheet() {
        Sheet sheet = super.createSheet();
        Sheet.Set sheetSet = sheet.get(Sheet.PROPERTIES);

        // Name property is replaced by so we could show the localized name
        // of node instead of the non-localized name of file on the disk 
        // which could differ at the start.
        sheetSet.remove(DataObject.PROP_NAME);
        sheetSet.put(createNameProperty());
        sheetSet.put(createURLStringProperty());

        return sheet;
    }

    /** Creates a name property. */
    private Node.Property createNameProperty() {
        String displayName = NbBundle.getMessage(
                URLNode.class, "PROP_Name");                            //NOI18N
        String shortDescription = NbBundle.getMessage(
                URLNode.class, "PROP_NameShortDescription");            //NOI18N
        return new PropertySupport.ReadWrite(DataObject.PROP_NAME,
                                             String.class,
                                             displayName,
                                             shortDescription) {

            public Object getValue() {
                return URLNode.this.getName();
            }

            public void setValue (Object val) throws IllegalAccessException,
                IllegalArgumentException, InvocationTargetException {
                if (!canWrite()) {
                    throw new IllegalAccessException();
                }
                if (!(val instanceof String)) {
                    throw new IllegalArgumentException();
                }

                try {
                    getDataObject().rename((String) val);
                } catch (IOException ex) {
                    throw new InvocationTargetException (ex);
                }
            }

            public boolean canWrite () {
                return getDataObject().isRenameAllowed();
            }
        };
    }

    /**
     * Creates a property for editing a URL string.
     *
     * @return  property for URL string or null
     */
    private Node.Property createURLStringProperty() {
        String displayName = NbBundle.getMessage(
                URLNode.class, "PROP_URLDisplayName");                  //NOI18N
        String shortDescription = NbBundle.getMessage(
                URLNode.class, "PROP_URLShortDescription");             //NOI18N
        Node.Property urlStringProperty = new PropertySupport.ReadWrite(
                URLDataObject.PROP_URL,
                String.class,
                displayName,
                shortDescription) {

            public Object getValue() {
                return ((URLDataObject) getDataObject()).getURLString();
            }

            public void setValue(Object val)
                    throws IllegalAccessException,
                           IllegalArgumentException,
                           InvocationTargetException {
                if (!canWrite()) {
                    throw new IllegalAccessException();
                }
                if (!(val instanceof String)) {
                    throw new IllegalArgumentException();
                }
                ((URLDataObject) getDataObject()).setURLString((String) val);
            }
        };

        urlStringProperty.setPreferred(true);

        return urlStringProperty;
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.