01 /*
02 * Copyright (c) 1995-2010, The University of Sheffield. See the file
03 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
04 *
05 * This file is part of GATE (see http://gate.ac.uk/), and is free
06 * software, licenced under the GNU Library General Public License,
07 * Version 2, June 1991 (in the distribution as file licence.html,
08 * and also available at http://gate.ac.uk/gate/licence.html).
09 *
10 * Valentin Tablan 20 Feb 2003
11 *
12 * $Id: XJPopupMenu.java 12006 2009-12-01 17:24:28Z thomas_heitz $
13 */
14
15 package gate.swing;
16
17
18 import javax.swing.*;
19 import java.awt.*;
20
21 /**
22 * A modified version of JPopupMenu that uses {@link MenuLayout} as its layout.
23 */
24 public class XJPopupMenu extends JPopupMenu {
25 public XJPopupMenu() {
26 super();
27 setLayout(new MenuLayout());
28 }
29
30 public XJPopupMenu(String label){
31 super(label);
32 setLayout(new MenuLayout());
33 }
34
35 /**
36 * Force separators to be the same width as the JPopupMenu.
37 * This is because the MenuLayout make separators invisible contrary
38 * to the default JPopupMenu layout manager.
39 * @param aFlag true if the popupmenu is visible
40 */
41 public void setVisible(boolean aFlag) {
42 super.setVisible(aFlag);
43 if (!aFlag) { return; }
44 MenuLayout layout = (MenuLayout) getLayout();
45 for (int i = 0; i < getComponents().length; i++) {
46 Component component = getComponents()[i];
47 if (component instanceof JSeparator) {
48 JSeparator separator = (JSeparator) component;
49 int column = layout.getColumnForComponentIndex(i);
50 int preferredWidth = layout.getPreferredWidthForColumn(column);
51 // use the popupmenu width to set the separators width
52 separator.setPreferredSize(new Dimension(
53 preferredWidth, separator.getHeight()));
54 }
55 }
56 revalidate();
57 }
58 }
|