隐藏Netbeans 7.2中的TopComponent选项卡

时间:2013-06-27 16:48:53

标签: java netbeans-platform

我正在使用Netbeans平台版本7.2和JDK 1.7创建应用程序。我希望能够隐藏安装到编辑器位置的TopComponent元素上的选项卡。我已经google了一下,在Geertjan Wielenga的博客上找到this post,似乎可以解释到底该做什么,但我似乎仍然无法使其发挥作用。

我的申请包括两个项目。其中一个是Netbeans模块,另一个是“Netbeans平台应用程序”项目。该模块由三个类组成:Installer.javaNoTabsTabDisplayerUI.javaStupidTopComponent.javaInstaller.java很简单,由

组成
package org.foo;

import javax.swing.UIManager;
import org.openide.modules.ModuleInstall;

public class Installer extends ModuleInstall {

    @Override
    public void restored() {
        UIManager.put("EditorTabDisplayerUI", "org.foo.NoTabsTabDisplayerUI");
    }
}

NoTabsTabDisplayerUI.java或多或少地从Geertjan的博客中逐字逐句看起来像

package org.foo;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import javax.swing.DefaultSingleSelectionModel;
import javax.swing.JComponent;
import javax.swing.SingleSelectionModel;
import javax.swing.plaf.ComponentUI;
import org.netbeans.swing.tabcontrol.TabDisplayer;
import org.netbeans.swing.tabcontrol.TabDisplayerUI;

public class NoTabsTabDisplayerUI extends TabDisplayerUI {

    public NoTabsTabDisplayerUI(TabDisplayer displayer) {
        super(displayer);
    }

    public static ComponentUI createUI(JComponent jc) {
        assert jc instanceof TabDisplayer;
        return new NoTabsTabDisplayerUI((TabDisplayer) jc);
    }

    private static final int[] PTS = new int[] { 0, 0, 0 };
    @Override
    public Polygon getExactTabIndication(int i) {
        //Should never be called
        return new Polygon(PTS, PTS, PTS.length);
    }

    @Override
    public Polygon getInsertTabIndication(int i) {
        return new Polygon(PTS, PTS, PTS.length);
    }

    @Override
    public int tabForCoordinate(Point point) {
        return -1;
    }

    @Override
    public Rectangle getTabRect(int i, Rectangle rectangle) {
        return new Rectangle(0,0,0,0);
    }

    @Override
    protected SingleSelectionModel createSelectionModel() {
        return new DefaultSingleSelectionModel();
    }

    public java.lang.String getCommandAtPoint(Point point) {
        return null;
    }

    @Override
    public int dropIndexOfPoint(Point point) {
        return -1;
    }

    @Override
    public void registerShortcuts(javax.swing.JComponent jComponent) {
        //do nothing
    }

    @Override
    public void unregisterShortcuts(javax.swing.JComponent jComponent) {
        //do nothing
    }

    @Override
    protected void requestAttention(int i) {
        //do nothing
    }

    @Override
    protected void cancelRequestAttention(int i) {
        //do nothing
    }

    @Override
    public Dimension getPreferredSize(javax.swing.JComponent c) {
        return new Dimension(0, 0);
    }

    @Override
    public Dimension getMinimumSize(javax.swing.JComponent c) {
        return new Dimension(0, 0);
    }

    @Override
    public Dimension getMaximumSize(javax.swing.JComponent c) {
        return new Dimension(0, 0);
    }
}

StupidTopComponent.java只是一个带有标签的TopComponent。代码如下:

package org.foo;

import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
    dtd = "-//org.foo//Stupid//EN",
autostore = false)
@TopComponent.Description(
    preferredID = "StupidTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "org.foo.StupidTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
    displayName = "#CTL_StupidAction",
preferredID = "StupidTopComponent")
@Messages({
    "CTL_StupidAction=Stupid",
    "CTL_StupidTopComponent=Stupid Window",
    "HINT_StupidTopComponent=This is a Stupid window"
})
public final class StupidTopComponent extends TopComponent {

    public StupidTopComponent() {
        initComponents();
        setName(Bundle.CTL_StupidTopComponent());
        setToolTipText(Bundle.HINT_StupidTopComponent());

    }

    /**
     * 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">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(StupidTopComponent.class, "StupidTopComponent.jLabel1.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addComponent(jLabel1)
                .addContainerGap(267, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(88, 88, 88)
                .addComponent(jLabel1)
                .addContainerGap(198, Short.MAX_VALUE))
        );
    }// </editor-fold>

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}

平台应用程序项目只包含此模块。当我运行它时,它仍然显示TopComponent上的选项卡。

在调试器中运行它,我从未看到它触及NoTabsTabDisplayerUI.java中的任何代码。它似乎永远不会调用该类中的构造函数或任何其他函数。我的第一个想法是,由于某些原因它没有找到班级,但我不确定如何测试是否是这种情况。

我有什么明显的遗漏吗?

1 个答案:

答案 0 :(得分:1)

你需要在Runnable中将调用包装到UIManager.put()并在EDT上运行它:

SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        UIManager.put("EditorTabDisplayerUI", "org.foo.NoTabsTabDisplayerUI");
    }
});

你不需要在Geertjan用于该博客文章的NB版本中这样做。