-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRXCardLayout.java
More file actions
196 lines (158 loc) · 4.29 KB
/
RXCardLayout.java
File metadata and controls
196 lines (158 loc) · 4.29 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* The <code>RXCardLayout</code> provides some extensions to the
* CardLayout class. In particular adding support for:
*
* a) setting focus on the card when it is displayed
* b) getting the currently displayed Card
* c) Next and Previous Actions
*
* This added support will only work when a JComponent is added as a Card.
*/
public class RXCardLayout extends CardLayout implements HierarchyListener
{
private ArrayList<JComponent> cards = new ArrayList<JComponent>();
private JComponent firstCard;
private JComponent lastCard;
private JComponent currentCard;
private boolean isRequestFocusOnCard = true;
private Action nextAction;
private Action previousAction;
/**
* Creates a new card layout with gaps of size zero.
*/
public RXCardLayout()
{
this(0, 0);
}
/**
* Creates a new card layout with the specified horizontal and
* vertical gaps. The horizontal gaps are placed at the left and
* right edges. The vertical gaps are placed at the top and bottom
* edges.
* @param hgap the horizontal gap.
* @param vgap the vertical gap.
*/
public RXCardLayout(int hgap, int vgap)
{
super(hgap, vgap);
}
// Overridden methods
public void addLayoutComponent(Component comp, Object constraints)
{
super.addLayoutComponent(comp, constraints);
if (! (comp instanceof JComponent)) return;
JComponent component = (JComponent)comp;
cards.add(component);
if (firstCard == null)
firstCard = component;
lastCard = component;
component.addHierarchyListener(this);
}
public void removeLayoutComponent(Component comp)
{
super.removeLayoutComponent(comp);
if (! (comp instanceof JComponent)) return;
JComponent component = (JComponent)comp;
component.removeHierarchyListener(this);
cards.remove(component);
if (component.equals(firstCard)
&& cards.size() > 0)
{
firstCard = cards.get(0);
}
if (component.equals(lastCard)
&& cards.size() > 0)
{
lastCard = cards.get(cards.size() - 1);
}
}
// New methods
public JComponent getCurrentCard()
{
return currentCard;
}
public Action getNextAction()
{
return getNextAction("Next");
}
public Action getNextAction(String name)
{
if (nextAction == null)
{
nextAction = new CardAction(name, true);
nextAction.putValue(Action.MNEMONIC_KEY, (int)name.charAt(0));
nextAction.setEnabled( isNextCardAvailable() );
}
return nextAction;
}
public Action getPreviousAction()
{
return getPreviousAction("Previous");
}
public Action getPreviousAction(String name)
{
if (previousAction == null)
{
previousAction = new CardAction(name, false);
previousAction.putValue(Action.MNEMONIC_KEY, (int)name.charAt(0));
previousAction.setEnabled( isNextCardAvailable() );
}
return previousAction;
}
public boolean isNextCardAvailable()
{
return currentCard != lastCard;
}
public boolean isPreviousCardAvailable()
{
return currentCard != firstCard;
}
public boolean isRequestFocusOnCard()
{
return isRequestFocusOnCard;
}
public void setRequestFocusOnCard(boolean isRequestFocusOnCard)
{
this.isRequestFocusOnCard = isRequestFocusOnCard;
}
// Implement Hierarchy Listener
@Override
public void hierarchyChanged(HierarchyEvent e)
{
JComponent component = (JComponent)e.getSource();
if ((HierarchyEvent.SHOWING_CHANGED & e.getChangeFlags()) != 0
&& component.isShowing())
{
currentCard = component;
if (isRequestFocusOnCard)
currentCard.transferFocus();
if (nextAction != null)
nextAction.setEnabled( isNextCardAvailable() );
if (previousAction != null)
previousAction.setEnabled( isPreviousCardAvailable() );
}
}
class CardAction extends AbstractAction
{
private boolean isNext;
public CardAction(String text, boolean isNext)
{
super(text);
this.isNext = isNext;
putValue( Action.SHORT_DESCRIPTION, getValue(Action.NAME) );
}
public void actionPerformed(ActionEvent e)
{
Container parent = getCurrentCard().getParent();
if (isNext)
next(parent);
else
previous(parent);
}
}
}