Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 6352ad14f70629e393c8c87b9a1b084368e1ce45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*******************************************************************************
 * Copyright (c) 2001, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xsd.ui.internal.refactor.structure;

import org.eclipse.xsd.XSDComplexTypeContent;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDFactory;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDTypeDefinition;

public final class MakeLocalElementGlobalCommand extends AbstractCommand
{
	
  public MakeLocalElementGlobalCommand
    (XSDConcreteComponent element)
  {
    super(element.getContainer());
    setModelObject(element);
  }
  
  public void run()
  {
    
   if(getModelObject() instanceof XSDElementDeclaration){
   
	   XSDElementDeclaration element = (XSDElementDeclaration)getModelObject();
 	XSDConcreteComponent parent = getParent();
 	XSDConcreteComponent container = parent.getContainer();
 	
 	// clone element with it's content and set it global
	XSDConcreteComponent  elementDecl = ((XSDElementDeclaration)getModelObject()).cloneConcreteComponent(true, true);
 	container.getSchema().getContents().add(elementDecl);
 	
 	// create local element and set it's reference to the global one
 	XSDElementDeclaration elementRef = 
	      XSDFactory.eINSTANCE.createXSDElementDeclaration();
	elementRef.setValue(element.getValue());
    elementRef.setResolvedElementDeclaration((XSDElementDeclaration)elementDecl); 
    
    // now set content models
 	if(parent instanceof XSDComplexTypeContent){
 		if(container instanceof XSDModelGroup){
 			XSDModelGroup modelGroup = (XSDModelGroup)container;
 			// disconnect parent from its container
 			int index = modelGroup.getContents().indexOf(parent);
 			 XSDParticle particle = 
 			      XSDFactory.eINSTANCE.createXSDParticle();
 		    particle.setContent(elementRef);
 		    modelGroup.getContents().add(index, particle); 
            // Copy over the max/minOccurs from the old local to the element ref
            if (parent instanceof XSDParticle) {
              XSDParticle parentParticle = (XSDParticle)parent;
              
              if (parentParticle.isSetMinOccurs()) {
                particle.setMinOccurs(parentParticle.getMinOccurs());
                parentParticle.unsetMinOccurs();
              }
              
              if (parentParticle.isSetMaxOccurs()) {
                particle.setMaxOccurs(parentParticle.getMaxOccurs());
                parentParticle.unsetMaxOccurs();
              }
            }          
            element.unsetForm();
 		   
 			modelGroup.getContents().remove(parent);
 		    modelGroup.updateElement(true);
  		    formatChild(modelGroup.getElement());
 		}
 	}
 	else if(parent instanceof XSDTypeDefinition){
		 		
 	}
 	
 	container.getSchema().updateElement(true);
    formatChild(elementDecl.getElement());
  
   }

  }
	/* (non-Javadoc)
	 * @see org.eclipse.wst.xsd.ui.internal.commands.AbstractCommand#adopt(org.eclipse.xsd.XSDConcreteComponent)
	 */
	protected boolean adopt(XSDConcreteComponent model) {
		// TODO Auto-generated method stub
		return true;
	}
}

Back to the top