Skip to content

Commit

Permalink
Merge pull request #100 from gamerson/issue-99
Browse files Browse the repository at this point in the history
Issue-99 Added ITernNatureCapability interface
  • Loading branch information
angelozerr committed Jul 7, 2014
2 parents 8de0af7 + f9e9efb commit 33e41d1
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 22 deletions.
16 changes: 13 additions & 3 deletions eclipse/tern.eclipse.ide.core/schema/ternNatureAdapters.exsd
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
</documentation>
</annotation>
</attribute>
<attribute name="class" type="string">
<annotation>
<documentation>
An optional interface to allow extenders to customize the nature mapping mechanism
</documentation>
<appInfo>
<meta.attribute kind="java" basedOn=":tern.eclipse.ide.core.ITernNatureCapability"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>

Expand Down Expand Up @@ -92,21 +102,21 @@
<attribute name="name" type="string" use="required">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="withDependencies" type="boolean">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="class" type="string">
<annotation>
<documentation>

</documentation>
<appInfo>
<meta.attribute kind="java" basedOn=":tern.eclipse.ide.core.ITernModuleConfigurator"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2014 Liferay, Inc.
* 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:
* Gregory Amerson <gregory.amerson@liferay.com> - initial API and implementation
*/
package tern.eclipse.ide.core;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;


/**
* Tern nature adapter, can be used by adopters to provide more
* advanced logic for which projects should be treated as tern projects
*/
public interface ITernNatureCapability {

boolean hasTernNature( IProject project ) throws CoreException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2014 Liferay, Inc.
* 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:
* Gregory Amerson <gregory.amerson@liferay.com> - initial API and implementation
*/
package tern.eclipse.ide.internal.core;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;

import tern.eclipse.ide.core.ITernNatureCapability;


/**
* Provides a default tern adapter since most adapters only need to match ids
*/
public class DefaultTernNatureAdapter implements ITernNatureCapability {

final String natureId;

public DefaultTernNatureAdapter( String id ) {
this.natureId = id;
}

@Override
public boolean hasTernNature( IProject project) throws CoreException {
return project != null && project.hasNature( this.natureId );
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
* Copyright (c) 2013-2014 Angelo ZERR.
* Copyright (c) 2013-2014 Angelo ZERR and Liferay, Inc.
* 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:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
* Gregory Amerson <gregory.amerson@liferay.com> - issue-99 add ITernNatureCapability
*/
package tern.eclipse.ide.internal.core;

Expand All @@ -26,6 +27,7 @@
import org.eclipse.core.runtime.Platform;

import tern.eclipse.ide.core.IDETernProject;
import tern.eclipse.ide.core.ITernNatureCapability;
import tern.eclipse.ide.core.TernCorePlugin;
import tern.eclipse.ide.core.TernNature;
import tern.metadata.TernModuleMetadata;
Expand All @@ -34,7 +36,6 @@
import tern.utils.TernModuleHelper;

import com.eclipsesource.json.JsonObject;

/**
* Manager of tern nature adapters loaded by the extension point
* "ternNatureAdapters"
Expand All @@ -47,7 +48,7 @@ public class TernNatureAdaptersManager implements IRegistryChangeListener {
private static final TernNatureAdaptersManager INSTANCE = new TernNatureAdaptersManager();

// cached copy of all tern nature adapaters
private Map<String, List<DefaultModule>> ternNatureAdapters;
private Map<ITernNatureCapability, List<DefaultModule>> ternNatureAdapters;

private boolean registryListenerIntialized;

Expand Down Expand Up @@ -108,7 +109,7 @@ private synchronized void loadTernNatureAdapters() {
IExtensionRegistry registry = Platform.getExtensionRegistry();
IConfigurationElement[] cf = registry.getConfigurationElementsFor(
TernCorePlugin.PLUGIN_ID, EXTENSION_TERN_NATURE_ADAPTERS);
Map<String, List<DefaultModule>> map = new HashMap<String, List<DefaultModule>>(
Map<ITernNatureCapability, List<DefaultModule>> map = new HashMap<ITernNatureCapability, List<DefaultModule>>(
cf.length);
addTernNatureAdapters(cf, map);
addRegistryListenerIfNeeded();
Expand All @@ -122,17 +123,24 @@ private synchronized void loadTernNatureAdapters() {
* Load the tern project describers.
*/
private synchronized void addTernNatureAdapters(IConfigurationElement[] cf,
Map<String, List<DefaultModule>> map) {
Map<ITernNatureCapability, List<DefaultModule>> map) {
for (IConfigurationElement ce : cf) {
String id = ce.getAttribute("id");
String className = ce.getAttribute("class");
try {
map.put(ce.getAttribute("id"), getDefaultModules(ce));
if (className != null) {
map.put((ITernNatureCapability)ce.createExecutableExtension("class"), getDefaultModules(ce));
}
else if (id != null) {
map.put(new DefaultTernNatureAdapter(id), getDefaultModules(ce));
}
Trace.trace(Trace.EXTENSION_POINT,
" Loaded project describer: " + ce.getAttribute("id"));
" Loaded project describer: " + id != null ? id : className != null ? className : "");
} catch (Throwable t) {
Trace.trace(
Trace.SEVERE,
" Could not load project describers: "
+ ce.getAttribute("id"), t);
+ id != null ? id : className != null ? className : "", t);
}
}
}
Expand Down Expand Up @@ -173,7 +181,7 @@ protected void handleTernNatureAdapterDelta(IExtensionDelta delta) {
IConfigurationElement[] cf = delta.getExtension()
.getConfigurationElements();

Map<String, List<DefaultModule>> map = new HashMap<String, List<DefaultModule>>(
Map<ITernNatureCapability, List<DefaultModule>> map = new HashMap<ITernNatureCapability, List<DefaultModule>>(
ternNatureAdapters);
if (delta.getKind() == IExtensionDelta.ADDED) {
addTernNatureAdapters(cf, map);
Expand Down Expand Up @@ -215,9 +223,9 @@ public boolean hasTernNature(IProject project) {
return true;

// use tern nature adapaters
Map<String, List<DefaultModule>> ternNatureAdapters = getTernNatureAdapters();
for (String adaptToNature : ternNatureAdapters.keySet()) {
if (project.hasNature(adaptToNature)) {
Map<ITernNatureCapability, List<DefaultModule>> ternNatureAdapters = getTernNatureAdapters();
for (ITernNatureCapability natureAdapter : ternNatureAdapters.keySet()) {
if (natureAdapter.hasTernNature(project)) {
return true;
}
}
Expand All @@ -228,7 +236,7 @@ public boolean hasTernNature(IProject project) {
return false;
}

private Map<String, List<DefaultModule>> getTernNatureAdapters() {
private Map<ITernNatureCapability, List<DefaultModule>> getTernNatureAdapters() {
if (ternNatureAdapters == null) {
loadTernNatureAdapters();
}
Expand All @@ -237,17 +245,16 @@ private Map<String, List<DefaultModule>> getTernNatureAdapters() {

/**
* Add default modules for the given tern project.
*
*
* @param project
* tern project
* @throws CoreException
*/
public void addDefaultModules(IDETernProject project) throws CoreException {
Map<String, List<DefaultModule>> ternNatureAdapters = getTernNatureAdapters();
for (String natureId : ternNatureAdapters.keySet()) {
if (project.getProject().hasNature(natureId)) {
List<DefaultModule> defaultModules = ternNatureAdapters
.get(natureId);
Map<ITernNatureCapability, List<DefaultModule>> ternNatureAdapters = getTernNatureAdapters();
for (ITernNatureCapability natureAdapter : ternNatureAdapters.keySet()) {
if (natureAdapter.hasTernNature(project.getProject())) {
List<DefaultModule> defaultModules = ternNatureAdapters.get(natureAdapter);
for (DefaultModule defaultModule : defaultModules) {
ITernModule module = TernCorePlugin
.getTernServerTypeManager().findTernModule(
Expand Down

0 comments on commit 33e41d1

Please sign in to comment.