【发布时间】:2013-06-27 16:48:52
【问题描述】:
我正在使用 Netbeans 平台版本 7.2 和 JDK 1.7 创建应用程序。我希望能够隐藏安装到编辑器位置的 TopComponent 元素上的选项卡。我在 Google 上四处搜索,在 Geertjan Wielenga 的博客上找到了 this post,它似乎准确地解释了该怎么做,但我似乎仍然无法让它发挥作用。
我的应用程序包含两个项目。其中一个是 Netbeans 模块,另一个是“Netbeans 平台应用程序”项目。该模块由三个类组成:Installer.java、NoTabsTabDisplayerUI.java 和StupidTopComponent.java。 Installer.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 中的任何代码。它似乎从不调用该类中的构造函数或任何其他函数。我的第一个想法是由于某种原因它没有找到课程,但我不确定如何测试是否是这种情况。
我在这里遗漏了什么明显的东西吗?
【问题讨论】: