csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 2004 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.wst.xsd.ui.internal.commands; |
| 12 | |
| 13 | import org.eclipse.xsd.XSDAttributeDeclaration; |
| 14 | import org.eclipse.xsd.XSDComplexTypeDefinition; |
| 15 | import org.eclipse.xsd.XSDConcreteComponent; |
| 16 | import org.eclipse.xsd.XSDElementDeclaration; |
| 17 | import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 18 | import org.eclipse.xsd.XSDTypeDefinition; |
| 19 | |
| 20 | public final class MakeAnonymousTypeGlobalCommand extends AbstractCommand { |
| 21 | |
| 22 | String fNewName; |
| 23 | |
| 24 | public MakeAnonymousTypeGlobalCommand(XSDConcreteComponent element, |
| 25 | String newName) { |
| 26 | super(element.getContainer()); |
| 27 | setModelObject(element); |
| 28 | fNewName = newName; |
| 29 | } |
| 30 | |
| 31 | public void run() { |
| 32 | XSDConcreteComponent model = getModelObject(); |
| 33 | XSDConcreteComponent parent = model.getContainer(); |
| 34 | XSDTypeDefinition globalTypeDef = null; |
| 35 | if (model instanceof XSDComplexTypeDefinition) { |
| 36 | if (parent instanceof XSDElementDeclaration) { |
| 37 | // clone typedef with it's content and set it global |
| 38 | globalTypeDef = (XSDComplexTypeDefinition) model |
| 39 | .cloneConcreteComponent(true, false); |
| 40 | globalTypeDef.setName(fNewName); |
| 41 | parent.getSchema().getContents().add(globalTypeDef); |
| 42 | ((XSDElementDeclaration) parent) |
| 43 | .setTypeDefinition(globalTypeDef); |
| 44 | } |
| 45 | } else if (model instanceof XSDSimpleTypeDefinition) { |
| 46 | |
| 47 | XSDSimpleTypeDefinition typeDef = (XSDSimpleTypeDefinition) model; |
| 48 | if (parent instanceof XSDElementDeclaration) { |
| 49 | // clone typedef with it's content and set it global |
| 50 | globalTypeDef = (XSDSimpleTypeDefinition) typeDef |
| 51 | .cloneConcreteComponent(true, false); |
| 52 | globalTypeDef.setName(fNewName); |
| 53 | parent.getSchema().getContents().add(globalTypeDef); |
| 54 | ((XSDElementDeclaration) parent) |
| 55 | .setTypeDefinition(globalTypeDef); |
| 56 | formatChild(globalTypeDef.getElement()); |
| 57 | |
| 58 | } else if (parent instanceof XSDAttributeDeclaration) { |
| 59 | // clone typedef with it's content and set it global |
| 60 | globalTypeDef = (XSDSimpleTypeDefinition) typeDef |
| 61 | .cloneConcreteComponent(true, false); |
| 62 | globalTypeDef.setName(fNewName); |
| 63 | parent.getSchema().getContents().add(globalTypeDef); |
| 64 | ((XSDAttributeDeclaration) parent) |
| 65 | .setTypeDefinition((XSDSimpleTypeDefinition) globalTypeDef); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | } |
| 70 | |
| 71 | formatChild(globalTypeDef.getElement()); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * (non-Javadoc) |
| 77 | * |
| 78 | * @see org.eclipse.wst.xsd.ui.internal.commands.AbstractCommand#adopt(org.eclipse.xsd.XSDConcreteComponent) |
| 79 | */ |
| 80 | protected boolean adopt(XSDConcreteComponent model) { |
| 81 | // TODO Auto-generated method stub |
| 82 | return true; |
| 83 | } |
| 84 | } |