Source code: com/virtuosotechnologies/lib/basiccommand/builder/MenuSeparatorBuilderNode.java
1 /*
2 ================================================================================
3
4 FILE: MenuSeparatorBuilderNode.java
5
6 PROJECT:
7
8 Virtuoso Utilities
9
10 CONTENTS:
11
12 A builder that creates a menu separator
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.lib.basiccommand.builder;
43
44
45 import javax.swing.JPopupMenu;
46 import java.awt.Dimension;
47 import java.awt.Container;
48 import java.awt.Graphics;
49
50 import com.virtuosotechnologies.lib.command.CommandNode;
51
52
53 /**
54 * A builder that creates a menu separator
55 */
56 public class MenuSeparatorBuilderNode
57 extends AbstractElementBuilderNode
58 {
59 /**
60 * Constructor
61 */
62 protected MenuSeparatorBuilderNode(
63 CommandNode commandNode,
64 AbstractBranchBuilderNode parent,
65 int index)
66 {
67 super(commandNode, parent, index);
68 }
69
70
71 /**
72 * Override this method to create the initial element object.
73 */
74 protected Object createInitialElement()
75 {
76 return new MySeparator();
77 }
78
79
80 /**
81 * Special separator that prevents itself from being displayed duplicated
82 * several times in a row.
83 */
84 private class MySeparator
85 extends JPopupMenu.Separator
86 {
87 MySeparator()
88 {
89 }
90
91 private boolean shouldBeHidden()
92 {
93 Container parent = this.getParent();
94 int numSiblings = parent.getComponentCount();
95 int pos = -1;
96 for (int i=0; i<numSiblings; ++i)
97 {
98 if (parent.getComponent(i) == this)
99 {
100 pos = i;
101 break;
102 }
103 }
104 if (pos == -1)
105 {
106 return false; // Should never happen, though.
107 }
108 if (pos == 0)
109 {
110 return true;
111 }
112 if (parent.getComponent(pos-1) instanceof MySeparator)
113 {
114 return true;
115 }
116 for (int i=pos+1; i<numSiblings; ++i )
117 {
118 if (!(parent.getComponent(i) instanceof MySeparator))
119 {
120 return false;
121 }
122 }
123 return true;
124 }
125
126 public Dimension getPreferredSize()
127 {
128 if (shouldBeHidden())
129 {
130 return new Dimension(0, 0);
131 }
132 else
133 {
134 return super.getPreferredSize();
135 }
136 }
137
138 public void paintComponent(
139 Graphics g)
140 {
141 if (!shouldBeHidden())
142 {
143 super.paintComponent(g);
144 }
145 }
146 }
147 }