-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPopupMenuAlignment.java
More file actions
121 lines (100 loc) · 3.31 KB
/
PopupMenuAlignment.java
File metadata and controls
121 lines (100 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Convenience class to align the text of JMenuItems within a specified
* JPopupMenu to be either CENTER or TRAILING aligned. The default for
* trailing would be right aligned unless the component orientation is
* changed.
*
* The class can be used in one of two ways. For a static popup menu you
* can just invoke the statick alignText() method once the menu has been
* created. For dynamic popup menus you can use this class as a
* ContainerListener on the popup menu so as menu items are added or
* removed the remaining items are adjusted accordingly.
*/
public class PopupMenuAlignment implements ContainerListener
{
public static int CENTER = SwingConstants.CENTER;
public static int TRAILING = SwingConstants.TRAILING;
private int alignment;
/**
* Use this class as a listener to handle dynamic changes to the
* popup menu.
*
* @param alignment alignment of each menu item. Either
* CENTER or TRAILING
*/
public PopupMenuAlignment(int alignment)
{
this.alignment = alignment;
}
//
// Implement the ContainerListener
//
// The menu items in the popup menu are dynamically aligned
//
public void componentAdded(ContainerEvent e)
{
JPopupMenu popup = (JPopupMenu)e.getContainer();
alignText(popup, alignment);
}
public void componentRemoved(ContainerEvent e)
{
JPopupMenu popup = (JPopupMenu)e.getContainer();
alignText(popup, alignment);
}
/**
* Align the menu items contained with the popup menu.
*
* @param popup the popup menu
* @param alignment alignment of each menu item. Either
* CENTER or TRAILING
*/
public static void alignText(JPopupMenu popup, int alignment)
{
int maxWidth = 0;
// Find the largest text string
for (int i = 0; i < popup.getComponentCount(); i++)
{
Component c = popup.getComponent(i);
if (c instanceof JMenuItem)
{
JMenuItem menuItem = (JMenuItem)c;
FontMetrics fm = menuItem.getFontMetrics(menuItem.getFont());
maxWidth = Math.max(maxWidth, fm.stringWidth(menuItem.getText().trim()));
}
}
// Align each menu item
for (int i = 0; i < popup.getComponentCount(); i++)
{
Component c = popup.getComponent(i);
if (! (c instanceof JMenuItem)) continue;
JMenuItem menuItem = (JMenuItem)c;
String actionCommand = menuItem.getActionCommand();
String text = menuItem.getText().trim();
ComponentOrientation orientation = menuItem.getComponentOrientation();
StringBuffer sb = new StringBuffer();
// Padding will be added to the right, so add text now
if (! orientation.isLeftToRight())
{
sb.append( text );
}
// Calculate the number of padding spaces to add
FontMetrics fm = menuItem.getFontMetrics(menuItem.getFont());
int spaceWidth = fm.stringWidth(" ") * (alignment == CENTER ? 2 : 1);
int spaces = (maxWidth - fm.stringWidth(text)) / spaceWidth;
for (int j = 0; j < spaces; j++)
{
sb.append(" ");
}
// Padding has been added to the left, so add text now
if (orientation.isLeftToRight())
{
sb.append( text );
}
menuItem.setText(sb.toString());
menuItem.setActionCommand(actionCommand);
}
}
}