Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ADT/Eclipse plug in java development - how to get object selected from outline view of a class

matt
Active Contributor
2,843

I'm developing a plugin that runs on ABAP development objects. It's launched by a run configuration. This is the code that runs when you right click on e.g. a method in a class in the outline view, and select run as <my_app>

public class LaunchShortcut implements ILaunchShortcut {
	public void launch(ISelection selection, String mode) {
		new RunAlchemist().run(selection);
	}
}

The run method here passes an ISelection instance. I check whether it's an instance of ITreeSelection, as it needs to be cast.

ITreeSelection treeSelection = (ITreeSelection) selection;

ITreeSelection has an interator on it, which I loop over. Each node comes out as type Object. Now, if its real type is IAdtOutlineTreeNode, I can use node.toString to get the name and type (which yields type method and the name of the method), but for the class name, I need the parent node so I can use parent.toString().

Object parent = ((IAdtOutlineTreeNode) node).getParent()

The problem is that IAdtOutlineTreeNode is "Discouraged access: The type 'IAdtOutlineTreeNode' is not API"

Can anyone tell me how I can determine from ISelection the class and method name selected?

1 ACCEPTED SOLUTION
Read only

stockbal
Participant
2,633

Hi Matthew,

if you want a way where you don't use non API classes you could try the following:

 ITreeSelection treeSel = (ITreeSelection) sel;

 for (Object node : treeSel) {
   System.out.println("Selected Node info:");
   TreePath[] paths = treeSel.getPathsFor(node);

   for (TreePath path : paths) {
     /*
      * the segments are sorted from top to bottom. the last segment should be the
      * selected node in the tree.
      */
      for (int i = path.getSegmentCount() - 1; i >= 0; i--) {
        Object segment = path.getSegment(i);

        IAdtObjectReference objRef = Adapters.adapt(segment, IAdtObjectReference.class);
        if (objRef != null) {
          System.out.printf("Type: %s, Name: %s\n", objRef.getType(), objRef.getName());
        }
      }
   }
} 

I hope this helps :).

P.S.: I sometimes have to use non API classes because there was no other way. I don't like it but I think it is better than using something like reflection because in that case you get immediate feedback if you test your coding against an updated version of the used plugin coding.

Kind Regards,
Ludwig

6 REPLIES 6
Read only

stockbal
Participant
2,634

Hi Matthew,

if you want a way where you don't use non API classes you could try the following:

 ITreeSelection treeSel = (ITreeSelection) sel;

 for (Object node : treeSel) {
   System.out.println("Selected Node info:");
   TreePath[] paths = treeSel.getPathsFor(node);

   for (TreePath path : paths) {
     /*
      * the segments are sorted from top to bottom. the last segment should be the
      * selected node in the tree.
      */
      for (int i = path.getSegmentCount() - 1; i >= 0; i--) {
        Object segment = path.getSegment(i);

        IAdtObjectReference objRef = Adapters.adapt(segment, IAdtObjectReference.class);
        if (objRef != null) {
          System.out.printf("Type: %s, Name: %s\n", objRef.getType(), objRef.getName());
        }
      }
   }
} 

I hope this helps :).

P.S.: I sometimes have to use non API classes because there was no other way. I don't like it but I think it is better than using something like reflection because in that case you get immediate feedback if you test your coding against an updated version of the used plugin coding.

Kind Regards,
Ludwig

Read only

matt
Active Contributor
2,633

Absolutely perfect.

I'd got to the segments a few hours ago, - one of those things were stepping back a little and looking again at what's being passed to the method - but

    IAdtObjectReference objRef = Adapters.adapt(segment, IAdtObjectReference.class);<br>

completes it!

Many thanks.

Read only

matt
Active Contributor
0 Likes
2,633

How exactly does the Adapters.adapt work?

Read only

2,633

I will try to explain it on the example i wrote in my answer.

IAdtObjectReference objRef = Adapters.adapt(segment, IAdtObjectReference.class);

Adaption to the type IAdtObjectReference is either possible if the type of segment implements the interface IAdaptable (prio 1) or if there is an adapter factory (class that implements IAdapterFactory) for the type IAdtObjectReference.

Adapter factories are registered via extension org.eclipse.core.runtime.adapters in a plugin

e.g.

Read only

matt
Active Contributor
0 Likes
2,633

Odd. Since I upgraded ADT, I'm getting that use of IAdtObjectReference is discouraged. And that's in the API!

Maybe I just parse the string... (ABAP Favorites plugin does that).

Read only

matt
Active Contributor
0 Likes
2,633

Oops. Wrong IADTObjectReference. Should be using

com.sap.adt.tools.core.model.adtcore.IAdtObjectReference