[platform-dev] Re: Problem with Global Selection Lookup

  • From: Frédéric Deniger < >
  • To:
  • Subject: [platform-dev] Re: Problem with Global Selection Lookup
  • Date: Tue, 30 Oct 2012 13:56:20 +0100

The hard reference is realised by the field result of the class OutputTopComponent.
To my mind, your code should work: if you select/unselect a node in the module 1, the method resultChanged of the module 2 should be called.

Fred
2012/10/29 kosmos890 < " target="_blank"> >
@markiewb
Thanks for your reply.

I publish the content of the node.

My code:

Module1 (explorer )

Code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.explorer;

import java.io.File;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
    dtd = "-//org.explorer//Explorer//EN",
autostore = false)
@TopComponent.Description(
    preferredID = "ExplorerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = true)
@ActionID(category = "Window", id = "org.explorer.ExplorerTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
    displayName = "#CTL_ExplorerAction",
preferredID = "ExplorerTopComponent")
@Messages({
   "CTL_ExplorerAction=Explorer",
   "CTL_ExplorerTopComponent=Explorer Window",
   "HINT_ExplorerTopComponent=This is a Explorer window"
})
public final class ExplorerTopComponent extends TopComponent implements ExplorerManager.Provider{

   private final ExplorerManager mgr=new ExplorerManager();

   public ExplorerTopComponent() {
      initComponents();
      setName(Bundle.CTL_ExplorerTopComponent());
      setToolTipText(Bundle.HINT_ExplorerTopComponent());

      Document doc = null;
      File input = new File("/home/km/JSoupTest.html");
      try {
         doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
      } catch (IOException ex) {
      }
      Element body = doc.select("body").first();

      associateLookup (ExplorerUtils.createLookup(mgr, getActionMap()));
      mgr.setRootContext(new AbstractNode(Children.create(new ElementChildFactory(body), true)));

   }

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

      jScrollPane1 = new BeanTreeView();

      setLayout(new java.awt.BorderLayout());
      add(jScrollPane1, java.awt.BorderLayout.CENTER);
   }// </editor-fold>

   // Variables declaration - do not modify
   private javax.swing.JScrollPane jScrollPane1;
   // 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
   }

   @Override
   public ExplorerManager getExplorerManager() {
      return mgr;
   }
}





Module2 (viewer)

Code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.output;

import org.jsoup.nodes.Element;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;

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

    private Lookup.Result<Element> result = null;

    public OutputTopComponent() {
        initComponents();
        setName(Bundle.CTL_OutputTopComponent());
        setToolTipText(Bundle.HINT_OutputTopComponent());

    }

    /**
     * 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() {
        // TODO add custom code on component opening
        result = Utilities.actionsGlobalContext().lookupResult(Element.class);
        result.addLookupListener(this);
    }

    @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
    }

    @Override
    public void resultChanged(LookupEvent ev) {
        System.out.println("OUTPUT TOP COMPONENT : A new Element is added");
    }
}





From here   http://wiki.netbeans.org/DevFaqTrackGlobalSelection I read this note

>
> Please note: Generally, keep a hard reference on the Lookup.Result (or make a closure on it with some final keyword and a reference from the anonymous listener). Because if you don't -- the garbage collector might kick in quite soon and your listener won't be called
>


Probably this is the cause of my problem.

Unfortunately, I am novice and I dont know how to keep a hard reference on the Lookup.Result







[platform-dev] Problem with Global Selection Lookup

kosmos890 10/29/2012

[platform-dev] Re: Problem with Global Selection Lookup

Benno Markiewicz 10/29/2012

[platform-dev] Problem with Global Selection Lookup

kosmos890 10/29/2012

[platform-dev] Re: Problem with Global Selection Lookup

Frédéric Deniger 10/30/2012

[platform-dev] Problem with Global Selection Lookup

kosmos890 10/30/2012

[platform-dev] Re: Problem with Global Selection Lookup

Frédéric Deniger 10/31/2012

[platform-dev] Problem with Global Selection Lookup

kosmos890 10/31/2012

Project Features

Project Links

About this Project

Platform was started in November 2009, is owned by Antonin Nebuzelsky, and has 138 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close