Skip to main content
summaryrefslogtreecommitdiffstats
blob: f17567c28cb598b44b4d6377c1b4789da3f0ed07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*******************************************************************************
 * Copyright (c) 2007, 2009 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.jpa.ui.internal;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jpt.jpa.core.JpaFile;
import org.eclipse.jpt.jpa.core.JptJpaCorePlugin;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;

/**
 * Factory to build adapters for a editor part:
 *   - JPA file (if the editor part is a file editor etc.)
 * 
 * See org.eclipse.jpt.jpa.ui plugin.xml.
 */
public class EditorPartAdapterFactory
	implements IAdapterFactory
{
	private static final Class<?>[] ADAPTER_LIST = new Class[] { JpaFile.class };

	public Class<?>[] getAdapterList() {
		return ADAPTER_LIST;
	}

	public Object getAdapter(Object adaptableObject, @SuppressWarnings("unchecked") Class adapterType) {
		if (adaptableObject instanceof IEditorPart) {
			return this.getAdapter((IEditorPart) adaptableObject, adapterType);
		}
		return null;
	}

	private Object getAdapter(IEditorPart editorPart, Class<?> adapterType) {
		if (adapterType == JpaFile.class) {
			return this.getJpaFile(editorPart);
		}
		return null;
	}

	private JpaFile getJpaFile(IEditorPart editorPart) {
		IEditorInput editorInput = editorPart.getEditorInput();
		if (editorInput instanceof IFileEditorInput) {
			return this.getJpaFile((IFileEditorInput) editorInput);
		}
		return null;
	}

	private JpaFile getJpaFile(IFileEditorInput fileEditorInput) {
		return this.getJpaFile(fileEditorInput.getFile());
	}

	private JpaFile getJpaFile(IFile file) {
		return JptJpaCorePlugin.getJpaFile(file);
	}

}

Back to the top