KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > NewElementAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.plugin;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.pde.core.plugin.IPluginElement;
16 import org.eclipse.pde.core.plugin.IPluginParent;
17 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
18 import org.eclipse.pde.internal.core.text.IDocumentNode;
19 import org.eclipse.pde.internal.core.text.plugin.PluginElementNode;
20 import org.eclipse.pde.internal.ui.PDEPlugin;
21 import org.eclipse.pde.internal.ui.PDEPluginImages;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.pde.internal.ui.editor.contentassist.XMLInsertionComputer;
24
25 public class NewElementAction extends Action {
26     public static final String JavaDoc UNKNOWN_ELEMENT_TAG = PDEUIMessages.NewElementAction_generic;
27
28     private ISchemaElement elementInfo;
29
30     private IPluginParent parent;
31
32     public NewElementAction(ISchemaElement elementInfo, 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 JavaDoc getElementName() {
42         return elementInfo != null ? elementInfo.getName() : UNKNOWN_ELEMENT_TAG;
43     }
44
45     public void run() {
46         IPluginElement newElement = parent.getModel().getFactory().createElement(parent);
47         try {
48             newElement.setName(getElementName());
49             ((PluginElementNode)newElement).setParentNode((IDocumentNode)parent);
50             parent.add(newElement);
51
52             // If there is an associated schema, recursively auto-insert
53
// required child elements and attributes respecting multiplicity
54
if (elementInfo != null) {
55                 XMLInsertionComputer.computeInsertion(elementInfo, newElement);
56             }
57             
58         } catch (CoreException e) {
59             PDEPlugin.logException(e);
60         }
61     }
62
63 }
64
Popular Tags