2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util.ui;
18 import com.intellij.ide.BrowserUtil;
19 import com.intellij.openapi.actionSystem.ActionManager;
20 import com.intellij.openapi.actionSystem.AnAction;
21 import com.intellij.openapi.actionSystem.AnActionEvent;
22 import com.intellij.openapi.actionSystem.DefaultActionGroup;
23 import com.intellij.openapi.application.ApplicationManager;
24 import com.intellij.openapi.application.ModalityState;
25 import com.intellij.openapi.diagnostic.Logger;
26 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
27 import com.intellij.openapi.fileChooser.FileChooserFactory;
28 import com.intellij.openapi.ide.CopyPasteManager;
29 import com.intellij.openapi.options.ex.SingleConfigurableEditor;
30 import com.intellij.openapi.options.newEditor.SettingsDialog;
31 import com.intellij.openapi.project.Project;
32 import com.intellij.openapi.ui.*;
33 import com.intellij.openapi.util.SystemInfo;
34 import com.intellij.openapi.util.text.StringUtil;
35 import com.intellij.ui.HyperlinkLabel;
36 import com.intellij.ui.TextFieldWithHistory;
37 import com.intellij.ui.TextFieldWithHistoryWithBrowseButton;
38 import com.intellij.ui.components.panels.NonOpaquePanel;
39 import com.intellij.util.Consumer;
40 import com.intellij.util.NotNullProducer;
41 import com.intellij.util.ObjectUtils;
42 import com.intellij.util.PlatformIcons;
43 import com.intellij.util.containers.ComparatorUtil;
44 import com.intellij.util.containers.ContainerUtil;
45 import com.intellij.util.ui.update.UiNotifyConnector;
46 import org.jetbrains.annotations.Nls;
47 import org.jetbrains.annotations.NotNull;
48 import org.jetbrains.annotations.Nullable;
51 import javax.swing.event.HyperlinkEvent;
52 import javax.swing.event.HyperlinkListener;
53 import javax.swing.event.PopupMenuEvent;
54 import javax.swing.event.PopupMenuListener;
55 import javax.swing.table.DefaultTableCellRenderer;
56 import javax.swing.table.TableCellRenderer;
57 import javax.swing.table.TableColumn;
58 import javax.swing.text.AttributeSet;
59 import javax.swing.text.BadLocationException;
60 import javax.swing.text.Document;
61 import javax.swing.text.Element;
62 import javax.swing.text.html.HTML;
63 import javax.swing.text.html.HTMLDocument;
65 import java.awt.datatransfer.StringSelection;
66 import java.awt.datatransfer.Transferable;
67 import java.awt.event.ActionListener;
69 import java.util.List;
71 public class SwingHelper {
73 private static final Logger LOG = Logger.getInstance(SwingHelper.class);
74 private static final String DIALOG_RESIZED_TO_FIT_TEXT = "INTELLIJ_DIALOG_RESIZED_TO_FIT_TEXT";
77 * Creates panel whose content consists of given {@code children} components
78 * stacked vertically each on another in a given order.
80 * @param childAlignmentX Component.LEFT_ALIGNMENT, Component.CENTER_ALIGNMENT or Component.RIGHT_ALIGNMENT
81 * @param children children components
82 * @return created panel
85 public static JPanel newVerticalPanel(float childAlignmentX, Component... children) {
86 return newGenericBoxPanel(true, childAlignmentX, children);
90 public static JPanel newLeftAlignedVerticalPanel(Component... children) {
91 return newVerticalPanel(Component.LEFT_ALIGNMENT, children);
95 public static JPanel newLeftAlignedVerticalPanel(@NotNull Collection<Component> children) {
96 return newVerticalPanel(Component.LEFT_ALIGNMENT, children);
100 public static JPanel newVerticalPanel(float childAlignmentX, @NotNull Collection<Component> children) {
101 return newVerticalPanel(childAlignmentX, children.toArray(new Component[children.size()]));
105 * Creates panel whose content consists of given {@code children} components horizontally
106 * stacked each on another in a given order.
108 * @param childAlignmentY Component.TOP_ALIGNMENT, Component.CENTER_ALIGNMENT or Component.BOTTOM_ALIGNMENT
109 * @param children children components
110 * @return created panel
113 public static JPanel newHorizontalPanel(float childAlignmentY, Component... children) {
114 return newGenericBoxPanel(false, childAlignmentY, children);
117 private static JPanel newGenericBoxPanel(boolean verticalOrientation,
118 float childAlignment,
119 Component... children) {
120 JPanel panel = new JPanel();
121 int axis = verticalOrientation ? BoxLayout.Y_AXIS : BoxLayout.X_AXIS;
122 panel.setLayout(new BoxLayout(panel, axis));
123 for (Component child : children) {
124 panel.add(child, childAlignment);
125 if (child instanceof JComponent) {
126 JComponent jChild = (JComponent)child;
127 if (verticalOrientation) {
128 jChild.setAlignmentX(childAlignment);
131 jChild.setAlignmentY(childAlignment);
139 public static JPanel wrapWithoutStretch(@NotNull JComponent component) {
140 JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
141 panel.add(component);
146 public static JPanel wrapWithHorizontalStretch(@NotNull JComponent component) {
147 JPanel panel = new JPanel(new BorderLayout(0, 0));
148 panel.add(component, BorderLayout.NORTH);
152 public static void setPreferredWidthToFitText(@NotNull TextFieldWithHistoryWithBrowseButton component) {
153 int childWidth = calcWidthToFitText(component.getChildComponent().getTextEditor(), JBUI.scale(32));
154 setPreferredWidthForComponentWithBrowseButton(component, childWidth);
157 public static void setPreferredWidthToFitText(@NotNull TextFieldWithBrowseButton component) {
158 int childWidth = calcWidthToFitText(component.getChildComponent(), JBUI.scale(20));
159 setPreferredWidthForComponentWithBrowseButton(component, childWidth);
162 private static <T extends JComponent> void setPreferredWidthForComponentWithBrowseButton(@NotNull ComponentWithBrowseButton<T> component,
163 int childPrefWidth) {
164 Dimension buttonPrefSize = component.getButton().getPreferredSize();
165 setPreferredWidth(component, childPrefWidth + buttonPrefSize.width);
168 public static void setPreferredWidthToFitText(@NotNull JTextField textField) {
169 setPreferredWidthToFitText(textField, JBUI.scale(15));
172 public static void setPreferredWidthToFitText(@NotNull JTextField textField, int additionalWidth) {
173 setPreferredSizeToFitText(textField, StringUtil.notNullize(textField.getText()), additionalWidth);
176 public static void setPreferredWidthToFitText(@NotNull JTextField textField, @NotNull String text) {
177 setPreferredSizeToFitText(textField, text, JBUI.scale(15));
180 private static void setPreferredSizeToFitText(@NotNull JTextField textField, @NotNull String text, int additionalWidth) {
181 int width = calcWidthToFitText(textField, text, additionalWidth);
182 setPreferredWidth(textField, width);
185 private static int calcWidthToFitText(@NotNull JTextField textField, int additionalWidth) {
186 return calcWidthToFitText(textField, textField.getText(), additionalWidth);
189 private static int calcWidthToFitText(@NotNull JTextField textField, @NotNull String text, int additionalWidth) {
190 return textField.getFontMetrics(textField.getFont()).stringWidth(text) + additionalWidth;
193 public static void adjustDialogSizeToFitPreferredSize(@NotNull DialogWrapper dialogWrapper) {
194 JRootPane rootPane = dialogWrapper.getRootPane();
195 Dimension componentSize = rootPane.getSize();
196 Dimension componentPreferredSize = rootPane.getPreferredSize();
197 if (componentPreferredSize.width <= componentSize.width && componentPreferredSize.height <= componentSize.height) {
200 int dw = Math.max(0, componentPreferredSize.width - componentSize.width);
201 int dh = Math.max(0, componentPreferredSize.height - componentSize.height);
203 Dimension oldDialogSize = dialogWrapper.getSize();
204 Dimension newDialogSize = new Dimension(oldDialogSize.width + dw, oldDialogSize.height + dh);
206 dialogWrapper.setSize(newDialogSize.width, newDialogSize.height);
207 rootPane.revalidate();
210 LOG.info("DialogWrapper '" + dialogWrapper.getTitle() + "' has been re-sized (added width: " + dw + ", added height: " + dh + ")");
213 public static void resizeDialogToFitTextFor(@NotNull final JComponent... components) {
214 if (components.length == 0) return;
215 doWithDialogWrapper(components[0], dialogWrapper -> {
216 if (dialogWrapper instanceof SettingsDialog || dialogWrapper instanceof SingleConfigurableEditor) {
217 for (Component component : components) {
218 if (component instanceof TextFieldWithHistoryWithBrowseButton) {
219 setPreferredWidthToFitText((TextFieldWithHistoryWithBrowseButton)component);
221 else if (component instanceof TextFieldWithBrowseButton) {
222 setPreferredWidthToFitText((TextFieldWithBrowseButton)component);
224 else if (component instanceof JTextField) {
225 setPreferredWidthToFitText((JTextField)component);
228 ApplicationManager.getApplication().invokeLater(() -> adjustDialogSizeToFitPreferredSize(dialogWrapper), ModalityState.any());
233 private static void doWithDialogWrapper(@NotNull final JComponent component, @NotNull final Consumer<DialogWrapper> consumer) {
234 UIUtil.invokeLaterIfNeeded(() -> {
235 if (component.getClientProperty(DIALOG_RESIZED_TO_FIT_TEXT) != null) {
238 component.putClientProperty(DIALOG_RESIZED_TO_FIT_TEXT, true);
239 DialogWrapper dialogWrapper = DialogWrapper.findInstance(component);
240 if (dialogWrapper != null) {
241 consumer.consume(dialogWrapper);
244 UiNotifyConnector.doWhenFirstShown(component, () -> {
245 DialogWrapper dialogWrapper1 = DialogWrapper.findInstance(component);
246 if (dialogWrapper1 != null) {
247 consumer.consume(dialogWrapper1);
254 public static <T> void updateItems(@NotNull JComboBox<T> comboBox,
255 @NotNull List<T> newItems,
256 @Nullable T newSelectedItemIfSelectionCannotBePreserved) {
257 if (!shouldUpdate(comboBox, newItems)) {
260 Object itemToSelect = comboBox.getSelectedItem();
261 boolean preserveSelection = true;
262 //noinspection SuspiciousMethodCalls
263 if (!newItems.contains(itemToSelect)) {
264 if (newItems.contains(newSelectedItemIfSelectionCannotBePreserved)) {
265 itemToSelect = newSelectedItemIfSelectionCannotBePreserved;
269 preserveSelection = false;
272 comboBox.removeAllItems();
273 for (T newItem : newItems) {
274 comboBox.addItem(newItem);
276 if (preserveSelection) {
277 int count = comboBox.getItemCount();
278 for (int i = 0; i < count; i++) {
279 Object item = comboBox.getItemAt(i);
280 if (ComparatorUtil.equalsNullable(itemToSelect, item)) {
281 comboBox.setSelectedIndex(i);
288 private static <T> boolean shouldUpdate(@NotNull JComboBox<T> comboBox, @NotNull List<T> newItems) {
289 int count = comboBox.getItemCount();
290 if (newItems.size() != count) {
293 for (int i = 0; i < count; i++) {
294 Object oldItem = comboBox.getItemAt(i);
295 T newItem = newItems.get(i);
296 if (!ComparatorUtil.equalsNullable(oldItem, newItem)) {
303 public static void setNoBorderCellRendererFor(@NotNull TableColumn column) {
304 final TableCellRenderer previous = column.getCellRenderer();
305 column.setCellRenderer(new DefaultTableCellRenderer() {
307 public Component getTableCellRendererComponent(JTable table,
314 if (previous != null) {
315 component = previous.getTableCellRendererComponent(table, value, isSelected, false, row, column);
318 component = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
320 if (component instanceof JComponent) {
321 ((JComponent)component).setBorder(null);
328 public static void addHistoryOnExpansion(@NotNull final TextFieldWithHistory textFieldWithHistory,
329 @NotNull final NotNullProducer<List<String>> historyProvider) {
330 textFieldWithHistory.addPopupMenuListener(new PopupMenuListener() {
332 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
333 List<String> history = historyProvider.produce();
334 setHistory(textFieldWithHistory, ContainerUtil.notNullize(history), true);
335 // one-time initialization
336 textFieldWithHistory.removePopupMenuListener(this);
340 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
344 public void popupMenuCanceled(PopupMenuEvent e) {
349 public static void setHistory(@NotNull TextFieldWithHistory textFieldWithHistory,
350 @NotNull List<String> history,
351 boolean mergeWithPrevHistory) {
352 Set<String> newHistorySet = ContainerUtil.newHashSet(history);
353 List<String> prevHistory = textFieldWithHistory.getHistory();
354 List<String> mergedHistory = ContainerUtil.newArrayListWithCapacity(history.size());
355 if (mergeWithPrevHistory) {
356 for (String item : prevHistory) {
357 if (!newHistorySet.contains(item)) {
358 mergedHistory.add(item);
362 mergedHistory.addAll(history);
363 String oldText = StringUtil.notNullize(textFieldWithHistory.getText());
364 String oldSelectedItem = ObjectUtils.tryCast(textFieldWithHistory.getSelectedItem(), String.class);
365 if (!mergedHistory.contains(oldSelectedItem)) {
366 oldSelectedItem = null;
368 textFieldWithHistory.setHistory(mergedHistory);
369 setLongestAsPrototype(textFieldWithHistory, mergedHistory);
370 if (oldSelectedItem != null) {
371 textFieldWithHistory.setSelectedItem(oldSelectedItem);
373 if (!oldText.equals(oldSelectedItem)) {
374 textFieldWithHistory.setText(oldText);
378 private static void setLongestAsPrototype(@NotNull JComboBox comboBox, @NotNull List<String> variants) {
379 Object prototypeDisplayValue = comboBox.getPrototypeDisplayValue();
380 String prototypeDisplayValueStr = null;
381 if (prototypeDisplayValue instanceof String) {
382 prototypeDisplayValueStr = (String)prototypeDisplayValue;
384 else if (prototypeDisplayValue != null) {
387 String longest = StringUtil.notNullize(prototypeDisplayValueStr);
388 boolean updated = false;
389 for (String s : variants) {
390 if (longest.length() < s.length()) {
396 comboBox.setPrototypeDisplayValue(longest);
400 public static void installFileCompletionAndBrowseDialog(@Nullable Project project,
401 @NotNull TextFieldWithHistoryWithBrowseButton textFieldWithHistoryWithBrowseButton,
402 @NotNull @Nls(capitalization = Nls.Capitalization.Title) String browseDialogTitle,
403 @NotNull FileChooserDescriptor fileChooserDescriptor) {
405 textFieldWithHistoryWithBrowseButton,
406 textFieldWithHistoryWithBrowseButton.getChildComponent().getTextEditor(),
408 fileChooserDescriptor,
409 TextComponentAccessor.TEXT_FIELD_WITH_HISTORY_WHOLE_TEXT);
412 public static void installFileCompletionAndBrowseDialog(@Nullable Project project,
413 @NotNull TextFieldWithBrowseButton textFieldWithBrowseButton,
414 @NotNull @Nls(capitalization = Nls.Capitalization.Title) String browseDialogTitle,
415 @NotNull FileChooserDescriptor fileChooserDescriptor) {
417 textFieldWithBrowseButton,
418 textFieldWithBrowseButton.getTextField(),
420 fileChooserDescriptor,
421 TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT);
424 private static <T extends JComponent> void doInstall(@Nullable Project project,
425 @NotNull ComponentWithBrowseButton<T> componentWithBrowseButton,
426 @NotNull JTextField textField,
427 @NotNull @Nls(capitalization = Nls.Capitalization.Title) String browseDialogTitle,
428 @NotNull FileChooserDescriptor fileChooserDescriptor,
429 @NotNull TextComponentAccessor<T> textComponentAccessor) {
430 fileChooserDescriptor = fileChooserDescriptor.withShowHiddenFiles(SystemInfo.isUnix);
431 componentWithBrowseButton.addBrowseFolderListener(
435 fileChooserDescriptor,
436 textComponentAccessor
438 FileChooserFactory.getInstance().installFileCompletion(
440 fileChooserDescriptor,
447 public static HyperlinkLabel createWebHyperlink(@NotNull String url) {
448 return createWebHyperlink(url, url);
452 public static HyperlinkLabel createWebHyperlink(@NotNull String text, @NotNull String url) {
453 HyperlinkLabel hyperlink = new HyperlinkLabel(text);
454 hyperlink.setHyperlinkTarget(url);
456 DefaultActionGroup actionGroup = new DefaultActionGroup();
457 actionGroup.add(new OpenLinkInBrowser(url));
458 actionGroup.add(new CopyLinkAction(url));
460 hyperlink.setComponentPopupMenu(ActionManager.getInstance().createActionPopupMenu("web hyperlink", actionGroup).getComponent());
464 public static void setPreferredWidth(@NotNull Component component, int width) {
465 Dimension preferredSize = component.getPreferredSize();
466 preferredSize.width = width;
467 component.setPreferredSize(preferredSize);
470 public static boolean scrollToReference(JEditorPane view, String reference) {
471 reference = StringUtil.trimStart(reference, "#");
472 List<String> toCheck = Arrays.asList("a", "h1", "h2", "h3", "h4");
473 Document document = view.getDocument();
474 if (document instanceof HTMLDocument) {
475 List<Element> list = new ArrayList<>();
476 for (Element root : document.getRootElements()) {
477 getAllElements(root, list, toCheck);
479 for (Element element : list) {
480 AttributeSet attributes = element.getAttributes();
481 String nm = (String)attributes.getAttribute(HTML.Attribute.NAME);
482 if (nm == null) nm = (String)attributes.getAttribute(HTML.Attribute.ID);
483 if ((nm != null) && nm.equals(reference)) {
485 int pos = element.getStartOffset();
486 Rectangle r = view.modelToView(pos);
488 Rectangle vis = view.getVisibleRect();
490 r.height = vis.height;
491 view.scrollRectToVisible(r);
495 catch (BadLocationException ex) {
504 private static void getAllElements(Element root, List<Element> list, List<String> toCheck) {
505 if (toCheck.contains(root.getName().toLowerCase(Locale.US))) {
508 for (int i = 0; i < root.getElementCount(); i++) {
509 getAllElements(root.getElement(i), list, toCheck);
513 public static class HtmlViewerBuilder {
514 private boolean myCarryTextOver;
515 private String myDisabledHtml;
517 private Color myBackground;
518 private Color myForeground;
520 public JEditorPane create() {
521 final JEditorPane textPane = new JEditorPane() {
522 private boolean myEnabled = true;
523 private String myEnabledHtml;
526 public Dimension getPreferredSize() {
527 // This trick makes text component to carry text over to the next line
528 // if the text line width exceeds parent's width
529 Dimension dimension = super.getPreferredSize();
530 if (myCarryTextOver) {
537 public void setText(String t) {
538 if (myDisabledHtml != null) {
547 public void setEnabled(boolean enabled) {
548 if (myDisabledHtml != null) {
551 setText(myEnabledHtml);
553 setText(myDisabledHtml);
555 super.setEnabled(true);
557 super.setEnabled(enabled);
561 textPane.setFont(myFont != null ? myFont : UIUtil.getLabelFont());
562 textPane.setContentType(UIUtil.HTML_MIME);
563 textPane.setEditable(false);
564 if (myBackground != null) {
565 textPane.setBackground(myBackground);
568 textPane.setOpaque(false);
570 textPane.setForeground(myForeground != null ? myForeground : UIUtil.getLabelForeground());
571 textPane.setFocusable(false);
575 public HtmlViewerBuilder setCarryTextOver(boolean carryTextOver) {
576 myCarryTextOver = carryTextOver;
580 public HtmlViewerBuilder setDisabledHtml(String disabledHtml) {
581 myDisabledHtml = disabledHtml;
585 public HtmlViewerBuilder setFont(Font font) {
590 public HtmlViewerBuilder setBackground(Color background) {
591 myBackground = background;
595 public HtmlViewerBuilder setForeground(Color foreground) {
596 myForeground = foreground;
602 public static JEditorPane createHtmlViewer(boolean lineWrap,
604 @Nullable Color background,
605 @Nullable Color foreground) {
606 final JEditorPane textPane;
608 textPane = new JEditorPane() {
610 public Dimension getPreferredSize() {
611 // This trick makes text component to carry text over to the next line
612 // if the text line width exceeds parent's width
613 Dimension dimension = super.getPreferredSize();
620 textPane = new JEditorPane();
622 textPane.setFont(font != null ? font : UIUtil.getLabelFont());
623 textPane.setEditorKit(UIUtil.getHTMLEditorKit());
624 textPane.setEditable(false);
625 if (background != null) {
626 textPane.setBackground(background);
629 NonOpaquePanel.setTransparent(textPane);
631 textPane.setForeground(foreground != null ? foreground : UIUtil.getLabelForeground());
632 textPane.setFocusable(false);
636 public static void setHtml(@NotNull JEditorPane editorPane,
637 @NotNull String bodyInnerHtml,
638 @Nullable Color foregroundColor) {
639 String html = String.format(
640 "<html><head>%s</head><body>%s</body></html>",
641 UIUtil.getCssFontDeclaration(editorPane.getFont(), foregroundColor, null, null),
644 editorPane.setText(html);
648 public static TextFieldWithHistoryWithBrowseButton createTextFieldWithHistoryWithBrowseButton(@Nullable Project project,
649 @NotNull String browseDialogTitle,
650 @NotNull FileChooserDescriptor fileChooserDescriptor,
651 @Nullable NotNullProducer<List<String>> historyProvider) {
652 TextFieldWithHistoryWithBrowseButton textFieldWithHistoryWithBrowseButton = new TextFieldWithHistoryWithBrowseButton();
653 TextFieldWithHistory textFieldWithHistory = textFieldWithHistoryWithBrowseButton.getChildComponent();
654 textFieldWithHistory.setHistorySize(-1);
655 textFieldWithHistory.setMinimumAndPreferredWidth(0);
656 if (historyProvider != null) {
657 addHistoryOnExpansion(textFieldWithHistory, historyProvider);
659 installFileCompletionAndBrowseDialog(
661 textFieldWithHistoryWithBrowseButton,
663 fileChooserDescriptor
665 return textFieldWithHistoryWithBrowseButton;
669 public static <C extends JComponent> ComponentWithBrowseButton<C> wrapWithInfoButton(@NotNull final C component,
670 @NotNull String infoButtonTooltip,
671 @NotNull ActionListener listener) {
672 ComponentWithBrowseButton<C> comp = new ComponentWithBrowseButton<>(component, listener);
673 FixedSizeButton uiHelpButton = comp.getButton();
674 uiHelpButton.setToolTipText(infoButtonTooltip);
675 uiHelpButton.setIcon(UIUtil.getBalloonInformationIcon());
676 uiHelpButton.setHorizontalAlignment(SwingConstants.CENTER);
677 uiHelpButton.setVerticalAlignment(SwingConstants.CENTER);
681 private static class CopyLinkAction extends AnAction {
683 private final String myUrl;
685 private CopyLinkAction(@NotNull String url) {
686 super("Copy Link Address", null, PlatformIcons.COPY_ICON);
691 public void update(AnActionEvent e) {
692 e.getPresentation().setEnabled(true);
696 public void actionPerformed(AnActionEvent e) {
697 Transferable content = new StringSelection(myUrl);
698 CopyPasteManager.getInstance().setContents(content);
702 private static class OpenLinkInBrowser extends AnAction {
704 private final String myUrl;
706 private OpenLinkInBrowser(@NotNull String url) {
707 super("Open Link in Browser", null, PlatformIcons.WEB_ICON);
712 public void update(AnActionEvent e) {
713 e.getPresentation().setEnabled(true);
717 public void actionPerformed(AnActionEvent e) {
718 BrowserUtil.browse(myUrl);
722 public final static String ELLIPSIS = "...";
723 public static final String ERROR_STR = "www";
724 public static String truncateStringWithEllipsis(final String text, final int maxWidth, final FontMetrics fm) {
725 return truncateStringWithEllipsis(text, maxWidth, new WidthCalculator() {
727 public int stringWidth(String s) {
728 return fm.stringWidth(s);
732 public int charWidth(char c) {
733 return fm.charWidth(c);
738 public interface WidthCalculator {
739 int stringWidth(final String s);
740 int charWidth(final char c);
743 public static String truncateStringWithEllipsis(@NotNull final String text, final int maxWidth, final WidthCalculator fm) {
744 final int error = fm.stringWidth(ERROR_STR);
745 final int wholeWidth = fm.stringWidth(text) + error;
746 if (wholeWidth <= maxWidth || text.isEmpty()) return text;
747 final int ellipsisWidth = fm.stringWidth(ELLIPSIS) + error; // plus some reserve
748 if (ellipsisWidth >= maxWidth) return ELLIPSIS;
750 final int availableWidth = maxWidth - ellipsisWidth;
751 int currentLen = (int)Math.floor(availableWidth / (((double) wholeWidth) / text.length()));
753 final String currentSubstring = text.substring(0, currentLen);
754 int realWidth = fm.stringWidth(currentSubstring);
756 if (realWidth >= availableWidth) {
758 for (int i = currentLen - 1; i >= 0; i--) {
759 if ((realWidth - delta) < availableWidth) return text.substring(0, i) + ELLIPSIS;
760 delta += fm.charWidth(currentSubstring.charAt(i));
762 return text.substring(0, 1) + ELLIPSIS;
765 for (int i = currentLen; i < text.length(); i++) {
766 if ((realWidth + delta) >= availableWidth) return text.substring(0, i) + ELLIPSIS;
767 delta += fm.charWidth(text.charAt(i));
769 return text.substring(0, currentLen) + ELLIPSIS;
773 public static JEditorPane createHtmlLabel(@NotNull final String innerHtml, @Nullable String disabledHtml,
774 @Nullable final Consumer<String> hyperlinkListener) {
775 disabledHtml = disabledHtml == null ? innerHtml : disabledHtml;
776 final Font font = UIUtil.getLabelFont();
777 String html = String.format(
778 "<html><head>%s</head><body>%s</body></html>",
779 UIUtil.getCssFontDeclaration(font, UIUtil.getInactiveTextColor(), null, null),
782 String disabled = String.format(
783 "<html><head>%s</head><body>%s</body></html>",
784 UIUtil.getCssFontDeclaration(font, UIUtil.getInactiveTextColor(), null, null),
788 final JEditorPane pane = new SwingHelper.HtmlViewerBuilder()
789 .setCarryTextOver(false)
790 .setFont(UIUtil.getLabelFont())
791 .setDisabledHtml(disabled)
794 pane.addHyperlinkListener(
795 new HyperlinkListener() {
796 public void hyperlinkUpdate(HyperlinkEvent e) {
797 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
798 if (hyperlinkListener != null) hyperlinkListener.consume(e.getURL() == null ? "" : e.getURL().toString());
799 else BrowserUtil.browse(e.getURL());