From 3d2f4f5c90bb4982620e39df791f4f64d24b2f02 Mon Sep 17 00:00:00 2001 From: Alexander Kass Date: Mon, 27 Feb 2017 11:20:27 +0300 Subject: [PATCH] DBE: pass value from editor on-the-fly (in mappings) --- .../encoding/FileEncodingConfigurable.java | 15 +++- .../util/ui/tree/PerFileConfigurableBase.java | 84 +++++++++++++------ 2 files changed, 71 insertions(+), 28 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/openapi/vfs/encoding/FileEncodingConfigurable.java b/platform/lang-impl/src/com/intellij/openapi/vfs/encoding/FileEncodingConfigurable.java index cc56a3f8f91a..e75d70b75c27 100644 --- a/platform/lang-impl/src/com/intellij/openapi/vfs/encoding/FileEncodingConfigurable.java +++ b/platform/lang-impl/src/com/intellij/openapi/vfs/encoding/FileEncodingConfigurable.java @@ -151,7 +151,20 @@ public class FileEncodingConfigurable extends PerFileConfigurableBase { @Override public JComponent createComponent() { myTablePanel.add(super.createComponent(), BorderLayout.CENTER); - JPanel p = createActionPanel(null, () -> myPropsCharset, o -> myPropsCharset = o); + JPanel p = createActionPanel(null, new Value() { + @Override + public void commit() {} + + @Override + public Charset get() { + return myPropsCharset; + } + + @Override + public void set(Charset value) { + myPropsCharset = value; + } + }); myPropertiesFilesEncodingCombo.add(p, BorderLayout.CENTER); return myPanel; } diff --git a/platform/lang-impl/src/com/intellij/util/ui/tree/PerFileConfigurableBase.java b/platform/lang-impl/src/com/intellij/util/ui/tree/PerFileConfigurableBase.java index 211801fb51b6..03f1f5407e52 100644 --- a/platform/lang-impl/src/com/intellij/util/ui/tree/PerFileConfigurableBase.java +++ b/platform/lang-impl/src/com/intellij/util/ui/tree/PerFileConfigurableBase.java @@ -93,6 +93,10 @@ public abstract class PerFileConfigurableBase implements SearchableConfigurab private final List, Consumer>> myDefaultProps = ContainerUtil.newArrayList(); private VirtualFile myFileToSelect; + protected interface Value extends Setter, Getter { + void commit(); + } + protected PerFileConfigurableBase(@NotNull Project project, @NotNull PerFileMappings mappings) { myProject = project; myMappings = mappings; @@ -210,9 +214,20 @@ public abstract class PerFileConfigurableBase implements SearchableConfigurab for (Trinity, Consumer> prop : myDefaultProps) { myDefaultVals.put(prop.first, prop.second.produce()); - JPanel p = createActionPanel(null, - () -> myDefaultVals.get(prop.first), - o -> myDefaultVals.put(prop.first, adjustChosenValue(null, o))); + JPanel p = createActionPanel(null, new Value() { + @Override + public void commit() {} + + @Override + public T get() { + return myDefaultVals.get(prop.first); + } + + @Override + public void set(T value) { + myDefaultVals.put(prop.first, adjustChosenValue(null, value)); + } + }); panel.add(new JBLabel(prop.first + ":"), cons1); panel.add(p, cons2); } @@ -477,20 +492,31 @@ public abstract class PerFileConfigurableBase implements SearchableConfigurab editorValue = pair.second; // (T)value if (!canEditTarget(target, editorValue)) return null; - JPanel panel = createActionPanel(target, () -> editorValue, chosen -> { - editorValue = adjustChosenValue(target, chosen); - TableUtil.stopEditing(myTable); - selectRows(new int[]{modelRow}, true); - if (Comparing.equal(editorValue, pair.second)) { - // do nothing + JPanel panel = createActionPanel(target, new Value() { + @Override + public T get() { + return editorValue; } - else { - int ret = clearSubdirectoriesOnDemandOrCancel(false, target); - if (ret == Messages.CANCEL) { - myModel.setValueAt(value, modelRow, column); - } + + @Override + public void set(T value) { + editorValue = adjustChosenValue(target, value); + } + + @Override + public void commit() { + TableUtil.stopEditing(myTable); selectRows(new int[]{modelRow}, true); - } + if (Comparing.equal(editorValue, pair.second)) { + // do nothing + } + else { + int ret = clearSubdirectoriesOnDemandOrCancel(false, target); + if (ret == Messages.CANCEL) { + myModel.setValueAt(value, modelRow, column); + } + selectRows(new int[]{modelRow}, true); + } } }, true); AbstractButton button = UIUtil.uiTraverser(panel).filter(JButton.class).first(); @@ -516,13 +542,13 @@ public abstract class PerFileConfigurableBase implements SearchableConfigurab } @NotNull - protected JPanel createActionPanel(@Nullable Object target, Producer value, @NotNull Consumer consumer) { - return createActionPanel(target, value, consumer, false); + protected JPanel createActionPanel(@Nullable Object target, @NotNull Value value) { + return createActionPanel(target, value, false); } @NotNull - private JPanel createActionPanel(@Nullable Object target, Producer value, @NotNull Consumer consumer, boolean editor) { - AnAction changeAction = createValueAction(target, value, consumer); + private JPanel createActionPanel(@Nullable Object target, @NotNull Value value, boolean editor) { + AnAction changeAction = createValueAction(target, value); JComponent comboComponent = ((CustomComponentAction)changeAction).createCustomComponent(changeAction.getTemplatePresentation()); JPanel panel = new JPanel(new BorderLayout()) { @Override @@ -620,10 +646,10 @@ public abstract class PerFileConfigurableBase implements SearchableConfigurab } @NotNull - protected final AnAction createValueAction(@Nullable Object target, Producer value, @NotNull Consumer consumer) { + protected final AnAction createValueAction(@Nullable Object target, @NotNull Value value) { return new ComboBoxAction() { void updateText() { - String text = renderValue(value.produce(), StringUtil.notNullize(getNullValueText(target))); + String text = renderValue(value.get(), StringUtil.notNullize(getNullValueText(target))); getTemplatePresentation().setText(StringUtil.shortenTextWithEllipsis(text, 40, 0)); } @@ -642,10 +668,10 @@ public abstract class PerFileConfigurableBase implements SearchableConfigurab protected ComboBoxButton createComboBoxButton(Presentation presentation) { return new ComboBoxButton(presentation) { protected JBPopup createPopup(Runnable onDispose) { - JBPopup popup = createValueEditorPopup(target, value.produce(), onDispose, getDataContext(), o -> { - consumer.consume(o); + JBPopup popup = createValueEditorPopup(target, value.get(), onDispose, getDataContext(), o -> { + value.set(o); updateText(); - }); + }, value::commit); popup.setMinimumSize(new Dimension(getMinWidth(), getMinHeight())); return popup; } @@ -654,7 +680,7 @@ public abstract class PerFileConfigurableBase implements SearchableConfigurab @Override public String getToolTipText() { boolean cellEditor = UIUtil.uiParents(this, true).take(4).filter(JBTable.class).first() != null; - return cellEditor ? null : getToolTipFor(value.produce()); + return cellEditor ? null : getToolTipFor(value.get()); } }; } @@ -666,8 +692,12 @@ public abstract class PerFileConfigurableBase implements SearchableConfigurab @Nullable T value, @Nullable Runnable onDispose, @NotNull DataContext dataContext, - @NotNull Consumer onChosen) { - return createValueEditorActionListPopup(target, onDispose, dataContext, onChosen); + @NotNull Consumer onChosen, + @NotNull Runnable onCommit) { + return createValueEditorActionListPopup(target, onDispose, dataContext, chosen -> { + onChosen.consume(chosen); + onCommit.run(); + }); } @NotNull