KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > ClassFileEditorInputFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.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 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
27
28 /**
29  * The factory which is capable of recreating class file editor
30  * inputs stored in a memento.
31  */

32 public class ClassFileEditorInputFactory implements IElementFactory {
33
34     public final static String JavaDoc ID= "org.eclipse.jdt.ui.ClassFileEditorInputFactory"; //$NON-NLS-1$
35
public final static String JavaDoc KEY= "org.eclipse.jdt.ui.ClassFileIdentifier"; //$NON-NLS-1$
36

37     public ClassFileEditorInputFactory() {
38     }
39
40     /**
41      * @see IElementFactory#createElement
42      */

43     public IAdaptable createElement(IMemento memento) {
44         String JavaDoc identifier= memento.getString(KEY);
45         if (identifier == null)
46             return null;
47             
48         IJavaElement element= JavaCore.create(identifier);
49         try {
50             if (!element.exists() && element instanceof IClassFile) {
51                 /*
52                  * Let's try to find the class file,
53                  * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=83221
54                  */

55                 IClassFile cf= (IClassFile)element;
56                 IType type= cf.getType(); // this will work, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=154667
57
IJavaProject project= element.getJavaProject();
58                 if (project != null) {
59                     type= JavaModelUtil.findType(project, type.getFullyQualifiedName());
60                     if (type == null)
61                         return null;
62                     element= type.getParent();
63                 }
64             }
65             return EditorUtility.getEditorInput(element);
66         } catch (JavaModelException x) {
67             // Don't report but simply return null
68
return null;
69         }
70     }
71
72     public static void saveState(IMemento memento, InternalClassFileEditorInput input) {
73         IClassFile c= input.getClassFile();
74         memento.putString(KEY, c.getHandleIdentifier());
75     }
76 }
77
Popular Tags