From cfe8bab3cef36a68437b56fe6ad11c01565a4103 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Tue, 14 Feb 2017 15:10:07 +0300 Subject: [PATCH] schemes ui: introduce description aware schemes panel --- ...AbstractDescriptionAwareSchemesPanel.java} | 136 +++++++++--------- .../schemes/AbstractSchemeActions.java | 4 +- .../DescriptionAwareSchemeActions.java | 59 ++++++++ 3 files changed, 133 insertions(+), 66 deletions(-) rename platform/lang-impl/src/com/intellij/{profile/codeInspection/ui/header/AuxiliaryRightPanel.java => application/options/schemes/AbstractDescriptionAwareSchemesPanel.java} (52%) create mode 100644 platform/lang-impl/src/com/intellij/application/options/schemes/DescriptionAwareSchemeActions.java diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/AuxiliaryRightPanel.java b/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractDescriptionAwareSchemesPanel.java similarity index 52% rename from platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/AuxiliaryRightPanel.java rename to platform/lang-impl/src/com/intellij/application/options/schemes/AbstractDescriptionAwareSchemesPanel.java index 83646e67cf64..f3394c839d22 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/AuxiliaryRightPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractDescriptionAwareSchemesPanel.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -13,63 +13,63 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.profile.codeInspection.ui.header; +package com.intellij.application.options.schemes; +import com.intellij.openapi.options.Scheme; +import com.intellij.openapi.ui.MessageType; import com.intellij.openapi.ui.ex.MultiLineLabel; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.ui.ClickListener; -import com.intellij.ui.JBColor; +import com.intellij.ui.components.JBTextField; import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; -import java.awt.event.MouseEvent; +import java.awt.event.*; -/** - * @author Dmitry Batkovich - */ -public class AuxiliaryRightPanel extends JPanel { +public abstract class AbstractDescriptionAwareSchemesPanel extends AbstractSchemesPanel { private static final String SHOW_DESCRIPTION_CARD = "show.description.card"; private static final String EDIT_DESCRIPTION_CARD = "edit.description.card"; private static final String ERROR_CARD = "error.card"; - private final DescriptionLabel myDescriptionLabel; - private final JLabel myErrorLabel; - private final ValidatedTextField myValidatedTextField; - private final CardLayout myLayout; - private final JPanel myCardPanel; + private final static KeyStroke ESC_KEY_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); + private final static KeyStroke ENTER_KEY_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false); - public AuxiliaryRightPanel(final DescriptionSaveListener descriptionListener) { - myCardPanel = new JPanel(); - myDescriptionLabel = new DescriptionLabel(); + private DescriptionLabel myDescriptionLabel; + private JLabel myWarningLabel; + private JBTextField myDescriptionTextField; + private CardLayout myLayout; - myErrorLabel = new JLabel(); - myErrorLabel.setBackground(UIUtil.isUnderDarcula() ? JBColor.PINK.darker() : JBColor.PINK); - myErrorLabel.setForeground(JBColor.BLACK); - myErrorLabel.setOpaque(true); + @NotNull + @Override + protected JPanel createInfoComponent() { + JPanel panel = new JPanel(); + myLayout = new CardLayout(); + panel.setLayout(myLayout); - myValidatedTextField = new ValidatedTextField(new SaveInputComponentValidator() { + myDescriptionTextField = new JBTextField(); + myDescriptionTextField.addFocusListener(new FocusAdapter() { @Override - public void doSave(@NotNull String text) { - descriptionListener.saveDescription(text.trim()); - } - - @Override - public boolean checkValid(@NotNull String text) { - return true; - } - - @Override - public void cancel() { - descriptionListener.cancel(); + public void focusLost(FocusEvent e) { + applyDescription(); } }); + myDescriptionTextField.registerKeyboardAction(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + applyDescription(); + } + }, ESC_KEY_STROKE, JComponent.WHEN_FOCUSED); + myDescriptionTextField.registerKeyboardAction(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + showDescription(); + } + }, ENTER_KEY_STROKE, JComponent.WHEN_FOCUSED); - myLayout = new CardLayout(); - myCardPanel.setLayout(myLayout); - myCardPanel.setBorder(BorderFactory.createEmptyBorder(2, 0, 4, 0)); - + myDescriptionLabel = new DescriptionLabel(); new ClickListener() { @Override public boolean onClick(@NotNull MouseEvent event, int clickCount) { @@ -81,43 +81,51 @@ public class AuxiliaryRightPanel extends JPanel { } }.installOn(myDescriptionLabel); - myCardPanel.add(myDescriptionLabel, SHOW_DESCRIPTION_CARD); - myCardPanel.add(myErrorLabel, ERROR_CARD); - myCardPanel.add(myValidatedTextField, EDIT_DESCRIPTION_CARD); + myWarningLabel = new JLabel(); - showDescription(null); + panel.add(myDescriptionTextField, EDIT_DESCRIPTION_CARD); + panel.add(myDescriptionLabel, SHOW_DESCRIPTION_CARD); + panel.add(myWarningLabel, ERROR_CARD); - setLayout(new BorderLayout()); - add(myValidatedTextField.getHintLabel(), BorderLayout.NORTH); - add(myCardPanel, BorderLayout.CENTER); + myLayout.show(panel, ERROR_CARD); + return panel; } - public void showDescription(@Nullable String newDescription) { - if (newDescription == null) { - newDescription = ""; + @Override + public final void showInfo(@Nullable String message, @NotNull MessageType messageType) { + myWarningLabel.setText(message); + myWarningLabel.setForeground(messageType.getTitleForeground()); + myLayout.show(myInfoComponent, ERROR_CARD); + } + + @Override + public void selectScheme(@Nullable T scheme) { + super.selectScheme(scheme); + if (scheme != null) { + showDescription(); } - myDescriptionLabel.setAllText(newDescription); - myLayout.show(myCardPanel, SHOW_DESCRIPTION_CARD); + } + + @Override + public final void clearInfo() { + myLayout.show(myInfoComponent, SHOW_DESCRIPTION_CARD); + } + + public void showDescription() { + String newDescription = (((DescriptionAwareSchemeActions)getActions()).getDescription(getSelectedScheme())); + myDescriptionLabel.setAllText(StringUtil.notNullize(newDescription)); + myLayout.show(myInfoComponent, SHOW_DESCRIPTION_CARD); } public void editDescription(@Nullable String startValue) { - if (startValue == null) { - startValue = ""; - } - myValidatedTextField.setText(startValue); - myLayout.show(myCardPanel, EDIT_DESCRIPTION_CARD); - myValidatedTextField.requestFocus(); + myLayout.show(myInfoComponent, EDIT_DESCRIPTION_CARD); + myDescriptionTextField.setText(StringUtil.notNullize(startValue)); + myDescriptionTextField.requestFocus(); } - public void showError(final @NotNull String errorText) { - myErrorLabel.setText(errorText); - myLayout.show(myCardPanel, ERROR_CARD); - } - - public interface DescriptionSaveListener { - void saveDescription(@NotNull String description); - - void cancel(); + private void applyDescription() { + (((DescriptionAwareSchemeActions)getActions())).setDescription(getSelectedScheme(), myDescriptionTextField.getText()); + showDescription(); } private static class DescriptionLabel extends MultiLineLabel { diff --git a/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemeActions.java b/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemeActions.java index f26338af189d..2e8fe58ae104 100644 --- a/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemeActions.java +++ b/platform/lang-impl/src/com/intellij/application/options/schemes/AbstractSchemeActions.java @@ -48,7 +48,7 @@ public abstract class AbstractSchemeActions { private final Collection mySchemeImportersNames; private final Collection mySchemeExporterNames; - private final AbstractSchemesPanel mySchemesPanel; + protected final AbstractSchemesPanel mySchemesPanel; protected AbstractSchemeActions(@NotNull AbstractSchemesPanel schemesPanel) { mySchemesPanel = schemesPanel; @@ -84,6 +84,7 @@ public abstract class AbstractSchemeActions { actions.add(new RenameAction()); actions.add(new ResetAction()); actions.add(new DeleteAction()); + addAdditionalActions(actions); if (!mySchemeExporterNames.isEmpty()) { actions.add(new ActionGroupPopupAction(ApplicationBundle.message("settings.editor.scheme.export"), mySchemeExporterNames) { @NotNull @@ -103,7 +104,6 @@ public abstract class AbstractSchemeActions { } }); } - addAdditionalActions(actions); return actions; } diff --git a/platform/lang-impl/src/com/intellij/application/options/schemes/DescriptionAwareSchemeActions.java b/platform/lang-impl/src/com/intellij/application/options/schemes/DescriptionAwareSchemeActions.java new file mode 100644 index 000000000000..e64f996ee5be --- /dev/null +++ b/platform/lang-impl/src/com/intellij/application/options/schemes/DescriptionAwareSchemeActions.java @@ -0,0 +1,59 @@ +/* + * Copyright 2000-2017 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.application.options.schemes; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.options.Scheme; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public abstract class DescriptionAwareSchemeActions extends AbstractSchemeActions { + protected DescriptionAwareSchemeActions(@NotNull AbstractDescriptionAwareSchemesPanel schemesPanel) { + super(schemesPanel); + } + + @Nullable + public abstract String getDescription(@NotNull T scheme); + + protected abstract void setDescription(@NotNull T scheme, @NotNull String newDescription); + + @Override + protected void addAdditionalActions(@NotNull List defaultActions) { + defaultActions.add(new AnAction("Edit description") { + + @Override + public void update(AnActionEvent e) { + final String text = getDescription(getSchemesPanel().getSelectedScheme()) == null ? "Add description" : "Edit description"; + e.getPresentation().setText(text); + } + + @Override + public void actionPerformed(AnActionEvent e) { + ((AbstractDescriptionAwareSchemesPanel) mySchemesPanel).editDescription(getDescription(getSchemesPanel().getSelectedScheme())); + } + }); + } + + @Override + protected void onSchemeChanged(@Nullable T scheme) { + if (scheme != null) { + ((AbstractDescriptionAwareSchemesPanel) mySchemesPanel).showDescription(); + } + } +}