new framework detection: editor for excludes

This commit is contained in:
nik
2011-08-08 13:18:52 +04:00
parent 59620fbdec
commit 840db70be6
12 changed files with 489 additions and 17 deletions
@@ -44,7 +44,7 @@ import java.util.List;
* @author nik
*/
class AddFacetOfTypeAction extends DumbAwareAction {
private FacetStructureConfigurable myFacetStructureConfigurable;
private final FacetStructureConfigurable myFacetStructureConfigurable;
AddFacetOfTypeAction(FacetStructureConfigurable facetStructureConfigurable) {
super("New Facet", null, PlatformIcons.ADD_ICON);
@@ -0,0 +1,44 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed 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 com.intellij.openapi.roots.ui.configuration.projectRoot;
import com.intellij.framework.detection.DetectionExcludesConfiguration;
import com.intellij.framework.detection.impl.exclude.DetectionExcludesConfigurable;
import com.intellij.framework.detection.impl.exclude.DetectionExcludesConfigurationImpl;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NotNull;
/**
* @author nik
*/
public class EditFrameworkDetectionExcludesAction extends DumbAwareAction {
private final Project myProject;
public EditFrameworkDetectionExcludesAction(@NotNull Project project) {
super("Edit Detection Excludes...", null, PlatformIcons.EDIT);
myProject = project;
}
@Override
public void actionPerformed(AnActionEvent e) {
final DetectionExcludesConfigurationImpl configuration = (DetectionExcludesConfigurationImpl)DetectionExcludesConfiguration.getInstance(myProject);
ShowSettingsUtil.getInstance().editConfigurable(myProject, new DetectionExcludesConfigurable(myProject, configuration));
}
}
@@ -187,6 +187,8 @@ public class FacetStructureConfigurable extends BaseStructureConfigurable {
actions.add(new MyRemoveAction());
actions.add(Separator.getInstance());
addCollapseExpandActions(actions);
actions.add(Separator.getInstance());
actions.add(new EditFrameworkDetectionExcludesAction(myProject));
return actions;
}
@@ -15,6 +15,7 @@
*/
package com.intellij.framework.detection;
import com.intellij.framework.FrameworkType;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.vfs.VirtualFile;
@@ -30,15 +31,15 @@ import java.util.List;
*/
public abstract class FrameworkDetector {
public static final ExtensionPointName<FrameworkDetector> EP_NAME = ExtensionPointName.create("com.intellij.framework.detector");
private final String myFrameworkTypeId;
private final String myDetectorId;
private final int myDetectorVersion;
protected FrameworkDetector(String frameworkTypeId) {
this(frameworkTypeId, 0);
protected FrameworkDetector(String detectorId) {
this(detectorId, 0);
}
protected FrameworkDetector(@NotNull String frameworkTypeId, int detectorVersion) {
myFrameworkTypeId = frameworkTypeId;
protected FrameworkDetector(@NotNull String detectorId, int detectorVersion) {
myDetectorId = detectorId;
myDetectorVersion = detectorVersion;
}
@@ -52,11 +53,13 @@ public abstract class FrameworkDetector {
@NotNull FrameworkDetectionContext context);
@NotNull
public final String getFrameworkTypeId() {
return myFrameworkTypeId;
public final String getDetectorId() {
return myDetectorId;
}
public final int getDetectorVersion() {
return myDetectorVersion;
}
public abstract FrameworkType getFrameworkType();
}
@@ -157,7 +157,7 @@ public class FrameworkDetectionManager extends AbstractProjectComponent implemen
if (detector != null) {
excludesConfiguration.removeExcluded(newFiles, detector);
if (LOG.isDebugEnabled()) {
LOG.debug("Detector '" + detector.getFrameworkTypeId() + "': " + files.size() + " accepted files, " + newFiles.size() + " files to process");
LOG.debug("Detector '" + detector.getDetectorId() + "': " + files.size() + " accepted files, " + newFiles.size() + " files to process");
}
final List<? extends DetectedFrameworkDescription> frameworks;
if (!newFiles.isEmpty()) {
@@ -15,6 +15,7 @@
*/
package com.intellij.framework.detection.impl;
import com.intellij.framework.FrameworkType;
import com.intellij.framework.detection.FrameworkDetector;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.fileTypes.FileType;
@@ -22,6 +23,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.List;
/**
* @author nik
@@ -31,6 +33,13 @@ public abstract class FrameworkDetectorRegistry {
return ServiceManager.getService(FrameworkDetectorRegistry.class);
}
@Nullable
public abstract FrameworkType findFrameworkType(@NotNull String typeId);
@NotNull
public abstract List<? extends FrameworkType> getFrameworkTypes();
public abstract int getDetectorsVersion();
public abstract int getDetectorId(@NotNull FrameworkDetector detector);
@@ -15,6 +15,7 @@
*/
package com.intellij.framework.detection.impl;
import com.intellij.framework.FrameworkType;
import com.intellij.framework.detection.FrameworkDetector;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
@@ -47,7 +48,7 @@ public class FrameworkDetectorRegistryImpl extends FrameworkDetectorRegistry {
private void loadDetectors() {
Map<String, FrameworkDetector> newDetectors = new HashMap<String, FrameworkDetector>();
for (FrameworkDetector detector : FrameworkDetector.EP_NAME.getExtensions()) {
newDetectors.put(detector.getFrameworkTypeId(), detector);
newDetectors.put(detector.getDetectorId(), detector);
}
myDetectorIds = new TObjectIntHashMap<String>();
@@ -106,10 +107,10 @@ public class FrameworkDetectorRegistryImpl extends FrameworkDetectorRegistry {
myDetectorById = new TIntObjectHashMap<FrameworkDetector>();
myDetectorsByFileType = new MultiMap<FileType, Integer>();
for (FrameworkDetector detector : FrameworkDetector.EP_NAME.getExtensions()) {
final int id = myDetectorIds.get(detector.getFrameworkTypeId());
final int id = myDetectorIds.get(detector.getDetectorId());
myDetectorsByFileType.putValue(detector.getFileType(), id);
myDetectorById.put(id, detector);
LOG.debug("'" + detector.getFrameworkTypeId() + "' framework detector: id = " + id);
LOG.debug("'" + detector.getDetectorId() + "' framework detector: id = " + id);
}
}
@@ -124,8 +125,8 @@ public class FrameworkDetectorRegistryImpl extends FrameworkDetectorRegistry {
final FrameworkDetector[] detectors = FrameworkDetector.EP_NAME.getExtensions();
output.writeInt(detectors.length);
for (FrameworkDetector detector : detectors) {
output.writeUTF(detector.getFrameworkTypeId());
output.writeInt(myDetectorIds.get(detector.getFrameworkTypeId()));
output.writeUTF(detector.getDetectorId());
output.writeInt(myDetectorIds.get(detector.getDetectorId()));
output.writeInt(detector.getDetectorVersion());
}
}
@@ -146,6 +147,26 @@ public class FrameworkDetectorRegistryImpl extends FrameworkDetectorRegistry {
return PathManager.getSystemPath() + File.separator + "frameworks" + File.separator + "detection";
}
@Override
public FrameworkType findFrameworkType(@NotNull String typeId) {
for (FrameworkType type : getFrameworkTypes()) {
if (typeId.equals(type.getId())) {
return type;
}
}
return null;
}
@NotNull
@Override
public List<? extends FrameworkType> getFrameworkTypes() {
final List<FrameworkType> types = new ArrayList<FrameworkType>();
for (FrameworkDetector detector : FrameworkDetector.EP_NAME.getExtensions()) {
types.add(detector.getFrameworkType());
}
return types;
}
@Override
public int getDetectorsVersion() {
return myDetectorsVersion;
@@ -153,7 +174,7 @@ public class FrameworkDetectorRegistryImpl extends FrameworkDetectorRegistry {
@Override
public int getDetectorId(@NotNull FrameworkDetector detector) {
return myDetectorIds.get(detector.getFrameworkTypeId());
return myDetectorIds.get(detector.getDetectorId());
}
@Override
@@ -0,0 +1,222 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed 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 com.intellij.framework.detection.impl.exclude;
import com.intellij.framework.FrameworkType;
import com.intellij.framework.detection.impl.FrameworkDetectorRegistry;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.SortedListModel;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.components.JBList;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @author nik
*/
public class DetectionExcludesConfigurable implements Configurable {
private final Project myProject;
private final DetectionExcludesConfigurationImpl myConfiguration;
private SortedListModel<ExcludeListItem> myModel;
private JPanel myMainPanel;
public DetectionExcludesConfigurable(@NotNull Project project, @NotNull DetectionExcludesConfigurationImpl configuration) {
myProject = project;
myConfiguration = configuration;
myModel = new SortedListModel<ExcludeListItem>(ExcludeListItem.COMPARATOR);
}
@Nls
@Override
public JComponent createComponent() {
myMainPanel = new JPanel(new BorderLayout());
final JBList excludesList = new JBList(myModel);
excludesList.setCellRenderer(new ColoredListCellRenderer() {
@Override
protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) {
if (value instanceof ExcludeListItem) {
((ExcludeListItem)value).renderItem(this);
}
}
});
final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(excludesList)
.disableUpAction().disableDownAction()
.setAddAction(new Runnable() {
@Override
public void run() {
doAddAction();
}
});
myMainPanel.add(decorator.createPanel());
return myMainPanel;
}
private void doAddAction() {
final List<FrameworkType> types = new ArrayList<FrameworkType>();
types.addAll(FrameworkDetectorRegistry.getInstance().getFrameworkTypes());
Collections.sort(types, new Comparator<FrameworkType>() {
@Override
public int compare(FrameworkType o1, FrameworkType o2) {
return o1.getPresentableName().compareToIgnoreCase(o2.getPresentableName());
}
});
types.add(0, null);
JBPopupFactory.getInstance().createListPopup(new BaseListPopupStep<FrameworkType>("Select Framework", types) {
@Override
public Icon getIconFor(FrameworkType value) {
return value != null ? value.getIcon() : null;
}
@NotNull
@Override
public String getTextFor(FrameworkType value) {
return value != null ? value.getPresentableName() : "All Frameworks";
}
@Override
public boolean hasSubstep(FrameworkType selectedValue) {
return selectedValue != null;
}
@Override
public PopupStep onChosen(final FrameworkType frameworkType, boolean finalChoice) {
if (frameworkType == null) {
return doFinalStep(new Runnable() {
@Override
public void run() {
chooseDirectoryAndAdd(null);
}
});
}
else {
return addExcludedFramework(frameworkType);
}
}
}).showInCenterOf(myMainPanel);
}
private PopupStep addExcludedFramework(final @NotNull FrameworkType frameworkType) {
final String projectItem = "In the whole project";
return new BaseListPopupStep<String>(null, new String[]{projectItem, "In directory..."}) {
@Override
public PopupStep onChosen(String selectedValue, boolean finalChoice) {
if (selectedValue.equals(projectItem)) {
myModel.add(new ValidExcludeListItem(frameworkType, null));
return FINAL_CHOICE;
}
else {
return doFinalStep(new Runnable() {
@Override
public void run() {
chooseDirectoryAndAdd(frameworkType);
}
});
}
}
};
}
private void chooseDirectoryAndAdd(final @Nullable FrameworkType type) {
final VirtualFile[] files = FileChooser.chooseFiles(myMainPanel, FileChooserDescriptorFactory.createSingleFolderDescriptor(), myProject.getBaseDir());
final VirtualFile file = files.length > 0 ? files[0] : null;
if (file != null) {
myModel.add(new ValidExcludeListItem(type, file));
}
}
@Override
public boolean isModified() {
return !computeState().equals(myConfiguration.getState());
}
@Override
public void apply() {
myConfiguration.loadState(computeState());
}
private ExcludesConfigurationState computeState() {
final ExcludesConfigurationState state = new ExcludesConfigurationState();
for (ExcludeListItem item : myModel.getItems()) {
final String url = item.getFileUrl();
final String typeId = item.getFrameworkTypeId();
if (url == null) {
state.getFrameworkTypes().add(typeId);
}
else {
state.getFiles().add(new ExcludedFileState(url, typeId));
}
}
return state;
}
@Override
public void reset() {
myModel.clear();
final ExcludesConfigurationState state = myConfiguration.getState();
for (String typeId : state.getFrameworkTypes()) {
final FrameworkType frameworkType = FrameworkDetectorRegistry.getInstance().findFrameworkType(typeId);
myModel.add(frameworkType != null ? new ValidExcludeListItem(frameworkType, null) : new InvalidExcludeListItem(typeId, null));
}
for (ExcludedFileState fileState : state.getFiles()) {
VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(fileState.getUrl());
final String typeId = fileState.getFrameworkType();
if (typeId == null) {
myModel.add(file != null ? new ValidExcludeListItem(null, file) : new InvalidExcludeListItem(null, fileState.getUrl()));
}
else {
final FrameworkType frameworkType = FrameworkDetectorRegistry.getInstance().findFrameworkType(typeId);
myModel.add(frameworkType != null && file != null? new ValidExcludeListItem(frameworkType, file) : new InvalidExcludeListItem(typeId, fileState.getUrl()));
}
}
}
@Override
public void disposeUIResources() {
}
@Nls
@Override
public String getDisplayName() {
return "Framework Detection Excludes";
}
@Override
public Icon getIcon() {
return null;
}
@Override
public String getHelpTopic() {
return null;
}
}
@@ -114,7 +114,7 @@ public class DetectionExcludesConfigurationImpl extends DetectionExcludesConfigu
@Override
public void removeExcluded(@NotNull Collection<VirtualFile> files, @NotNull FrameworkDetector detector) {
if (myExcludedFrameworks.contains(detector.getFrameworkTypeId())) {
if (myExcludedFrameworks.contains(detector.getFrameworkType().getId())) {
files.clear();
return;
}
@@ -122,7 +122,7 @@ public class DetectionExcludesConfigurationImpl extends DetectionExcludesConfigu
final Iterator<VirtualFile> iterator = files.iterator();
while (iterator.hasNext()) {
VirtualFile file = iterator.next();
if (isFileExcluded(file, detector.getFrameworkTypeId())) {
if (isFileExcluded(file, detector.getFrameworkType().getId())) {
iterator.remove();
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed 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 com.intellij.framework.detection.impl.exclude;
import com.intellij.openapi.util.Comparing;
import com.intellij.ui.ColoredListCellRenderer;
import org.jetbrains.annotations.Nullable;
import java.util.Comparator;
/**
* @author nik
*/
abstract class ExcludeListItem {
public static final Comparator<ExcludeListItem> COMPARATOR = new Comparator<ExcludeListItem>() {
@Override
public int compare(ExcludeListItem o1, ExcludeListItem o2) {
return Comparing.comparePairs(o1.getPresentableFrameworkName(), o1.getFileUrl(), o2.getPresentableFrameworkName(), o2.getFileUrl());
}
};
public abstract void renderItem(ColoredListCellRenderer renderer);
@Nullable
public abstract String getPresentableFrameworkName();
@Nullable
public abstract String getFrameworkTypeId();
@Nullable
public abstract String getFileUrl();
}
@@ -0,0 +1,60 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed 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 com.intellij.framework.detection.impl.exclude;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.SimpleTextAttributes;
/**
* @author nik
*/
class InvalidExcludeListItem extends ExcludeListItem {
private String myFileUrl;
private String myFrameworkTypeId;
InvalidExcludeListItem(String frameworkTypeId, String fileUrl) {
myFrameworkTypeId = frameworkTypeId;
myFileUrl = fileUrl;
}
@Override
public String getFrameworkTypeId() {
return myFrameworkTypeId;
}
@Override
public String getFileUrl() {
return myFileUrl;
}
@Override
public void renderItem(ColoredListCellRenderer renderer) {
if (myFrameworkTypeId != null) {
renderer.append(myFrameworkTypeId, SimpleTextAttributes.ERROR_ATTRIBUTES);
if (myFileUrl != null) {
renderer.append(" in " + myFileUrl, SimpleTextAttributes.ERROR_ATTRIBUTES);
}
}
else {
renderer.append(myFileUrl, SimpleTextAttributes.ERROR_ATTRIBUTES);
}
}
@Override
public String getPresentableFrameworkName() {
return myFrameworkTypeId;
}
}
@@ -0,0 +1,66 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed 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 com.intellij.framework.detection.impl.exclude;
import com.intellij.framework.FrameworkType;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.SimpleTextAttributes;
/**
* @author nik
*/
class ValidExcludeListItem extends ExcludeListItem {
private VirtualFile myFile;
private FrameworkType myFrameworkType;
ValidExcludeListItem(FrameworkType frameworkType, VirtualFile file) {
myFrameworkType = frameworkType;
myFile = file;
}
@Override
public String getFrameworkTypeId() {
return myFrameworkType != null ? myFrameworkType.getId() : null;
}
@Override
public String getFileUrl() {
return myFile != null ? myFile.getUrl() : null;
}
@Override
public void renderItem(ColoredListCellRenderer renderer) {
if (myFrameworkType != null) {
renderer.setIcon(myFrameworkType.getIcon());
renderer.append(myFrameworkType.getPresentableName());
if (myFile != null) {
renderer.append(" in " + myFile.getName());
renderer.append(" (" + myFile.getPresentableUrl() + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
}
}
else {
renderer.setIcon(myFile.getIcon());
renderer.append(myFile.getName());
renderer.append(" (" + myFile.getPresentableUrl() + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
}
}
@Override
public String getPresentableFrameworkName() {
return myFrameworkType != null ? myFrameworkType.getPresentableName() : null;
}
}