--- title: Eclipse RCP Plugin Development --- <h1 style="font-size: 4rem">Eclipse RCP Plugin Dev</h1> [TOC] # Useful Links - (Tutorial on vogella.com) https://www.vogella.com/tutorials/EclipseRCP/article.html - (How to add toolbar menu & menubar menu & popup menu item) https://www.vogella.com/tutorials/EclipseCommands/article.html#menu-extension-points - (Books) http://libgen.is/search.php?req=Eclipse+Rich+Client+Platform&lg_topic=libgen&open=0&view=simple&res=25&phrase=1&column=def - (CDT code) https://github.com/eclipse-cdt/cdt - E4AP = [Eclipse 4 Application Platform](https://wiki.eclipse.org/Eclipse4/RCP/FAQ#Adopting_the_Eclipse_4_Application_Platform) # Eclipse RCP Lifecycle https://wiki.eclipse.org/Eclipse4/RCP/Lifecycle 1. Component Lifecycle - `@PostConstruct private void init()` - `@PreDestroy private void dispose()` 2. Application Lifecycle - A "Product" can provide a "lifecycle manager object" # Misc Tasks ## Conditionally Visible Popup Menu Item **NOTE: When the handler is already disabled (see next section), there's no need to disable menu item anymore** - Add a `<visibleWhen>` tag for the condition, under the `<command>` tag - The "Extensions" tab doesn't provide way to write this; edit `plugin.xml` directly - Hint taken from [Name filter for eclipse.ui.menus (SO)](https://stackoverflow.com/questions/19776876/name-filter-for-eclipse-ui-menus) - Detailed info on [ Platform Expression Framework - Eclipse Wiki](https://wiki.eclipse.org/Platform_Expression_Framework) - `popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu` is the locationURI for project explorer ```xml <menuContribution allPopups="false" locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?before=group.edit"> <command commandId="com.example.rcpproject.commands.flashCommand" label="Flash hex" style="push"> <visibleWhen checkEnabled="false"> <iterate ifEmpty="false" operator="or"> <test property="org.eclipse.core.resources.name" value="*hex"></test> </iterate> </visibleWhen> </command> </menuContribution> ``` ## Conditionally Enable Command Handler - Add a `<enabledWhen>` tag for the condition, under the `<handler>` tag - See links in previous section for docs - Example: We want to enable the handler only when **both** (i.e. `<and>`) conditions meet 1. There's exactly one file selected (we don't want to handle multi-selection) - Solved using `<count>` 2. All selected items (there's only one) are .hex files - Solved using `<iterate>`, `<adapt>` (kinda like a type cast?), and `<test>` ```xml <handler class="com.example.rcpproject.handlers.FlashHandler" commandId="com.example.rcpproject.commands.flashCommand"> <enabledWhen> <with variable="selection"> <and> <count value="1"/> <iterate ifEmpty="false" operator="or"> <adapt type="org.eclipse.core.resources.IResource"> <test property="org.eclipse.core.resources.name" value="*hex"></test> </adapt> </iterate> </and> </with> </enabledWhen> </handler> ``` ## Run Code on Plugin Startup - https://stackoverflow.com/questions/21377071/how-to-make-an-eclipse-plugin-load-on-startup - Use `org.eclipse.ui.startup` extension point - The relevant part of `plugin.xml` ```xml! <extension point="org.eclipse.ui.startup"> <startup class="com.example.rcpproject.handlers.Startup"> </startup> </extension> ``` - The handler class ```java! public class Startup implements IStartup { @Override public void earlyStartup() { System.out.println("Plugin early startup"); } } ``` ## Get Notified on Window Open and Close - Use `PlatformUI.getWorkbench().addWindowListener(listener)` in `Startup::earlyStartup` - Known Problem: First (the initial one) window is not detected if put in `earlyStartup` - Example code ```java PlatformUI.getWorkbench().addWindowListener(new IWindowListener() { @Override public void windowActivated(IWorkbenchWindow window) { System.out.println("Window activated"); } @Override public void windowDeactivated(IWorkbenchWindow window) { System.out.println("Window de-activated"); } @Override public void windowClosed(IWorkbenchWindow window) { System.out.println("Window closed"); } @Override public void windowOpened(IWorkbenchWindow window) { System.out.println("Window opened"); } }); ``` - `IWindowListener` build issue: Maybe follow https://stackoverflow.com/questions/3086859/importing-org-eclipse-core-packages-for-eclipse-plug-in 1. Window - Show View - Others - (type "Plug-ins") - On `org.eclipse.core.runtime`, "Add to Java Workspace Scope" 2. Add it on `plugin.xml` - Dependencies tab - Required plug-ins ## Get File Selected in Project Explorer https://stackoverflow.com/questions/27954738/how-can-i-get-the-path-of-a-file-in-the-project-explorer-of-exlipse ```java ISelectionService service = PlatformUI .getWorkbench() .getActiveWorkbenchWindow() .getSelectionService(); ISelection selection = service.getSelection(); if (selection instanceof IStructuredSelection) { Object selected = ((IStructuredSelection)selection).getFirstElement(); IFile file = (IFile) Platform.getAdapterManager().getAdapter(selected, IFile.class); } ``` ## Build Eclipse Plugin without Eclipse - Use *Tycho* (https://github.com/eclipse-tycho/tycho) - Not tested yet - See eclipse markdown editor plugin (https://github.com/winterstein/Eclipse-Markdown-Editor-Plugin/blob/master/pom.xml) for example, it uses tycho to build