Re: lookup confusing me

Peter Cheung <[email protected]> Sat, 16 Sep 2017 01:24:26 +0000
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <ME1PR01MB0260CBD021513ADC9C8F3846B56D0@ME1PR01MB0260.ausprd01.prod.outlook.com>
--_000_ME1PR01MB0260CBD021513ADC9C8F3846B56D0ME1PR01MB0260ausp_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Dear Ingleby
   Thank you for your help, from you link I finally can get all current edi=
torpanes and the dataobjects from it, below are the code and saved it to my=
 blog, thanks

Set<TopComponent> comps =3D TopComponent.getRegistry().getOpened();
for (TopComponent tc : comps) {
ModuleLib.log("tc=3D" + tc);
Node[] arr =3D tc.getActivatedNodes();
if (arr !=3D null) {
for (int j =3D 0; j < arr.length; j++) {
DataObject dataObject =3D (DataObject) arr[j].getCookie(DataObject.class);
ModuleLib.log("dataObject=3D" + dataObject);
EditorCookie ec =3D (EditorCookie) arr[j].getCookie(EditorCookie.class);
if (ec !=3D null) {
JEditorPane[] editorPane =3D ec.getOpenedPanes();
if (editorPane !=3D null) {
ModuleLib.log("editorPane=3D" + editorPane);
}
}
}
}
}

________________________________
From: Ingleby, Graeme <[email protected]>
Sent: Friday, September 15, 2017 1:39 PM
To: [email protected]
Subject: [platform-dev] Re: lookup confusing me

It's not clear exactly what you are trying to achieve when you say "all ope=
ned files".

As others have said, you don't seem to understand what NetBeans Lookups are=
 or what they are used for so I would recommend reading up on Lookup.

If you understood Lookup, you would know that the global Lookup does not al=
low you to find all instances of a particular class type within your applic=
ation.

It's not really clear if you are trying to find all DataObject instances or=
 if you are trying to find opened files that are associated with "editor" T=
op Components.

Neil provided a link to a Wiki page http://wiki.netbeans.org/DevFaqGetOpenE=
ditorWindows that shows you how you can determine which editors are open fr=
om which you can get the associated DataObject.  If you are trying to find =
editor files - this is all you need.

That however would not find other DataObjects that were opened using the Fi=
leSystem/DataSystems API's directly e.g.

If you called DataObject dobj =3D DataObject.find(FileUtil.toFileObject(new=
 File("/path/to/somefile"));

When a DataObject instance is created it is registered in a DataObjectPool =
unfortunately this pool is not public.  You could however get the DataObjec=
ts registered in the pool using reflection.  Using a dirty hack like this r=
eally isn't recommended and you really need to understand what you are doin=
g.

This approach would return ALL DataObject instances and not just DataObject=
 instances that you opened (the platform opens many configuration files, ot=
her plugins could create DataObjects).  Determining which DataObjects you a=
re truly interested in would be another problem for you to solve. Manipulat=
ing objects you did not create/own could have serious consequences!

For demonstration purposes only:

            Field poolField =3D Class.forName("org.openide.loaders.DataObje=
ctPool").getDeclaredField("POOL");
            poolField.setAccessible(true);
            Object pool =3D poolField.get(null);
            Field mapField =3D pool.getClass().getDeclaredField("map");
            mapField.setAccessible(true);
            Object map =3D mapField.get(pool);
            Map<Object, Object> refMap =3D (Map<Object,Object>)map;
            for(Object o: refMap.values()) {
                Field objField =3D o.getClass().getDeclaredField("obj");
                objField.setAccessible(true);
                Reference<DataObject> result =3D (Reference<DataObject>)obj=
Field.get(o);
                DataObject dataObject =3D (DataObject)result.get();
                System.out.println(dataObject); // Here's the DataObject yo=
u want
            }

The above code would print all DataObject instances - you could return them=
 in a list, filter objects of interest etc.

Do you intend your modules to be used within the IDE or a custom NB Platfor=
m application where you might be able to customize your harness?  If the la=
ter you could provide the functionality you need without using reflection b=
y accessing DataObjectPool and providing a public method to return the info=
rmation you need.

Hopefully you are only interested in Editor files and Neil has already prov=
ided the perfect solution for that.

Using non-public API's is a really bad idea, but sometimes the only way to =
achieve what you want (I've seen reflection used within NB sources).

Just make sure you fully understand the ramifications.


________________________________
From: Peter Cheung [[email protected]]
Sent: Thursday, September 14, 2017 12:39 PM
To: [email protected]
Subject: [platform-dev] lookup confusing me

Hi
   I have trouble with lookup, the below code only print out the current fi=
le instead of all opened files.
Lookup lookup =3D Utilities.actionsGlobalContext();

Collection<? extends DataObject> list =3D 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


--_000_ME1PR01MB0260CBD021513ADC9C8F3846B56D0ME1PR01MB0260ausp_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html dir=3D"ltr">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"">=0A=
<!--=0A=
p=0A=
	{margin-top:0;=0A=
	margin-bottom:0}=0A=
-->=0A=
</style><style type=3D"text/css" id=3D"owaParaStyle"></style>
</head>
<body dir=3D"ltr" fpstyle=3D"1" ocsi=3D"0">
<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=
n-bottom:0;} --></style>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
Dear Ingleby</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp;Thank you for your help, from you link&nbsp;I finally can get =
all current editorpanes and the dataobjects from it, below are the code and=
 saved it to my blog, thanks</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>Set&lt=
;TopComponent&gt; comps =3D TopComponent.getRegistry().getOpened();</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>for (T=
opComponent tc : comps) {</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>Module=
Lib.log(&quot;tc=3D&quot; &#43; tc);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>Node[]=
 arr =3D tc.getActivatedNodes();</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>if (ar=
r !=3D null) {</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>for (i=
nt j =3D 0; j &lt; arr.length; j&#43;&#43;) {</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>DataOb=
ject dataObject =3D (DataObject) arr[j].getCookie(DataObject.class);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>Module=
Lib.log(&quot;dataObject=3D&quot; &#43; dataObject);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>Editor=
Cookie ec =3D (EditorCookie) arr[j].getCookie(EditorCookie.class);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>if (ec=
 !=3D null) {</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>JEdito=
rPane[] editorPane =3D ec.getOpenedPanes();</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>if (ed=
itorPane !=3D null) {</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>Module=
Lib.log(&quot;editorPane=3D&quot; &#43; editorPane);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>}</div=
>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>}</div=
>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>}</div=
>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>}</div=
>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>}</div=
>
<br>
</div>
<hr style=3D"display:inline-block;width:98%" tabindex=3D"-1">
<div id=3D"divRplyFwdMsg" dir=3D"ltr"><font face=3D"Calibri, sans-serif" st=
yle=3D"font-size:11pt" color=3D"#000000"><b>From:</b> Ingleby, Graeme &lt;G=
[email protected]&gt;<br>
<b>Sent:</b> Friday, September 15, 2017 1:39 PM<br>
<b>To:</b> [email protected]<br>
<b>Subject:</b> [platform-dev] Re: lookup confusing me</font>
<div>&nbsp;</div>
</div>
<div>
<div style=3D"direction: ltr;font-family: Tahoma;color: #000000;font-size: =
10pt;">
<div><span style=3D"font-size: 10pt;">It's not clear exactly what you are t=
rying to achieve when you say &quot;all opened files&quot;. &nbsp;</span></=
div>
<div><br>
</div>
<div>As others have said, you don't seem to understand what NetBeans Lookup=
s are or what they are used for so I would recommend reading up on Lookup.<=
/div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">If you understood Lookup, you would k=
now that the global Lookup does not allow you to find all instances of a pa=
rticular class type within your application.</span></div>
<div><br>
</div>
<div>It's not really clear if you are trying to find all DataObject instanc=
es or if you are trying to find opened files that are associated with &quot=
;editor&quot; Top Components.</div>
<div><br>
</div>
<div>Neil provided a link to a Wiki page&nbsp;<a href=3D"http://wiki.netbea=
ns.org/DevFaqGetOpenEditorWindows" target=3D"_blank" style=3D"font-size: 10=
pt; font-family: &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif;">http:=
//wiki.netbeans.org/DevFaqGetOpenEditorWindows</a><span style=3D"font-famil=
y: &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif; font-size: medium;">=
&nbsp;</span><span style=3D"font-size: 10pt;">that
 shows you how you can determine which editors are open from which you can =
get the associated DataObject. &nbsp;If you are trying to find editor files=
 - this is all you need.</span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">That however would not find other Dat=
aObjects that were opened using the FileSystem/DataSystems API's directly e=
.g.&nbsp;</span><span style=3D"font-size: 10pt;">&nbsp; &nbsp; &nbsp; &nbsp=
; &nbsp; &nbsp;&nbsp;</span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">If you called&nbsp;</span><span style=
=3D"font-size: 10pt;">DataObject dobj =3D DataObject.find(FileUtil.toFileOb=
ject(new File(&quot;/path/to/somefile&quot;));</span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">When a DataObject instance is created=
 it is registered in a DataObjectPool unfortunately this pool is not public=
. &nbsp;You could however get the DataObjects registered in the pool using =
reflection. &nbsp;</span><font size=3D"2">Using
 a dirty hack like this really isn't recommended and you really need to und=
erstand what you are doing. &nbsp;&nbsp;</font></div>
<div><font size=3D"2"><br>
</font></div>
<div><font size=3D"2">This approach would return ALL DataObject instances a=
nd not just DataObject instances&nbsp;</font><span style=3D"font-size: 10pt=
;">that you opened (the platform opens many configuration files, other plug=
ins could create DataObjects). &nbsp;Determining
 which DataObjects you are truly interested in would be another problem for=
 you to solve. Manipulating objects you did not create/own could have serio=
us consequences!</span></div>
<div><br>
</div>
<div>For demonstration purposes only:</div>
<div><br>
</div>
<div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field poolField =3D Class.fo=
rName(&quot;org.openide.loaders.DataObjectPool&quot;).getDeclaredField(&quo=
t;POOL&quot;);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; poolField.setAccessible(true=
);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object pool =3D poolField.ge=
t(null);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field mapField =3D pool.getC=
lass().getDeclaredField(&quot;map&quot;);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapField.setAccessible(true)=
;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object map =3D mapField.get(=
pool); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map&lt;Object, Object&gt; re=
fMap =3D (Map&lt;Object,Object&gt;)map;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(Object o: refMap.values(=
)) {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field objField=
 =3D o.getClass().getDeclaredField(&quot;obj&quot;);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objField.setAc=
cessible(true);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Reference&lt;D=
ataObject&gt; result =3D (Reference&lt;DataObject&gt;)objField.get(o);</div=
>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataObject dat=
aObject =3D (DataObject)result.get();</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.pri=
ntln(dataObject); // Here's the DataObject you want</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div>
</div>
<div><br>
</div>
<div>The above code would print all DataObject instances - you could return=
 them in a list, filter objects of interest etc.</div>
<div><br>
</div>
<div>Do you intend your modules to be used within the IDE or a custom NB Pl=
atform application where you might be able to customize your harness? &nbsp=
;If the later you could provide the functionality you need without using re=
flection by accessing DataObjectPool
 and providing a public method to return the information you need.</div>
<div><br>
</div>
<div>Hopefully you are only interested in Editor files and Neil has already=
 provided the perfect solution for that. &nbsp;</div>
<div><br>
</div>
<div>Using non-public API's is a really bad idea, but sometimes the only wa=
y to achieve what you want&nbsp;<span style=3D"font-size: 10pt;">(I've seen=
 reflection used within NB sources)</span><span style=3D"font-size: 10pt;">=
. &nbsp;</span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">Just make sure you fully understand t=
he ramifications.</span></div>
<div><br>
</div>
<div><br>
</div>
<div style=3D"font-family: Times New Roman; color: #000000; font-size: 16px=
">
<hr tabindex=3D"-1">
<div id=3D"divRpF972446" style=3D"direction: ltr;"><font face=3D"Tahoma" si=
ze=3D"2" color=3D"#000000"><b>From:</b> Peter Cheung [[email protected]=
]<br>
<b>Sent:</b> Thursday, September 14, 2017 12:39 PM<br>
<b>To:</b> [email protected]<br>
<b>Subject:</b> [platform-dev] lookup confusing me<br>
</font><br>
</div>
<div></div>
<div>
<div style=3D"font-family:Calibri,Helvetica,sans-serif; font-size:12pt; col=
or: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=3D"Apple-tab-span" style=3D"white-space:pre">Lookup lookup=
 =3D Utilities.actionsGlobalContext();</span></div>
<div><br>
</div>
<div>Collection&lt;? extends DataObject&gt; list =3D lookup.lookupAll(DataO=
bject.class);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>for (D=
ataObject dataObject : list) {</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>Module=
Lib.log(dataObject);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>}</div=
>
<div><br>
</div>
<div>If i use &quot;<span style=3D"color:rgb(0,0,0); font-family:Calibri,He=
lvetica,sans-serif; font-size:16px; font-style:normal">Lookup.getDefault().=
lookupAll&quot;, looupAll() even returns nothing. Please help</span></div>
<div><span style=3D"color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-se=
rif; font-size:16px; font-style:normal"><br>
</span></div>
<div><span style=3D"color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-se=
rif; font-size:16px; font-style:normal">Thanks</span></div>
<div>FromPeter</div>
<br>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

--_000_ME1PR01MB0260CBD021513ADC9C8F3846B56D0ME1PR01MB0260ausp_--