Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Prouvost2018-11-16 11:41:39 -0500
committerOlivier Prouvost2018-12-11 10:35:53 -0500
commitb4bc1a0be0f20e5424ea448e12d9a69480fdc0cb (patch)
tree18c3e30552a3187fc95e9414c765eb6ecfdbd572
parentad31434b1e34c1cef863057239280311c6ebc4f3 (diff)
downloadorg.eclipse.e4.tools-b4bc1a0be0f20e5424ea448e12d9a69480fdc0cb.tar.gz
org.eclipse.e4.tools-b4bc1a0be0f20e5424ea448e12d9a69480fdc0cb.tar.xz
org.eclipse.e4.tools-b4bc1a0be0f20e5424ea448e12d9a69480fdc0cb.zip
Bug 541581 - Spy Processor could be simplified using model fragment
Change-Id: Ie23460f5f82711f09f58c5e9f2e4f551c09d0218 Signed-off-by: Olivier Prouvost <olivier.prouvost@opcoach.com>
-rw-r--r--bundles/org.eclipse.e4.tools.spy/fragment.e4xmi18
-rw-r--r--bundles/org.eclipse.e4.tools.spy/src/org/eclipse/e4/tools/spy/SpyHandler.java101
-rw-r--r--bundles/org.eclipse.e4.tools.spy/src/org/eclipse/e4/tools/spy/SpyProcessor.java152
3 files changed, 142 insertions, 129 deletions
diff --git a/bundles/org.eclipse.e4.tools.spy/fragment.e4xmi b/bundles/org.eclipse.e4.tools.spy/fragment.e4xmi
index 732c1296..b300f3da 100644
--- a/bundles/org.eclipse.e4.tools.spy/fragment.e4xmi
+++ b/bundles/org.eclipse.e4.tools.spy/fragment.e4xmi
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="ASCII"?>
-<fragment:ModelFragments xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fragment="http://www.eclipse.org/ui/2010/UIModel/fragment" xmlns:menu="http://www.eclipse.org/ui/2010/UIModel/application/ui/menu" xmi:id="_Yv7zwLnQEeWXX4KE4W3FyQ">
+<fragment:ModelFragments xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:basic="http://www.eclipse.org/ui/2010/UIModel/application/ui/basic" xmlns:commands="http://www.eclipse.org/ui/2010/UIModel/application/commands" xmlns:fragment="http://www.eclipse.org/ui/2010/UIModel/fragment" xmlns:menu="http://www.eclipse.org/ui/2010/UIModel/application/ui/menu" xmi:id="_Yv7zwLnQEeWXX4KE4W3FyQ">
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_ZeXAALnQEeWXX4KE4W3FyQ" featurename="menuContributions" parentElementId="xpath:/" positionInList="">
<elements xsi:type="menu:MenuContribution" xmi:id="_br97sLn_EeWJoqtWvx7b2g" elementId="org.eclipse.e4.tools.spy.menucontribution.0" positionInParent="after=additions" parentId="org.eclipse.ui.main.menu">
<children xsi:type="menu:Menu" xmi:id="_fUHHELn_EeWJoqtWvx7b2g" elementId="window" label="Window"/>
@@ -11,4 +11,20 @@
</children>
</elements>
</fragments>
+ <fragments xsi:type="fragment:StringModelFragment" xmi:id="_PV6YoOmnEei5O53ZMcQRyw" featurename="commands" parentElementId="xpath:/">
+ <elements xsi:type="commands:Command" xmi:id="_QytGwOmnEei5O53ZMcQRyw" elementId="org.eclipse.e4.tools.spy.command" commandName="Open Spy" description="The command to open any spy in the E4 Spies window">
+ <parameters xmi:id="_WFMUEOmnEei5O53ZMcQRyw" elementId="org.eclipse.e4.tools.spy.command.partID" name="viewPart"/>
+ </elements>
+ </fragments>
+ <fragments xsi:type="fragment:StringModelFragment" xmi:id="_bH9DUOmnEei5O53ZMcQRyw" featurename="handlers" parentElementId="xpath:/">
+ <elements xsi:type="commands:Handler" xmi:id="_cVMSgOmnEei5O53ZMcQRyw" elementId="org.eclipse.e4.tools.spy.handler.0" contributionURI="bundleclass://org.eclipse.e4.tools.spy/org.eclipse.e4.tools.spy.SpyHandler" command="_QytGwOmnEei5O53ZMcQRyw"/>
+ </fragments>
+ <fragments xsi:type="fragment:StringModelFragment" xmi:id="_D-9ugOmnEei5O53ZMcQRyw" featurename="snippets" parentElementId="xpath:/">
+ <elements xsi:type="basic:TrimmedWindow" xmi:id="_I_PxgOmnEei5O53ZMcQRyw" elementId="org.eclipse.e4.tools.spy.window" selectedElement="_UUrRUOmqEei5O53ZMcQRyw" label="E4 Spies window" iconURI="platform:/plugin/org.eclipse.e4.tools.spy/icons/spyicon.png" width="600" height="1000">
+ <children xsi:type="basic:PartStack" xmi:id="_UUrRUOmqEei5O53ZMcQRyw" elementId="org.eclipse.e4.tools.spy.partStack"/>
+ <trimBars xmi:id="_MYxKsOmqEei5O53ZMcQRyw" elementId="org.eclipse.e4.tools.spy.trimbar">
+ <children xsi:type="menu:ToolBar" xmi:id="_RKEWgOmqEei5O53ZMcQRyw" elementId="org.eclipse.e4.tools.spy.toolbar"/>
+ </trimBars>
+ </elements>
+ </fragments>
</fragment:ModelFragments>
diff --git a/bundles/org.eclipse.e4.tools.spy/src/org/eclipse/e4/tools/spy/SpyHandler.java b/bundles/org.eclipse.e4.tools.spy/src/org/eclipse/e4/tools/spy/SpyHandler.java
index ee1a9cd9..0891181c 100644
--- a/bundles/org.eclipse.e4.tools.spy/src/org/eclipse/e4/tools/spy/SpyHandler.java
+++ b/bundles/org.eclipse.e4.tools.spy/src/org/eclipse/e4/tools/spy/SpyHandler.java
@@ -21,6 +21,7 @@ import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.commands.MCommand;
import org.eclipse.e4.ui.model.application.commands.MParameter;
import org.eclipse.e4.ui.model.application.descriptor.basic.MPartDescriptor;
+import org.eclipse.e4.ui.model.application.ui.SideValue;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar;
@@ -30,16 +31,9 @@ import org.eclipse.e4.ui.model.application.ui.menu.MHandledToolItem;
import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
-import org.osgi.framework.FrameworkUtil;
public class SpyHandler {
- private static final int DEFAULT_SPY_WINDOW_HEIGHT = 600;
- private static final int DEFAULT_SPY_WINDOW_WIDTH = 1000;
- private static final String E4_SPIES_WINDOW_LABEL = "E4 Spies window";
private static final String E4_SPIES_WINDOW = "org.eclipse.e4.tools.spy.window";
- private static final String E4_SPIES_ICON = "platform:/plugin/org.eclipse.e4.tools.spy/icons/spyicon.png";
- private static final String E4_SPIES_TRIMBAR = "org.eclipse.e4.tools.spy.trimbar";
- private static final String E4_SPIES_TOOLBAR = "org.eclipse.e4.tools.spy.toolbar";
private static final String E4_SPIES_PART_STACK = "org.eclipse.e4.tools.spy.partStack";
/**
@@ -60,7 +54,8 @@ public class SpyHandler {
@Execute
public void run(EPartService ps, @Optional @Named(SpyProcessor.SPY_COMMAND_PARAM) String viewID, MApplication appli,
EModelService modelService) {
- MWindow spyWindow = getOrCreateSpyWindow(appli, modelService);
+
+ MWindow spyWindow = prepareSpyWindow(appli, modelService);
MPartStack partStack = (MPartStack) modelService.find(E4_SPIES_PART_STACK, spyWindow);
@@ -79,46 +74,46 @@ public class SpyHandler {
}
- private MWindow getOrCreateSpyWindow(MApplication appli, EModelService modelService) {
+ /**
+ * Prepare the spy window : add the toolbar Item to be called for each spies
+ * in the tool bar The structure is defined in the fragment.
+ *
+ * @param appli
+ * @param modelService
+ * @return the model Spies window found in fragment.
+ */
+
+ private MWindow prepareSpyWindow(MApplication appli, EModelService modelService) {
+
+ // If window already in application, it is already prepared -> Return it
List<MWindow> existingWindow = modelService.findElements(appli, E4_SPIES_WINDOW, MWindow.class, null);
if (existingWindow.size() >= 1)
return existingWindow.get(0);
- // Now spy window for the moment... add one...
- String contributorURI = "platform:/plugin/" + FrameworkUtil.getBundle(getClass()).getSymbolicName();
-
- MTrimmedWindow tw = modelService.createModelElement(MTrimmedWindow.class);
- tw.setElementId(E4_SPIES_WINDOW);
- tw.setLabel(E4_SPIES_WINDOW_LABEL);
- tw.setIconURI(E4_SPIES_ICON);
- tw.setContributorURI(contributorURI);
-
- MTrimBar trimBar = modelService.createModelElement(MTrimBar.class);
- trimBar.setElementId(E4_SPIES_TRIMBAR);
- trimBar.setContributorURI(contributorURI);
- tw.getTrimBars().add(trimBar);
-
- MToolBar toolbar = modelService.createModelElement(MToolBar.class);
- toolbar.setElementId(E4_SPIES_TOOLBAR);
- toolbar.setContributorURI(contributorURI);
- trimBar.getChildren().add(toolbar);
+ // No spy window in main windows for the moment... extract the structure
+ // from the
+ // snippet.
+ MTrimmedWindow tw = (MTrimmedWindow) modelService.findSnippet(appli, E4_SPIES_WINDOW);
+ MTrimBar trimBar = tw.getTrimBars().stream().filter((t) -> t.getSide() == SideValue.TOP).findFirst().get();
+ MToolBar toolbar = (MToolBar) trimBar.getChildren().get(0);
+
+ // Get the spy command (added by fragment)
+ MCommand spyCmd = null;
+ for (MCommand cmd : appli.getCommands()) {
+ if (SpyProcessor.SPY_COMMAND.equals(cmd.getElementId())) {
+ // Do nothing if command exists
+ spyCmd = cmd;
+ break;
+ }
+ }
// Create one toolbar element for each 'spy' tagged descriptor
for (MPartDescriptor mp : appli.getDescriptors()) {
if (mp.getTags().contains(SpyProcessor.SPY_TAG)) {
// Create a toolitem bound to the command.
MHandledToolItem toolItem = modelService.createModelElement(MHandledToolItem.class);
- toolItem.setContributorURI(contributorURI);
-
- // Search for spy command (added by processor)
- MCommand spyCmd = null;
- for (MCommand cmd : appli.getCommands()) {
- if (SpyProcessor.SPY_COMMAND.equals(cmd.getElementId())) {
- // Do nothing if command exists
- spyCmd = cmd;
- break;
- }
- }
+ toolItem.setContributorURI(mp.getContributorURI());
+
toolItem.setCommand(spyCmd);
toolItem.setIconURI(mp.getIconURI());
toolItem.setLabel(mp.getLabel());
@@ -134,15 +129,33 @@ public class SpyHandler {
}
}
- tw.setWidth(DEFAULT_SPY_WINDOW_WIDTH);
- tw.setHeight(DEFAULT_SPY_WINDOW_HEIGHT);
+ // Can not use move here because it is only for MWindowElement
+ centerSpyWindow(appli, tw);
appli.getChildren().get(0).getWindows().add(tw);
- MPartStack partStack = modelService.createModelElement(MPartStack.class);
- partStack.setElementId(E4_SPIES_PART_STACK);
- tw.getChildren().add(partStack);
-
return tw;
}
+
+ /**
+ * Make the spy window centered on top of main window.
+ *
+ * @param appli
+ * current appli
+ * @param tw
+ * main trim window
+ */
+ private void centerSpyWindow(MApplication appli, MTrimmedWindow tw) {
+ MWindow mainWindow = appli.getChildren().get(0);
+ float ratio = 0.75f;
+ int spyW = (int) (mainWindow.getWidth() * ratio);
+ int spyH = (int) (mainWindow.getHeight() * ratio);
+
+ tw.setX(mainWindow.getX() + (mainWindow.getWidth() - spyW) / 2);
+ tw.setY(mainWindow.getY() + (mainWindow.getHeight() - spyH) / 2);
+ tw.setWidth(spyW);
+ tw.setHeight(spyH);
+ }
+
+
}
diff --git a/bundles/org.eclipse.e4.tools.spy/src/org/eclipse/e4/tools/spy/SpyProcessor.java b/bundles/org.eclipse.e4.tools.spy/src/org/eclipse/e4/tools/spy/SpyProcessor.java
index 5cb402f5..854c37b8 100644
--- a/bundles/org.eclipse.e4.tools.spy/src/org/eclipse/e4/tools/spy/SpyProcessor.java
+++ b/bundles/org.eclipse.e4.tools.spy/src/org/eclipse/e4/tools/spy/SpyProcessor.java
@@ -29,8 +29,6 @@ import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.commands.MBindingContext;
import org.eclipse.e4.ui.model.application.commands.MBindingTable;
import org.eclipse.e4.ui.model.application.commands.MCommand;
-import org.eclipse.e4.ui.model.application.commands.MCommandParameter;
-import org.eclipse.e4.ui.model.application.commands.MHandler;
import org.eclipse.e4.ui.model.application.commands.MKeyBinding;
import org.eclipse.e4.ui.model.application.commands.MParameter;
import org.eclipse.e4.ui.model.application.descriptor.basic.MPartDescriptor;
@@ -47,7 +45,6 @@ public class SpyProcessor {
public static final String SPY_COMMAND = "org.eclipse.e4.tools.spy.command";
public static final String SPY_COMMAND_PARAM = "org.eclipse.e4.tools.spy.command.partID";
- private static final String SPY_HANDLER = "org.eclipse.e4.tools.spy.handler";
private static final String E4_SPIES_BINDING_TABLE = "org.eclipse.e4.tools.spy.bindings";
MApplication application;
@@ -63,16 +60,11 @@ public class SpyProcessor {
@Execute
public void process(IExtensionRegistry extRegistry) {
- // This processor will read all spy extensions and automatically create
- // the command, handler and binding
- // to open this spy in the dedicated spy window.
+ // This processor will read all spy extensions and automatically fill
+ // the dynamics contents where spies are used
- // First of all, it creates the spyCommand having one parameter (Id of
- // the part to display) and default handler for this command.
-
-
-
- MCommand command = getOrCreateSpyCommand();
+ MCommand command = getSpyCommand();
+ MBindingTable bindingTable = getBindingTable();
for (IConfigurationElement e : extRegistry.getConfigurationElementsFor("org.eclipse.e4.tools.spy.spyPart")) {
String partName = e.getAttribute("name");
@@ -87,7 +79,7 @@ public class SpyProcessor {
// Bind the command with the binding, and add the view ID as
// parameter.
// The part class name will be the ID of the part descriptor
- bindSpyKeyBinding(shortCut, command, partID);
+ bindSpyKeyBinding(bindingTable, shortCut, command, partID);
// Add the descriptor in application
addSpyPartDescriptor(partID, partName, iconPath, partClass, desc);
@@ -103,8 +95,9 @@ public class SpyProcessor {
}
- public MCommand getOrCreateSpyCommand() {
- // DO NOT USE findElement on ModelService (it searches only in
+ public MCommand getSpyCommand() {
+
+ // Warning : DO NOT USE findElement on ModelService (it searches only in
// MUIElements)
for (MCommand cmd : application.getCommands()) {
if (SPY_COMMAND.equals(cmd.getElementId())) {
@@ -113,60 +106,80 @@ public class SpyProcessor {
}
}
- MCommand command = modelService.createModelElement(MCommand.class);
- command.setElementId(SPY_COMMAND);
- command.setCommandName("Open a spy");
- String contributorURI = "platform:/plugin/" + FrameworkUtil.getBundle(getClass()).getSymbolicName();
- command.setContributorURI(contributorURI);
- command.setDescription("Open a spy in the E4 spy window");
-
- // Parameter (will be the ID of the part descriptor (ie, the full
- // qualified class name))
- // It will be received in the Handler and send by keybinding
- MCommandParameter cp = modelService.createModelElement(MCommandParameter.class);
- cp.setElementId(SPY_COMMAND_PARAM);
- cp.setName("viewPart");
- cp.setContributorURI(contributorURI);
- command.getParameters().add(cp);
-
- application.getCommands().add(command);
-
- // Create the default handler for this command
- // (will receive the parameter)
- for (MHandler hdl : application.getHandlers()) {
- if (SPY_HANDLER.equals(hdl.getElementId())) {
- // Do nothing if handler exists, return the command
- return command;
+ log.error("The Spy command (with ID : " + SPY_COMMAND
+ + " cannot be found (It should be provided by org.elipse.e4.tools/fragmenE4.xmi");
+
+ return null;
+ }
+
+
+
+ /**
+ * Helper method to get or create the binding table for all spies (where
+ * spies will add their key binding). Bind this table with the
+ * org.eclipse.ui.contexts.dialogAndWindow binding context which should be
+ * present (create it if not)
+ *
+ * This method will probably move to the common spy plugin providing common
+ * spy stuff (see bug #428903)
+ *
+ * @param keySequence
+ * @param cmd
+ * @param paramViewId
+ */
+ public void bindSpyKeyBinding(MBindingTable spyBindingTable, String keySequence, MCommand cmd, String paramViewId) {
+ // This method must :
+ // search for a binding table having the binding context 'dialog and
+ // window'
+ // If none found, create it and also the binding context
+ // Then can add the KeyBinding if not already added
+
+
+ // Search for the key binding if already present
+ for (MKeyBinding kb : spyBindingTable.getBindings())
+ if (keySequence.equals(kb.getKeySequence())) {
+ // A binding with this key sequence is already present. Check if
+ // command is the same
+ if (kb.getCommand().getElementId().equals(cmd.getElementId()))
+ return;
+ else {
+ // Must log an error : key binding already exists in this
+ // table but with another command
+ System.out.println("WARNING : Cannot bind the command '" + cmd.getElementId()
+ + "' to the keySequence : " + keySequence + " because the command "
+ + kb.getCommand().getElementId() + " is already bound !");
+ return;
+ }
}
- }
- // Create the handler for this command.
- MHandler handler = modelService.createModelElement(MHandler.class);
- handler.setElementId(SPY_HANDLER);
- handler.setContributionURI("bundleclass://org.eclipse.e4.tools.spy/org.eclipse.e4.tools.spy.SpyHandler");
- handler.setContributorURI("platform:/plugin/org.eclipse.e4.tools.spy");
- application.getHandlers().add(handler);
+ // Key binding is not yet in table... can add it now.
+ MKeyBinding binding = modelService.createModelElement(MKeyBinding.class);
+ binding.setElementId(paramViewId + ".binding");
+ binding.setContributorURI(cmd.getContributorURI());
+ binding.setKeySequence(keySequence);
- handler.setCommand(command);
+ MParameter p = modelService.createModelElement(MParameter.class);
+ p.setName(SPY_COMMAND_PARAM);
+ p.setValue(paramViewId);
+ binding.getParameters().add(p);
- return command;
+ spyBindingTable.getBindings().add(binding);
+ binding.setCommand(cmd);
}
/**
- * Helper method to get or create the binding table for all spies (where spies
- * will add their key binding). Bind this table with the
+ * Helper method to get or create the binding table for all spies (where
+ * spies will add their key binding). Bind this table with the
* org.eclipse.ui.contexts.dialogAndWindow binding context which should be
* present (create it if not)
*
- * This method will probably move to the common spy plugin providing common spy
- * stuff (see bug #428903)
*
* @param keySequence
* @param cmd
* @param paramViewId
*/
- public void bindSpyKeyBinding(String keySequence, MCommand cmd, String paramViewId) {
+ private MBindingTable getBindingTable() {
// This method must :
// search for a binding table having the binding context 'dialog and
// window'
@@ -210,36 +223,7 @@ public class SpyProcessor {
}
- // Search for the key binding if already present
- for (MKeyBinding kb : spyBindingTable.getBindings())
- if (keySequence.equals(kb.getKeySequence())) {
- // A binding with this key sequence is already present. Check if
- // command is the same
- if (kb.getCommand().getElementId().equals(cmd.getElementId()))
- return;
- else {
- // Must log an error : key binding already exists in this
- // table but with another command
- System.out.println("WARNING : Cannot bind the command '" + cmd.getElementId()
- + "' to the keySequence : " + keySequence + " because the command "
- + kb.getCommand().getElementId() + " is already bound !");
- return;
- }
- }
-
- // Key binding is not yet in table... can add it now.
- MKeyBinding binding = modelService.createModelElement(MKeyBinding.class);
- binding.setElementId(cmd.getElementId() + ".binding");
- binding.setContributorURI(cmd.getContributorURI());
- binding.setKeySequence(keySequence);
-
- MParameter p = modelService.createModelElement(MParameter.class);
- p.setName(SPY_COMMAND_PARAM);
- p.setValue(paramViewId);
- binding.getParameters().add(p);
-
- spyBindingTable.getBindings().add(binding);
- binding.setCommand(cmd);
+ return spyBindingTable;
}
@@ -274,7 +258,7 @@ public class SpyProcessor {
@AboutToShow
public void fillE4SpyMenu(List<MMenuElement> items) {
- MCommand command = getOrCreateSpyCommand();
+ MCommand command = getSpyCommand();
for (MPartDescriptor mp : application.getDescriptors()) {
if (mp.getTags().contains(SPY_TAG)) {
MHandledMenuItem hi = modelService.createModelElement(MHandledMenuItem.class);

Back to the top