]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/javaeditor/ClassFileEditorInputFactory.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / javaeditor / ClassFileEditorInputFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2009 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.javaeditor;
12
13
14 import org.eclipse.core.runtime.IAdaptable;
15
16 import org.eclipse.ui.IElementFactory;
17 import org.eclipse.ui.IMemento;
18
19 import org.eclipse.jdt.core.IClassFile;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IType;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.jdt.core.JavaModelException;
25
26 /**
27  * The factory which is capable of recreating class file editor
28  * inputs stored in a memento.
29  */
30 public class ClassFileEditorInputFactory implements IElementFactory {
31
32         public final static String ID=  "org.eclipse.jdt.ui.ClassFileEditorInputFactory"; //$NON-NLS-1$
33         public final static String KEY= "org.eclipse.jdt.ui.ClassFileIdentifier"; //$NON-NLS-1$
34
35         public ClassFileEditorInputFactory() {
36         }
37
38         /*
39          * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
40          */
41         public IAdaptable createElement(IMemento memento) {
42                 String identifier= memento.getString(KEY);
43                 if (identifier == null)
44                         return null;
45
46                 IJavaElement element= JavaCore.create(identifier);
47                 try {
48                         if (!element.exists() && element instanceof IClassFile) {
49                                 /*
50                                  * Let's try to find the class file,
51                                  * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=83221
52                                  */
53                                 IClassFile cf= (IClassFile)element;
54                                 IType type= cf.getType();
55                                 IJavaProject project= element.getJavaProject();
56                                 if (project != null) {
57                                         type= project.findType(type.getFullyQualifiedName());
58                                         if (type == null)
59                                                 return null;
60                                         element= type.getParent();
61                                 }
62                         }
63                         return EditorUtility.getEditorInput(element);
64                 } catch (JavaModelException x) {
65                         // Don't report but simply return null
66                         return null;
67                 }
68         }
69
70         public static void saveState(IMemento memento, InternalClassFileEditorInput input) {
71                 IClassFile c= input.getClassFile();
72                 memento.putString(KEY, c.getHandleIdentifier());
73         }
74 }