private void handleOverflowAddittion(int visibleButtons) { Component[] comps = getAllComponents(); removeAll(); overflowToolbar.setOrientation(getOrientation() == HORIZONTAL ? VERTICAL : HORIZONTAL); popup.removeAll(); for (Component comp : comps) { if (visibleButtons > 0) { add(comp); if (comp.isVisible()) { visibleButtons--; } } else { overflowToolbar.add(comp); } } popup.add(overflowToolbar); add(overflowButton); }
@Override public void addNotify() { super.addNotify(); if (!Boolean.TRUE.equals(getClientProperty(PROP_JDEV_DISABLE_OVERFLOW))) { addComponentListener(getComponentListener()); Toolkit.getDefaultToolkit().addAWTEventListener(getAWTEventListener(), AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK); } }
@Override public void validate() { if (!Boolean.TRUE.equals(getClientProperty(PROP_JDEV_DISABLE_OVERFLOW))) { int visibleButtons = computeVisibleButtons(); if (visibleButtons == -1) { handleOverflowRemoval(); } else { handleOverflowAddittion(visibleButtons); } } super.validate(); }
private Component[] getAllComponents() { Component[] toolbarComps; Component[] overflowComps = overflowToolbar.getComponents(); if (overflowComps.length == 0) { toolbarComps = getComponents(); } else { if (getComponentCount() > 0) { toolbarComps = new Component[getComponents().length - 1]; System.arraycopy(getComponents(), 0, toolbarComps, 0, toolbarComps.length); } else { toolbarComps = new Component[0]; } } Component[] comps = new Component[toolbarComps.length + overflowComps.length]; System.arraycopy(toolbarComps, 0, comps, 0, toolbarComps.length); System.arraycopy(overflowComps, 0, comps, toolbarComps.length, overflowComps.length); return comps; } }
private int computeVisibleButtons() { if (isShowing()) { int w = getOrientation() == HORIZONTAL ? overflowButton.getIcon().getIconWidth() + 4 : getWidth() - getInsets().left - getInsets().right; int h = getOrientation() == HORIZONTAL ? getHeight() - getInsets().top - getInsets().bottom : overflowButton.getIcon().getIconHeight() + 4; overflowButton.setMaximumSize(new Dimension(w, h)); overflowButton.setMinimumSize(new Dimension(w, h)); overflowButton.setPreferredSize(new Dimension(w, h)); handleIconResize(); Component[] comps = getAllComponents(); int sizeSoFar = 0; int maxSize = getOrientation() == HORIZONTAL ? getWidth() : getHeight(); int overflowButtonSize = getOrientation() == HORIZONTAL ? overflowButton.getPreferredSize().width : overflowButton.getPreferredSize().height; int showingButtons = 0; // all that return true from isVisible() int visibleButtons = 0; // all visible that fit into the given space (maxSize) Insets insets = getInsets(); if( null != insets ) { sizeSoFar = getOrientation() == HORIZONTAL ? insets.left+insets.right : insets.top+insets.bottom; int size = getOrientation() == HORIZONTAL ? comp.getPreferredSize().width : comp.getPreferredSize().height; if (sizeSoFar + size <= maxSize) { sizeSoFar += size;
@Override public Dimension getPreferredSize() { Component[] comps = getAllComponents(); Insets insets = getInsets(); int width = null == insets ? 0 : insets.left + insets.right; int height = null == insets ? 0 : insets.top + insets.bottom; for (int i = 0; i < comps.length; i++) { Component comp = comps[i]; if (!comp.isVisible()) { continue; } width += getOrientation() == HORIZONTAL ? comp.getPreferredSize().width : comp.getPreferredSize().height; height = Math.max( height, getOrientation() == HORIZONTAL ? comp.getPreferredSize().height : comp.getPreferredSize().width ); } if(overflowToolbar.getComponentCount() > 0) { width += getOrientation() == HORIZONTAL ? overflowButton.getPreferredSize().width : overflowButton.getPreferredSize().height; } Dimension dim = getOrientation() == HORIZONTAL ? new Dimension(width, height) : new Dimension(height, width); return dim; }
private void handleOverflowRemoval() { if (overflowToolbar.getComponents().length > 0) { remove(overflowButton); handleIconResize(); for (Component comp : overflowToolbar.getComponents()) { add(comp); } overflowToolbar.removeAll(); popup.removeAll(); } }
private void handleIconResize() { for (Component comp : overflowToolbar.getComponents()) { boolean smallToolbarIcons = getClientProperty(PROP_PREF_ICON_SIZE) == null; if (smallToolbarIcons) { ((JComponent) comp).putClientProperty(PROP_PREF_ICON_SIZE, null); } else { ((JComponent) comp).putClientProperty(PROP_PREF_ICON_SIZE, Integer.valueOf(24)); } } }
String testingFramework = Manager.getInstance().getTestingFramework(); JToolBar toolbar = new ToolbarWithOverflow(SwingConstants.VERTICAL); toolbar.add(rerunButton); toolbar.add(rerunFailedButton);