Skip to content

Commit 07e7282

Browse files
feilimblaeubi
authored andcommitted
Simple example of a dummy e4 editor addition, which is contributed via a Model Fragment.
1 parent fe1c0be commit 07e7282

File tree

9 files changed

+174
-0
lines changed

9 files changed

+174
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
4+
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
5+
<classpathentry kind="src" path="src"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>org.eclipse.e4.ui.examples.dummyeditor</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.pde.ManifestBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.pde.SchemaBuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.eclipse.pde.PluginNature</nature>
26+
<nature>org.eclipse.jdt.core.javanature</nature>
27+
</natures>
28+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
3+
org.eclipse.jdt.core.compiler.compliance=21
4+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
5+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
6+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
8+
org.eclipse.jdt.core.compiler.release=enabled
9+
org.eclipse.jdt.core.compiler.source=21
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Manifest-Version: 1.0
2+
Bundle-ManifestVersion: 2
3+
Bundle-Name: DummyEditor
4+
Bundle-SymbolicName: org.eclipse.e4.ui.examples.dummyeditor;singleton:=true
5+
Bundle-Version: 1.0.0.qualifier
6+
Require-Bundle: org.eclipse.swt,
7+
org.eclipse.jface,
8+
org.eclipse.e4.core.contexts;bundle-version="1.13.0",
9+
org.eclipse.e4.core.di;bundle-version="1.9.500",
10+
org.eclipse.e4.ui.workbench;bundle-version="1.16.0",
11+
org.eclipse.e4.ui.model.workbench;bundle-version="2.4.400",
12+
jakarta.annotation-api,
13+
org.eclipse.ui.workbench;bundle-version="3.133.100"
14+
Automatic-Module-Name: org.eclipse.e4.ui.examples.dummyeditor
15+
Bundle-ActivationPolicy: lazy
16+
Bundle-RequiredExecutionEnvironment: JavaSE-21
17+
Model-Fragment: fragment.e4xmi
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bin.includes = META-INF/,\
2+
.,\
3+
fragment.e4xmi
4+
source.. = src/
5+
output.. = bin/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="ASCII"?>
2+
<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/descriptor/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="_6pcn8N7gEe-4ibwCH2r47g">
3+
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_Bzi3IN7hEe-4ibwCH2r47g" featurename="menuContributions" parentElementId="org.eclipse.e4.legacy.ide.application">
4+
<elements xsi:type="menu:MenuContribution" xmi:id="_JtwUoN7hEe-4ibwCH2r47g" elementId="org.eclipse.e4.ui.examples.dummyeditor.menucontribution.0" positionInParent="after=additions" parentId="window">
5+
<children xsi:type="menu:HandledMenuItem" xmi:id="_XMBQkN7hEe-4ibwCH2r47g" elementId="org.eclipse.e4.ui.examples.dummyeditor.handledmenuitem.opendummyeditor" label="Open Dummy Editor" command="_e-OBkN7hEe-4ibwCH2r47g"/>
6+
</elements>
7+
</fragments>
8+
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_c1D8sN7hEe-4ibwCH2r47g" featurename="commands" parentElementId="org.eclipse.e4.legacy.ide.application">
9+
<elements xsi:type="commands:Command" xmi:id="_e-OBkN7hEe-4ibwCH2r47g" elementId="org.eclipse.e4.ui.examples.dummyeditor.command.opendummyeditor" commandName="Open Dummy Editor"/>
10+
</fragments>
11+
<fragments xsi:type="fragment:StringModelFragment" xmi:id="__zpxEN7iEe-4ibwCH2r47g" featurename="handlers" parentElementId="org.eclipse.e4.legacy.ide.application">
12+
<elements xsi:type="commands:Handler" xmi:id="_DRaRYN7jEe-4ibwCH2r47g" elementId="org.eclipse.e4.ui.examples.dummyeditor.handler.0" contributionURI="bundleclass://org.eclipse.e4.ui.examples.dummyeditor/org.eclipse.e4.ui.examples.dummyeditor.OpenEditorHandler" command="_e-OBkN7hEe-4ibwCH2r47g"/>
13+
</fragments>
14+
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_WJS9YN7jEe-4ibwCH2r47g" featurename="descriptors" parentElementId="org.eclipse.e4.legacy.ide.application">
15+
<elements xsi:type="basic:PartDescriptor" xmi:id="_YIdYwN7jEe-4ibwCH2r47g" elementId="org.eclipse.e4.ui.examples.dummyeditor.partdescriptor.dummyeditor" label="DummyEditor" closeable="true" contributionURI="bundleclass://org.eclipse.e4.ui.examples.dummyeditor/org.eclipse.e4.ui.examples.dummyeditor.DummyEditor">
16+
<tags>Editor</tags>
17+
<tags>removeOnHide</tags>
18+
</elements>
19+
</fragments>
20+
</fragment:ModelFragments>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.eclipse.e4.ui.examples.dummyeditor;
2+
3+
import jakarta.annotation.PostConstruct;
4+
5+
import org.eclipse.swt.SWT;
6+
import org.eclipse.swt.widgets.Composite;
7+
import org.eclipse.swt.widgets.Text;
8+
9+
public class DummyEditor {
10+
11+
@PostConstruct
12+
public void postConstruct(Composite parent) {
13+
Text label = new Text(parent, SWT.ITALIC);
14+
label.setText("Dummy Editor Content...");
15+
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.eclipse.e4.ui.examples.dummyeditor;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.function.Predicate;
6+
7+
import org.eclipse.e4.core.di.annotations.Execute;
8+
import org.eclipse.e4.ui.model.application.MApplication;
9+
import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
10+
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
11+
import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainer;
12+
import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
13+
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
14+
import org.eclipse.e4.ui.workbench.modeling.EModelService;
15+
import org.eclipse.e4.ui.workbench.modeling.EPartService;
16+
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
17+
18+
public class OpenEditorHandler {
19+
20+
public static final String DUMMY_EDITOR_DESCRIPTOR_ID = "org.eclipse.e4.ui.examples.dummyeditor.partdescriptor.dummyeditor";
21+
22+
@Execute
23+
public void execute(EModelService modelService, MApplication application, EPartService partService) {
24+
Predicate<MPart> isEditorAlreadyOpenFilter = part -> DUMMY_EDITOR_DESCRIPTOR_ID.equals(part.getElementId());
25+
Optional<MPart> alreadyOpenMatchingPart = partService.getParts().stream().filter(isEditorAlreadyOpenFilter).findFirst();
26+
27+
MPart dummyPart = alreadyOpenMatchingPart.orElse(partService.createPart(DUMMY_EDITOR_DESCRIPTOR_ID));
28+
if (alreadyOpenMatchingPart.isEmpty())
29+
{
30+
dummyPart = partService.createPart(DUMMY_EDITOR_DESCRIPTOR_ID);
31+
32+
// not entirely necessary: but for consistency let's place our dummy editor
33+
// where editor instances appear
34+
Optional<MPartStack> primaryDataStack = findPrimaryConfiguationAreaPartStack(application, modelService);
35+
if (primaryDataStack.isPresent())
36+
{
37+
primaryDataStack.get().getChildren().add(dummyPart);
38+
}
39+
}
40+
else
41+
{
42+
dummyPart = alreadyOpenMatchingPart.get();
43+
}
44+
45+
partService.showPart(DUMMY_EDITOR_DESCRIPTOR_ID, PartState.ACTIVATE);
46+
partService.bringToTop(dummyPart);
47+
}
48+
49+
private Optional<MPartStack> findPrimaryConfiguationAreaPartStack(MApplication application,
50+
EModelService modelService) {
51+
// find the part stack from the application model which represents the area
52+
// where our main 'editors' appear, ie. the main workspace area
53+
List<MArea> areaCandidates = modelService.findElements(application, org.eclipse.ui.IPageLayout.ID_EDITOR_AREA,
54+
MArea.class, null, EModelService.IN_SHARED_ELEMENTS);
55+
if (areaCandidates.size() == 1) {
56+
MArea primaryArea = areaCandidates.get(0);
57+
for (MPartSashContainerElement element : primaryArea.getChildren()) {
58+
if (element instanceof MPartStack) {
59+
return Optional.of((MPartStack) element);
60+
} else if (element instanceof MPartSashContainer) {
61+
return ((MPartSashContainer) element).getChildren().stream().filter(c -> c instanceof MPartStack)
62+
.map(c -> (MPartStack) c).findFirst();
63+
}
64+
}
65+
}
66+
67+
return Optional.empty();
68+
}
69+
}

0 commit comments

Comments
 (0)