mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-15 12:29:26 +01:00
Updated MenuScroller with latest from https://tips4java.wordpress.com/2009/02/01/menu-scroller/
Reviewed its getMaximumItems method (now getMaximumDrawableMenuItems) so that it returns a 1/3rd less the max number, avoiding a bad crash on linuxes. Fixes #3174
This commit is contained in:
parent
d8b6619de0
commit
17598368ac
@ -3,43 +3,31 @@
|
|||||||
*/
|
*/
|
||||||
package processing.app.tools;
|
package processing.app.tools;
|
||||||
|
|
||||||
import java.awt.Color;
|
import javax.swing.*;
|
||||||
import java.awt.Component;
|
import javax.swing.event.ChangeEvent;
|
||||||
import java.awt.Dimension;
|
import javax.swing.event.ChangeListener;
|
||||||
import java.awt.Graphics;
|
import javax.swing.event.PopupMenuEvent;
|
||||||
|
import javax.swing.event.PopupMenuListener;
|
||||||
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.MouseWheelEvent;
|
import java.awt.event.MouseWheelEvent;
|
||||||
import java.awt.event.MouseWheelListener;
|
import java.awt.event.MouseWheelListener;
|
||||||
|
|
||||||
import javax.swing.Icon;
|
|
||||||
import javax.swing.JComponent;
|
|
||||||
import javax.swing.JMenu;
|
|
||||||
import javax.swing.JMenuItem;
|
|
||||||
import javax.swing.JPopupMenu;
|
|
||||||
import javax.swing.MenuSelectionManager;
|
|
||||||
import javax.swing.Timer;
|
|
||||||
import javax.swing.event.ChangeEvent;
|
|
||||||
import javax.swing.event.ChangeListener;
|
|
||||||
import javax.swing.event.PopupMenuEvent;
|
|
||||||
import javax.swing.event.PopupMenuListener;
|
|
||||||
import javax.swing.plaf.ButtonUI;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that provides scrolling capabilities to a long menu dropdown or
|
* A class that provides scrolling capabilities to a long menu dropdown or
|
||||||
* popup menu. A number of items can optionally be frozen at the top and/or
|
* popup menu. A number of items can optionally be frozen at the top and/or
|
||||||
* bottom of the menu.
|
* bottom of the menu.
|
||||||
* <P>
|
* <p/>
|
||||||
* <B>Implementation note:</B> The default number of items to display
|
* <B>Implementation note:</B> The default number of items to display
|
||||||
* at a time is 15, and the default scrolling interval is 125 milliseconds.
|
* at a time is 15, and the default scrolling interval is 125 milliseconds.
|
||||||
* <P>
|
* <p/>
|
||||||
*
|
*
|
||||||
* @version 1.5.0 04/05/12
|
|
||||||
* @author Darryl
|
* @author Darryl
|
||||||
|
* @version 1.5.0 04/05/12
|
||||||
*/
|
*/
|
||||||
public class MenuScroller {
|
public class MenuScroller {
|
||||||
|
|
||||||
//private JMenu menu;
|
|
||||||
private JPopupMenu menu;
|
private JPopupMenu menu;
|
||||||
private Component[] menuItems;
|
private Component[] menuItems;
|
||||||
private MenuScrollItem upItem;
|
private MenuScrollItem upItem;
|
||||||
@ -53,25 +41,6 @@ public class MenuScroller {
|
|||||||
private int firstIndex = 0;
|
private int firstIndex = 0;
|
||||||
private int keepVisibleIndex = -1;
|
private int keepVisibleIndex = -1;
|
||||||
|
|
||||||
private static int getMaximumItems(JPopupMenu menu) {
|
|
||||||
JMenuItem test = new JMenuItem("test");
|
|
||||||
ButtonUI ui = test.getUI();
|
|
||||||
Dimension d = ui.getPreferredSize(test);
|
|
||||||
double item_height = d.getHeight();
|
|
||||||
//System.out.println("JMenuItem Height " + item_height);
|
|
||||||
JMenuItem up = new JMenuItem(MenuIcon.UP);
|
|
||||||
ui = up.getUI();
|
|
||||||
d = ui.getPreferredSize(up);
|
|
||||||
double icon_height = d.getHeight();
|
|
||||||
//System.out.println("icon item height " + icon_height);
|
|
||||||
double menu_border_height = 8.0; // kludge - how to detect this?
|
|
||||||
double screen_height = java.awt.Toolkit.getDefaultToolkit().getScreenSize().getHeight();
|
|
||||||
//System.out.println("screen height " + screen_height);
|
|
||||||
int n = (int)((screen_height - icon_height * 2 - menu_border_height) / item_height);
|
|
||||||
//System.out.println("max items " + n);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a menu to be scrolled with the default number of items to
|
* Registers a menu to be scrolled with the default number of items to
|
||||||
* display at a time and the default scrolling interval.
|
* display at a time and the default scrolling interval.
|
||||||
@ -159,9 +128,9 @@ public class MenuScroller {
|
|||||||
* @param interval the scroll interval, in milliseconds
|
* @param interval the scroll interval, in milliseconds
|
||||||
* @param topFixedCount the number of items to fix at the top. May be 0.
|
* @param topFixedCount the number of items to fix at the top. May be 0.
|
||||||
* @param bottomFixedCount the number of items to fix at the bottom. May be 0
|
* @param bottomFixedCount the number of items to fix at the bottom. May be 0
|
||||||
|
* @return the MenuScroller
|
||||||
* @throws IllegalArgumentException if scrollCount or interval is 0 or
|
* @throws IllegalArgumentException if scrollCount or interval is 0 or
|
||||||
* negative or if topFixedCount or bottomFixedCount is negative
|
* negative or if topFixedCount or bottomFixedCount is negative
|
||||||
* @return the MenuScroller
|
|
||||||
*/
|
*/
|
||||||
public static MenuScroller setScrollerFor(JMenu menu, int scrollCount, int interval,
|
public static MenuScroller setScrollerFor(JMenu menu, int scrollCount, int interval,
|
||||||
int topFixedCount, int bottomFixedCount) {
|
int topFixedCount, int bottomFixedCount) {
|
||||||
@ -180,9 +149,9 @@ public class MenuScroller {
|
|||||||
* @param interval the scroll interval, in milliseconds
|
* @param interval the scroll interval, in milliseconds
|
||||||
* @param topFixedCount the number of items to fix at the top. May be 0
|
* @param topFixedCount the number of items to fix at the top. May be 0
|
||||||
* @param bottomFixedCount the number of items to fix at the bottom. May be 0
|
* @param bottomFixedCount the number of items to fix at the bottom. May be 0
|
||||||
|
* @return the MenuScroller
|
||||||
* @throws IllegalArgumentException if scrollCount or interval is 0 or
|
* @throws IllegalArgumentException if scrollCount or interval is 0 or
|
||||||
* negative or if topFixedCount or bottomFixedCount is negative
|
* negative or if topFixedCount or bottomFixedCount is negative
|
||||||
* @return the MenuScroller
|
|
||||||
*/
|
*/
|
||||||
public static MenuScroller setScrollerFor(JPopupMenu menu, int scrollCount, int interval,
|
public static MenuScroller setScrollerFor(JPopupMenu menu, int scrollCount, int interval,
|
||||||
int topFixedCount, int bottomFixedCount) {
|
int topFixedCount, int bottomFixedCount) {
|
||||||
@ -198,7 +167,7 @@ public class MenuScroller {
|
|||||||
* @param menu the menu
|
* @param menu the menu
|
||||||
*/
|
*/
|
||||||
public MenuScroller(JMenu menu) {
|
public MenuScroller(JMenu menu) {
|
||||||
this(menu, -1);
|
this(menu, 15);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -209,7 +178,7 @@ public class MenuScroller {
|
|||||||
* @param menu the popup menu
|
* @param menu the popup menu
|
||||||
*/
|
*/
|
||||||
public MenuScroller(JPopupMenu menu) {
|
public MenuScroller(JPopupMenu menu) {
|
||||||
this(menu, -1);
|
this(menu, 15);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -302,11 +271,10 @@ public class MenuScroller {
|
|||||||
public MenuScroller(JPopupMenu menu, int scrollCount, int interval,
|
public MenuScroller(JPopupMenu menu, int scrollCount, int interval,
|
||||||
int topFixedCount, int bottomFixedCount) {
|
int topFixedCount, int bottomFixedCount) {
|
||||||
|
|
||||||
if(scrollCount == -1)
|
int autoSizeScrollCount = getMaximumDrawableMenuItems();
|
||||||
scrollCount = getMaximumItems(menu)-topFixedCount-bottomFixedCount; // Autosize
|
if (autoSizeScrollCount > scrollCount) {
|
||||||
|
scrollCount = autoSizeScrollCount;
|
||||||
if(interval == -1)
|
}
|
||||||
interval = 150; // Default value
|
|
||||||
|
|
||||||
if (scrollCount <= 0 || interval <= 0) {
|
if (scrollCount <= 0 || interval <= 0) {
|
||||||
throw new IllegalArgumentException("scrollCount and interval must be greater than 0");
|
throw new IllegalArgumentException("scrollCount and interval must be greater than 0");
|
||||||
@ -427,8 +395,7 @@ public class MenuScroller {
|
|||||||
if (item == null) {
|
if (item == null) {
|
||||||
keepVisibleIndex = -1;
|
keepVisibleIndex = -1;
|
||||||
} else {
|
} else {
|
||||||
int index = menu.getComponentIndex(item);
|
keepVisibleIndex = menu.getComponentIndex(item);
|
||||||
keepVisibleIndex = index;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,27 +427,19 @@ public class MenuScroller {
|
|||||||
* Ensures that the <code>dispose</code> method of this MenuScroller is
|
* Ensures that the <code>dispose</code> method of this MenuScroller is
|
||||||
* called when there are no more refrences to it.
|
* called when there are no more refrences to it.
|
||||||
*
|
*
|
||||||
* @exception Throwable if an error occurs.
|
* @throws Throwable if an error occurs.
|
||||||
* @see MenuScroller#dispose()
|
* @see MenuScroller#dispose()
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void finalize() throws Throwable {
|
public void finalize() throws Throwable {
|
||||||
|
super.finalize();
|
||||||
dispose();
|
dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refreshMenu() {
|
private void refreshMenu() {
|
||||||
if (menuItems == null || menuItems.length == 0) {
|
if (menuItems != null && menuItems.length > 0) {
|
||||||
return;
|
firstIndex = Math.max(topFixedCount, firstIndex);
|
||||||
}
|
firstIndex = Math.min(menuItems.length - bottomFixedCount - scrollCount, firstIndex);
|
||||||
|
|
||||||
int newFirstIndex = Math.max(topFixedCount, firstIndex);
|
|
||||||
newFirstIndex = Math.min(menuItems.length - bottomFixedCount - scrollCount, newFirstIndex);
|
|
||||||
|
|
||||||
if (newFirstIndex < 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
firstIndex = newFirstIndex;
|
|
||||||
|
|
||||||
upItem.setEnabled(firstIndex > topFixedCount);
|
upItem.setEnabled(firstIndex > topFixedCount);
|
||||||
downItem.setEnabled(firstIndex + scrollCount < menuItems.length - bottomFixedCount);
|
downItem.setEnabled(firstIndex + scrollCount < menuItems.length - bottomFixedCount);
|
||||||
@ -489,9 +448,9 @@ public class MenuScroller {
|
|||||||
for (int i = 0; i < topFixedCount; i++) {
|
for (int i = 0; i < topFixedCount; i++) {
|
||||||
menu.add(menuItems[i]);
|
menu.add(menuItems[i]);
|
||||||
}
|
}
|
||||||
/*if (topFixedCount > 0) {
|
if (topFixedCount > 0) {
|
||||||
menu.addSeparator();
|
menu.addSeparator();
|
||||||
}*/
|
}
|
||||||
|
|
||||||
menu.add(upItem);
|
menu.add(upItem);
|
||||||
for (int i = firstIndex; i < scrollCount + firstIndex; i++) {
|
for (int i = firstIndex; i < scrollCount + firstIndex; i++) {
|
||||||
@ -499,9 +458,9 @@ public class MenuScroller {
|
|||||||
}
|
}
|
||||||
menu.add(downItem);
|
menu.add(downItem);
|
||||||
|
|
||||||
/*if (bottomFixedCount > 0) {
|
if (bottomFixedCount > 0) {
|
||||||
menu.addSeparator();
|
menu.addSeparator();
|
||||||
}*/
|
}
|
||||||
for (int i = menuItems.length - bottomFixedCount; i < menuItems.length; i++) {
|
for (int i = menuItems.length - bottomFixedCount; i < menuItems.length; i++) {
|
||||||
menu.add(menuItems[i]);
|
menu.add(menuItems[i]);
|
||||||
}
|
}
|
||||||
@ -510,6 +469,22 @@ public class MenuScroller {
|
|||||||
parent.revalidate();
|
parent.revalidate();
|
||||||
parent.repaint();
|
parent.repaint();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getMaximumDrawableMenuItems() {
|
||||||
|
JMenuItem test = new JMenuItem("test");
|
||||||
|
double itemHeight = test.getUI().getPreferredSize(test).getHeight();
|
||||||
|
|
||||||
|
JMenuItem arrowMenuItem = new JMenuItem(MenuIcon.UP);
|
||||||
|
double arrowMenuItemHeight = arrowMenuItem.getUI().getPreferredSize(arrowMenuItem).getHeight();
|
||||||
|
|
||||||
|
double menuBorderHeight = 8.0; // kludge - how to detect this?
|
||||||
|
double screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().getHeight();
|
||||||
|
|
||||||
|
int maxItems = (int) ((screenHeight - arrowMenuItemHeight * 2 - menuBorderHeight) / itemHeight);
|
||||||
|
maxItems -= maxItems / 3;
|
||||||
|
return maxItems;
|
||||||
|
}
|
||||||
|
|
||||||
private class MouseScrollListener implements MouseWheelListener {
|
private class MouseScrollListener implements MouseWheelListener {
|
||||||
public void mouseWheelMoved(MouseWheelEvent mwe) {
|
public void mouseWheelMoved(MouseWheelEvent mwe) {
|
||||||
@ -538,25 +513,6 @@ public class MenuScroller {
|
|||||||
|
|
||||||
private void setMenuItems() {
|
private void setMenuItems() {
|
||||||
menuItems = menu.getComponents();
|
menuItems = menu.getComponents();
|
||||||
|
|
||||||
// Hack for auto detect the topFixed total
|
|
||||||
/*int topFixedCountPrev = topFixedCount;
|
|
||||||
for(int i=menuItems.length-1;i>0;i--)
|
|
||||||
{
|
|
||||||
if(menuItems[i].getClass().getName().endsWith("Separator"))
|
|
||||||
{
|
|
||||||
System.out.println(i);
|
|
||||||
setTopFixedCount(i+1);
|
|
||||||
|
|
||||||
if(topFixedCount!=topFixedCountPrev)
|
|
||||||
{
|
|
||||||
scrollCount = getMaximumItems()-topFixedCount;
|
|
||||||
System.out.println(getMaximumItems()-topFixedCount);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
if (keepVisibleIndex >= topFixedCount
|
if (keepVisibleIndex >= topFixedCount
|
||||||
&& keepVisibleIndex <= menuItems.length - bottomFixedCount
|
&& keepVisibleIndex <= menuItems.length - bottomFixedCount
|
||||||
&& (keepVisibleIndex > firstIndex + scrollCount
|
&& (keepVisibleIndex > firstIndex + scrollCount
|
||||||
@ -577,7 +533,6 @@ public class MenuScroller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
|
||||||
private class MenuScrollTimer extends Timer {
|
private class MenuScrollTimer extends Timer {
|
||||||
|
|
||||||
public MenuScrollTimer(final int increment, int interval) {
|
public MenuScrollTimer(final int increment, int interval) {
|
||||||
@ -592,11 +547,10 @@ public class MenuScroller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
|
||||||
private class MenuScrollItem extends JMenuItem
|
private class MenuScrollItem extends JMenuItem
|
||||||
implements ChangeListener {
|
implements ChangeListener {
|
||||||
|
|
||||||
private MenuScrollTimer timer;
|
private final MenuScrollTimer timer;
|
||||||
|
|
||||||
public MenuScrollItem(MenuIcon icon, int increment) {
|
public MenuScrollItem(MenuIcon icon, int increment) {
|
||||||
setIcon(icon);
|
setIcon(icon);
|
||||||
@ -620,7 +574,7 @@ public class MenuScroller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static enum MenuIcon implements Icon {
|
private enum MenuIcon implements Icon {
|
||||||
|
|
||||||
UP(9, 1, 9),
|
UP(9, 1, 9),
|
||||||
DOWN(1, 9, 1);
|
DOWN(1, 9, 1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user