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

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.xml.tree.editor;

import java.io.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.border.*;

import org.openide.explorer.*;
import org.openide.nodes.Node;
import org.netbeans.modules.xml.tree.nodes.ElementNode;

/** Explorer view displaying a customizer for selected node (if the node provides one) or textual label telling
 * that there is no customizer available or that there are no/multiple components selected.
 *
 * @author Ian Formanek, Libor Kramolis
 */
public class CustomizerView extends JPanel implements Externalizable {
    
    /** generated Serialized Version UID */
    private static final long serialVersionUID = -2922109093973271414L;
    
    /** The current selectedNodes */
    transient private Node[] selectedNodes;
    /** The local reference to the explorerManager. It is transient
     * because it will be reset in initializeManager() after deserialization.*/
    transient private ExplorerManager explorerManager;
    /** Listens on ExplorerManager. */
    transient private PropertyIL iListener;
    
    /** Currently displayed component. */
    transient private java.awt.Component currentComponent;
    /** Label for notification in case when the customizer cannot be displayed. */
    transient private JLabel textLabel;
    
    
    // init .................................................................................
    
    /** Default constructor. */
    public CustomizerView() {
        super ();
        setLayout(new java.awt.BorderLayout());
        textLabel = new JLabel(Util.THIS.getString("PROP_viewNoNode"), SwingConstants.CENTER);
        currentComponent = textLabel;
        selectedNodes = new Node[0];
        iListener = new PropertyIL();
        updateSelection();
    }
    
    /*
     * Write view's state to output stream.
     */
    public void writeExternal(ObjectOutput out) throws IOException {
    }
    
    /*
     * Reads view's state form output stream.
     */
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    }
    
    
    // main methods .........................................................................
    
    /* Initializes view.
     */
    public void addNotify() {
        super.addNotify();
        explorerManager = ExplorerManager.find(this);
        explorerManager.addPropertyChangeListener(iListener);
        
        //show initial customizer
        selectedNodes = explorerManager.getSelectedNodes();
        updateSelection();
    }
    
    /* Deinitializes view.
     */
    public void removeNotify() {
        super.removeNotify();
        explorerManager = ExplorerManager.find(this);
        explorerManager.removePropertyChangeListener(iListener);
        explorerManager = null;
    }
    
    private void updateSelection() {
        remove(currentComponent);
        if (selectedNodes.length == 0) {
            textLabel.setText(Util.THIS.getString("PROP_viewNoNode"));
            textLabel.setName(Util.THIS.getString("PROP_viewInfo"));
            currentComponent = textLabel;
        } else if (selectedNodes.length > 1) {
            textLabel.setText(Util.THIS.getString("PROP_viewMultiple"));
            textLabel.setName(Util.THIS.getString("PROP_viewInfo"));
            currentComponent = textLabel;
        } else {
            selectedNodes[0].addPropertyChangeListener(iListener);
            java.awt.Component customizer = selectedNodes[0].getCustomizer();
            if (customizer == null) {
                textLabel.setText(Util.THIS.getString("PROP_viewNoCustomizer"));
                textLabel.setName(Util.THIS.getString("PROP_viewInfo"));
                currentComponent = textLabel;
            } else {
                // add bottom margin 
                JPanel customizerPanel = new JPanel();
                customizerPanel.setLayout(new java.awt.GridBagLayout());
                java.awt.GridBagConstraints gbc = new java.awt.GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
                gbc.gridheight = java.awt.GridBagConstraints.REMAINDER;
                gbc.fill = java.awt.GridBagConstraints.BOTH;
                gbc.insets = new java.awt.Insets(0, 0, 11, 0);
                gbc.weightx = 1.0;               
                gbc.weighty = 1.0;
                gbc.anchor = java.awt.GridBagConstraints.NORTHWEST;
                customizerPanel.add(customizer, gbc);
                
                currentComponent = new JScrollPane(customizerPanel);
            }
        }
        
        add(currentComponent, java.awt.BorderLayout.CENTER);
        validate();
        repaint();
    }
    
    // innerclasses .........................................................................
    
    /* The inner adaptor class for listening to the ExplorerManager's property changes. */
    class PropertyIL implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent evt) {
            if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
                if (selectedNodes.equals(evt.getNewValue())) {
                    return;
                }
                if( selectedNodes.length == 1 )
                { 
                    selectedNodes[0].removePropertyChangeListener(this);
                } 

                selectedNodes = (Node[])evt.getNewValue();
                updateSelection();
            }
            else if( ElementNode.PROP_CUSTOMIZER.equals(evt.getPropertyName()) )
            { 
                // the tag name has changed, and the customizer needs to be redrawn
                updateSelection();
            } 
        }
    }
    
}
... 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.