Domanda

I have a netbeans RCP application that shows a set of nodes in the explorertopcomponent. When selected, I display details on the editortopcomponent and it works well. When I show a dialog using JOptionPage on the editor, the selected node in the tree is deselected, and eventually my editortopcomponent also loses the selected node details. Is there a way to save the selected node in the tree from being deselected if a dialog opens?

Thanks.

È stato utile?

Soluzione

it is simple.

In your explorertopcomponent you have LookupListner, that "is waiting" on event "someYourNodeClass" (for example Album)appears in the lookup. You must removeLookupListener, when your explorertopcomponent is not visible or yust do nothing.

/**
 * your explorertopcomponent
 */
@ConvertAsProperties(
    dtd = "-//com.galileo.netbeans.module//Y//EN",
    autostore = false)
@TopComponent.Description(
    preferredID = "YTopComponent",
    //iconBase="SET/PATH/TO/ICON/HERE", 
    persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = false)
@ActionID(category = "Window", id = "com.galileo.netbeans.module.YTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
    displayName = "#CTL_YAction",
    preferredID = "YTopComponent")
@Messages({
"CTL_YAction=Y",
"CTL_YTopComponent=Y Window",
"HINT_YTopComponent=This is a Y window"
})
public final class YTopComponent extends TopComponent implements LookupListener {

private Lookup.Result<Album> result;

public YTopComponent() {
    initComponents();
    setName(Bundle.CTL_YTopComponent());
    setToolTipText(Bundle.HINT_YTopComponent());

}

/**
 * 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() {

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );
}// </editor-fold>                        

// Variables declaration - do not modify                     
// End of variables declaration                   
@Override
public void componentOpened() {
    result = Utilities.actionsGlobalContext().lookupResult(Album.class);
    result.addLookupListener(this);
}

@Override
public void componentClosed() {
    result.removeLookupListener(this);
}

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
}

public void resultChanged(LookupEvent le) {
    Collection<? extends Album> allInstances = result.allInstances();
    TopComponent findTopComponent = WindowManager.getDefault().findTopComponent("YourNodeExplorerWindow");
    if (findTopComponent == null) {
        return;
    }
    if (!findTopComponent.isShowing()) {
        return;
    }
    if (!allInstances.isEmpty()) {
        showDetail(allInstances.iterator().next());
    }
}
}

Jirka

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top