generalize dialog resizing to show whole text appcode/144.94 clion/144.93
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Mon, 5 Oct 2015 08:30:13 +0000 (11:30 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Mon, 5 Oct 2015 08:30:13 +0000 (11:30 +0300)
platform/platform-impl/src/com/intellij/util/ui/SwingHelper.java

index bf9e8a783adc90a3012896f293342b88d25cf8d9..d4e97f2fffd1026831c40a9fd49439d6c5ee7017 100644 (file)
@@ -20,10 +20,14 @@ import com.intellij.openapi.actionSystem.ActionManager;
 import com.intellij.openapi.actionSystem.AnAction;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.DefaultActionGroup;
 import com.intellij.openapi.actionSystem.AnAction;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.DefaultActionGroup;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.application.ModalityState;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
 import com.intellij.openapi.fileChooser.FileChooserFactory;
 import com.intellij.openapi.ide.CopyPasteManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
 import com.intellij.openapi.fileChooser.FileChooserFactory;
 import com.intellij.openapi.ide.CopyPasteManager;
+import com.intellij.openapi.options.ex.SingleConfigurableEditor;
+import com.intellij.openapi.options.newEditor.SettingsDialog;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.*;
 import com.intellij.openapi.util.SystemInfo;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.*;
 import com.intellij.openapi.util.SystemInfo;
@@ -31,11 +35,13 @@ import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.ui.HyperlinkLabel;
 import com.intellij.ui.TextFieldWithHistory;
 import com.intellij.ui.TextFieldWithHistoryWithBrowseButton;
 import com.intellij.ui.HyperlinkLabel;
 import com.intellij.ui.TextFieldWithHistory;
 import com.intellij.ui.TextFieldWithHistoryWithBrowseButton;
+import com.intellij.util.Consumer;
 import com.intellij.util.NotNullProducer;
 import com.intellij.util.ObjectUtils;
 import com.intellij.util.PlatformIcons;
 import com.intellij.util.containers.ComparatorUtil;
 import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.NotNullProducer;
 import com.intellij.util.ObjectUtils;
 import com.intellij.util.PlatformIcons;
 import com.intellij.util.containers.ComparatorUtil;
 import com.intellij.util.containers.ContainerUtil;
+import com.intellij.util.ui.update.UiNotifyConnector;
 import org.jetbrains.annotations.Nls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.annotations.Nls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -57,6 +63,7 @@ import java.util.Set;
 public class SwingHelper {
 
   private static final Logger LOG = Logger.getInstance(SwingHelper.class);
 public class SwingHelper {
 
   private static final Logger LOG = Logger.getInstance(SwingHelper.class);
+  private static final String DIALOG_RESIZED_TO_FIT_TEXT = "INTELLIJ_DIALOG_RESIZED_TO_FIT_TEXT";
 
   /**
    * Creates panel whose content consists of given {@code children} components
 
   /**
    * Creates panel whose content consists of given {@code children} components
@@ -200,6 +207,61 @@ public class SwingHelper {
     LOG.info("DialogWrapper '" + dialogWrapper.getTitle() + "' has been re-sized (added width: " + dw + ", added height: " + dh + ")");
   }
 
     LOG.info("DialogWrapper '" + dialogWrapper.getTitle() + "' has been re-sized (added width: " + dw + ", added height: " + dh + ")");
   }
 
+  public static void resizeDialogToFitTextFor(@NotNull final JComponent... components) {
+    if (components.length == 0) return;
+    doWithDialogWrapper(components[0], new Consumer<DialogWrapper>() {
+      @Override
+      public void consume(final DialogWrapper dialogWrapper) {
+        if (dialogWrapper instanceof SettingsDialog || dialogWrapper instanceof SingleConfigurableEditor) {
+          for (Component component : components) {
+            if (component instanceof TextFieldWithHistoryWithBrowseButton) {
+              setPreferredWidthToFitText((TextFieldWithHistoryWithBrowseButton)component);
+            }
+            else if (component instanceof TextFieldWithBrowseButton) {
+              setPreferredWidthToFitText((TextFieldWithBrowseButton)component);
+            }
+            else if (component instanceof JTextField) {
+              setPreferredWidthToFitText((JTextField)component);
+            }
+          }
+          ApplicationManager.getApplication().invokeLater(new Runnable() {
+            @Override
+            public void run() {
+              adjustDialogSizeToFitPreferredSize(dialogWrapper);
+            }
+          }, ModalityState.any());
+        }
+      }
+    });
+  }
+
+  private static void doWithDialogWrapper(@NotNull final JComponent component, @NotNull final Consumer<DialogWrapper> consumer) {
+    UIUtil.invokeLaterIfNeeded(new Runnable() {
+      @Override
+      public void run() {
+        if (component.getClientProperty(DIALOG_RESIZED_TO_FIT_TEXT) != null) {
+          return;
+        }
+        component.putClientProperty(DIALOG_RESIZED_TO_FIT_TEXT, true);
+        DialogWrapper dialogWrapper = DialogWrapper.findInstance(component);
+        if (dialogWrapper != null) {
+          consumer.consume(dialogWrapper);
+        }
+        else {
+          UiNotifyConnector.doWhenFirstShown(component, new Runnable() {
+            @Override
+            public void run() {
+              DialogWrapper dialogWrapper = DialogWrapper.findInstance(component);
+              if (dialogWrapper != null) {
+                consumer.consume(dialogWrapper);
+              }
+            }
+          });
+        }
+      }
+    });
+  }
+
   public static <T> void updateItems(@NotNull JComboBox comboBox,
                                      @NotNull List<T> newItems,
                                      @Nullable T newSelectedItemIfSelectionCannotBePreserved) {
   public static <T> void updateItems(@NotNull JComboBox comboBox,
                                      @NotNull List<T> newItems,
                                      @Nullable T newSelectedItemIfSelectionCannotBePreserved) {
@@ -625,4 +687,4 @@ public class SwingHelper {
       BrowserUtil.browse(myUrl);
     }
   }
       BrowserUtil.browse(myUrl);
     }
   }
-}
\ No newline at end of file
+}