KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.schema;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.pde.internal.core.ischema.ISchema;
18 import org.eclipse.pde.internal.core.schema.Schema;
19 import org.eclipse.pde.internal.core.schema.SchemaElement;
20 import org.eclipse.pde.internal.core.schema.SchemaRootElement;
21 import org.eclipse.pde.internal.core.schema.SchemaSimpleType;
22 import org.eclipse.pde.internal.ui.PDEPlugin;
23 import org.eclipse.pde.internal.ui.PDEPluginImages;
24 import org.eclipse.pde.internal.ui.PDEUIMessages;
25
26 public class NewElementAction extends Action {
27     private Schema schema;
28     private static final String JavaDoc NAME_COUNTER_KEY = "__schema_element_name"; //$NON-NLS-1$
29
public NewElementAction() {
30         setText(PDEUIMessages.SchemaEditor_NewElement_label);
31         setImageDescriptor(PDEPluginImages.DESC_GEL_SC_OBJ);
32         setToolTipText(PDEUIMessages.SchemaEditor_NewElement_tooltip);
33     }
34     private String JavaDoc getInitialName() {
35         Hashtable JavaDoc counters = PDEPlugin.getDefault().getDefaultNameCounters();
36         Integer JavaDoc counter = (Integer JavaDoc) counters.get(NAME_COUNTER_KEY);
37         if (counter == null) {
38             counter = new Integer JavaDoc(1);
39         } else {
40             counter = new Integer JavaDoc(counter.intValue() + 1);
41         }
42         counters.put(NAME_COUNTER_KEY, counter);
43         return NLS.bind(PDEUIMessages.SchemaEditor_NewElement_initialName, counter.intValue() + ""); //$NON-NLS-1$
44
}
45     public org.eclipse.pde.internal.core.schema.Schema getSchema() {
46         return schema;
47     }
48     public void run() {
49         String JavaDoc name = getInitialName();
50         SchemaElement element;
51         if (name.equals("extension")) //$NON-NLS-1$
52
element = new SchemaRootElement(schema, name);
53         else
54             element = new SchemaElement(schema, name);
55         element.setType(new SchemaSimpleType(schema, "string")); //$NON-NLS-1$
56
schema.addElement(element);
57         schema.updateReferencesFor(element, ISchema.REFRESH_ADD);
58     }
59     public void setSchema(Schema newSchema) {
60         schema = newSchema;
61     }
62 }
63
Popular Tags