Skip to content

[NETBEANS-1802] Basic infrastructure to load a project with a Project Type #1403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.netbeans.modules.gradle;

import java.io.File;
Expand All @@ -42,7 +41,11 @@ public final class NbGradleProjectFactory implements ProjectFactory2 {

@Override
public ProjectManager.Result isProject2(FileObject dir) {
return isProject(dir) ? new ProjectManager.Result(NbGradleProject.getIcon()) : null;
return isProject(dir) ? new ProjectManager.Result(
dir.getName(),
NbGradleProject.GRADLE_PROJECT_TYPE,
NbGradleProject.getIcon())
: null;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion ide/projectapi.nb/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
# specific language governing permissions and limitations
# under the License.
is.eager=true
javac.source=1.6
javac.source=1.8
javac.compilerargs=-Xlint -Xlint:-serial

Large diffs are not rendered by default.

38 changes: 37 additions & 1 deletion ide/projectapi/src/org/netbeans/api/project/ProjectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.netbeans.spi.project.FileOwnerQueryImplementation;
import org.netbeans.spi.project.ProjectFactory;
import org.netbeans.spi.project.ProjectManagerImplementation;
import org.netbeans.spi.project.ProjectManagerImplementation2;
import org.openide.filesystems.FileObject;
import org.openide.util.Lookup;
import org.openide.util.Mutex;
Expand Down Expand Up @@ -207,7 +208,42 @@ public Result isProject2(@NonNull final FileObject projectDirectory) throws Ille
}
return impl.isProject(projectDirectory);
}


/**
* Returns all {@link ProjectManager.Result} that can be associated with
* the given folder. If the actual implementation does not support
* {@link ProjectManagerImplementation2} this method just fallback to
* {@link ProjectManagerImplementation#isProject(org.openide.filesystems.FileObject)} method.
*
* @since 1.73
* @param projectDirectory the folder for inspection
* @return {@link ProjectManager.Result} that can be associated with the
* folder or an empty array if none has found.
* @throws IllegalArgumentException
*/
public Result[] checkProject(@NonNull final FileObject projectDirectory) throws IllegalArgumentException {
if (projectDirectory == null) {
throw new IllegalArgumentException("Attempted to pass a null directory to isProject"); // NOI18N
}
if (!projectDirectory.isFolder() ) {
//#78215 it can happen that a no longer existing folder is queried. throw
// exception only for real wrong usage..
if (projectDirectory.isValid()) {
throw new IllegalArgumentException("Attempted to pass a non-directory to isProject: " + projectDirectory); // NOI18N
} else {
return null;
}
}
Result[] ret = new Result[0];
if (impl instanceof ProjectManagerImplementation2) {
ret = ((ProjectManagerImplementation2) impl).checkProject(projectDirectory);
} else {
Result p = impl.isProject(projectDirectory);
ret = p != null ? new Result[] {p} : ret;
}
return ret;
}

/**
* Clear the cached list of folders thought <em>not</em> to be projects.
* This may be useful after creating project metadata in a folder, etc.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.spi.project;

import java.io.IOException;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectManager;
import org.openide.filesystems.FileObject;

/**
* Trivial extension of {@link ProjectManagerImplementation} to support access
* to multiple project types per project directories.
*
* @since 1.73
* @author lkishalmi
*/
public interface ProjectManagerImplementation2 extends ProjectManagerImplementation {
/**
* Find a NetBeans {@link Project} for the given directory and project type.
*
* @param projectDirectory the project folder
* @param projectType the project type to load. This can be {@code null}, that
* case the first identified project shall be used and the call
* is equivalent with {@link #findProject(org.openide.filesystems.FileObject)}.
* @see ProjectManager.Result#getProjectType()
* @return a project with the requested type or {@code null}.
* @throws IOException
* @throws IllegalArgumentException
*/
@CheckForNull
Project findProject(@NonNull FileObject projectDirectory, String projectType) throws IOException, IllegalArgumentException;

/**
* Returns all {@link ProjectManager.Result} that can be associated with
* the given folder.
*
* @since 1.73
* @param projectDirectory the folder for inspection
* @return {@link ProjectManager.Result} that can be associated with the
* folder or an empty array if none has found.
* @throws IllegalArgumentException
*/
ProjectManager.Result[] checkProject(@NonNull FileObject projectDirectory) throws IllegalArgumentException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.project.ui.actions;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import org.netbeans.api.project.ProjectManager;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.util.ContextAwareAction;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;

import static org.netbeans.modules.project.ui.actions.Bundle.*;
import org.openide.awt.DynamicMenuContent;
import org.openide.loaders.DataFolder;
import org.openide.util.actions.Presenter;

/**
*
* @author lkishalmi
*/
@ActionID(id = "org.netbeans.modules.project.ui.actions.OpenProjectAsFolderAction", category = "Project")
@ActionRegistration(displayName = "#OpenProjectAsFolderAction.LBL_action", lazy=false)
@ActionReference(path = "Loaders/folder/any/Actions", position = 101)
@NbBundle.Messages("OpenProjectAsFolderAction.LBL_action=Open Project of Folder")
public class OpenProjectAsFolderAction extends AbstractAction implements ContextAwareAction {

public OpenProjectAsFolderAction() {
super(OpenProjectAsFolderAction_LBL_action());
}

public @Override void actionPerformed(ActionEvent e) {
// Cannot be invoked without any context.
assert false;
}

@Override
public Action createContextAwareInstance(Lookup ctx) {
return new CustomPopupAction(ctx);
}

private final class OpenAsProjectAction extends AbstractAction {

@Override
public void actionPerformed(ActionEvent e) {

}

}

private final class CustomPopupAction extends AbstractAction implements Presenter.Popup {

private final Lookup context;
private ProjectManager.Result[] projects;

public CustomPopupAction(Lookup context) {
super(OpenProjectAsFolderAction_LBL_action());
this.context = context;

projects = new ProjectManager.Result[0];
for (DataFolder d : context.lookupAll(DataFolder.class)) {
projects = ProjectManager.getDefault().checkProject(d.getPrimaryFile());
if (projects.length > 0) {
break;
}
}
if (projects.length <= 1) {
putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
setEnabled(false);
}
}

@Override
public void actionPerformed(ActionEvent e) {
}

@Override
public JMenuItem getPopupPresenter() {
if (projects.length <=1) return null;

final JMenu menu = new JMenu(OpenProjectAsFolderAction_LBL_action());

for (ProjectManager.Result project : projects) {
if (project.getProjectType() != null) {
JMenuItem item = new JMenuItem("Open as: ");
item.setText(project.getProjectType());
menu.add(item);
}
}
return menu;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ private static final class ContextAction extends AbstractAction {
public ContextAction(Lookup context) {
super(OpenProjectFolderAction_LBL_action());
this.context = context;
boolean foundProject = false;
ProjectManager.Result[] projects = new ProjectManager.Result[0];
for (DataFolder d : context.lookupAll(DataFolder.class)) {
if (ProjectManager.getDefault().isProject(d.getPrimaryFile())) {
foundProject = true;
projects = ProjectManager.getDefault().checkProject(d.getPrimaryFile());
if (projects.length > 0) {
break;
}
}
if (!foundProject) {
if (projects.length != 1) {
putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
setEnabled(false);
}
Expand Down