01: /*******************************************************************************
02: * Copyright (c) 2000, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.plugin;
11:
12: import org.eclipse.core.runtime.CoreException;
13: import org.eclipse.jface.action.Action;
14: import org.eclipse.pde.core.plugin.IPluginElement;
15: import org.eclipse.pde.core.plugin.IPluginParent;
16: import org.eclipse.pde.internal.core.ischema.ISchemaElement;
17: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
18: import org.eclipse.pde.internal.core.text.plugin.PluginElementNode;
19: import org.eclipse.pde.internal.ui.PDEPlugin;
20: import org.eclipse.pde.internal.ui.PDEPluginImages;
21: import org.eclipse.pde.internal.ui.PDEUIMessages;
22: import org.eclipse.pde.internal.ui.editor.contentassist.XMLInsertionComputer;
23:
24: public class NewElementAction extends Action {
25: public static final String UNKNOWN_ELEMENT_TAG = PDEUIMessages.NewElementAction_generic;
26:
27: private ISchemaElement elementInfo;
28:
29: private IPluginParent parent;
30:
31: public NewElementAction(ISchemaElement elementInfo,
32: IPluginParent parent) {
33: this .elementInfo = elementInfo;
34: // this.project = project;
35: this .parent = parent;
36: setText(getElementName());
37: setImageDescriptor(PDEPluginImages.DESC_GENERIC_XML_OBJ);
38: setEnabled(parent.getModel().isEditable());
39: }
40:
41: private String getElementName() {
42: return elementInfo != null ? elementInfo.getName()
43: : UNKNOWN_ELEMENT_TAG;
44: }
45:
46: public void run() {
47: IPluginElement newElement = parent.getModel().getFactory()
48: .createElement(parent);
49: try {
50: newElement.setName(getElementName());
51: ((PluginElementNode) newElement)
52: .setParentNode((IDocumentElementNode) parent);
53:
54: // If there is an associated schema, recursively auto-insert
55: // required child elements and attributes respecting multiplicity
56: if (elementInfo != null) {
57: XMLInsertionComputer.computeInsertion(elementInfo,
58: newElement);
59: }
60: parent.add(newElement);
61: } catch (CoreException e) {
62: PDEPlugin.logException(e);
63: }
64: }
65:
66: }
|