int totalMinWidth = 0;
List<RibbonTask> visibleTasks = getCurrentlyShownRibbonTasks();
Map<JRibbonTaskToggleButton, Integer> diffMap = new HashMap<JRibbonTaskToggleButton, Integer>();
int totalDiff = 0;
for (RibbonTask task : visibleTasks) {
JRibbonTaskToggleButton tabButton = taskToggleButtons.get(task);
int pw = tabButton.getPreferredSize().width;
int mw = tabButton.getMinimumSize().width;
diffMap.put(tabButton, pw - mw);
totalDiff += (pw - mw);
totalPrefWidth += pw;
totalMinWidth += mw;
}
totalPrefWidth += tabButtonGap * visibleTasks.size();
totalMinWidth += tabButtonGap * visibleTasks.size();
boolean ltr = c.getComponentOrientation().isLeftToRight();
// do we have enough width?
if (totalPrefWidth <= c.getWidth()) {
// compute bounds for the tab buttons
int x = ltr ? 0 : c.getWidth();
for (RibbonTask task : visibleTasks) {
JRibbonTaskToggleButton tabButton = taskToggleButtons
.get(task);
int pw = tabButton.getPreferredSize().width;
if (ltr) {
tabButton.setBounds(x, y + 1, pw,
taskToggleButtonHeight - 1);
x += (pw + tabButtonGap);
} else {
tabButton.setBounds(x - pw, y + 1, pw,
taskToggleButtonHeight - 1);
x -= (pw + tabButtonGap);
}
tabButton.setActionRichTooltip(null);
}
((JComponent) c).putClientProperty(
TaskToggleButtonsHostPanel.IS_SQUISHED, null);
} else {
if (totalMinWidth > c.getWidth()) {
throw new IllegalStateException(
"Available width not enough to host minimized task tab buttons");
}
int x = ltr ? 0 : c.getWidth();
// how much do we need to take from each toggle button?
int toDistribute = totalPrefWidth - c.getWidth() + 2;
for (RibbonTask task : visibleTasks) {
JRibbonTaskToggleButton tabButton = taskToggleButtons
.get(task);
int pw = tabButton.getPreferredSize().width;
int delta = (toDistribute * diffMap.get(tabButton) / totalDiff);
int finalWidth = pw - delta;
if (ltr) {
tabButton.setBounds(x, y + 1, finalWidth,
taskToggleButtonHeight - 1);
x += (finalWidth + tabButtonGap);
} else {
tabButton.setBounds(x - finalWidth, y + 1, finalWidth,
taskToggleButtonHeight - 1);
x -= (finalWidth + tabButtonGap);
}
// show the tooltip with the full title
RichTooltip tooltip = new RichTooltip();
tooltip.setTitle(task.getTitle());
tabButton.setActionRichTooltip(tooltip);
}
((JComponent) c).putClientProperty(
TaskToggleButtonsHostPanel.IS_SQUISHED, Boolean.TRUE);
}
}