01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 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.schema;
11:
12: import org.eclipse.jface.action.Action;
13: import org.eclipse.jface.resource.ImageDescriptor;
14: import org.eclipse.osgi.util.NLS;
15: import org.eclipse.pde.internal.core.ischema.ISchemaCompositor;
16: import org.eclipse.pde.internal.core.ischema.ISchemaElement;
17: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
18: import org.eclipse.pde.internal.core.schema.SchemaComplexType;
19: import org.eclipse.pde.internal.core.schema.SchemaCompositor;
20: import org.eclipse.pde.internal.core.schema.SchemaElement;
21: import org.eclipse.pde.internal.ui.PDEPluginImages;
22: import org.eclipse.pde.internal.ui.PDEUIMessages;
23:
24: public class NewCompositorAction extends Action {
25: private ISchemaElement source;
26: private Object object;
27: private int kind;
28:
29: public NewCompositorAction(ISchemaElement source, Object object,
30: int kind) {
31: this .source = source;
32: this .object = object;
33: this .kind = kind;
34:
35: String text = upperCaseFirstLetter(ISchemaCompositor.kindTable[kind]);
36: setText("&" + text); //$NON-NLS-1$
37: setToolTipText(NLS.bind(
38: PDEUIMessages.SchemaEditor_NewCompositor_tooltip, text));
39: ImageDescriptor desc = null;
40:
41: switch (kind) {
42: case ISchemaCompositor.SEQUENCE:
43: desc = PDEPluginImages.DESC_SEQ_SC_OBJ;
44: break;
45: case ISchemaCompositor.CHOICE:
46: desc = PDEPluginImages.DESC_CHOICE_SC_OBJ;
47: break;
48: }
49: setImageDescriptor(desc);
50: setEnabled(source.getSchema().isEditable());
51: }
52:
53: /**
54: * @param text must have a length of at least two
55: * @return
56: */
57: private String upperCaseFirstLetter(String text) {
58: if ((text == null) || (text.length() < 2)) {
59: return text;
60: }
61: String firstLetter = text.substring(0, 1).toUpperCase();
62: String rest = text.substring(1);
63:
64: return firstLetter + rest;
65: }
66:
67: public void run() {
68: SchemaCompositor compositor = new SchemaCompositor(source, kind);
69: if (object instanceof SchemaElement) {
70: SchemaComplexType type = null;
71: SchemaElement element = (SchemaElement) source;
72: if (element.getType() instanceof SchemaComplexType) {
73: type = (SchemaComplexType) element.getType();
74: ISchemaCompositor oldComp = type.getCompositor();
75: if (oldComp != null) {
76: ISchemaObject[] oldChildren = oldComp.getChildren();
77: for (int i = 0; i < oldChildren.length; i++) {
78: compositor.addChild(oldChildren[i]);
79: }
80: }
81: type.setCompositor(compositor);
82: } else {
83: type = new SchemaComplexType(source.getSchema());
84: type.setCompositor(compositor);
85: ((SchemaElement) source).setType(type);
86: }
87: // Any element that defines a root compositor cannot be translatable
88: if (element.hasTranslatableContent()) {
89: element.setTranslatableProperty(false);
90: }
91: } else if (object instanceof SchemaCompositor) {
92: ((SchemaCompositor) object).addChild(compositor);
93: }
94: }
95: }
|