View Javadoc
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  	// EPartService partService;
31  	// ECommandService commandService;
32  	// EHandlerService handlerService;
33  
34  	private String defaultEditorId = null;// DefaultDashboardEditor.ID;
35  	private SortedMap<ServiceRanking, AppWorkbenchService> knownAppWbServices;
36  
37  	// public SystemE4Service(SortedMap<ServiceRanking, AppWorkbenchService>
38  	// knownAppWbServices) {
39  	// super();
40  	// this.knownAppWbServices = knownAppWbServices;
41  	// }
42  
43  	public SystemE4Service(SortedMap<ServiceRanking, AppWorkbenchService> knownAppWbServices,
44  			IEclipseContext eclipseContext) {
45  		super();
46  		this.knownAppWbServices = knownAppWbServices;
47  		this.eclipseContext = eclipseContext;
48  		// this.partService = partService;
49  		// this.commandService = commandService;
50  		// this.handlerService = handlerService;
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  			// new part
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  			// part.getPersistedState().put("nodeWorkspace",
96  			// entity.getSession().getWorkspace().getName());
97  			// part.getPersistedState().put("nodePath", entity.getPath());
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 	// APP SERVICE
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 	// @Override
133 	// public void openEntityEditor(Node entity) {
134 	// String result = null;
135 	// for (AppWorkbenchService appWbService : knownAppWbServices) {
136 	// // TODO make it more robust
137 	// result = appWbService.getEntityEditorId(entity);
138 	// if (EclipseUiUtils.notEmpty(result))
139 	// appWbService.openEntityEditor(entity);
140 	// }
141 	// }
142 
143 	@Override
144 	public void openSearchEntityView(String nodeType, String label) {
145 		EPartService partService = eclipseContext.get(EPartService.class);
146 		// try {
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 		// new part
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 		// } catch (RepositoryException e) {
164 		// throw new ConnectException("Cannot open search entity for type " + nodeType,
165 		// e);
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 	// void setKnownAppWbServices(SortedMap<ServiceRanking, AppWorkbenchService>
203 	// knownAppWbServices) {
204 	// this.knownAppWbServices = knownAppWbServices;
205 	// }
206 
207 }