use of com.intellij.ui.components.GradientViewport in project intellij-community by JetBrains.
the class NotificationsManagerImpl method createBalloon.
@NotNull
public static Balloon createBalloon(@Nullable final JComponent windowComponent, @NotNull final Notification notification, final boolean showCallout, final boolean hideOnClickOutside, @NotNull Ref<BalloonLayoutData> layoutDataRef, @NotNull Disposable parentDisposable) {
final BalloonLayoutData layoutData = layoutDataRef.isNull() ? new BalloonLayoutData() : layoutDataRef.get();
if (layoutData.groupId == null) {
if (NotificationsConfigurationImpl.getSettings(notification.getGroupId()).isShouldLog()) {
layoutData.groupId = notification.getGroupId();
layoutData.id = notification.id;
}
} else {
layoutData.groupId = null;
layoutData.mergeData = null;
}
layoutDataRef.set(layoutData);
if (layoutData.fillColor == null) {
layoutData.fillColor = FILL_COLOR;
}
if (layoutData.borderColor == null) {
layoutData.borderColor = BORDER_COLOR;
}
boolean actions = !notification.getActions().isEmpty();
boolean showFullContent = layoutData.showFullContent || notification instanceof NotificationActionProvider;
Color foregroundR = Gray._0;
Color foregroundD = Gray._191;
final Color foreground = new JBColor(foregroundR, foregroundD);
final JEditorPane text = new JEditorPane() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (layoutData.showMinSize) {
Point location = getCollapsedTextEndLocation(this, layoutData);
if (location != null) {
g.setColor(getForeground());
g.drawString("...", location.x, location.y + g.getFontMetrics().getAscent());
}
}
}
};
text.setEditorKit(new HTMLEditorKit() {
final HTMLEditorKit kit = UIUtil.getHTMLEditorKit();
final HTMLFactory factory = new HTMLFactory() {
public View create(Element e) {
View view = super.create(e);
if (view instanceof ParagraphView) {
// wrap too long words, for example: ATEST_TABLE_SIGNLE_ROW_UPDATE_AUTOCOMMIT_A_FIK
return new ParagraphView(e) {
protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
if (r == null) {
r = new SizeRequirements();
}
r.minimum = (int) layoutPool.getMinimumSpan(axis);
r.preferred = Math.max(r.minimum, (int) layoutPool.getPreferredSpan(axis));
r.maximum = Integer.MAX_VALUE;
r.alignment = 0.5f;
return r;
}
};
}
return view;
}
};
@Override
public StyleSheet getStyleSheet() {
return kit.getStyleSheet();
}
@Override
public ViewFactory getViewFactory() {
return factory;
}
});
text.setForeground(foreground);
final HyperlinkListener listener = NotificationsUtil.wrapListener(notification);
if (listener != null) {
text.addHyperlinkListener(listener);
}
String fontStyle = NotificationsUtil.getFontStyle();
int prefSize = new JLabel(NotificationsUtil.buildHtml(notification, null, true, null, fontStyle)).getPreferredSize().width;
String style = prefSize > BalloonLayoutConfiguration.MaxWidth ? BalloonLayoutConfiguration.MaxWidthStyle : null;
if (layoutData.showFullContent) {
style = prefSize > BalloonLayoutConfiguration.MaxFullContentWidth ? BalloonLayoutConfiguration.MaxFullContentWidthStyle : null;
}
String textR = NotificationsUtil.buildHtml(notification, style, true, foregroundR, fontStyle);
String textD = NotificationsUtil.buildHtml(notification, style, true, foregroundD, fontStyle);
LafHandler lafHandler = new LafHandler(text, textR, textD);
layoutData.lafHandler = lafHandler;
text.setEditable(false);
text.setOpaque(false);
if (UIUtil.isUnderNimbusLookAndFeel()) {
text.setBackground(UIUtil.TRANSPARENT_COLOR);
}
text.setBorder(null);
final JPanel content = new NonOpaquePanel(new BorderLayout());
if (text.getCaret() != null) {
text.setCaretPosition(0);
}
final JScrollPane pane = createBalloonScrollPane(text, false);
pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
JScrollBar scrollBar = pane.getVerticalScrollBar();
if (layoutData.showMinSize && scrollBar.getValue() > 0) {
scrollBar.removeAdjustmentListener(this);
scrollBar.setValue(0);
scrollBar.addAdjustmentListener(this);
}
}
});
LinkLabel<Void> expandAction = null;
int lines = 3;
if (notification.hasTitle()) {
lines--;
}
if (actions) {
lines--;
}
layoutData.fullHeight = text.getPreferredSize().height;
layoutData.twoLineHeight = calculateContentHeight(lines);
layoutData.maxScrollHeight = Math.min(layoutData.fullHeight, calculateContentHeight(10));
layoutData.configuration = BalloonLayoutConfiguration.create(notification, layoutData, actions);
if (layoutData.welcomeScreen) {
layoutData.maxScrollHeight = layoutData.fullHeight;
} else if (!showFullContent && layoutData.maxScrollHeight != layoutData.fullHeight) {
pane.setViewport(new GradientViewport(text, JBUI.insets(10, 0), true) {
@Nullable
@Override
protected Color getViewColor() {
return layoutData.fillColor;
}
@Override
protected void paintGradient(Graphics g) {
if (!layoutData.showMinSize) {
super.paintGradient(g);
}
}
});
}
configureBalloonScrollPane(pane, layoutData.fillColor);
if (showFullContent) {
pane.setPreferredSize(text.getPreferredSize());
} else if (layoutData.twoLineHeight < layoutData.fullHeight) {
text.setPreferredSize(null);
Dimension size = text.getPreferredSize();
size.height = layoutData.twoLineHeight;
text.setPreferredSize(size);
text.setSize(size);
layoutData.showMinSize = true;
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
pane.setPreferredSize(size);
text.setCaret(new TextCaret(layoutData));
expandAction = new LinkLabel<>(null, AllIcons.Ide.Notification.Expand, new LinkListener<Void>() {
@Override
public void linkSelected(LinkLabel link, Void ignored) {
layoutData.showMinSize = !layoutData.showMinSize;
text.setPreferredSize(null);
Dimension size = text.getPreferredSize();
if (layoutData.showMinSize) {
size.height = layoutData.twoLineHeight;
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
link.setIcon(AllIcons.Ide.Notification.Expand);
link.setHoveringIcon(AllIcons.Ide.Notification.ExpandHover);
} else {
text.select(0, 0);
size.height = layoutData.fullHeight;
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
link.setIcon(AllIcons.Ide.Notification.Collapse);
link.setHoveringIcon(AllIcons.Ide.Notification.CollapseHover);
}
text.setPreferredSize(size);
text.setSize(size);
if (!layoutData.showMinSize) {
size = new Dimension(size.width, layoutData.maxScrollHeight);
}
pane.setPreferredSize(size);
content.doLayout();
layoutData.doLayout.run();
}
});
expandAction.setHoveringIcon(AllIcons.Ide.Notification.ExpandHover);
}
final CenteredLayoutWithActions layout = new CenteredLayoutWithActions(text, layoutData);
JPanel centerPanel = new NonOpaquePanel(layout) {
@Override
protected void paintChildren(Graphics g) {
super.paintChildren(g);
Component title = layout.getTitle();
if (title != null && layoutData.showActions != null && layoutData.showActions.compute()) {
int width = layoutData.configuration.allActionsOffset;
int x = getWidth() - width - JBUI.scale(5);
int y = layoutData.configuration.topSpaceHeight;
int height = title instanceof JEditorPane ? getFirstLineHeight((JEditorPane) title) : title.getHeight();
g.setColor(layoutData.fillColor);
g.fillRect(x, y, width, height);
width = layoutData.configuration.beforeGearSpace;
x -= width;
((Graphics2D) g).setPaint(new GradientPaint(x, y, ColorUtil.withAlpha(layoutData.fillColor, 0.2), x + width, y, layoutData.fillColor));
g.fillRect(x, y, width, height);
}
}
};
content.add(centerPanel, BorderLayout.CENTER);
if (notification.hasTitle()) {
String titleStyle = StringUtil.defaultIfEmpty(fontStyle, "") + "white-space:nowrap;";
String titleR = NotificationsUtil.buildHtml(notification, titleStyle, false, foregroundR, null);
String titleD = NotificationsUtil.buildHtml(notification, titleStyle, false, foregroundD, null);
JLabel title = new JLabel();
lafHandler.setTitle(title, titleR, titleD);
title.setOpaque(false);
if (UIUtil.isUnderNimbusLookAndFeel()) {
title.setBackground(UIUtil.TRANSPARENT_COLOR);
}
title.setForeground(foreground);
centerPanel.add(title, BorderLayout.NORTH);
}
if (expandAction != null) {
centerPanel.add(expandAction, BorderLayout.EAST);
}
if (notification.hasContent()) {
centerPanel.add(layoutData.welcomeScreen ? text : pane, BorderLayout.CENTER);
}
if (!layoutData.welcomeScreen) {
final Icon icon = NotificationsUtil.getIcon(notification);
JComponent iconComponent = new JComponent() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
icon.paintIcon(this, g, layoutData.configuration.iconOffset.width, layoutData.configuration.iconOffset.height);
}
};
iconComponent.setOpaque(false);
iconComponent.setPreferredSize(new Dimension(layoutData.configuration.iconPanelWidth, 2 * layoutData.configuration.iconOffset.height + icon.getIconHeight()));
content.add(iconComponent, BorderLayout.WEST);
}
JPanel buttons = createButtons(notification, content, listener);
if (buttons != null) {
layoutData.groupId = null;
layoutData.mergeData = null;
buttons.setBorder(new EmptyBorder(0, 0, JBUI.scale(5), JBUI.scale(7)));
}
HoverAdapter hoverAdapter = new HoverAdapter();
hoverAdapter.addSource(content);
hoverAdapter.addSource(centerPanel);
hoverAdapter.addSource(text);
hoverAdapter.addSource(pane);
if (buttons == null && actions) {
createActionPanel(notification, centerPanel, layoutData.configuration.actionGap, hoverAdapter);
}
if (expandAction != null) {
hoverAdapter.addComponent(expandAction, component -> {
Rectangle bounds;
Point location = SwingUtilities.convertPoint(content.getParent(), content.getLocation(), component.getParent());
if (layoutData.showMinSize) {
Component centerComponent = layoutData.welcomeScreen ? text : pane;
Point centerLocation = SwingUtilities.convertPoint(centerComponent.getParent(), centerComponent.getLocation(), component.getParent());
bounds = new Rectangle(location.x, centerLocation.y, content.getWidth(), centerComponent.getHeight());
} else {
bounds = new Rectangle(location.x, component.getY(), content.getWidth(), component.getHeight());
JBInsets.addTo(bounds, JBUI.insets(5, 0, 7, 0));
}
return bounds;
});
}
hoverAdapter.initListeners();
if (layoutData.mergeData != null) {
createMergeAction(layoutData, content);
}
text.setSize(text.getPreferredSize());
Dimension paneSize = new Dimension(text.getPreferredSize());
int maxWidth = JBUI.scale(600);
if (windowComponent != null) {
maxWidth = Math.min(maxWidth, windowComponent.getWidth() - 20);
}
if (paneSize.width > maxWidth) {
pane.setPreferredSize(new Dimension(maxWidth, paneSize.height + UIUtil.getScrollBarWidth()));
}
final BalloonBuilder builder = JBPopupFactory.getInstance().createBalloonBuilder(content);
builder.setFillColor(layoutData.fillColor).setCloseButtonEnabled(buttons == null).setShowCallout(showCallout).setShadow(false).setAnimationCycle(200).setHideOnClickOutside(hideOnClickOutside).setHideOnAction(hideOnClickOutside).setHideOnKeyOutside(hideOnClickOutside).setHideOnFrameResize(false).setBorderColor(layoutData.borderColor).setBorderInsets(JBUI.emptyInsets());
if (layoutData.fadeoutTime != 0) {
builder.setFadeoutTime(layoutData.fadeoutTime);
}
final BalloonImpl balloon = (BalloonImpl) builder.createBalloon();
balloon.setAnimationEnabled(false);
notification.setBalloon(balloon);
balloon.setShadowBorderProvider(new NotificationBalloonShadowBorderProvider(layoutData.fillColor, layoutData.borderColor));
if (!layoutData.welcomeScreen && buttons == null) {
balloon.setActionProvider(new NotificationBalloonActionProvider(balloon, layout.getTitle(), layoutData, notification.getGroupId()));
}
Disposer.register(parentDisposable, balloon);
return balloon;
}
use of com.intellij.ui.components.GradientViewport in project intellij-community by JetBrains.
the class ShortcutDialog method createCenterPanel.
@Override
protected JComponent createCenterPanel() {
JLabel icon = new JLabel(AllIcons.General.BalloonWarning);
icon.setVerticalAlignment(SwingConstants.TOP);
JLabel label = new JLabel(KeyMapBundle.message("dialog.conflicts.text"));
label.setBorder(JBUI.Borders.emptyLeft(2));
JScrollPane scroll = ScrollPaneFactory.createScrollPane(null, true);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setViewport(new GradientViewport(myConflictsContainer, JBUI.insets(5), false));
scroll.getVerticalScrollBar().setOpaque(false);
scroll.getViewport().setOpaque(false);
scroll.setOpaque(false);
JBPanel panel = new JBPanel(new BorderLayout());
panel.add(BorderLayout.NORTH, label);
panel.add(BorderLayout.CENTER, scroll);
myConflictsPanel.add(BorderLayout.WEST, icon);
myConflictsPanel.add(BorderLayout.CENTER, panel);
myConflictsContainer.setOpaque(false);
return myConflictsPanel;
}
use of com.intellij.ui.components.GradientViewport in project intellij-community by JetBrains.
the class ConfigurableCardPanel method createConfigurableComponent.
/**
* Creates UI component for the specified configurable.
* If a component is created successfully the configurable will be reset.
* If the configurable implements {@link MasterDetails},
* created component will not have the following modifications.
* If the configurable does not implement {@link Configurable.NoMargin},
* this method sets an empty border with default margins for created component.
* If the configurable does not implement {@link Configurable.NoScroll},
* this method adds a scroll bars for created component.
*/
public static JComponent createConfigurableComponent(Configurable configurable) {
return configurable == null ? null : ApplicationManager.getApplication().runReadAction(new Computable<JComponent>() {
@Override
public JComponent compute() {
JComponent component = null;
long time = System.currentTimeMillis();
try {
component = configurable.createComponent();
} catch (Exception unexpected) {
LOG.error("cannot create configurable component", unexpected);
} finally {
warn(configurable, "create", time);
}
if (component != null) {
reset(configurable);
if (ConfigurableWrapper.cast(MasterDetails.class, configurable) == null) {
if (ConfigurableWrapper.cast(Configurable.NoMargin.class, configurable) == null) {
if (!component.getClass().equals(JPanel.class)) {
// some custom components do not support borders
JPanel panel = new JPanel(new BorderLayout());
panel.add(BorderLayout.CENTER, component);
component = panel;
}
component.setBorder(JBUI.Borders.empty(5, 10, 10, 10));
}
if (ConfigurableWrapper.cast(Configurable.NoScroll.class, configurable) == null) {
JScrollPane scroll = ScrollPaneFactory.createScrollPane(null, true);
scroll.setViewport(new GradientViewport(component, JBUI.insetsTop(5), true));
component = scroll;
}
}
}
return component;
}
});
}
Aggregations