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: 891476461f76adb36438c0e542f5f591bba73ca6 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
 * Copyright (c) 2000, 2010 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.actions;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
import org.eclipse.wst.xml.core.internal.document.DocumentImpl;
import org.eclipse.wst.xsd.ui.internal.editor.XSDEditorPlugin;
import org.eclipse.wst.xsd.ui.internal.refactor.handlers.MakeAnonymousTypeGobalHandler;
import org.eclipse.wst.xsd.ui.internal.refactor.structure.MakeAnonymousTypeGlobalCommand;
import org.eclipse.wst.xsd.ui.internal.refactor.structure.MakeTypeGlobalProcessor;
import org.eclipse.wst.xsd.ui.internal.refactor.wizard.RefactoringWizardMessages;
import org.eclipse.wst.xsd.ui.internal.refactor.wizard.RenameRefactoringWizard;
import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDTypeDefinition;
import org.w3c.dom.Node;

/**
 * @deprecated Use {@link MakeAnonymousTypeGobalHandler}
 */
public class MakeAnonymousTypeGlobalAction extends XSDSelectionDispatchAction {

	private String fParentName;
	private boolean isComplexType = true;
	private XSDTypeDefinition fSelectedComponent;
	
	public MakeAnonymousTypeGlobalAction(ISelection selection, XSDSchema schema) {
		super(selection, schema);
		setText(RefactoringWizardMessages.MakeAnonymousTypeGlobalAction_text); //$NON-NLS-1$
	}
	
	public boolean canRun() {

		return fSelectedComponent != null;
	}
	

	private String getNewDefaultName(){
		if(fParentName != null && !"".equals(fParentName)){ //$NON-NLS-1$
			if(isComplexType){
				return fParentName + "ComplexType"; //$NON-NLS-1$
			}
			else{
				return fParentName + "SimpleType"; //$NON-NLS-1$
			}
		}
		else{
			if(isComplexType){
				return "NewComplexType"; //$NON-NLS-1$
			}
			else{
				return "NewSimpleType"; //$NON-NLS-1$
			}
		}
		
	}
	private boolean canEnable(XSDConcreteComponent xsdComponent){
		if (xsdComponent instanceof XSDComplexTypeDefinition) {
			fSelectedComponent = (XSDComplexTypeDefinition)xsdComponent;
			isComplexType = true;
			XSDComplexTypeDefinition typeDef = (XSDComplexTypeDefinition) xsdComponent;
			XSDConcreteComponent parent = typeDef.getContainer();
			if(parent instanceof XSDElementDeclaration){
				fParentName = ((XSDElementDeclaration)parent).getName();
				return true;
			}
		} 
		else if (xsdComponent instanceof XSDSimpleTypeDefinition){
			fSelectedComponent = (XSDSimpleTypeDefinition)xsdComponent;
			isComplexType = false;
			XSDSimpleTypeDefinition typeDef = (XSDSimpleTypeDefinition) xsdComponent;
			XSDConcreteComponent parent = typeDef.getContainer();
			if(parent instanceof XSDElementDeclaration){
				fParentName = ((XSDElementDeclaration)parent).getName();
				return true;
			}
			else if(parent instanceof XSDAttributeDeclaration){
				fParentName = ((XSDAttributeDeclaration)parent).getName();
				return true;
			}
			
		}
		return false;
	}

	protected boolean canEnable(Object selectedObject) {
		
		if (selectedObject instanceof XSDConcreteComponent) {
			return canEnable((XSDConcreteComponent)selectedObject) && super.canEnable(selectedObject);
		}
		else if (selectedObject instanceof Node) {
			Node node = (Node) selectedObject;
			XSDConcreteComponent concreteComponent = getSchema().getCorrespondingComponent(node);
			return canEnable(concreteComponent) && super.canEnable(concreteComponent);
		
		}
		return false;
		
	}

	public void run1() {
		
		if(fSelectedComponent == null){
			return;
		}
		
		if(fSelectedComponent.getSchema() == null){
			getSchema().updateElement(true);
		}
		MakeTypeGlobalProcessor processor = new MakeTypeGlobalProcessor(fSelectedComponent, getNewDefaultName());
		RenameRefactoring refactoring = new RenameRefactoring(processor);
		try {
			RefactoringWizard wizard = new RenameRefactoringWizard(
					refactoring,
					RefactoringWizardMessages.RenameComponentWizard_defaultPageTitle, // TODO: provide correct strings
					RefactoringWizardMessages.RenameComponentWizard_inputPage_description, null);
			RefactoringWizardOpenOperation op= new RefactoringWizardOpenOperation(wizard);
			op.run(XSDEditorPlugin.getShell(), wizard.getDefaultPageTitle());
			//triggerBuild();
		} catch (InterruptedException e) {
			// do nothing. User action got cancelled
		}
		
	}
	
	public void run(){
		if(fSelectedComponent == null){
			return;
		}
		
		if(fSelectedComponent.getSchema() == null){
			getSchema().updateElement(true);
		}
		DocumentImpl doc = (DocumentImpl) fSelectedComponent.getElement().getOwnerDocument();
		doc.getModel().beginRecording(
						this,
						RefactoringWizardMessages.MakeAnonymousTypeGlobalAction_text);
		MakeAnonymousTypeGlobalCommand command = new MakeAnonymousTypeGlobalCommand(
				fSelectedComponent, getNewDefaultName());
		command.run();
		doc.getModel().endRecording(this);
	}
	
	

}

Back to the top