Skip to main content
summaryrefslogtreecommitdiffstats
blob: b96e14324cdaed2211482f703b7c2e4c365dac33 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 * Copyright (c) 2005, 2013 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.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jpt.common.utility.internal.filter.SimpleFilter;
import org.eclipse.jpt.common.utility.internal.model.value.ElementPropertyValueModelAdapter;
import org.eclipse.jpt.common.utility.internal.model.value.TransformationPropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.CollectionValueModel;
import org.eclipse.jpt.jpa.core.JpaProject;
import org.eclipse.jpt.jpa.core.context.JpaRootContextNode;
import org.eclipse.jpt.jpa.ui.JpaProjectModel;
import org.eclipse.jpt.jpa.ui.JpaProjectsModel;
import org.eclipse.jpt.jpa.ui.JpaRootContextNodeModel;

/**
 * Factory to build Dali adapters for an {@link IProject}:<ul>
 * <li>{@link org.eclipse.jpt.jpa.ui.JpaProjectModel JpaProjectModel} -
 *     This adapter will only return a JPA project if it is immediately
 *     available; but it will also notify listeners if the JPA project is
 *     ever created.
 *     This adapter should be used by any process that can temporarily ignore
 *     any uncreated JPA projects but should be notified if the JPA project
 *     <em>is</em> ever created (e.g. UI views).
 * <li>{@link org.eclipse.jpt.jpa.ui.JpaRootContextNodeModel JpaRootContextNodeModel} -
 *     This adapter is much like the {@link org.eclipse.jpt.jpa.ui.JpaProjectModel JpaProjectModel}
 *     adapter described above.
 * </ul>
 * See <code>org.eclipse.jpt.jpa.ui/plugin.xml:org.eclipse.core.runtime.adapters</code>.
 */
public class ProjectAdapterFactory
	implements IAdapterFactory
{
	private static final Class<?>[] ADAPTER_LIST = new Class[] {
			JpaProjectModel.class,
			JpaRootContextNodeModel.class
		};

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

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

	private Object getAdapter(IProject project, Class<?> adapterType) {
		if (adapterType == JpaProjectModel.class) {
			return this.getJpaProjectModel(project);
		}
		if (adapterType == JpaRootContextNodeModel.class) {
			return this.getJpaRootContextNodeModel(project);
		}
		return null;
	}

	private JpaProjectModel getJpaProjectModel(IProject project) {
		return new JpaProjectModelAdapter(this.getJpaProjectsModel(project.getWorkspace()), project);
	}

	private JpaProjectsModel getJpaProjectsModel(IWorkspace workspace) {
		return (JpaProjectsModel) workspace.getAdapter(JpaProjectsModel.class);
	}

	private JpaRootContextNodeModel getJpaRootContextNodeModel(IProject project) {
		return new JpaRootContextNodeModelAdapter(this.getJpaProjectModel(project));
	}


	// ********** JPA project model **********

	/**
	 * Implement a property value model for the JPA project corresponding to a
	 * {@link IProject project}. The model will fire change events when the
	 * corresponding JPA project is added or removed from the JPA project
	 * manager. This is useful for UI code that does not want to wait to
	 * retrieve a JPA project but wants to be notified when it is available.
	 * <p>
	 * Subclass {@link ElementPropertyValueModelAdapter} so we can
	 * implement {@link org.eclipse.jpt.jpa.ui.JpaProjectModel}.
	 * <p>
	 * <strong>NB:</strong> This model operates outside of all the other
	 * activity synchronized by the JPA project manager; but that should be OK
	 * since it will be kept synchronized with the JPA manager's collection of
	 * JPA projects in the end.
	 */
	/* CU private */ static class JpaProjectModelAdapter
		extends ElementPropertyValueModelAdapter<JpaProject>
		implements JpaProjectModel
	{
		JpaProjectModelAdapter(CollectionValueModel<JpaProject> jpaProjectsModel, IProject project) {
			super(jpaProjectsModel, new Predicate(project));
		}

		public IProject getProject() {
			return ((Predicate) this.predicate).getCriterion();
		}

		/* class private */ static class Predicate
			extends SimpleFilter<JpaProject, IProject>
		{
			private static final long serialVersionUID = 1L;
			Predicate(IProject project) {
				super(project);
			}
			@Override
			public boolean accept(JpaProject jpaProject) {
				return jpaProject.getProject().equals(this.criterion);
			}
		}
	}


	// ********** JPA root context node model **********

	/**
	 * Implement a property value model for the JPA root context node
	 * corresponding to a {@link JpaProject JPA project}. The model will fire
	 * change events when the corresponding JPA project is added or removed
	 * from the JPA project manager. This is useful for UI code that does not
	 * want to wait to retrieve a JPA root context node but wants to be notified
	 * when it is available.
	 * <p>
	 * <strong>NB:</strong> This model operates outside of all the other
	 * activity synchronized by the JPA project manager; but that should be OK
	 * since it will be kept synchronized with the JPA manager's collection of
	 * JPA projects in the end.
	 */
	/* CU private */ static class JpaRootContextNodeModelAdapter
		extends TransformationPropertyValueModel<JpaProject, JpaRootContextNode>
		implements JpaRootContextNodeModel
	{
		JpaRootContextNodeModelAdapter(JpaProjectModel jpaProjectsModel) {
			super(jpaProjectsModel, JpaProject.ROOT_CONTEXT_NODE_TRANSFORMER);
		}

		public IProject getProject() {
			return ((JpaProjectModel) this.valueModel).getProject();
		}
	}
}

Back to the top