Re: lookup confusing me

arsi <[email protected]> Thu, 14 Sep 2017 23:59:04 +0200
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------000902030807000208030108
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit


Hi,

Test this:

|import  java.beans.PropertyChangeEvent;
import  java.beans.PropertyChangeListener;
import  java.util.Map;
import  java.util.WeakHashMap;
import  javax.swing.text.Document;
import  javax.swing.text.JTextComponent;
import  org.netbeans.api.editor.EditorRegistry;
import  org.netbeans.modules.editor.NbEditorUtilities;
import  org.openide.loaders.DataObject;
import  org.openide.modules.OnStart;

/**
  *
  *  @author  arsi
  */
@OnStart
public  class  Demo  implements  Runnable  {

     public  static  final  Map<DataObject, Object> opened =new  WeakHashMap<>();

     @Override
     public  void  run()  {
         PropertyChangeListener l =new  PropertyChangeListener() {
             @Override
             public  void  propertyChange(PropertyChangeEvent evt)  {
                 if  (evt.getPropertyName().equals(EditorRegistry.FOCUS_GAINED_PROPERTY) || evt.getPropertyName().equals(EditorRegistry.FOCUSED_DOCUMENT_PROPERTY)) {
                     JTextComponent jtc = (JTextComponent) evt.getNewValue();
                     if  (jtc !=null) {
                         Document d = jtc.getDocument();
                         DataObject dobj = NbEditorUtilities.getDataObject(d);
                         if  (dobj !=null  && !opened.containsKey(dobj)) {
                             opened.put(dobj,null);
                         }
                     }
                 }else  if  (evt.getPropertyName().equals(EditorRegistry.COMPONENT_REMOVED_PROPERTY)){
                     JTextComponent jtc = (JTextComponent) evt.getOldValue();
                     if  (jtc !=null) {
                         Document d = jtc.getDocument();
                         DataObject dobj = NbEditorUtilities.getDataObject(d);
                         opened.remove(dobj);
                     }
                 }
             }
         };

         EditorRegistry.addPropertyChangeListener(l);
     }

}
|

ArSi

------------------------------------------------------------------------

*From:* Peter Cheung
*Sent:* Thursday, September 14, 2017 6:39PM
*To:* Dev
*Subject:* [platform-dev] lookup confusing me


> Hi
>    I have trouble with lookup, the below code only print out the 
> current file instead of all opened files.
> Lookup lookup = Utilities.actionsGlobalContext();
>
> Collection<? extends DataObject> list = 
> lookup.lookupAll(DataObject.class);
> for (DataObject dataObject : list) {
> ModuleLib.log(dataObject);
> }
>
> If i use "Lookup.getDefault().lookupAll", looupAll() even returns 
> nothing. Please help
>
> Thanks
> FromPeter
>







--------------000902030807000208030108
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body smarttemplateinserted="true" bgcolor="#FFFFFF" text="#000000">
    <div id="smartTemplate4-quoteHeader"><br>
      Hi,<br>
      <br>
      Test this:<br>
      <br>
      <div style="" class="markdown-here-wrapper"
        data-md-url="Thunderbird">
        <pre style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;font-size: 1em; line-height: 1.2em;margin: 1.2em 0px;"><code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px 3px 3px 3px; display: inline;white-space: pre; overflow: auto; border-radius: 3px 3px 3px 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em; display: block ! important;display: block; overflow-x: auto; padding: 0.5em; color: rgb(0, 0, 0); background: none repeat scroll 0% 0% rgb(255, 255, 255);" class="hljs language-java"><span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keywor
 d">import</span> java.beans.PropertyChangeEvent;
<span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">import</span> java.beans.PropertyChangeListener;
<span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">import</span> java.util.Map;
<span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">import</span> java.util.WeakHashMap;
<span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">import</span> javax.swing.text.Document;
<span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">import</span> javax.swing.text.JTextComponent;
<span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">import</span> org.netbeans.api.editor.EditorRegistry;
<span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">import</span> org.netbeans.modules.editor.NbEditorUtilities;
<span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">import</span> org.openide.loaders.DataObject;
<span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">import</span> org.openide.modules.OnStart;

<span style="color: rgb(128, 128, 128); font-style: italic;" class="hljs-javadoc">/**
 *
 *<span style="text-decoration: underline;" class="hljs-javadoctag"> @author</span> arsi
 */</span>
<span style="color: rgb(128, 128, 0);" class="hljs-annotation">@OnStart</span>
<span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">public</span> <span class="hljs-class"><span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">class</span> <span style="font-weight: normal; color: rgb(0, 0, 0);" class="hljs-title">Demo</span> <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">implements</span> <span style="font-weight: normal; color: rgb(0, 0, 0);" class="hljs-title">Runnable</span> </span>{

    <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">public</span> <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">static</span> <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">final</span> Map&lt;DataObject, Object&gt; opened = <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">new</span> WeakHashMap&lt;&gt;();

    <span style="color: rgb(128, 128, 0);" class="hljs-annotation">@Override</span>
    <span class="hljs-function"><span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">public</span> <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">void</span> <span style="font-weight: normal; color: rgb(0, 0, 0);" class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
        PropertyChangeListener l = <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">new</span> PropertyChangeListener() {
            <span style="color: rgb(128, 128, 0);" class="hljs-annotation">@Override</span>
            <span class="hljs-function"><span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">public</span> <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">void</span> <span style="font-weight: normal; color: rgb(0, 0, 0);" class="hljs-title">propertyChange</span><span class="hljs-params">(PropertyChangeEvent evt)</span> </span>{
                <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">if</span> (evt.getPropertyName().equals(EditorRegistry.FOCUS_GAINED_PROPERTY) || evt.getPropertyName().equals(EditorRegistry.FOCUSED_DOCUMENT_PROPERTY)) {
                    JTextComponent jtc = (JTextComponent) evt.getNewValue();
                    <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">if</span> (jtc != <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">null</span>) {
                        Document d = jtc.getDocument();
                        DataObject dobj = NbEditorUtilities.getDataObject(d);
                        <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">if</span> (dobj != <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">null</span> &amp;&amp; !opened.containsKey(dobj)) {
                            opened.put(dobj, <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">null</span>);
                        }
                    }
                } <span class="hljs-function"><span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">else</span> <span style="font-weight: normal; color: rgb(0, 0, 0);" class="hljs-title">if</span> <span class="hljs-params">(evt.getPropertyName()</span>.<span style="font-weight: normal; color: rgb(0, 0, 0);" class="hljs-title">equals</span><span class="hljs-params">(EditorRegistry.COMPONENT_REMOVED_PROPERTY)</span>) </span>{
                    JTextComponent jtc = (JTextComponent) evt.getOldValue();
                    <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">if</span> (jtc != <span style="font-weight: bold; color: rgb(0, 0, 128);" class="hljs-keyword">null</span>) {
                        Document d = jtc.getDocument();
                        DataObject dobj = NbEditorUtilities.getDataObject(d);
                        opened.remove(dobj);
                    }
                }
            }
        };

        EditorRegistry.addPropertyChangeListener(l);
    }

}
</code></pre>
        <div
title="MDH:YGBgamF2YTxicj5pbXBvcnQgamF2YS5iZWFucy5Qcm9wZXJ0eUNoYW5nZUV2ZW50Ozxicj5pbXBvcnQgamF2YS5iZWFucy5Qcm9wZXJ0eUNoYW5nZUxpc3RlbmVyOzxicj5pbXBvcnQgamF2YS51dGls
Lk1hcDs8YnI+aW1wb3J0IGphdmEudXRpbC5XZWFrSGFzaE1hcDs8YnI+aW1wb3J0IGphdmF4LnN3
aW5nLnRleHQuRG9jdW1lbnQ7PGJyPmltcG9ydCBqYXZheC5zd2luZy50ZXh0LkpUZXh0Q29tcG9u
ZW50Ozxicj5pbXBvcnQgb3JnLm5ldGJlYW5zLmFwaS5lZGl0b3IuRWRpdG9yUmVnaXN0cnk7PGJy
PmltcG9ydCBvcmcubmV0YmVhbnMubW9kdWxlcy5lZGl0b3IuTmJFZGl0b3JVdGlsaXRpZXM7PGJy
PmltcG9ydCBvcmcub3BlbmlkZS5sb2FkZXJzLkRhdGFPYmplY3Q7PGJyPmltcG9ydCBvcmcub3Bl
bmlkZS5tb2R1bGVzLk9uU3RhcnQ7PGJyPjxicj4vKio8YnI+wqAqPGJyPsKgKiBAYXV0aG9yIGFy
c2k8YnI+wqAqLzxicj5AT25TdGFydDxicj5wdWJsaWMgY2xhc3MgRGVtbyBpbXBsZW1lbnRzIFJ1
bm5hYmxlIHs8YnI+PGJyPsKgwqDCoCBwdWJsaWMgc3RhdGljIGZpbmFsIE1hcCZsdDtEYXRhT2Jq
ZWN0LCBPYmplY3QmZ3Q7IG9wZW5lZCA9IG5ldyBXZWFrSGFzaE1hcCZsdDsmZ3Q7KCk7PGJyPjxi
cj7CoMKgwqAgQE92ZXJyaWRlPG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gwqDCoMKgIG9wZW5lZC5yZW1vdmUoZG9iaik7PGJyPsKg
wqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgIH08YnI+wqDCoMKgwqDCoMKgwqDC
oMKgwqDCoMKgwqDCoMKgIH08YnI+wqDCoMKgwqDCoMKgwqDCoMKgwqDCoCB9PGJyPsKgwqDCoMKg
wqDCoMKgIH07PGJyPjxicj7CoMKgwqDCoMKgwqDCoCBFZGl0b3JSZWdpc3RyeS5hZGRQcm9wZXJ0
eUNoYW5nZUxpc3RlbmVyKGwpOzxicj7CoMKgwqAgfTxicj48YnI+fTxicj5gYGA="
style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0;">&#8203;</div>
      </div>
      ArSi<br>
      <br>
      <hr><br>
      <b>From:</b> Peter Cheung<br>
      <b>Sent:</b> Thursday, September 14, 2017 6:39PM<br>
      <b>To:</b> Dev<br>
      <b>Subject:</b> [platform-dev] lookup confusing me<br>
      <br>
      <br>
    </div>
    <blockquote
cite="mid:MEXPR01MB0261C6F00C7675D86631D781B56F0@MEXPR01MB0261.ausprd01.prod.outlook.com"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=ISO-8859-1">
      <style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
      <div style="font-family: Calibri, Helvetica, sans-serif;
        font-size: 12pt; color: rgb(0, 0, 0);">
        <div>Hi</div>
        <div>&nbsp; &nbsp;I have trouble with lookup, the below code only print
          out the current file instead of all opened files.</div>
        <div><span class="Apple-tab-span" style="white-space:pre">Lookup
            lookup = Utilities.actionsGlobalContext();</span></div>
        <div><br>
        </div>
        <div>Collection&lt;? extends DataObject&gt; list =
          lookup.lookupAll(DataObject.class);</div>
        <div><span class="Apple-tab-span" style="white-space:pre"></span>for
          (DataObject dataObject : list) {</div>
        <div><span class="Apple-tab-span" style="white-space:pre"></span>ModuleLib.log(dataObject);</div>
        <div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
        <div><br>
        </div>
        <div>If i use "<span style="color: rgb(0, 0, 0); font-family:
            Calibri, Helvetica, sans-serif; font-size: 16px; font-style:
            normal; font-variant-caps: normal;">Lookup.getDefault().lookupAll",
            looupAll() even returns nothing. Please help</span></div>
        <div><span style="color: rgb(0, 0, 0); font-family: Calibri,
            Helvetica, sans-serif; font-size: 16px; font-style: normal;
            font-variant-caps: normal;"><br>
          </span></div>
        <div><span style="color: rgb(0, 0, 0); font-family: Calibri,
            Helvetica, sans-serif; font-size: 16px; font-style: normal;
            font-variant-caps: normal;">Thanks</span></div>
        <div>FromPeter</div>
        <br>
      </div>
    </blockquote>
    <br>
    <div id="smartTemplate4-template">
      <p><br>
        <br>
        <br>
        <br>
        <br>
        &nbsp;</p>
    </div>
  </body>
</html>

--------------000902030807000208030108--