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.netbeans.modules.tasklist.usertasks;

import java.awt.BorderLayout;
import java.awt.datatransfer.Transferable;
import java.io.IOException;
import java.util.Random;
import javax.swing.Action;
import javax.swing.ActionMap;
import junit.framework.Test;
import org.openide.explorer.view.TreeTableView;
import org.openide.windows.TopComponent;
import org.openide.nodes.*;
import org.openide.*;
import org.openide.explorer.*;
import org.netbeans.junit.*;
import org.openide.actions.CopyAction;
import org.openide.actions.CutAction;
import org.openide.actions.DeleteAction;
import org.openide.actions.PasteAction;
import org.openide.windows.Mode;
import org.openide.windows.WindowManager;

/**
 * Tests Ctrl+C and Ctrl+V
 */
public class UserTaskViewTest extends NbTestCase {
    public static void main(String[] params) {
        try {
            new UserTaskViewTest("test").testUserTaskList();
        } catch (Exception e) {
            ErrorManager.getDefault().notify(e);
        }
    }
    
    public UserTaskViewTest(String name) {
        super(name);
    }
    
    public static Test suite () {
        return new NbTestSuite(UserTaskViewTest.class);
    }

    public void testUserTaskList() throws Exception {
        TopComponent tc = new UserTaskViewTest.TestTopComponent();
        Mode mode = WindowManager.getDefault().findMode("output"); // NOI18N
        if (mode != null) {
            mode.dockInto(tc);
        }
        tc.open();
        tc.requestActive();
        tc.requestFocus();
        
        Thread.sleep(10000);
    }
    
    private static class TestChildren extends Children.Array {
        public TestChildren(int nchildren) {
            for (int i = 0; i < nchildren; i++) {
                this.add(new Node[] {new TestNode(0)});
            }
        }
    }
    
    private static class TestNode extends AbstractNode {
        public TestNode(int nchildren) {
            super(nchildren == 0 ? Children.LEAF : new TestChildren(nchildren));
            this.setName("node " + new Random().nextInt());
        }       
        
        public Transferable clipboardCopy () throws IOException {
            System.out.println("TestNode.clipboardCopy");
            return super.clipboardCopy();
        }

        public Transferable clipboardCut () throws IOException {
            System.out.println("TestNode.clipboardCut");
            this.destroy();
            return super.clipboardCut();
        }
        
        public Action[] getActions (boolean context) {
            return new Action[] {
                CutAction.get(CutAction.class),
                PasteAction.get(PasteAction.class),
                CopyAction.get(CopyAction.class),
                DeleteAction.get(DeleteAction.class)
            };
        }
        
        public boolean canDestroy() {
            return true;
        }        
        
        public boolean canCut() {
            return true;
        }
        
    }
    
    public static class TestTopComponent extends TopComponent implements
    ExplorerManager.Provider {
        private ExplorerManager manager;
        private TreeTableView ttv;

        /**
         * Creates a new instance
         */
        public TestTopComponent() {
            manager = new ExplorerManager();
            ActionMap map = getActionMap();
            map.put(javax.swing.text.DefaultEditorKit.copyAction, 
                ExplorerUtils.actionCopy(manager));
            map.put(javax.swing.text.DefaultEditorKit.cutAction, 
                ExplorerUtils.actionCut(manager));
            map.put(javax.swing.text.DefaultEditorKit.pasteAction, 
                ExplorerUtils.actionPaste(manager));
            map.put("delete", ExplorerUtils.actionDelete(manager, true));  // NOI18N

            // following line tells the top component which lookup should be associated with it
            associateLookup(ExplorerUtils.createLookup(manager, map));

            ttv = new TreeTableView();
            this.setLayout(new BorderLayout());
            this.add(ttv, BorderLayout.CENTER);

            manager.setRootContext(new TestNode(10));
        }
        
        public ExplorerManager getExplorerManager() {
            return manager;
        }
    }
}
... 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.