How do I make eclipse custom view take data from the file currently active in the editor? - eclipse

I recently went into creating my own personal DSL with xtext and manage to create a mini programming language based on C (simple expressions and basic functions). My current task it to create a custom tree view for the language that would allow me to see all the functions as root elements and the instructions inside them as children.
My actual problem that I can't seem to resolve is exactly how do I make the custom tree view I wish to create take the data from the file that I'm currently working on.
I have an RCP product ready for the DSL that I can use and I would like to include this view over there.
I have created the interface for the view with WindowBuilder and made it as a ViewPart.
In the end I wish it to look close to what the standard outline for a java program looks like.
Thanks for the help in advance.

If you work with your own view, you can add an IPartListener implementation that will notify you on when an editor is activated with the following code:
getViewSite().getPage().addPartListener(new IPartListener() {
#Override
public void partOpened(IWorkbenchPart part) {
}
#Override
public void partDeactivated(IWorkbenchPart part) {
}
#Override
public void partClosed(IWorkbenchPart part) {
}
#Override
public void partBroughtToTop(IWorkbenchPart part) {
}
#Override
public void partActivated(IWorkbenchPart part) {
// Add view initialization from the new part
}
});

Related

Nested Variables in Unity Inspector

I was wondering if anyone knew how to make nested variables inside the unity inspector with a script, kind of like this:
Doing so does require knowledge of UnityEditor and not only (as you say ... nested variables) can give you many other control options in the inspector. To do this, I created a sample code called MenuManager. As you can see this code:
public class MenuManager : MonoBehaviour
{
public bool variable1;
public float nestedVariable;
//...
}
Unity itself does not provide any attributes such as [Range] or [Header] for such a request, and to do this you need to define a CustomEditor for the class but Before to do that, you need to create a folder similar to the photo with name of Editor and put it in the Assets folder. Then create another script with name of MenuEditor (for example here ..) and put it in a Editor folder.
Now open the MenuEditor code. Inherit it from the Editor class. Editor class is base class for editing inspector and more. It will give you many override methods with access to the features inside the unity editor. and make sure it has two Attributes Custom Editor as well as CanEditMultipleObjects.
[CustomEditor(typeof(MenuManager))]
[CanEditMultipleObjects]
public class MenuEditor : Editor
{
//..
}
This code gives you access to the MenuManager script. According to the following code, I coded a Nested variable to the first one.
[CustomEditor(typeof(MenuManager))]
[CanEditMultipleObjects]
public class MenuEditor : Editor
{
public override void OnInspectorGUI()
{
var myMenu = target as MenuManager; // target is script reference that we want to manipulate it
myMenu.variable1 = EditorGUILayout.Toggle("Variable 1", myMenu.variable1); // show first variable on inspector
GUI.enabled = myMenu.variable1; // access to second variable depend of first
myMenu.nestedVariable =EditorGUILayout.Toggle("Nested Variable", myMenu.nestedVariable);
GUI.enabled = true;
}
}
After finishing the work, you can access the nested variable only by setting first one to true.
Remember after doing this you can access many other features just inside MenuEditor class but if you find this difficult, I suggest you use Odin Inspecter. I hope you have reached your answer. comment under answer if you need more information.

How to add action item to the coolbar of e4 eclipse rcp application?

I am currently trying to port my eclipse 3 rcp application to e4.The major hurdle I am facing is to use action item which i was using in e3.In eclipse 3 application i was creating action item of coolbar by extending action.The code was looking like below spinets.
public class Testaction extends Action {
private IWorkbenchWindow window;
public Testaction (IWorkbenchWindow window, String string) {
setText(string);
setToolTipText(string);
setId("ID");
setImageDescriptor(Activator.getImageDescriptor("/icons/some.png"));
this.window = window;
}
#override
public void run() {
/**
Do something
**/
super.run();
}
was adding it to coolbar through
toolbar.add(demoaction);
But with e4 this part seems to be changed and I understand that there we need to have annotation #Execute which will excute the contribution which we will be giving through setcontribuitionuri as below snippet
part.setContributionURI(
"bundleclass://bundle/bundle.contribuitionclass");
I just want to know whether I can use my old action class here or i need to port everything to newer style .
Any help on this will be appreciated.Thanks in advance...
e4 does not support Actions for model elements in the Application.e4xmi.
The simplest conversion is to use a Direct ToolItem in the tool bar. However using a Handled ToolItem with a Command and Handler is more flexible.
In either case the Image, Label and Tooltip are specified in the Application.e4xmi.

Use eclipse GMF to create read only diagram

I followed the file system example http://gmfsamples.tuxfamily.org/wiki/doku.php?id=gmf_tutorial1
what I wanted to do is not using the generated editor with its palette.
I created a new plugin with one view and I wanted to create a diagram programatically inside this view to show for instance 2 objects connected with link
I came across this answer GMF display diagram example
but it didn't help me a lot.
in createPartControl of my view I did
#Override
public void createPartControl(Composite parent) {
DiagramGraphicalViewer viewer = new DiagramGraphicalViewer();
viewer.createControl(parent);
RootEditPart root = EditPartService.getInstance().createRootEditPart(diagram);
viewer.setRootEditPart(root);
viewer.setEditPartFactory(new EcoreEditPartProvider());
viewer.getControl().setBackground(ColorConstants.listBackground);
viewer.setContents(diagram);
}
as in the answer but I didn't know how to get that "diagram" variable
The easiest would be use the same GraphicalViewer for your view and the same diagram as well. Just get your DiagramEditPart from the viewer and call disableEditMode() on it. (Do the appropriate type casting if necessary).

Update property view on click of project explorer, eclipse plugin

I have created a custom project in project explorer. Whenever I click on custom project folder currently it shows default property sheet but I want to customize this property sheet.
I have gone through the tabbed property example but I am not able customize it.
Please can anyone provide me some sample examples or code for same.
Thanks.
how to connect that property view to an editor or project explorer
model classes for you custom project and its folders should implement IAdaptable interface and return an object implementing IPropertySource that describes given element. it will be passed to properties view automatically when you click on the element.
alternativelly, you can avoid implementing IAdaptable and create an IAdapterFactory that converts an instance of you project/folder element into corresponding IPropertySoure but then you have to make Eclipse framework aware of your IAdapterFactory implementation.
public class MyProjectAdapterFactory implements IAdapterFactory {
#Override
public Object getAdapter(Object adaptableObject, Class adapterType) {
if (adapterType== IPropertySource.class && adaptableObject instanceof MyProject){
return new MyProjectPropertySource((MyProject) adaptableObject);
}
return null;
}
#Override
public Class[] getAdapterList() {
return new Class[] { IPropertySource.class };
}
}
register it in you plugin.xml file:
<extension point="org.eclipse.core.runtime.adapters">
<factory adaptableType="my.example.MyProject" class="my.example.MyProjectAdapterFactory">
<adapter type="org.eclipse.ui.views.properties.IPropertySource"/>
</factory>
</extension>
look at the full tutorial: http://www.vogella.de/articles/EclipsePlugIn/article.html

How to run a class anytime a editor page receive focus on Eclipse?

Is there a way to run a class everytime editor page receive focus, something like prompt message when a class source has changed outside eclipse? Can a plug-in editor or extension do this work?
The FAQ "How do I find out what view or editor is selected?" can help you call your class when the Editor is active (which is when you can test if it has focus as well), by using a IPartService:
Two types of listeners can be added to the part service:
IPartListener
and the poorly named IPartListener2.
You should always use this second one as it can handle part-change events on parts that have not yet been created because they are hidden in a stack behind another part.
This listener will also tell you when a part is made visible or hidden or when an editor's input is changed:
IWorkbenchPage page = ...;
//the active part
IWorkbenchPart active = page.getActivePart();
//adding a listener
IPartListener2 pl = new IPartListener2() {
public void partActivated(IWorkbenchPartReference ref)
System.out.println("Active: "+ref.getTitle());
}
... other listener methods ...
};
page.addPartListener(pl);
Note: IWorkbenchPage implements IPartService directly.
You can also access an activation service by using IWorkbenchWindow.getPartService().
I am click Toolbar or Button to get focus which view or editor current working on RCP eclipse
//class:Current_Workbech extends AbstractHandler to execute() method
public class Current_Workbech extends AbstractHandler{
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IPartService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();
//MessageDialog box open to get title which view or editor focus and current working
MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(
event).getShell(), "Current Workbench Window", service.getActivePart().getTitle()+"");
return null;
}
}