View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.cms.ui.workbench.internal.jcr.commands;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.jcr.Property;
22  import javax.jcr.nodetype.NodeType;
23  
24  import org.argeo.cms.ui.workbench.internal.jcr.parts.NodeEditorInput;
25  import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
26  import org.eclipse.core.commands.AbstractHandler;
27  import org.eclipse.core.commands.ExecutionEvent;
28  import org.eclipse.core.commands.ExecutionException;
29  import org.eclipse.ui.PartInitException;
30  import org.eclipse.ui.handlers.HandlerUtil;
31  
32  /** Generic command to open a Node in an editor. */
33  public class EditNode extends AbstractHandler {
34  	public final static String PARAM_EDITOR_ID = "editor";
35  
36  	private String defaultEditorId;
37  
38  	private Map<String, String> nodeTypeToEditor = new HashMap<String, String>();
39  
40  	public Object execute(ExecutionEvent event) throws ExecutionException {
41  		String path = event.getParameter(Property.JCR_PATH);
42  		String type = event.getParameter(NodeType.NT_NODE_TYPE);
43  		if (type == null)
44  			type = NodeType.NT_UNSTRUCTURED;
45  
46  		String editorId = event.getParameter(PARAM_EDITOR_ID);
47  		if (editorId == null)
48  			editorId = nodeTypeToEditor.containsKey(type) ? nodeTypeToEditor
49  					.get(type) : defaultEditorId;
50  
51  		NodeEditorInput nei = new NodeEditorInput(path);
52  		try {
53  			HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
54  					.openEditor(nei, editorId);
55  		} catch (PartInitException e) {
56  			ErrorFeedback.show("Cannot open " + editorId + " with " + path
57  					+ " of type " + type, e);
58  		}
59  		return null;
60  	}
61  
62  	public void setDefaultEditorId(String defaultEditorId) {
63  		this.defaultEditorId = defaultEditorId;
64  	}
65  }