1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-17 22:23:10 +03:00
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:
Federico Fissore
2015-05-21 11:28:35 +02:00
parent d8b6619de0
commit 17598368ac

View File

@ -3,43 +3,31 @@
*/
package processing.app.tools;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseWheelEvent;
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
* popup menu. A number of items can optionally be frozen at the top and/or
* bottom of the menu.
* <P>
* <p/>
* <B>Implementation note:</B> The default number of items to display
* at a time is 15, and the default scrolling interval is 125 milliseconds.
* <P>
* <p/>
*
* @version 1.5.0 04/05/12
* @author Darryl
* @version 1.5.0 04/05/12
*/
public class MenuScroller {
//private JMenu menu;
private JPopupMenu menu;
private Component[] menuItems;
private MenuScrollItem upItem;
@ -53,25 +41,6 @@ public class MenuScroller {
private int firstIndex = 0;
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
* display at a time and the default scrolling interval.
@ -159,9 +128,9 @@ public class MenuScroller {
* @param interval the scroll interval, in milliseconds
* @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
* @return the MenuScroller
* @throws IllegalArgumentException if scrollCount or interval is 0 or
* negative or if topFixedCount or bottomFixedCount is negative
* @return the MenuScroller
*/
public static MenuScroller setScrollerFor(JMenu menu, int scrollCount, int interval,
int topFixedCount, int bottomFixedCount) {
@ -180,9 +149,9 @@ public class MenuScroller {
* @param interval the scroll interval, in milliseconds
* @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
* @return the MenuScroller
* @throws IllegalArgumentException if scrollCount or interval is 0 or
* negative or if topFixedCount or bottomFixedCount is negative
* @return the MenuScroller
*/
public static MenuScroller setScrollerFor(JPopupMenu menu, int scrollCount, int interval,
int topFixedCount, int bottomFixedCount) {
@ -198,7 +167,7 @@ public class MenuScroller {
* @param menu the menu
*/
public MenuScroller(JMenu menu) {
this(menu, -1);
this(menu, 15);
}
/**
@ -209,7 +178,7 @@ public class MenuScroller {
* @param menu the popup 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,
int topFixedCount, int bottomFixedCount) {
if(scrollCount == -1)
scrollCount = getMaximumItems(menu)-topFixedCount-bottomFixedCount; // Autosize
if(interval == -1)
interval = 150; // Default value
int autoSizeScrollCount = getMaximumDrawableMenuItems();
if (autoSizeScrollCount > scrollCount) {
scrollCount = autoSizeScrollCount;
}
if (scrollCount <= 0 || interval <= 0) {
throw new IllegalArgumentException("scrollCount and interval must be greater than 0");
@ -427,8 +395,7 @@ public class MenuScroller {
if (item == null) {
keepVisibleIndex = -1;
} else {
int index = menu.getComponentIndex(item);
keepVisibleIndex = index;
keepVisibleIndex = menu.getComponentIndex(item);
}
}
@ -460,27 +427,19 @@ public class MenuScroller {
* Ensures that the <code>dispose</code> method of this MenuScroller is
* called when there are no more refrences to it.
*
* @exception Throwable if an error occurs.
* @throws Throwable if an error occurs.
* @see MenuScroller#dispose()
*/
@Override
public void finalize() throws Throwable {
super.finalize();
dispose();
}
private void refreshMenu() {
if (menuItems == null || menuItems.length == 0) {
return;
}
int newFirstIndex = Math.max(topFixedCount, firstIndex);
newFirstIndex = Math.min(menuItems.length - bottomFixedCount - scrollCount, newFirstIndex);
if (newFirstIndex < 0) {
return;
}
firstIndex = newFirstIndex;
if (menuItems != null && menuItems.length > 0) {
firstIndex = Math.max(topFixedCount, firstIndex);
firstIndex = Math.min(menuItems.length - bottomFixedCount - scrollCount, firstIndex);
upItem.setEnabled(firstIndex > topFixedCount);
downItem.setEnabled(firstIndex + scrollCount < menuItems.length - bottomFixedCount);
@ -489,9 +448,9 @@ public class MenuScroller {
for (int i = 0; i < topFixedCount; i++) {
menu.add(menuItems[i]);
}
/*if (topFixedCount > 0) {
if (topFixedCount > 0) {
menu.addSeparator();
}*/
}
menu.add(upItem);
for (int i = firstIndex; i < scrollCount + firstIndex; i++) {
@ -499,9 +458,9 @@ public class MenuScroller {
}
menu.add(downItem);
/*if (bottomFixedCount > 0) {
if (bottomFixedCount > 0) {
menu.addSeparator();
}*/
}
for (int i = menuItems.length - bottomFixedCount; i < menuItems.length; i++) {
menu.add(menuItems[i]);
}
@ -510,6 +469,22 @@ public class MenuScroller {
parent.revalidate();
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 {
public void mouseWheelMoved(MouseWheelEvent mwe) {
@ -538,25 +513,6 @@ public class MenuScroller {
private void setMenuItems() {
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
&& keepVisibleIndex <= menuItems.length - bottomFixedCount
&& (keepVisibleIndex > firstIndex + scrollCount
@ -577,7 +533,6 @@ public class MenuScroller {
}
}
@SuppressWarnings("serial")
private class MenuScrollTimer extends Timer {
public MenuScrollTimer(final int increment, int interval) {
@ -592,11 +547,10 @@ public class MenuScroller {
}
}
@SuppressWarnings("serial")
private class MenuScrollItem extends JMenuItem
implements ChangeListener {
private MenuScrollTimer timer;
private final MenuScrollTimer timer;
public MenuScrollItem(MenuIcon icon, int increment) {
setIcon(icon);
@ -620,7 +574,7 @@ public class MenuScroller {
}
}
private static enum MenuIcon implements Icon {
private enum MenuIcon implements Icon {
UP(9, 1, 9),
DOWN(1, 9, 1);