如何在Netbeans Platform

时间:2015-08-14 11:08:19

标签: java netbeans action netbeans-platform

我花了将近三天的时间尝试在netbeans plaform中对Actions进行简单的启用/禁用,虽然这样做很简单,但应该是一个常见的功能比我想象的要复杂。

在乞讨时,我试图查看生成的默认操作是否存在setEnable()方法,令我惊讶的是没有。然后我开始研究它,我发现最常用的方法是设置一个有条件启用的动作(取决于一个Cookie类),所以我想出了如何在Lookup中添加一个假类,以便它被启用和禁用,我是按照以下方式做到的。为了测试它我将以下代码添加到另一个应该启用或禁用第二个操作的操作。

private final PlottingStarted plottingStarted = new PlottingStarted(); 


@Override 
public void actionPerformed(ActionEvent e) { 
    // TODO implement action body 
    if (Lookup.getDefault().lookup(PlottingStarted.class) == null) { 
        ic.add(plottingStarted); 
    }else{ 
        ic.remove(plottingStarted); 
    }

因此PlottingStarted是我创建的假对象,其唯一目的是在查找中禁用或启用该操作。

由于某些原因,它根本没有做任何事情,因为Action总是被禁用。我尝试了很多东西,最后我放弃了。

然后我尝试了一种不同的方法,并使用了具有setEnabled()功能的AbstractActions。

要检索我基于Geertjan博客的动作,我创建了以下方法

public Action findAction(String actionName) { 
    FileObject myActionsFolder = FileUtil.getConfigFile("Actions/RealTimeViewer"); 
    if (myActionsFolder != null){ 
        FileObject[] myActionsFolderKids = myActionsFolder.getChildren(); 
        for (FileObject fileObject : myActionsFolderKids) { 
            //Probably want to make this more robust, 
            //but the point is that here we find a particular Action: 
            if (fileObject.getName().contains(actionName)) { 
                try { 
                    DataObject dob = DataObject.find(fileObject); 
                    InstanceCookie ic = dob.getLookup().lookup(InstanceCookie.class); 
                    if (ic != null) { 
                        Object instance = ic.instanceCreate(); 
                        if (instance instanceof Action) { 
                            Action a = (Action) instance; 
                            return a; 
                        } 
                    } 
                } catch (Exception e) { 
                    ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 
                    return null; 
                } 
            } 
        } 
    } 
    return null; 
}

这种方法运行良好,我能够检索动作并调用其setEnabled()方法。不幸的是,无论我为什么行动总是启用。

阅读一些文献我发现我应该在行动的注册中添加以下内容" lazy = false"最后我能够启用和禁用Action ...但是当然默认注册丢失了,我没有图标和名称。

现在我决定再次发帖,因为我无法相信它需要那么复杂,必须有一种方法可以更容易地做到这一点。我唯一需要的是具有PLAY / STOP功能,当PLAY启用时STOP被禁用,反之亦然。

2 个答案:

答案 0 :(得分:0)

我自己并没有这样做,但似乎在第5.1.2.1章和第34段中介绍了#34;复杂的启用"这本书" Netbeans初学者平台"。 https://leanpub.com/nbp4beginners

这本书不是免费的,但可以使用相应的代码示例 github上。 https://github.com/walternyland/nbp4beginners/tree/master/chapters/ch05/5.1.2.1他扩展AbstractAction会覆盖resultChanged方法并使用super.setEnabled()。

@ActionID(id = "org.carsales.evaluator.EvaluateCarAction1", category = "Car")
@ActionRegistration(displayName = "not-used", lazy = false)
public class EvaluateCarAction extends AbstractAction
implements ContextAwareAction, LookupListener {
// ...

@Override
public void resultChanged(LookupEvent le) {
    //Optionally, check if the property is set to the value you're interested in
   //prior to enabling the Action.
   super.setEnabled(result.allInstances().size() > 0);
}

答案 1 :(得分:0)

感谢大家的回复。我终于通过扩展AbstractAction来实现它,似乎即使你注册“lazy = false”,一些注册仍然由平台完成,你只需要在Action构造函数中进行一些小的调整。最终的结果是

@ActionID(
        category = "RealTimeViewer",
        id = "main.java.com.graph.actions.StopPlotting"
)
@ActionRegistration(
        //iconBase = "main/java/com/graph/images/stop-plotting-24x24.png",
        displayName = "#CTL_StopPlotting",
        lazy = false
)
@ActionReference(path = "Toolbars/RealTimeViewer", position = 600)
@Messages("CTL_StopPlotting=Stop Plotting")
public final class StopPlotting extends AbstractAction{

private static final String ICON = "main/java/com/dacsys/cna/core/graph/images/stop-plotting-24x24.png";

public StopPlotting() {
    putValue(SMALL_ICON, ImageUtilities.loadImageIcon(ICON, false));
    putValue(NAME, Bundle.CTL_StopPlotting());
    this.setEnabled(false);
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO implement action body
    Action a = new ActionsHelper().findAction("StartPlotting");
    if (a != null){
        if (a != null){
            if (a.isEnabled()){
                a.setEnabled(false);
                this.setEnabled(true);
            }else{
                a.setEnabled(true);
                this.setEnabled(false);
            }
        }
    } 
}

}

相关问题