extract SdkPathEditor subclass from PathEditor (to make it easier to use it for editing lists of paths in places which are unrelated to an SDK)

This commit is contained in:
Dmitry Jemerov
2011-09-28 13:57:50 +02:00
parent b95ae38eeb
commit 1b4f1ccebf
9 changed files with 86 additions and 63 deletions
@@ -62,7 +62,7 @@ import java.util.Set;
public class SdkEditor implements Configurable, Place.Navigator {
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.projectRoots.ui.SdkEditor");
private Sdk mySdk;
private final Map<OrderRootType, PathEditor> myPathEditors = new HashMap<OrderRootType, PathEditor>();
private final Map<OrderRootType, SdkPathEditor> myPathEditors = new HashMap<OrderRootType, SdkPathEditor>();
private TextFieldWithBrowseButton myHomeComponent;
private final Map<SdkType, AdditionalDataConfigurable> myAdditionalDataConfigurables = new HashMap<SdkType, AdditionalDataConfigurable>();
@@ -131,7 +131,7 @@ public class SdkEditor implements Configurable, Place.Navigator {
myTabbedPane = new TabbedPaneWrapper(myDisposable);
for (OrderRootType type : OrderRootType.getAllTypes()) {
if (mySdk == null || showTabForType(type)) {
final PathEditor pathEditor = OrderRootTypeUIFactory.FACTORY.getByKey(type).createPathEditor(mySdk);
final SdkPathEditor pathEditor = OrderRootTypeUIFactory.FACTORY.getByKey(type).createPathEditor(mySdk);
if (pathEditor != null) {
myTabbedPane.addTab(pathEditor.getDisplayName(), pathEditor.createComponent());
myPathEditors.put(type, pathEditor);
@@ -201,7 +201,7 @@ public class SdkEditor implements Configurable, Place.Navigator {
myInitialPath = mySdk.getHomePath();
final SdkModificator sdkModificator = mySdk.getSdkModificator();
sdkModificator.setHomePath(getHomeValue().replace(File.separatorChar, '/'));
for (PathEditor pathEditor : myPathEditors.values()) {
for (SdkPathEditor pathEditor : myPathEditors.values()) {
pathEditor.apply(sdkModificator);
}
ApplicationManager.getApplication().runWriteAction(new Runnable() { // fix SCR #29193
@@ -219,7 +219,7 @@ public class SdkEditor implements Configurable, Place.Navigator {
public void reset(){
if (mySdk == null){
setHomePathValue("");
for (PathEditor pathEditor : myPathEditors.values()) {
for (SdkPathEditor pathEditor : myPathEditors.values()) {
pathEditor.reset(null);
}
}
@@ -23,7 +23,7 @@ package com.intellij.openapi.roots.ui.configuration.libraryEditor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.ui.PathEditor;
import com.intellij.openapi.projectRoots.ui.SdkPathEditor;
import com.intellij.openapi.roots.AnnotationOrderRootType;
import com.intellij.openapi.roots.ui.OrderRootTypeUIFactory;
import com.intellij.openapi.util.IconLoader;
@@ -43,8 +43,8 @@ public class AnnotationsOrderRootTypeUIFactory implements OrderRootTypeUIFactory
return ProjectBundle.message("sdk.configure.annotations.tab");
}
public PathEditor createPathEditor(Sdk sdk) {
return new PathEditor(ProjectBundle.message("sdk.configure.annotations.tab"), AnnotationOrderRootType.getInstance(),
public SdkPathEditor createPathEditor(Sdk sdk) {
return new SdkPathEditor(ProjectBundle.message("sdk.configure.annotations.tab"), AnnotationOrderRootType.getInstance(),
FileChooserDescriptorFactory.createSingleFolderDescriptor());
}
}
@@ -23,7 +23,7 @@ package com.intellij.openapi.roots.ui.configuration.libraryEditor;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.ui.PathEditor;
import com.intellij.openapi.projectRoots.ui.SdkPathEditor;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.ui.OrderRootTypeUIFactory;
import com.intellij.openapi.util.IconLoader;
@@ -33,8 +33,8 @@ import javax.swing.*;
public class ClassesOrderRootTypeUIFactory implements OrderRootTypeUIFactory {
private static final Icon ICON = IconLoader.getIcon("/nodes/compiledClassesFolder.png");
public PathEditor createPathEditor(Sdk sdk) {
return new PathEditor(ProjectBundle.message("sdk.configure.classpath.tab"), OrderRootType.CLASSES, new FileChooserDescriptor(true, true, true, false, true, true));
public SdkPathEditor createPathEditor(Sdk sdk) {
return new SdkPathEditor(ProjectBundle.message("sdk.configure.classpath.tab"), OrderRootType.CLASSES, new FileChooserDescriptor(true, true, true, false, true, true));
}
@Override
@@ -20,13 +20,12 @@
*/
package com.intellij.openapi.roots.ui.configuration.libraryEditor;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.ui.PathEditor;
import com.intellij.openapi.projectRoots.ui.SdkPathEditor;
import com.intellij.openapi.projectRoots.ui.Util;
import com.intellij.openapi.roots.JavadocOrderRootType;
import com.intellij.openapi.roots.ui.OrderRootTypeUIFactory;
@@ -38,7 +37,7 @@ import javax.swing.*;
public class JavadocOrderRootTypeUIFactory implements OrderRootTypeUIFactory {
private static final Icon ICON = IconLoader.getIcon("/nodes/javaDocFolder.png");
public PathEditor createPathEditor(Sdk sdk) {
public SdkPathEditor createPathEditor(Sdk sdk) {
return new JavadocPathsEditor(sdk);
}
@@ -52,7 +51,7 @@ public class JavadocOrderRootTypeUIFactory implements OrderRootTypeUIFactory {
return ProjectBundle.message("library.javadocs.node");
}
static class JavadocPathsEditor extends PathEditor {
static class JavadocPathsEditor extends SdkPathEditor {
private final Sdk mySdk;
public JavadocPathsEditor(Sdk sdk) {
@@ -23,9 +23,9 @@ package com.intellij.openapi.roots.ui.configuration.libraryEditor;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ui.OrderRootTypeUIFactory;
import com.intellij.openapi.projectRoots.ui.PathEditor;
import com.intellij.openapi.projectRoots.ui.SdkPathEditor;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.ui.OrderRootTypeUIFactory;
import com.intellij.openapi.roots.ui.configuration.PathUIUtils;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.vfs.VirtualFile;
@@ -36,8 +36,8 @@ import java.awt.*;
public class SourcesOrderRootTypeUIFactory implements OrderRootTypeUIFactory {
private static final Icon ICON = IconLoader.getIcon("/nodes/sourceFolder.png");
public PathEditor createPathEditor(Sdk sdk) {
return new PathEditor(ProjectBundle.message("sdk.configure.sourcepath.tab"), OrderRootType.SOURCES, new FileChooserDescriptor(true, true, true, false, true, true)) {
public SdkPathEditor createPathEditor(Sdk sdk) {
return new SdkPathEditor(ProjectBundle.message("sdk.configure.sourcepath.tab"), OrderRootType.SOURCES, new FileChooserDescriptor(true, true, true, false, true, true)) {
@Override
protected VirtualFile[] adjustAddedFileSet(final Component component, final VirtualFile[] files) {
return PathUIUtils.scanAndSelectDetectedJavaSourceRoots(component, files);
@@ -15,7 +15,6 @@
*/
package com.intellij.openapi.projectRoots.ui;
import com.google.common.collect.Lists;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.CommonShortcuts;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
@@ -28,8 +27,6 @@ import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.projectRoots.SdkModificator;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.vfs.JarFileSystem;
@@ -45,7 +42,6 @@ import com.intellij.util.containers.HashSet;
import com.intellij.util.ui.UIUtil;
import gnu.trove.TIntArrayList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
@@ -77,21 +73,14 @@ public class PathEditor {
private boolean myEnabled = false;
private static final Icon ICON_INVALID = IconLoader.getIcon("/nodes/ppInvalid.png");
private static final Icon ICON_EMPTY = IconLoader.getIcon("/nodes/emptyNode.png");
private final String myDisplayName;
private final OrderRootType myOrderRootType;
private final FileChooserDescriptor myDescriptor;
public PathEditor(final String displayName,
final OrderRootType orderRootType,
final FileChooserDescriptor descriptor) {
myDisplayName = displayName;
myOrderRootType = orderRootType;
public PathEditor(final FileChooserDescriptor descriptor) {
myDescriptor = descriptor;
myDescriptor.putUserData(FileChooserDialog.PREFER_LAST_OVER_TO_SELECT, Boolean.TRUE);
myModel = createListModel();
}
protected void setEnabled(boolean enabled) {
myEnabled = enabled;
}
@@ -103,10 +92,6 @@ public class PathEditor {
protected void onSpecifyUrlButtonClicked() {
}
public String getDisplayName() {
return myDisplayName;
}
protected void setModified(boolean modified) {
myModified = modified;
}
@@ -115,20 +100,6 @@ public class PathEditor {
return myModified;
}
public OrderRootType getOrderRootType() {
return myOrderRootType;
}
public void apply(SdkModificator sdkModificator) {
sdkModificator.removeRoots(myOrderRootType);
// add all items
for (int i = 0; i < getRowCount(); i++) {
sdkModificator.addRoot(getValueAt(i), myOrderRootType);
}
setModified(false);
updateButtons();
}
public VirtualFile[] getRoots() {
final int count = getRowCount();
if (count == 0) {
@@ -152,15 +123,6 @@ public class PathEditor {
updateButtons();
}
public void reset(@Nullable SdkModificator modificator) {
if (modificator != null) {
resetPath(Lists.newArrayList(modificator.getRoots(myOrderRootType)));
}
else {
setEnabled(false);
}
}
public JComponent createComponent() {
myPanel = new JPanel(new GridBagLayout());
@@ -282,7 +244,7 @@ public class PathEditor {
return files;
}
protected void updateButtons() {
public void updateButtons() {
Object[] values = getSelectedRoots();
myRemoveButton.setEnabled((values.length > 0) && myEnabled);
myAddButton.setEnabled(myEnabled);
@@ -389,11 +351,11 @@ public class PathEditor {
return myList.getSelectedValues();
}
private int getRowCount() {
protected int getRowCount() {
return getListModel().getSize();
}
private VirtualFile getValueAt(int row) {
protected VirtualFile getValueAt(int row) {
return (VirtualFile)getListModel().get(row);
}
@@ -0,0 +1,61 @@
/*
* 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.projectRoots.ui;
import com.google.common.collect.Lists;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.projectRoots.SdkModificator;
import com.intellij.openapi.roots.OrderRootType;
import org.jetbrains.annotations.Nullable;
public class SdkPathEditor extends PathEditor {
private final String myDisplayName;
private final OrderRootType myOrderRootType;
public SdkPathEditor(String displayName, OrderRootType orderRootType, FileChooserDescriptor descriptor) {
super(descriptor);
myDisplayName = displayName;
myOrderRootType = orderRootType;
}
public String getDisplayName() {
return myDisplayName;
}
public OrderRootType getOrderRootType() {
return myOrderRootType;
}
public void apply(SdkModificator sdkModificator) {
sdkModificator.removeRoots(myOrderRootType);
// add all items
for (int i = 0; i < getRowCount(); i++) {
sdkModificator.addRoot(getValueAt(i), myOrderRootType);
}
setModified(false);
updateButtons();
}
public void reset(@Nullable SdkModificator modificator) {
if (modificator != null) {
resetPath(Lists.newArrayList(modificator.getRoots(myOrderRootType)));
}
else {
setEnabled(false);
}
}
}
@@ -22,6 +22,7 @@ package com.intellij.openapi.roots.ui;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.ui.PathEditor;
import com.intellij.openapi.projectRoots.ui.SdkPathEditor;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.util.KeyedExtensionFactory;
@@ -34,7 +35,7 @@ public interface OrderRootTypeUIFactory {
}
};
PathEditor createPathEditor(Sdk sdk);
SdkPathEditor createPathEditor(Sdk sdk);
Icon getIcon();
@@ -17,7 +17,7 @@ package com.intellij.openapi.roots.ui.configuration.libraryEditor;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.ui.PathEditor;
import com.intellij.openapi.projectRoots.ui.SdkPathEditor;
import com.intellij.openapi.roots.ui.OrderRootTypeUIFactory;
import com.intellij.openapi.util.IconLoader;
import org.jetbrains.annotations.Nullable;
@@ -33,7 +33,7 @@ public class DocumentationOrderRootTypeUIFactory implements OrderRootTypeUIFacto
@Override
@Nullable
public PathEditor createPathEditor(Sdk sdk) {
public SdkPathEditor createPathEditor(Sdk sdk) {
return null;
}