IDEA-104500 Gradle: Allow to reuse common logic for other external systems

1. Fold external project path to the project name on external system run configuration dialog opening;
2. Correct external system run configuration settings deserialization;
This commit is contained in:
Denis.Zhdanov
2013-05-29 18:19:48 +04:00
parent e75c665b51
commit 8f0fd533ba
4 changed files with 92 additions and 50 deletions
@@ -71,8 +71,7 @@ public class ExternalSystemTaskExecutionSettings implements Cloneable {
}
public void setTaskNames(List<String> taskNames) {
myTaskNames.clear();
myTaskNames.addAll(taskNames);
myTaskNames = taskNames;
}
@Override
@@ -15,7 +15,6 @@
*/
package com.intellij.openapi.externalSystem.service.execution;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.externalSystem.ExternalSystemManager;
import com.intellij.openapi.externalSystem.ExternalSystemUiAware;
import com.intellij.openapi.externalSystem.model.ProjectSystemId;
@@ -30,11 +29,10 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.RawCommandLineEditor;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.Consumer;
import com.intellij.util.ui.GridBag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import static com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil.normalizePath;
@@ -94,52 +92,15 @@ public class ExternalSystemTaskSettingsControl implements ExternalSystemSettings
myTasksLabel = new JBLabel(ExternalSystemBundle.message("run.configuration.settings.label.tasks"));
myTasksTextField = new JBTextField(ExternalSystemConstants.TEXT_FIELD_WIDTH_IN_COLUMNS);
canvas.add(myTasksLabel, ExternalSystemUiUtil.getLabelConstraints(0));
canvas.add(myTasksTextField, ExternalSystemUiUtil.getFillLineConstraints(0));
GridBag c = ExternalSystemUiUtil.getFillLineConstraints(0);
c.insets.right = myProjectPathField.getButton().getPreferredSize().width + 8 /* street magic, sorry */;
canvas.add(myTasksTextField, c);
myVmOptionsLabel = new JBLabel(ExternalSystemBundle.message("run.configuration.settings.label.vmoptions"));
myVmOptionsEditor = new RawCommandLineEditor();
myVmOptionsEditor.setDialogCaption(ExternalSystemBundle.message("run.configuration.settings.caption.vmoptions"));
canvas.add(myVmOptionsLabel, ExternalSystemUiUtil.getLabelConstraints(0));
canvas.add(myVmOptionsEditor, ExternalSystemUiUtil.getFillLineConstraints(0));
canvas.setPaintCallback(new Consumer<Graphics>() {
@Nullable private Dimension myDimension;
@Nullable private Dimension myBounds;
private boolean myBorderReset;
@Override
public void consume(Graphics graphics) {
if (!myBorderReset) {
Editor editor = myProjectPathField.getChildComponent().getEditor();
if (editor != null) {
myBorderReset = true;
editor.setBorder(null);
}
}
Dimension size = myVmOptionsEditor.getTextField().getPreferredSize();
Rectangle bounds = myVmOptionsEditor.getTextField().getBounds();
if (!size.equals(myDimension)) {
myDimension = size;
}
if (myBounds == null || myBounds.width != bounds.width || myBounds.height == bounds.height) {
myBounds = bounds.getSize();
}
applyDimension(myProjectPathField.getChildComponent(), false);
applyDimension(myTasksTextField, true);
}
private void applyDimension(@NotNull JComponent component, boolean processBounds) {
Rectangle bounds = component.getBounds();
if (bounds != null && myBounds != null && (bounds.width != myBounds.width || bounds.height != myBounds.height)) {
component.setPreferredSize(myDimension);
if (processBounds) {
component.setBounds(bounds.x, bounds.y, myBounds.width, myBounds.height);
}
}
}
});
}
@Override
@@ -16,6 +16,9 @@
package com.intellij.openapi.externalSystem.service.ui;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.FoldRegion;
import com.intellij.openapi.editor.FoldingModel;
import com.intellij.openapi.externalSystem.ExternalSystemManager;
import com.intellij.openapi.externalSystem.ExternalSystemUiAware;
import com.intellij.openapi.externalSystem.model.ProjectSystemId;
@@ -27,6 +30,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComponentWithBrowseButton;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.TextAccessor;
import com.intellij.util.Consumer;
import com.intellij.util.TextFieldCompletionProvider;
import com.intellij.util.TextFieldCompletionProviderDumbAware;
import com.intellij.util.ui.UIUtil;
@@ -43,17 +47,22 @@ import java.util.Map;
*/
public class ExternalProjectPathField extends ComponentWithBrowseButton<EditorTextField> implements TextAccessor {
@NotNull private final Project myProject;
@NotNull private final ProjectSystemId myExternalSystemId;
public ExternalProjectPathField(@NotNull Project project,
@NotNull ProjectSystemId externalSystemId,
@NotNull FileChooserDescriptor descriptor,
@NotNull String fileChooserTitle)
{
super(createTextField(project, externalSystemId), createBrowseListener(descriptor, fileChooserTitle));
myProject = project;
myExternalSystemId = externalSystemId;
}
@NotNull
private static EditorTextField createTextField(@NotNull Project project, @NotNull ProjectSystemId externalSystemId) {
ExternalSystemManager<?,?,?,?,?> manager = ExternalSystemApiUtil.getManager(externalSystemId);
private static EditorTextField createTextField(@NotNull final Project project, @NotNull final ProjectSystemId externalSystemId) {
ExternalSystemManager<?, ?, ?, ?, ?> manager = ExternalSystemApiUtil.getManager(externalSystemId);
assert manager != null;
final AbstractExternalSystemLocalSettings settings = manager.getLocalSettingsProvider().fun(project);
final ExternalSystemUiAware uiAware;
@@ -84,14 +93,19 @@ public class ExternalProjectPathField extends ComponentWithBrowseButton<EditorTe
result.stopHere();
}
};
EditorTextField result = provider.createEditor(project, false);
EditorTextField result = provider.createEditor(project, false, new Consumer<Editor>() {
@Override
public void consume(Editor editor) {
collapseIfPossible(editor, externalSystemId, project);
}
});
result.setBorder(UIUtil.getTextFieldBorder());
result.setOneLineMode(true);
result.setOpaque(true);
result.setBackground(UIUtil.getTextFieldBackground());
return result;
}
@NotNull
private static ActionListener createBrowseListener(@NotNull final FileChooserDescriptor descriptor,
final @NotNull String fileChooserTitle)
@@ -110,8 +124,56 @@ public class ExternalProjectPathField extends ComponentWithBrowseButton<EditorTe
}
@Override
public void setText(String text) {
public void setText(final String text) {
getChildComponent().setText(text);
Editor editor = getChildComponent().getEditor();
if (editor != null) {
collapseIfPossible(editor, myExternalSystemId, myProject);
}
}
private static void collapseIfPossible(@NotNull final Editor editor,
@NotNull ProjectSystemId externalSystemId,
@NotNull Project project)
{
ExternalSystemManager<?,?,?,?,?> manager = ExternalSystemApiUtil.getManager(externalSystemId);
assert manager != null;
final AbstractExternalSystemLocalSettings settings = manager.getLocalSettingsProvider().fun(project);
final ExternalSystemUiAware uiAware;
if (manager instanceof ExternalSystemUiAware) {
uiAware = (ExternalSystemUiAware)manager;
}
else {
uiAware = DefaultExternalSystemUiAware.INSTANCE;
}
String rawText = editor.getDocument().getText();
for (Map.Entry<ExternalProjectPojo, Collection<ExternalProjectPojo>> entry : settings.getAvailableProjects().entrySet()) {
if (entry.getKey().getPath().equals(rawText)) {
collapse(editor, uiAware.getProjectRepresentationName(entry.getKey().getPath(), null));
return;
}
for (ExternalProjectPojo pojo : entry.getValue()) {
if (pojo.getPath().equals(rawText)) {
collapse(editor, uiAware.getProjectRepresentationName(pojo.getPath(), entry.getKey().getPath()));
return;
}
}
}
}
private static void collapse(@NotNull final Editor editor, @NotNull final String placeholder) {
final FoldingModel foldingModel = editor.getFoldingModel();
foldingModel.runBatchFoldingOperation(new Runnable() {
@Override
public void run() {
FoldRegion region = foldingModel.addFoldRegion(0, editor.getDocument().getTextLength(), placeholder);
if (region != null) {
region.setExpanded(false);
}
}
});
}
@Override
@@ -13,6 +13,7 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.ui.EditorTextField;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author sergey.evdokimov
@@ -68,11 +69,21 @@ public abstract class TextFieldCompletionProvider {
protected abstract void addCompletionVariants(@NotNull String text, int offset, @NotNull String prefix, @NotNull CompletionResultSet result);
@NotNull
public EditorTextField createEditor(Project project) {
return createEditor(project, true);
}
@NotNull
public EditorTextField createEditor(Project project, final boolean shouldHaveBorder) {
return createEditor(project, shouldHaveBorder, null);
}
@NotNull
public EditorTextField createEditor(Project project,
final boolean shouldHaveBorder,
@Nullable final Consumer<Editor> editorConstructionCallback)
{
return new EditorTextField(createDocument(project, ""), project, PlainTextLanguage.INSTANCE.getAssociatedFileType()) {
@Override
protected boolean shouldHaveBorder() {
@@ -88,6 +99,15 @@ public abstract class TextFieldCompletionProvider {
editor.setBorder(null);
}
}
@Override
protected EditorEx createEditor() {
EditorEx result = super.createEditor();
if (editorConstructionCallback != null) {
editorConstructionCallback.consume(result);
}
return result;
}
};
}
}