blob: 80790ff765a3445ccc1063ab0a29751183d92f95 (
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
|
/*******************************************************************************
* <copyright>
*
* Copyright (c) 2012 SAP AG 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:
* Petya Sabeva - initial API, implementation and documentation
*
* </copyright>
*
*******************************************************************************/
package org.eclipse.jpt.jpadiagrameditor.ui.internal.command;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jface.text.Document;
import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
import org.eclipse.jpt.common.utility.command.Command;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.JPADiagramEditorPlugin;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorUtil;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.text.edits.TextEditCopier;
public class CreateEntityTypeHierarchy implements Command {
private PersistentType subclass;
private PersistentType superclass;
private boolean shouldCreate;
public CreateEntityTypeHierarchy(PersistentType superclass, PersistentType subclass, boolean shouldCreate) {
super();
this.superclass = superclass;
this.subclass = subclass;
this.shouldCreate = shouldCreate;
}
public void execute() {
buildHierarchy(superclass, subclass, shouldCreate);
JavaResourceType jrt = subclass.getJavaResourceType();
jrt.getJavaResourceCompilationUnit().synchronizeWithJavaSource();
}
private void buildHierarchy(PersistentType superclass,
PersistentType subclass, boolean build) {
try {
ICompilationUnit subCU = JPAEditorUtil.getCompilationUnit(subclass);
final Document document = new Document(subCU.getBuffer()
.getContents());
MultiTextEdit edit = new MultiTextEdit();
String str = document.get();
if (build) {
int offset = str.indexOf(subclass.getSimpleName())
+ subclass.getSimpleName().length();
edit.addChild(new InsertEdit(offset, " extends " //$NON-NLS-1$
+ superclass.getSimpleName()));
} else {
int length = ("extends " + superclass.getSimpleName() + " ") //$NON-NLS-1$ //$NON-NLS-2$
.length();
int offset = str.indexOf("extends"); //$NON-NLS-1$
edit.addChild(new DeleteEdit(offset, length));
}
TextEditCopier copier = new TextEditCopier(edit);
TextEdit copy = copier.perform();
subCU.applyTextEdit(copy, new NullProgressMonitor());
} catch (CoreException exception) {
JPADiagramEditorPlugin.logError(exception);
} catch (MalformedTreeException exception) {
JPADiagramEditorPlugin.logError(exception);
}
}
}
|