KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > compare > JavaElementHistoryPageSource


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.jdt.internal.ui.compare;
12
13 import org.eclipse.core.resources.IFile;
14
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IMember;
18
19 import org.eclipse.team.ui.history.ElementLocalHistoryPageSource;
20
21 public class JavaElementHistoryPageSource extends ElementLocalHistoryPageSource {
22     
23     private static JavaElementHistoryPageSource instance;
24
25     public static JavaElementHistoryPageSource getInstance() {
26         if (instance == null)
27             instance = new JavaElementHistoryPageSource();
28         return instance;
29     }
30     
31     /**
32      * Returns true if the given IJavaElement maps to a JavaNode.
33      * The JavaHistoryAction uses this function to determine whether
34      * a selected Java element can be replaced by some piece of
35      * code from the local history.
36      */

37     public static boolean hasEdition(IJavaElement je) {
38
39         if (je instanceof IMember && ((IMember)je).isBinary())
40             return false;
41             
42         switch (je.getElementType()) {
43         case IJavaElement.COMPILATION_UNIT:
44         case IJavaElement.TYPE:
45         case IJavaElement.FIELD:
46         case IJavaElement.METHOD:
47         case IJavaElement.INITIALIZER:
48         case IJavaElement.PACKAGE_DECLARATION:
49         case IJavaElement.IMPORT_CONTAINER:
50         case IJavaElement.IMPORT_DECLARATION:
51             return true;
52         }
53         return false;
54     }
55
56     /* (non-Javadoc)
57      * @see org.eclipse.team.ui.history.ElementLocalHistoryPageSource#getFile(java.lang.Object)
58      */

59     public IFile getFile(Object JavaDoc input) {
60         // extract CU from input
61
ICompilationUnit cu= null;
62         if (input instanceof ICompilationUnit)
63             cu= (ICompilationUnit) input;
64         else if (input instanceof IMember)
65             cu= ((IMember)input).getCompilationUnit();
66             
67         if (cu == null || !cu.exists())
68             return null;
69             
70         // get to original CU
71
cu= cu.getPrimary();
72             
73         // find underlying file
74
IFile file= (IFile) cu.getResource();
75         if (file != null && file.exists())
76             return file;
77         return null;
78     }
79 }
80
Popular Tags