update the lookup renderer width when resized

This commit is contained in:
peter
2011-09-20 17:27:56 +02:00
parent 8096a204c6
commit 97935608d3
2 changed files with 25 additions and 31 deletions
@@ -40,6 +40,8 @@ import javax.swing.border.EmptyBorder;
import java.awt.*;
public class LookupCellRenderer implements ListCellRenderer {
private static final int AFTER_TAIL = 10;
private static final int AFTER_TYPE = 6;
private Icon myEmptyIcon = EmptyIcon.create(5);
private final Font myNormalFont;
private final Font myBoldFont;
@@ -90,10 +92,10 @@ public class LookupCellRenderer implements ListCellRenderer {
myPanel = new LookupPanel();
myPanel.add(myNameComponent, BorderLayout.WEST);
myPanel.add(myTailComponent, BorderLayout.CENTER);
myTailComponent.setBorder(new EmptyBorder(0, 0, 0, 10));
myTailComponent.setBorder(new EmptyBorder(0, 0, 0, AFTER_TAIL));
myPanel.add(myTypeLabel, BorderLayout.EAST);
myTypeLabel.setBorder(new EmptyBorder(0, 0, 0, 6));
myTypeLabel.setBorder(new EmptyBorder(0, 0, 0, AFTER_TYPE));
myNormalMetrics = myLookup.getEditor().getComponent().getFontMetrics(myNormalFont);
myBoldMetrics = myLookup.getEditor().getComponent().getFontMetrics(myBoldFont);
@@ -117,12 +119,12 @@ public class LookupCellRenderer implements ListCellRenderer {
final Color foreground = isSelected ? SELECTED_FOREGROUND_COLOR : FOREGROUND_COLOR;
final Color background = getItemBackground(list, index, isSelected);
int allowedWidth = list.getWidth() - getCommonGapsWidth() - getIconIndent();
int allowedWidth = list.getWidth() - AFTER_TAIL - AFTER_TYPE - getIconIndent();
final LookupElementPresentation presentation = new RealLookupElementPresentation(allowedWidth, myNormalMetrics, myBoldMetrics);
item.renderElement(presentation);
myNameComponent.clear();
myNameComponent.setIcon(getIcon(presentation.getIcon()));
myNameComponent.setIcon(augmentIcon(presentation.getIcon(), myEmptyIcon));
myNameComponent.setBackground(background);
allowedWidth -= setItemTextLabel(item, foreground, isSelected, presentation, allowedWidth);
@@ -293,10 +295,6 @@ public class LookupCellRenderer implements ListCellRenderer {
return used;
}
private Icon getIcon(Icon icon){
return augmentIcon(icon, myEmptyIcon);
}
public static Icon augmentIcon(@Nullable Icon icon, @NotNull Icon standard) {
if (icon == null) {
return standard;
@@ -318,11 +316,7 @@ public class LookupCellRenderer implements ListCellRenderer {
myEmptyIcon = new EmptyIcon(Math.max(icon.getIconWidth(), myEmptyIcon.getIconWidth()), Math.max(icon.getIconHeight(), myEmptyIcon.getIconHeight()));
}
return RealLookupElementPresentation.calculateWidth(p, myNormalMetrics, myBoldMetrics) + getCommonGapsWidth();
}
private int getCommonGapsWidth() {
return 2 * myNormalMetrics.stringWidth("W"); //tail-type separation and a space after type
return RealLookupElementPresentation.calculateWidth(p, myNormalMetrics, myBoldMetrics) + AFTER_TAIL + AFTER_TYPE;
}
public int getIconIndent() {
@@ -333,7 +327,6 @@ public class LookupCellRenderer implements ListCellRenderer {
private static class MySimpleColoredComponent extends SimpleColoredComponent {
private MySimpleColoredComponent() {
setFocusBorderAroundIcon(true);
setBorderInsets(new Insets(0, 0, 0, 0));
}
@Override
@@ -443,9 +443,6 @@ public class LookupImpl extends LightweightHint implements LookupEx, Disposable
updateListHeight(listModel);
if (!model.isEmpty()) {
int listWidth = Math.min(myLookupTextWidth + myCellRenderer.getIconIndent(), UISettings.getInstance().MAX_LOOKUP_LIST_WIDTH);
myList.setFixedCellWidth(Math.max(listWidth, myAdComponent.getAdComponent().getPreferredSize().width));
LookupElement first = model.iterator().next();
if (isFocused() && (!(isExactPrefixItem(first, true) || isExactPrefixItem(first, false)) || mySelectionTouched)) {
restoreSelection(oldSelected, hasPreselected, oldInvariant, snapshot.second);
@@ -536,10 +533,6 @@ public class LookupImpl extends LightweightHint implements LookupEx, Disposable
private void addEmptyItem(DefaultListModel model) {
LookupItem<String> item = new EmptyLookupItem(myCalculating ? " " : LangBundle.message("completion.no.suggestions"));
myMatchers.put(item, new CamelHumpMatcher(""));
if (!myCalculating) {
myList.setFixedCellWidth(Math.max(myCellRenderer.updateMaximumWidth(renderItemApproximately(item)), myLookupTextWidth));
}
model.addElement(item);
}
@@ -1274,12 +1267,13 @@ public class LookupImpl extends LightweightHint implements LookupEx, Disposable
LOG.error("Null root pane");
}
updateScrollbarVisibility();
if (myResizePending) {
myResizePending = false;
pack();
}
updateScrollbarVisibility();
updateLookupLocation();
if (reused || selectionVisible) {
@@ -1358,8 +1352,12 @@ public class LookupImpl extends LightweightHint implements LookupEx, Disposable
setLayout(new AbstractLayoutManager() {
@Override
public Dimension preferredLayoutSize(Container parent) {
return mainPanel.getPreferredSize();
public Dimension preferredLayoutSize(@Nullable Container parent) {
int maxCellWidth = myLookupTextWidth + myCellRenderer.getIconIndent();
int width = Math.max(myScrollPane.getPreferredSize().width - myScrollPane.getViewport().getPreferredSize().width + maxCellWidth,
myAdComponent.getAdComponent().getPreferredSize().width);
return new Dimension(Math.min(width, UISettings.getInstance().MAX_LOOKUP_LIST_WIDTH),
mainPanel.getPreferredSize().height);
}
@Override
@@ -1368,16 +1366,19 @@ public class LookupImpl extends LightweightHint implements LookupEx, Disposable
mainPanel.setSize(size);
mainPanel.validate();
Dimension preferredSize = mainPanel.getPreferredSize();
if (preferredSize.width != size.width) {
UISettings.getInstance().MAX_LOOKUP_LIST_WIDTH = Math.max(300, myScrollPane.getViewport().getWidth());
}
if (!myResizePending) {
Dimension preferredSize = preferredLayoutSize(null);
if (preferredSize.width != size.width) {
UISettings.getInstance().MAX_LOOKUP_LIST_WIDTH = Math.max(300, size.width);
}
int listHeight = myList.getLastVisibleIndex() - myList.getFirstVisibleIndex() + 1;
if (listHeight != myList.getModel().getSize() && listHeight != myList.getVisibleRowCount() && preferredSize.height != size.height) {
UISettings.getInstance().MAX_LOOKUP_ITEM_COUNT = Math.max(5, listHeight);
int listHeight = myList.getLastVisibleIndex() - myList.getFirstVisibleIndex() + 1;
if (listHeight != myList.getModel().getSize() && listHeight != myList.getVisibleRowCount() && preferredSize.height != size.height) {
UISettings.getInstance().MAX_LOOKUP_ITEM_COUNT = Math.max(5, listHeight);
}
}
myList.setFixedCellWidth(myScrollPane.getViewport().getWidth());
layoutStatusIcons();
layoutHint();