diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index 88e0c67..d3ffe0f 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -12,4 +12,5 @@ org.eclipse.jdt.ui;bundle-version="3.18.0", org.eclipse.jface;bundle-version="3.16.0", org.eclipse.ui.workbench;bundle-version="3.115.0", - org.eclipse.core.resources;bundle-version="3.13.400" + org.eclipse.core.resources;bundle-version="3.13.400", + org.eclipse.jface.text;bundle-version="3.15.200" diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index bb3f21d..cb64da5 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -76,31 +76,15 @@ return null; } + //���̒��ŕύX���e���쐬���� @Override public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { - // TODO Auto-generated method stub + DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, PushPullRefactoring.NAME); ICompilationUnit[] icus = new ICompilationUnit[1]; -// IResourceVisitor visitor = new IResourceVisitor() { -// @Override -// public boolean visit(IResource resource) throws CoreException { -// // TODO Auto-generated method stub -// if (resource.getType() == IResource.FILE) { -// IJavaElement je = JavaCore.create(resource); -// if (je != null && je.getElementType() == -// IJavaElement.COMPILATION_UNIT) { -// ICompilationUnit icu = (ICompilationUnit)je; -// System.out.println("name:" + icu.getElementName()); -// if (icu.getElementName().equals("A")) { -// icus[0] = icu; -// } -// } -// } -// return false; -// } -// }; + - IWorkspace w = ResourcesPlugin.getWorkspace(); + //IWorkspace w = ResourcesPlugin.getWorkspace(); //w.getRoot().accept(visitor, IContainer.DEPTH_INFINITE, false); SearchPattern pattern = createSearchPattern(); @@ -108,6 +92,7 @@ IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); // IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject }); + //��������v�����m�[�h�ɑ΂��čs���ύX SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { diff --git a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java index f129ac5..aadd956 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java @@ -7,9 +7,27 @@ import org.eclipse.core.commands.IHandler; import org.eclipse.core.commands.IHandlerListener; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.jdt.core.IAnnotation; +import org.eclipse.jdt.core.ICodeAssist; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.ITypeRoot; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.internal.ui.refactoring.RefactoringExecutionHelper; +import org.eclipse.jdt.ui.JavaElementLabels; +import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.text.ITextSelection; import org.eclipse.ltk.core.refactoring.RefactoringCore; import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.handlers.HandlerUtil; public class PushPullRefactoringHandler implements IHandler { @@ -32,6 +50,23 @@ @Override public Object execute(ExecutionEvent event) throws ExecutionException { // TODO Auto-generated method stub + + IType targetType = getTargetType(event); + + + if (targetType == null) { + return null; + } + + RefactoringStatus availability = checkAvailability(targetType); + if (availability.hasError()){ + MessageDialog.openError( + HandlerUtil.getActiveShell(event), + "mi", + "Some Probrems"); + return null; + } + System.out.println("Hello Push-Pull Refactoring!!"); PushPullRefactoringContribution contribution = (PushPullRefactoringContribution)RefactoringCore.getRefactoringContribution(PushPullRefactoring.ID); PushPullDescriptor descriptor = (PushPullDescriptor) contribution.createDescriptor(); @@ -50,7 +85,89 @@ } return null; } + // + private IType getTargetType(ExecutionEvent event) { + IJavaElement targetType = null; + + try { + targetType = codeResolve(event); + } catch (JavaModelException e) { + return null; + } + + if (!(targetType instanceof IType)) { + return null; + } + + return (IType) targetType; + } + + private IJavaElement codeResolve(ExecutionEvent event) throws JavaModelException { + IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event); + ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editorInput); + IAnnotation[] annos = typeRoot.findPrimaryType().getAnnotations(); + if (typeRoot instanceof ICodeAssist) { + if (typeRoot instanceof ICompilationUnit) { + + ((ICompilationUnit) typeRoot).reconcile( + ICompilationUnit.NO_AST, + false /* don't force problem detection */, + null /* use primary owner */, + null /* no progress monitor */); + + + } +// ITextSelection selection = (ITextSelection) HandlerUtil.getActiveMenuSelection(event); + ITextSelection selection = (ITextSelection) HandlerUtil.getCurrentSelection(event); + + IJavaElement[] elements = ((ICodeAssist)typeRoot).codeSelect(selection.getOffset() + selection.getLength(), 0); + ICompilationUnit ic =(ICompilationUnit)elements[0].getAncestor(IJavaElement.COMPILATION_UNIT); + IType type = ic.findPrimaryType(); + IAnnotation annotation = type.getAnnotation(type.getElementName()); + if (elements.length > 0) { + int t = elements[0].getElementType(); + return elements[0]; + } + } + return null; + } + public static ASTNode getASTNode(ICompilationUnit unit) { + ASTParser parser = ASTParser.newParser(AST.JLS4); + parser.setSource(unit); + ASTNode node = parser.createAST(new NullProgressMonitor()); + return node; + } + private IAnnotation[] getValidAnnotations(ExecutionEvent event) throws JavaModelException { + + IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event); + + IEditorPart editor = window.getActivePage().getActiveEditor(); + IJavaElement javaElement = JavaUI.getEditorInputJavaElement(editor.getEditorInput()); + + ICompilationUnit cunit = (ICompilationUnit)javaElement; + IType type = cunit.findPrimaryType(); + IAnnotation[] annotations = type.getAnnotations(); + + return annotations; + } + + private RefactoringStatus checkAvailability(IJavaElement element) { + RefactoringStatus result = new RefactoringStatus(); + if (!element.exists()) { + result.addFatalError(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT) + " does not exist in the model"); + } + + try { + if (element.exists() && !element.isStructureKnown()) { + result.addFatalError(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT) + " - unknown structure"); + } + } catch (JavaModelException e) { + e.printStackTrace(); + } + + return result; + } @Override public boolean isEnabled() { System.out.println(this.getClass().getSimpleName() + "#" + "isEnabled()");