From efd42ff4ffe3ad4d3191b24b5a923f67263a9cd8 Mon Sep 17 00:00:00 2001 From: irengrig Date: Mon, 25 Jan 2016 11:57:36 +0100 Subject: [PATCH] change the presentation of the remote mappings field according to IDEA-150347 Design for remote interpreters mapping settings also fixes WEB-19904 Node remote interpreter: Vagrant: show absolute path instead of relative to the project root --- .../src/com/intellij/util/ui/SwingHelper.java | 51 ++++++++++++ .../com/intellij/ui/TextTruncationTest.java | 81 +++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 platform/platform-tests/testSrc/com/intellij/ui/TextTruncationTest.java diff --git a/platform/platform-impl/src/com/intellij/util/ui/SwingHelper.java b/platform/platform-impl/src/com/intellij/util/ui/SwingHelper.java index d4e97f2fffd1..2dbc6c58b07c 100644 --- a/platform/platform-impl/src/com/intellij/util/ui/SwingHelper.java +++ b/platform/platform-impl/src/com/intellij/util/ui/SwingHelper.java @@ -687,4 +687,55 @@ public class SwingHelper { BrowserUtil.browse(myUrl); } } + + public final static String ELLIPSIS = "..."; + public static final String ERROR_STR = "www"; + public static String truncateStringWithEllipsis(final String text, final int maxWidth, final FontMetrics fm) { + return truncateStringWithEllipsis(text, maxWidth, new WidthCalculator() { + @Override + public int stringWidth(String s) { + return fm.stringWidth(s); + } + + @Override + public int charWidth(char c) { + return fm.charWidth(c); + } + }); + } + + public interface WidthCalculator { + int stringWidth(final String s); + int charWidth(final char c); + } + + public static String truncateStringWithEllipsis(final String text, final int maxWidth, final WidthCalculator fm) { + final int error = fm.stringWidth(ERROR_STR); + final int wholeWidth = fm.stringWidth(text) + error; + if (wholeWidth <= maxWidth || text.isEmpty()) return text; + final int ellipsisWidth = fm.stringWidth(ELLIPSIS) + error; // plus some reserve + if (ellipsisWidth >= maxWidth) return ELLIPSIS; + + final int availableWidth = maxWidth - ellipsisWidth; + int currentLen = (int)Math.floor(availableWidth / (((double) wholeWidth) / text.length())); + + final String currentSubstring = text.substring(0, currentLen); + int realWidth = fm.stringWidth(currentSubstring); + + if (realWidth >= availableWidth) { + int delta = 0; + for (int i = currentLen - 1; i >= 0; i--) { + if ((realWidth - delta) < availableWidth) return text.substring(0, i) + ELLIPSIS; + delta += fm.charWidth(currentSubstring.charAt(i)); + } + return text.substring(0, 1) + ELLIPSIS; + } else { + int delta = 0; + for (int i = currentLen; i < text.length(); i++) { + if ((realWidth + delta) >= availableWidth) return text.substring(0, i) + ELLIPSIS; + delta += fm.charWidth(text.charAt(i)); + } + return text.substring(0, currentLen) + ELLIPSIS; + } + } } diff --git a/platform/platform-tests/testSrc/com/intellij/ui/TextTruncationTest.java b/platform/platform-tests/testSrc/com/intellij/ui/TextTruncationTest.java new file mode 100644 index 000000000000..955ee1b52244 --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/ui/TextTruncationTest.java @@ -0,0 +1,81 @@ +/* + * Copyright 2000-2016 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.ui; + +import com.intellij.util.ui.SwingHelper; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Irina.Chernushina on 1/25/2016. + */ +public class TextTruncationTest { + @Test + public void testVariants() throws Exception { + final MyWidthCalculator calculator = new MyWidthCalculator(); + testString("this is just a test", "this is just a test", calculator); + testString("this is just a test", "this is just a ", calculator); + testString("dark side of the moon", "dark side of th", calculator); + testString("my width calculator", "my", calculator); + } + + private static void testString(final String string, final String truncated, MyWidthCalculator calculator) { + int width = calculator.stringWidth(truncated); + final boolean equals = string.equals(truncated); + if (equals) { + width += calculator.stringWidth(SwingHelper.ERROR_STR); + } + else { + width += calculator.stringWidth(SwingHelper.ELLIPSIS) + calculator.stringWidth(SwingHelper.ERROR_STR); + } + final String result = SwingHelper.truncateStringWithEllipsis(string, width, calculator); + if (equals) Assert.assertEquals(truncated, result); + else Assert.assertEquals(truncated + SwingHelper.ELLIPSIS, result); + } + + private static class MyWidthCalculator implements SwingHelper.WidthCalculator { + private final Map myMap; + private final static int DEFAULT = 3; + + private MyWidthCalculator() { + myMap = new HashMap<>(); + + final String alphabet = "abcdefghijklmnopqrstuvwxyz "; + for (int i = 0; i < alphabet.length(); i++) { + if (i % 3 == 0) myMap.put(alphabet.charAt(i), 5); + myMap.put(alphabet.charAt(i), 4); + } + } + + @Override + public int stringWidth(String s) { + int result = 0; + for (int i = 0; i < s.length(); i++) { + result += charWidth(s.charAt(i)); + } + return result; + } + + @Override + public int charWidth(char c) { + final Integer integer = myMap.get(c); + return integer == null ? DEFAULT : integer; + } + } +}