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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.javaeditor;
14 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.ui.IElementFactory;
17 import org.eclipse.ui.IMemento;
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;
27 * The factory which is capable of recreating class file editor
28 * inputs stored in a memento.
30 public class ClassFileEditorInputFactory implements IElementFactory {
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$
35 public ClassFileEditorInputFactory() {
39 * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
41 public IAdaptable createElement(IMemento memento) {
42 String identifier= memento.getString(KEY);
43 if (identifier == null)
46 IJavaElement element= JavaCore.create(identifier);
48 if (!element.exists() && element instanceof IClassFile) {
50 * Let's try to find the class file,
51 * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=83221
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());
60 element= type.getParent();
63 return EditorUtility.getEditorInput(element);
64 } catch (JavaModelException x) {
65 // Don't report but simply return null
70 public static void saveState(IMemento memento, InternalClassFileEditorInput input) {
71 IClassFile c= input.getClassFile();
72 memento.putString(KEY, c.getHandleIdentifier());