1 package org.argeo.connect.e4;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.SortedMap;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9
10 import org.argeo.cms.CmsException;
11 import org.argeo.connect.ConnectException;
12 import org.argeo.connect.ServiceRanking;
13 import org.argeo.connect.ui.AppWorkbenchService;
14 import org.argeo.connect.ui.SystemWorkbenchService;
15 import org.argeo.eclipse.ui.EclipseUiUtils;
16 import org.eclipse.core.commands.Command;
17 import org.eclipse.core.commands.ParameterizedCommand;
18 import org.eclipse.e4.core.commands.ECommandService;
19 import org.eclipse.e4.core.commands.EHandlerService;
20 import org.eclipse.e4.core.contexts.IEclipseContext;
21 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22 import org.eclipse.e4.ui.workbench.modeling.EPartService;
23 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
24 import org.eclipse.jface.wizard.Wizard;
25 import org.eclipse.swt.graphics.Image;
26
27 @SuppressWarnings("restriction")
28 public class SystemE4Service implements SystemWorkbenchService, AppE4Service {
29 IEclipseContext eclipseContext;
30
31
32
33
34 private String defaultEditorId = null;
35 private SortedMap<ServiceRanking, AppWorkbenchService> knownAppWbServices;
36
37
38
39
40
41
42
43 public SystemE4Service(SortedMap<ServiceRanking, AppWorkbenchService> knownAppWbServices,
44 IEclipseContext eclipseContext) {
45 super();
46 this.knownAppWbServices = knownAppWbServices;
47 this.eclipseContext = eclipseContext;
48
49
50
51 }
52
53 @Override
54 public void callCommand(String commandId, Map<String, String> parameters) {
55 final Command command = eclipseContext.get(ECommandService.class).getCommand(commandId);
56 final ParameterizedCommand pcmd = ParameterizedCommand.generateCommand(command,
57 parameters != null ? parameters : new HashMap<>());
58 if (pcmd == null)
59 throw new ConnectException("No command found for id " + commandId + " and parameters " + parameters);
60 eclipseContext.get(EHandlerService.class).executeHandler(pcmd);
61 }
62
63 @Override
64 public String getOpenEntityEditorCmdId() {
65 return "org.argeo.suite.e4.command.openEntity";
66 }
67
68 @Override
69 public String getDeleteEntityCmdId() {
70 return "org.argeo.suite.e4.command.deleteEntity";
71 }
72
73 @Override
74 public void openEntityEditor(Node entity) {
75 EPartService partService = eclipseContext.get(EPartService.class);
76 try {
77 String entityId = entity.getIdentifier();
78 String entityEditorId = getEntityEditorId(entity);
79 parts: for (MPart part : partService.getParts()) {
80 String elementId = part.getElementId();
81 if (!elementId.equals(entityEditorId))
82 continue parts;
83 String id = part.getPersistedState().get(ConnectE4Constants.ENTITY_ID);
84 if (id != null && entityId.equals(id)) {
85 partService.showPart(part, PartState.ACTIVATE);
86 return;
87 }
88 }
89
90
91 MPart part = partService.createPart(entityEditorId);
92 if (part == null)
93 throw new CmsException("No entity editor found for id " + entityEditorId);
94 part.setLabel(entity.getName());
95
96
97
98 part.getPersistedState().put(ConnectE4Constants.ENTITY_ID, entityId);
99 partService.showPart(part, PartState.ACTIVATE);
100 } catch (RepositoryException e) {
101 throw new ConnectException("Cannot open " + entity, e);
102 }
103
104 }
105
106
107
108
109
110 @Override
111 public String getDefaultEditorId() {
112 String result = null;
113 for (AppWorkbenchService appWbService : knownAppWbServices.values()) {
114 result = appWbService.getDefaultEditorId();
115 if (EclipseUiUtils.notEmpty(result))
116 return result;
117 }
118 return defaultEditorId;
119 }
120
121 @Override
122 public String getEntityEditorId(Node entity) {
123 String result = null;
124 for (AppWorkbenchService appWbService : knownAppWbServices.values()) {
125 result = appWbService.getEntityEditorId(entity);
126 if (EclipseUiUtils.notEmpty(result))
127 return result;
128 }
129 return null;
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143 @Override
144 public void openSearchEntityView(String nodeType, String label) {
145 EPartService partService = eclipseContext.get(EPartService.class);
146
147 String entityEditorId = getSearchEntityEditorId(nodeType);
148 for (MPart part : partService.getParts()) {
149 String id = part.getPersistedState().get(ConnectE4Constants.NODE_TYPE);
150 if (id != null && nodeType.equals(id)) {
151 partService.showPart(part, PartState.ACTIVATE);
152 return;
153 }
154 }
155
156
157 MPart part = partService.createPart(entityEditorId);
158 if (part == null)
159 throw new CmsException("No entity editor found for id " + entityEditorId);
160 part.setLabel(label);
161 part.getPersistedState().put(ConnectE4Constants.NODE_TYPE, nodeType);
162 partService.showPart(part, PartState.ACTIVATE);
163
164
165
166
167 }
168
169 @Override
170 public String getSearchEntityEditorId(String nodeType) {
171 String result = null;
172 for (AppWorkbenchService appWbService : knownAppWbServices.values()) {
173 result = appWbService.getSearchEntityEditorId(nodeType);
174 if (EclipseUiUtils.notEmpty(result))
175 return result;
176 }
177 return null;
178 }
179
180 @Override
181 public Image getIconForType(Node entity) {
182 Image result = null;
183 for (AppWorkbenchService appWbService : knownAppWbServices.values()) {
184 result = appWbService.getIconForType(entity);
185 if (result != null)
186 return result;
187 }
188 return null;
189 }
190
191 @Override
192 public Wizard getCreationWizard(Node node) {
193 Wizard result = null;
194 for (AppWorkbenchService appWbService : knownAppWbServices.values()) {
195 result = appWbService.getCreationWizard(node);
196 if (result != null)
197 return result;
198 }
199 return null;
200 }
201
202
203
204
205
206
207 }