jtviewertopcomponent.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 193 行

SVN-BASE
193
字号
/*
 * $Id$
 *
 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt).
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 *
 */

package com.sun.testme.jtviewer;

import com.sun.testme.JTUtils;
import com.sun.testme.jtharness.testsuite.JTharnessSuiteUtil;
import com.sun.testme.jtviewer.cookies.RefreshCookie;
import java.io.File;
import java.io.Serializable;
import javax.swing.ActionMap;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ui.OpenProjects;
import org.openide.ErrorManager;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.filesystems.FileUtil;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 * Top component which displays something.
 */
public final class JTViewerTopComponent extends TopComponent implements ExplorerManager.Provider {
    
    private transient ExplorerManager explorerManager = new ExplorerManager();
    
    private static RefreshTestAction refreshTestAction;
    private static JTViewerTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    
    private static final String PREFERRED_ID = "JTViewerTopComponent";
    
    private JTViewerTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(JTViewerTopComponent.class, "CTL_JTViewerTopComponent"));
        setToolTipText(NbBundle.getMessage(JTViewerTopComponent.class, "HINT_JTViewerTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
        
        associateLookup(ExplorerUtils.createLookup(explorerManager, getActionMap()));
        
        ActionMap actionMap = this.getActionMap();
        if (refreshTestAction == null) {
            refreshTestAction = new RefreshTestAction();
        }
        actionMap.put ("RefreshTestAction", refreshTestAction.createContextAwareInstance(Utilities.actionsGlobalContext()));
    }
    
    public void initWD() {
        OpenProjects openProjects = OpenProjects.getDefault();
        Project mainProject = openProjects.getMainProject();
        if (mainProject == null) {
            return;
        }
        File testSuiteRoot = new File(FileUtil.toFile(mainProject.getProjectDirectory()), "tests");
        
        Project project = OpenProjects.getDefault().getMainProject();
        JTharnessSuiteUtil testSuiteInfo = (JTharnessSuiteUtil)project.getLookup().lookup(JTharnessSuiteUtil.class);
        
        File workDir = testSuiteInfo.getWorkDirectory();
        JTUtils.initTestSuite(null, testSuiteRoot, workDir);
        explorerManager.setRootContext(new RootNode(new TestPackageChildren(testSuiteRoot)));
        explorerManager.getRootContext().setDisplayName("Tests Suite Root");
        updateContent();
    }
    
    private void updateContent() {
        Node [] nodes = explorerManager.getRootContext().getChildren().getNodes(true);
        for (Node node : nodes) {
            ((RefreshCookie) node.getLookup().lookup(RefreshCookie.class)).refresh();
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        treePane = new BeanTreeView();

        setLayout(new java.awt.BorderLayout());

        add(treePane, java.awt.BorderLayout.CENTER);

    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane treePane;
    // End of variables declaration//GEN-END:variables
    
    public ExplorerManager getExplorerManager() {
        return explorerManager;
    }
    
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized JTViewerTopComponent getDefault() {
        if (instance == null) {
            instance = new JTViewerTopComponent();
        }
        return instance;
    }
    
    /**
     * Obtain the JTViewerTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized JTViewerTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            ErrorManager.getDefault().log(ErrorManager.WARNING, "Cannot find JTViewer component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof JTViewerTopComponent) {
            return (JTViewerTopComponent)win;
        }
        ErrorManager.getDefault().log(ErrorManager.WARNING, "There seem to be multiple components with the '" + PREFERRED_ID + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }
    
    
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_NEVER;
    }
    
    public void componentOpened() {
        // TODO add custom code on component opening
    }
    
    public void componentClosed() {
        // TODO add custom code on component closing
    }
    
    /** replaces this in object stream */
    public Object writeReplace() {
        return new ResolvableHelper();
    }
    
    protected String preferredID() {
        return PREFERRED_ID;
    }
    
    final static class ResolvableHelper implements Serializable {
        private static final long serialVersionUID = 1L;
        public Object readResolve() {
            return JTViewerTopComponent.getDefault();
        }
    }
    
    public void open() {
        Project project = OpenProjects.getDefault().getMainProject();
        JTharnessSuiteUtil testSuiteInfo = (JTharnessSuiteUtil)project.getLookup().lookup(JTharnessSuiteUtil.class);
        if (testSuiteInfo.getWorkDirectory0() == null) {
            return;
        }
        super.open();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?