001: package org.uispec4j;
002:
003: import junit.framework.Assert;
004: import junit.framework.AssertionFailedError;
005: import org.uispec4j.assertion.Assertion;
006: import org.uispec4j.assertion.UISpecAssert;
007: import org.uispec4j.xml.XmlWriter;
008:
009: import javax.swing.*;
010: import java.awt.*;
011:
012: /**
013: * Wrapper for window components such as JFrame, JDialog, JInternalFrame.
014: */
015: public class Window extends Panel {
016:
017: public static final String TYPE_NAME = "Window";
018: public static final Class[] SWING_CLASSES = { JFrame.class,
019: JDialog.class, JInternalFrame.class, Frame.class,
020: java.awt.Window.class };
021: private final Adapter adapter;
022:
023: public Window(JFrame frame) {
024: super (frame);
025: this .adapter = new JFrameAdapter(frame);
026: }
027:
028: public Window(JDialog dialog) {
029: super (dialog);
030: this .adapter = new JDialogAdapter(dialog);
031: }
032:
033: public Window(JInternalFrame frame) {
034: super (frame);
035: this .adapter = new JInternalFrameAdapter(frame);
036: }
037:
038: public Window(Frame frame) {
039: super (frame);
040: this .adapter = new FrameAdapter(frame);
041: }
042:
043: public Window(java.awt.Window window) {
044: super (window);
045: this .adapter = new WindowAdapter(window);
046: }
047:
048: public String getDescriptionTypeName() {
049: return "window";
050: }
051:
052: protected void addAttributes(Component component, XmlWriter.Tag tag) {
053: tag.addAttribute("title", adapter.getTitle());
054: }
055:
056: public MenuBar getMenuBar() {
057: return new MenuBar(adapter.getJMenuBar());
058: }
059:
060: public String getTitle() {
061: return adapter.getTitle();
062: }
063:
064: public Assertion titleEquals(final String expected) {
065: return new Assertion() {
066: public void check() {
067: Assert.assertEquals(expected, getTitle());
068: }
069: };
070: }
071:
072: public void assertTitleEquals(String expected) {
073: UISpecAssert.assertTrue(titleEquals(expected));
074: }
075:
076: protected void getSubDescription(Container container,
077: XmlWriter.Tag tag) {
078: Container internalAwtContainer = adapter
079: .getInternalAwtContainer();
080: Panel contentPane = new Panel(internalAwtContainer);
081: contentPane.getSubDescription(internalAwtContainer, tag);
082: }
083:
084: public Container getInternalAwtContainer() {
085: return adapter.getInternalAwtContainer();
086: }
087:
088: public Assertion isModal() {
089: return new Assertion() {
090: public void check() {
091: Assert.assertTrue(adapter.isModal());
092: }
093: };
094: }
095:
096: static interface Adapter {
097: JMenuBar getJMenuBar();
098:
099: String getTitle();
100:
101: boolean isModal();
102:
103: Container getInternalAwtContainer();
104: }
105:
106: private static class JInternalFrameAdapter implements Adapter {
107: private final JInternalFrame frame;
108:
109: JInternalFrameAdapter(JInternalFrame frame) {
110: this .frame = frame;
111: }
112:
113: public JMenuBar getJMenuBar() {
114: return frame.getJMenuBar();
115: }
116:
117: public String getTitle() {
118: return frame.getTitle();
119: }
120:
121: public boolean isModal() {
122: return false;
123: }
124:
125: public Container getInternalAwtContainer() {
126: return frame.getContentPane();
127: }
128: }
129:
130: private static class JFrameAdapter implements Adapter {
131: private final JFrame frame;
132:
133: JFrameAdapter(JFrame frame) {
134: this .frame = frame;
135: }
136:
137: public JMenuBar getJMenuBar() {
138: return frame.getJMenuBar();
139: }
140:
141: public String getTitle() {
142: return frame.getTitle();
143: }
144:
145: public boolean isModal() {
146: return false;
147: }
148:
149: public Container getInternalAwtContainer() {
150: return frame.getContentPane();
151: }
152: }
153:
154: private static class JDialogAdapter implements Adapter {
155: private final JDialog dialog;
156:
157: JDialogAdapter(JDialog dialog) {
158: this .dialog = dialog;
159: }
160:
161: public JMenuBar getJMenuBar() {
162: return dialog.getJMenuBar();
163: }
164:
165: public String getTitle() {
166: return dialog.getTitle();
167: }
168:
169: public boolean isModal() {
170: return dialog.isModal();
171: }
172:
173: public Container getInternalAwtContainer() {
174: return dialog.getContentPane();
175: }
176: }
177:
178: private static class FrameAdapter implements Adapter {
179: Frame frame;
180:
181: public FrameAdapter(Frame frame) {
182: this .frame = frame;
183: }
184:
185: public JMenuBar getJMenuBar() {
186: throw new AssertionFailedError(
187: "This component has no menu bar");
188: }
189:
190: public String getTitle() {
191: return frame.getTitle();
192: }
193:
194: public boolean isModal() {
195: return false;
196: }
197:
198: public Container getInternalAwtContainer() {
199: return frame;
200: }
201: }
202:
203: private static class WindowAdapter implements Adapter {
204:
205: private java.awt.Window window;
206:
207: public WindowAdapter(java.awt.Window window) {
208: this .window = window;
209: }
210:
211: public JMenuBar getJMenuBar() {
212: throw new AssertionFailedError(
213: "This component has no menu bar");
214: }
215:
216: public String getTitle() {
217: return "";
218: }
219:
220: public boolean isModal() {
221: return false;
222: }
223:
224: public Container getInternalAwtContainer() {
225: return window;
226: }
227: }
228: }
|