diff --git a/java/idea-ui/src/com/intellij/ide/util/projectWizard/JavaSettingsStep.java b/java/idea-ui/src/com/intellij/ide/util/projectWizard/JavaSettingsStep.java index 3ac5962fbd30..93e17e56bcd6 100644 --- a/java/idea-ui/src/com/intellij/ide/util/projectWizard/JavaSettingsStep.java +++ b/java/idea-ui/src/com/intellij/ide/util/projectWizard/JavaSettingsStep.java @@ -16,12 +16,12 @@ package com.intellij.ide.util.projectWizard; import com.intellij.ide.IdeBundle; -import com.intellij.ide.util.BrowseFilesListener; import com.intellij.ide.util.PropertiesComponent; +import com.intellij.openapi.fileChooser.FileChooserDescriptor; +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.SdkTypeId; -import com.intellij.openapi.ui.ComponentWithBrowseButton; -import com.intellij.openapi.ui.TextComponentAccessor; +import com.intellij.openapi.ui.TextBrowseFolderListener; import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Pair; @@ -64,26 +64,17 @@ public class JavaSettingsStep extends SdkSettingsStep { private void addSourcePath(SettingsStep settingsStep) { Project project = settingsStep.getContext().getProject(); - ComponentWithBrowseButton.BrowseFolderActionListener listener = - new ComponentWithBrowseButton.BrowseFolderActionListener( - IdeBundle.message("prompt.select.source.directory"), null, mySourcePath, project, BrowseFilesListener.SINGLE_DIRECTORY_DESCRIPTOR, - TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT) - { - @Override - protected void onFileChoosen(VirtualFile chosenFile) { - String contentEntryPath = myModuleBuilder.getContentEntryPath(); - String path = chosenFile.getPath(); - if (contentEntryPath != null) { - - int i = StringUtil.commonPrefixLength(contentEntryPath, path); - mySourcePath.setText(path.substring(i)); - } - else { - mySourcePath.setText(path); - } - } - }; - mySourcePath.addBrowseFolderListener(project, listener); + FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); + descriptor.setTitle(IdeBundle.message("prompt.select.source.directory")); + mySourcePath.addBrowseFolderListener(new TextBrowseFolderListener(descriptor, project) { + @NotNull + @Override + protected String chosenFileToResultingText(@NotNull VirtualFile chosenFile) { + String contentEntryPath = myModuleBuilder.getContentEntryPath(); + String path = chosenFile.getPath(); + return contentEntryPath == null ? path : path.substring(StringUtil.commonPrefixLength(contentEntryPath, path)); + } + }); myCreateSourceRoot.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { diff --git a/platform/platform-api/src/com/intellij/openapi/fileChooser/FileChooserDescriptor.java b/platform/platform-api/src/com/intellij/openapi/fileChooser/FileChooserDescriptor.java index d3076401e291..d2ab0405b3f2 100644 --- a/platform/platform-api/src/com/intellij/openapi/fileChooser/FileChooserDescriptor.java +++ b/platform/platform-api/src/com/intellij/openapi/fileChooser/FileChooserDescriptor.java @@ -255,6 +255,7 @@ public class FileChooserDescriptor implements Cloneable { return this; } + @Override public final Object clone() { try { return super.clone(); diff --git a/platform/platform-api/src/com/intellij/openapi/ui/ComponentWithBrowseButton.java b/platform/platform-api/src/com/intellij/openapi/ui/ComponentWithBrowseButton.java index 76504210e4cd..d3e058f02e77 100644 --- a/platform/platform-api/src/com/intellij/openapi/ui/ComponentWithBrowseButton.java +++ b/platform/platform-api/src/com/intellij/openapi/ui/ComponentWithBrowseButton.java @@ -42,6 +42,7 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.util.List; @@ -84,6 +85,7 @@ public class ComponentWithBrowseButton extends JPanel i setPreferredSize(new Dimension(size.width + preferredSize.width + 2, SystemInfo.isMac && UIUtil.isUnderAquaLookAndFeel() ? preferredSize.height : preferredSize.height + 2)); } + @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); myBrowseButton.setEnabled(enabled && myButtonEnabled); @@ -129,9 +131,11 @@ public class ComponentWithBrowseButton extends JPanel i public void addBrowseFolderListener(@Nullable Project project, final BrowseFolderActionListener actionListener, boolean autoRemoveOnHide) { if (autoRemoveOnHide) { new LazyUiDisposable>(null, this, this) { + @Override protected void initialize(@NotNull Disposable parent, @NotNull ComponentWithBrowseButton child, @Nullable Project project) { addActionListener(actionListener); Disposer.register(child, new Disposable() { + @Override public void dispose() { removeActionListener(actionListener); } @@ -143,6 +147,7 @@ public class ComponentWithBrowseButton extends JPanel i } } + @Override public void dispose() { } @@ -159,12 +164,13 @@ public class ComponentWithBrowseButton extends JPanel i myBrowseButton = browseButton; } + @Override public void actionPerformed(AnActionEvent e){ myBrowseButton.doClick(); } public void registerShortcut(JComponent textField) { - ShortcutSet shiftEnter = new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK)); + ShortcutSet shiftEnter = new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK)); registerCustomShortcutSet(shiftEnter, textField); myBrowseButton.setToolTipText(KeymapUtil.getShortcutsText(shiftEnter.getShortcuts())); } @@ -177,10 +183,10 @@ public class ComponentWithBrowseButton extends JPanel i public static class BrowseFolderActionListener implements ActionListener { private final String myTitle; private final String myDescription; - private final ComponentWithBrowseButton myTextComponent; + protected ComponentWithBrowseButton myTextComponent; private final TextComponentAccessor myAccessor; private final Project myProject; - private final FileChooserDescriptor myFileChooserDescriptor; + protected final FileChooserDescriptor myFileChooserDescriptor; public BrowseFolderActionListener(@Nullable String title, @Nullable String description, ComponentWithBrowseButton textField, @Nullable Project project, FileChooserDescriptor fileChooserDescriptor, TextComponentAccessor accessor) { myTitle = title; @@ -191,6 +197,7 @@ public class ComponentWithBrowseButton extends JPanel i myAccessor = accessor; } + @Override public void actionPerformed(ActionEvent e){ FileChooserDescriptor fileChooserDescriptor = (FileChooserDescriptor)myFileChooserDescriptor.clone(); if (myTitle != null) { @@ -199,9 +206,8 @@ public class ComponentWithBrowseButton extends JPanel i if (myDescription != null) { fileChooserDescriptor.setDescription(myDescription); } - VirtualFile initialFile = getInitialFile(); - FileChooser.chooseFiles(fileChooserDescriptor, myProject, initialFile, new Consumer>() { + FileChooser.chooseFiles(fileChooserDescriptor, myProject, getInitialFile(), new Consumer>() { @Override public void consume(List files) { onFileChoosen(files.get(0)); @@ -212,7 +218,7 @@ public class ComponentWithBrowseButton extends JPanel i @Nullable protected VirtualFile getInitialFile() { String directoryName = getComponentText(); - if (StringUtil.isEmptyOrSpaces(directoryName) || directoryName == null) { + if (StringUtil.isEmptyOrSpaces(directoryName)) { return null; } @@ -231,22 +237,29 @@ public class ComponentWithBrowseButton extends JPanel i return myAccessor.getText(myTextComponent.getChildComponent()).trim(); } - protected void onFileChoosen(VirtualFile chosenFile) { - myAccessor.setText(myTextComponent.getChildComponent(), chosenFile.getPresentableUrl()); + @NotNull + protected String chosenFileToResultingText(@NotNull VirtualFile chosenFile) { + return chosenFile.getPresentableUrl(); } + protected void onFileChoosen(@NotNull VirtualFile chosenFile) { + myAccessor.setText(myTextComponent.getChildComponent(), chosenFileToResultingText(chosenFile)); + } } + @Override public final void requestFocus() { myComponent.requestFocus(); } + @Override public final void setNextFocusableComponent(Component aComponent) { super.setNextFocusableComponent(aComponent); myComponent.setNextFocusableComponent(aComponent); } private KeyEvent myCurrentEvent = null; + @Override protected final boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) { if (condition == WHEN_FOCUSED && myCurrentEvent != e) try { diff --git a/platform/platform-api/src/com/intellij/openapi/ui/TextBrowseFolderListener.java b/platform/platform-api/src/com/intellij/openapi/ui/TextBrowseFolderListener.java new file mode 100644 index 000000000000..5bb1f74f3bcf --- /dev/null +++ b/platform/platform-api/src/com/intellij/openapi/ui/TextBrowseFolderListener.java @@ -0,0 +1,41 @@ +/* + * Copyright 2000-2013 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.ui; + +import com.intellij.openapi.fileChooser.FileChooserDescriptor; +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +public class TextBrowseFolderListener extends ComponentWithBrowseButton.BrowseFolderActionListener { + public TextBrowseFolderListener(@NotNull FileChooserDescriptor fileChooserDescriptor) { + this(fileChooserDescriptor, null); + } + + public TextBrowseFolderListener(@NotNull FileChooserDescriptor fileChooserDescriptor, @Nullable Project project) { + super(null, null, null, project, fileChooserDescriptor, TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT); + } + + void setOwnerComponent(@NotNull TextFieldWithBrowseButton component) { + myTextComponent = component; + } + + FileChooserDescriptor gFileChooserDescriptor() { + return myFileChooserDescriptor; + } +} \ No newline at end of file diff --git a/platform/platform-api/src/com/intellij/openapi/ui/TextFieldWithBrowseButton.java b/platform/platform-api/src/com/intellij/openapi/ui/TextFieldWithBrowseButton.java index 995efc9140c8..e04243a8564f 100644 --- a/platform/platform-api/src/com/intellij/openapi/ui/TextFieldWithBrowseButton.java +++ b/platform/platform-api/src/com/intellij/openapi/ui/TextFieldWithBrowseButton.java @@ -23,6 +23,7 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; import com.intellij.openapi.fileChooser.FileChooserFactory; import com.intellij.openapi.project.Project; import com.intellij.ui.TextAccessor; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; @@ -53,6 +54,12 @@ public class TextFieldWithBrowseButton extends ComponentWithBrowseButton