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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.explorer.propertysheet;

import java.awt.BorderLayout;
import org.openide.*;
import org.openide.nodes.*;
import org.openide.explorer.propertysheet.*;
import org.openide.explorer.propertysheet.editors.*;
import java.beans.*;
import java.lang.reflect.*;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.JFrame;
import junit.framework.*;
import junit.textui.TestRunner;
import org.netbeans.junit.*;
import org.openide.util.Lookup;
import org.openide.explorer.propertysheet.PropertySheet;

/**
 * Ensures that the proper property editor is used for indexed properties
 */
public class IndexedPropertyTest extends ExtTestCase {
    private PropertySheet ps = null;
    public IndexedPropertyTest(String name) {
        super(name);
        super.installCorePropertyEditors();
    }
   
    protected boolean runInEQ() {
        return false;
    }    
  
    private static boolean setup = false;
/*
 * This test creates a Property, Editor and Node. First test checks if initialized
 * editor contains the same value as property. The second checks if the property
 * value is changed if the same change will be done in the editor.
 */
    protected void setUp() throws Exception {
        // Create new TEditor
        te = new TEditor();
        // Create new TNode
        tn = new TNode();
        
        //Replacing NodeOp w/ JFrame to eliminate depending on full IDE init
        //and long delay while waiting for property sheet thus requested to
        //initialize
        final JFrame jf = new JFrame();
        ps = new PropertySheet();
        jf.getContentPane().setLayout(new BorderLayout());
        jf.getContentPane().add(ps, BorderLayout.CENTER);
        jf.setLocation(30,30);
        jf.setSize(500,500);
        
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                ps.setNodes(new Node[] {tn});
            //ps.setCurrentNode(tn);
                jf.show();
            }
        });
   
        jf.show();
        new ExtTestCase.WaitWindow(jf);

        try {
            // Wait for the initialization
            for (int i = 0; i < 10; i++) {
                if (te.getAsText().equals("null")) {
                    //System.out.println("null");
                    Thread.sleep(1000);
                }
                else break;
            }
            ensurePainted(ps);
            
        } catch (Exception e) {
            fail("FAILED - Exception thrown "+e.getClass().toString());
        }
    }
    
    private void ensurePainted(final PropertySheet ps) throws Exception {
        //issues 39205 & 39206 - ensure the property sheet really repaints
        //before we get the value, or the value in the editor will not
        //have changed
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                java.awt.Graphics g = ps.getGraphics();
                ps.paintImmediately (0,0,ps.getWidth(), ps.getHeight());
            }
        });
    }
        
    public void testIndexedProperty() throws Exception {
        System.err.println("Plain Editor: " + PropUtils.getPropertyEditor(plain));
        System.err.println("Fancy Editor: " + PropUtils.getPropertyEditor(fancy));
        
        assertTrue ("Plain editor should be an IndexedPropertyEditor ", PropUtils.getPropertyEditor(plain) instanceof IndexedPropertyEditor);
        assertTrue ("Overridden editor should be used if present", PropUtils.getPropertyEditor(fancy) == te);
        
    }
 
    
    //Node definition
    public class TNode extends AbstractNode {
        //create Node
        public TNode() throws Exception {
            super (Children.LEAF);
            setName("TNode"); // or, super.setName if needed
            setDisplayName("TNode");
        }
        //clone existing Node
        public Node cloneNode() {
            try {
                return new TNode();
            } catch (Exception e) {
                throw new RuntimeException ("Failed to clone node");
            }
        }
        
        public void destroy() {
            fireNodeDestroyed();
        }
        
        // Create a property sheet:
        protected Sheet createSheet() {
            Sheet sheet = super.createSheet();
            // Make sure there is a "Properties" set:
            Sheet.Set props = sheet.get(Sheet.PROPERTIES);
            if (props == null) {
                props = Sheet.createPropertiesSet();
                sheet.put(props);
            }
            props.put(plain);
            props.put(fancy);
            return sheet;
        }
        // Method firing changes
        public void fireMethod(String s, Object o1, Object o2) {
            firePropertyChange(s,o1,o2);
        }
    }
    
    PlainIndexedProperty plain = new PlainIndexedProperty();
    FancyIndexedProperty fancy = new FancyIndexedProperty();
    
    public class PlainIndexedProperty extends Node.IndexedProperty {
        private StringBuffer value = new StringBuffer (getClass().getName());
        public PlainIndexedProperty() {
            super (char[].class, Character.TYPE);
            setDisplayName("Plain");
            setName(getDisplayName());
        }
        
        public boolean canIndexedRead() {
            return true;
        }

        public boolean canIndexedWrite() {
            return true;
        }

        public boolean canRead() {
            return true;
        }

        public boolean canWrite() {
            return true;
        }
        
        public PropertyEditor getPropertyEditor() {
            return null;
        }

        public java.lang.Object getIndexedValue(int index) throws java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException {
            return new Character(value.charAt(index));
        }

        public java.lang.Object getValue() throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException {
            return value.toString().toCharArray();
        }

        public void setIndexedValue(int indx, java.lang.Object val) throws java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException {
            String old = value.toString();
            value.setCharAt(indx, ((Character) val).charValue());
            tn.fireMethod (getName(), old, val);
        }

        public void setValue(java.lang.Object val) throws java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException {
            String old = value.toString();
            value = new StringBuffer (val == null ? "" : val.toString());
            tn.fireMethod (getName(), old, val);
        }
        
        public int hashCode() {
            return 23;
        }
        
        public boolean equals (Object o) {
            return o == this;
        }
    }
    
    public class FancyIndexedProperty extends PlainIndexedProperty {
        public FancyIndexedProperty() {
            setDisplayName("Fancy");
            setName(getDisplayName());
        }
        
        public PropertyEditor getPropertyEditor() {
            return te;
        }

        public int hashCode() {
            return 24;
        }
    }
    
    // Editor definition
    public class TEditor extends PropertyEditorSupport implements ExPropertyEditor {
        PropertyEnv env;
        
        // Create new TEditor
        public TEditor() {
        }
        
        /*
         * This method is called by the IDE to pass
         * the environment to the property editor.
         */
        public void attachEnv(PropertyEnv env) {
            this.env = env;
        }
        
        // Set that this Editor doesn't support custom Editor
        public boolean supportsCustomEditor() {
            return false;
        }
        
        public void addPropertyChangeListener(PropertyChangeListener l) {
            super.addPropertyChangeListener(l);
        }
        
        public void removePropertyChangeListener(PropertyChangeListener l) {
            super.removePropertyChangeListener(l);
        }
        
        
        
        // Set the Property value threw the Editor
        public void setValue(Object newValue) {
            super.setValue(newValue);
        }
        
        public void firePropertyChange() {
            super.firePropertyChange();
        }
    }
    
    public static void main(String args[]) {
         TestRunner.run(new NbTestSuite(IndexedPropertyTest.class));
    }
    
    private TNode tn;
    private TEditor te;
}
... 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.