KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > layout > swing > SwingLE


1 package jimm.datavision.layout.swing;
2 import jimm.datavision.*;
3 import jimm.datavision.field.*;
4 import jimm.datavision.layout.LayoutEngine;
5 import jimm.datavision.gui.ExportWin;
6 import jimm.datavision.gui.MenuUtils;
7 import jimm.datavision.gui.StatusDialog;
8 import jimm.util.I18N;
9 import java.awt.*;
10 import java.awt.print.*;
11 import java.awt.event.*;
12 import javax.swing.*;
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 /**
17  * <code>SwingLE</code> is a layout engine that creates a Swing window.
18  * The window can be printed by selecting the appropriate menu item.
19  *
20  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
21  * @see SwingPageContents
22  * @see SwingPage
23  * @see SwingField
24  * @see SwingPrintBook
25  */

26 public class SwingLE extends LayoutEngine {
27
28 protected static final Dimension WINDOW_START_SIZE = new Dimension(600, 400);
29 protected static final String JavaDoc PRINT_ICON = "images/Print16.gif";
30 protected static final String JavaDoc EXPORT_ICON = "images/Export16.gif";
31 protected static final String JavaDoc FIRST_ICON = "images/Home16.gif";
32 protected static final String JavaDoc PREV_ICON = "images/Back16.gif";
33 protected static final String JavaDoc NEXT_ICON = "images/Forward16.gif";
34 protected static final String JavaDoc LAST_ICON = "images/Down16.gif";
35
36 protected JFrame frame;
37 protected Dimension pageDim;
38 protected JPanel cardPanel;
39 protected JScrollPane scroller;
40 protected int displayPageNum; // Starts at 1
41
protected ArrayList JavaDoc pageContents;
42 protected SwingPageContents pageBeingBuilt;
43 protected JLabel pageCountLabel;
44 protected Action printAction;
45 protected Action closeAction;
46 protected Action exportAction;
47 protected Action goFirstAction;
48 protected Action goPrevAction;
49 protected Action goNextAction;
50 protected Action goLastAction;
51
52 /**
53  * Constructor.
54  */

55 public SwingLE() {
56     super();
57     pageContents = new ArrayList JavaDoc();
58 }
59
60 public void cancel() {
61     super.cancel();
62     close();
63 }
64
65 public JFrame getJFrame() { return frame; }
66
67 /**
68  * Creates window and displays a blank page.
69  */

70 protected void doStart() {
71     pageDim = new Dimension((int)pageWidth(), (int)pageHeight());
72     frame = new JFrame(report.getTitle());
73     makeActions();
74     makeMenu(frame);
75     frame.getContentPane().add(makeToolbar(), BorderLayout.NORTH);
76
77     // Card panel for displaying pages
78
cardPanel = new JPanel();
79     CardLayout cardLayout = new CardLayout(0, 0);
80     cardPanel.setLayout(cardLayout);
81
82     // Set sizes of scroller (window size) and card panel (page size)
83
cardPanel.setPreferredSize(pageDim);
84
85     // New, blank, dummy card we can display until the first page is
86
// generated.
87
JPanel blankPage = new JPanel();
88     blankPage.setBackground(Color.white);
89     blankPage.setPreferredSize(pageDim);
90     cardPanel.add(blankPage, "blank page");
91     cardLayout.show(cardPanel, "blank page");
92
93     // Scroller containing the card panel
94
scroller = new JScrollPane(cardPanel);
95     scroller.setPreferredSize(WINDOW_START_SIZE);
96     frame.getContentPane().add(scroller, BorderLayout.CENTER);
97
98     displayPageNum = 0;
99
100     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
101     frame.addWindowListener(new WindowAdapter() {
102     public void windowClosing(WindowEvent e) {
103         close();
104     }
105     });
106
107     frame.pack();
108     frame.setVisible(true);
109 }
110
111 /**
112  * Done loading report.
113  */

114 protected void doEnd() {
115     printAction.setEnabled(true);
116
117     if (pageContents.size() > 1) // Start prebuilding the report's last page
118
pageBeingBuilt.prebuildPage();
119
120     // Calling displayPage() will force page 1 to finish building and
121
// will display it.
122
if (displayPageNum == 0)
123     displayPage(1);
124 }
125
126 /**
127  * Creates the actions used by menu items and toolbar widgets.
128  */

129 protected void makeActions() {
130     URL JavaDoc url = getClass().getClassLoader().getResource(PRINT_ICON);
131     String JavaDoc str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_print");
132     printAction = new AbstractAction(str, new ImageIcon(url, str)) {
133     public void actionPerformed(ActionEvent e) { printReport(); }
134     };
135     printAction.putValue(Action.SHORT_DESCRIPTION, str);
136     printAction.setEnabled(false);
137
138     str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_close");
139     closeAction = new AbstractAction(str) {
140     public void actionPerformed(ActionEvent e) { close(); }
141     };
142
143     url = getClass().getClassLoader().getResource(EXPORT_ICON);
144     str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_export");
145     exportAction = new AbstractAction(str, new ImageIcon(url, str)) {
146     public void actionPerformed(ActionEvent e) { export(); }
147     };
148     exportAction.putValue(Action.SHORT_DESCRIPTION, str);
149
150     url = getClass().getClassLoader().getResource(FIRST_ICON);
151     str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_first_page");
152     goFirstAction = new AbstractAction(str, new ImageIcon(url, str)) {
153     public void actionPerformed(ActionEvent e) { displayFirstPage(); }
154     };
155     goFirstAction.putValue(Action.SHORT_DESCRIPTION, str);
156     goFirstAction.setEnabled(false);
157
158     url = getClass().getClassLoader().getResource(PREV_ICON);
159     str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_previous_page");
160     goPrevAction = new AbstractAction(str, new ImageIcon(url, str)) {
161     public void actionPerformed(ActionEvent e) { displayPrevPage(); }
162     };
163     goPrevAction.putValue(Action.SHORT_DESCRIPTION, str);
164     goPrevAction.setEnabled(false);
165
166     url = getClass().getClassLoader().getResource(NEXT_ICON);
167     str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_next_page");
168     goNextAction = new AbstractAction(str, new ImageIcon(url, str)) {
169     public void actionPerformed(ActionEvent e) { displayNextPage(); }
170     };
171     goNextAction.putValue(Action.SHORT_DESCRIPTION, str);
172     goNextAction.setEnabled(false);
173
174     url = getClass().getClassLoader().getResource(LAST_ICON);
175     str = I18N.get(I18N.MENU_FILE_PREFIX, "SwingLE.action_last_page");
176     goLastAction = new AbstractAction(str, new ImageIcon(url, str)) {
177     public void actionPerformed(ActionEvent e) { displayLastPage(); }
178     };
179     goLastAction.putValue(Action.SHORT_DESCRIPTION, str);
180     goLastAction.setEnabled(false);
181 }
182
183 /**
184  * Creates the window menu.
185  *
186  * @param frame the window that will contain the menu
187  */

188 protected void makeMenu(JFrame frame) {
189     JMenuBar menuBar = new JMenuBar();
190     frame.setJMenuBar(menuBar);
191
192     // File menu
193
JMenu menu = MenuUtils.readMenu("SwingLE.menu_file");
194     menuBar.add(menu);
195
196     MenuUtils.addToMenu(menu, printAction, "SwingLE.action_print");
197     menu.addSeparator();
198     MenuUtils.addToMenu(menu, closeAction, "SwingLE.action_close");
199
200     // View menu
201
menu = MenuUtils.readMenu("SwingLE.menu_view");
202     menuBar.add(menu);
203
204     MenuUtils.addToMenu(menu, goFirstAction, "SwingLE.action_first_page");
205     MenuUtils.addToMenu(menu, goPrevAction, "SwingLE.action_previous_page");
206     MenuUtils.addToMenu(menu, goNextAction, "SwingLE.action_next_page");
207     MenuUtils.addToMenu(menu, goLastAction, "SwingLE.action_last_page");
208
209     // Report menu
210
menu = MenuUtils.readMenu("SwingLE.menu_report");
211     menuBar.add(menu);
212
213     MenuUtils.addToMenu(menu, exportAction, "SwingLE.action_export");
214 }
215
216 /**
217  * Creates and returns a new tool bar.
218  */

219 protected JToolBar makeToolbar() {
220     JToolBar bar = new JToolBar(javax.swing.SwingConstants.HORIZONTAL);
221     bar.add(printAction);
222     bar.addSeparator();
223     bar.add(exportAction);
224     bar.addSeparator();
225     bar.add(goFirstAction);
226     bar.add(goPrevAction);
227     bar.add(goNextAction);
228     bar.add(goLastAction);
229     pageCountLabel = new JLabel(I18N.get("SwingLE.loading_first_page"));
230     bar.addSeparator();
231     bar.add(pageCountLabel);
232     return bar;
233 }
234
235 /**
236  * Creates a new page.
237  */

238 protected void doStartPage() {
239     int pageNum = pageContents.size() + 1;
240     pageBeingBuilt = new SwingPageContents(cardPanel, pageNum, pageDim);
241     pageContents.add(pageBeingBuilt);
242 }
243
244 /**
245  * At the end of the first page, starts building it in a separate thread.
246  * Check to see if the first page is done being built. If so, it is
247  * displayed.
248  */

249 protected void doEndPage() {
250     int numPages = pageContents.size();
251     if (numPages == 1) { // The first page
252
// Start building the page in a separate thread
253
pageBeingBuilt.prebuildPage();
254     pageCountLabel.setText(I18N.get("SwingLE.building_first_page"));
255     }
256
257     // If the first page is done being built and is not yet displayed,
258
// display it.
259
if (((SwingPageContents)pageContents.get(0)).isPageBuilt()
260     && displayPageNum == 0)
261     displayPage(1); // Updates page count label and nav actions
262
else {
263     updatePageCountLabel();
264     updateNavActions();
265     }
266 }
267
268 /**
269  * Closes this window. Does not call <code>System.exit</code>.
270  */

271 public void close() {
272     if (frame != null) {
273     frame.setVisible(false);
274     frame.dispose();
275     }
276     wantsMoreData = false; // Signal report that we're done
277

278     // Clean up memory a bit
279
pageContents = null;
280     pageBeingBuilt = null;
281 }
282
283 /**
284  * Opens the report export dialog.
285  */

286 protected void export() {
287     new ExportWin(frame, report);
288 }
289
290 /**
291  * Updates the navigation buttons based on number of pages and the current
292  * display page.
293  */

294 protected void updateNavActions() {
295     int numPages = pageContents.size();
296
297     boolean canGoBack = numPages > 0 && displayPageNum > 1;
298     goFirstAction.setEnabled(canGoBack);
299     goPrevAction.setEnabled(canGoBack);
300
301     boolean canGoForward = numPages > 0 && displayPageNum < numPages;
302     goNextAction.setEnabled(canGoForward);
303     goLastAction.setEnabled(canGoForward);
304 }
305
306 /**
307  * Updates the page count label based on the current page number and the
308  * total number of pages.
309  */

310 protected void updatePageCountLabel() {
311     if (displayPageNum > 0)
312     pageCountLabel.setText(I18N.get("SwingLE.page")
313                    + ' ' + displayPageNum + ' '
314                    + I18N.get("SwingLE.of")
315                    + ' ' + pageContents.size());
316 }
317
318 /** Performs the "First Page" command. */
319 protected void displayFirstPage() {
320     if (pageContents.size() > 0)
321     displayPage(1);
322 }
323
324 /** Performs the "Next Page" command. */
325 protected void displayNextPage() {
326     if (pageContents.size() > displayPageNum)
327     displayPage(displayPageNum + 1);
328 }
329
330 /** Performs the "Previous Page" command. */
331 protected void displayPrevPage() {
332     if (displayPageNum > 1)
333     displayPage(displayPageNum - 1);
334 }
335
336 /** Performs the "Last Page" command. */
337 protected void displayLastPage() {
338     int numPages = pageContents.size();
339     if (numPages > 0)
340     displayPage(numPages);
341 }
342
343 /**
344  * Fills the window with the contents of the specified page. If necessary,
345  * builds the page. Starts building the next and previous pages in separate
346  * threads, if they have not already been built.
347  *
348  * @param num page number, starting at 1
349  * @see SwingPageContents
350  */

351 protected void displayPage(int num) {
352     if (num == displayPageNum)
353     return;
354
355     // Retrieve page. Build if necessary. Show it.
356
SwingPageContents contents = (SwingPageContents)pageContents.get(num - 1);
357     if (!contents.isPageBuilt())
358     pageCountLabel.setText(I18N.get("SwingLE.building_page")
359                    + ' ' + num + "...");
360
361     int vertPosition = scroller.getVerticalScrollBar().getValue();
362     contents.showPage(); // Builds page if necessary
363
scroller.getVerticalScrollBar().setValue(vertPosition);
364
365     // If next page not already built, start building it in a separate
366
// thread.
367
int numPages = pageContents.size();
368     if (numPages > num) { // Spawn thread to generate *next* page
369
contents = (SwingPageContents)pageContents.get(num);
370     contents.prebuildPage();
371     }
372
373     // If previous page not already built, start building it in a separate
374
// thread.
375
// if (num > 2) { // We know pages 1 and 2 are already prebuilt
376
if (num > 1) {
377     contents = (SwingPageContents)pageContents.get(num - 2);
378     contents.prebuildPage();
379     }
380
381     displayPageNum = num;
382     updatePageCountLabel();
383     updateNavActions();
384
385     // Erase all other pages but the first and last pages and the two
386
// surrounding pages we just started generating; they are probably the
387
// most-visited.
388
for (int i = 1; i < (numPages - 1); ++i) {
389     if (i < (displayPageNum - 2) || i > (displayPageNum)) {
390         contents = (SwingPageContents)pageContents.get(i);
391         contents.forgetPage();
392     }
393     }
394 }
395
396 /**
397  * Creates a new {@link SwingField} and adds it to the current page.
398  *
399  * @param field the report field
400  */

401 protected void doOutputField(Field field) {
402     if (!field.isVisible())
403     return;
404
405     String JavaDoc fieldAsString = field.toString();
406     if (fieldAsString == null || fieldAsString.length() == 0) {
407     // FIX: implement makeBorders
408
// makeBorders(field);
409
return;
410     }
411
412     jimm.datavision.field.Rectangle bounds = field.getBounds();
413
414     // Page footers are anchored to page bottom.
415
int y = (currentSection.getArea().getArea() == SectionArea.PAGE_FOOTER)
416     ? (int)(pageHeight() - currentSection.getOutputHeight() + bounds.y)
417     : (int)(pageHeightUsed + bounds.y);
418     java.awt.Rectangle JavaDoc fieldBounds =
419     new java.awt.Rectangle JavaDoc((int)bounds.x, y, (int)bounds.width,
420                    (int)field.getOutputHeight());
421     pageBeingBuilt.add(field, fieldAsString, fieldBounds);
422
423     // FIX: implement makeBorders
424
// makeBorders(field);
425
}
426
427 protected void doOutputImage(ImageField image) {
428     doOutputField(image);
429 }
430
431 /**
432  * Creates a new line. <em>Unimplemented</em>.
433  *
434  * @param line a line
435  */

436 protected void doOutputLine(Line line) {}
437
438 /**
439  * Prints the report.
440  */

441 public void printReport() {
442     final PrinterJob printJob = PrinterJob.getPrinterJob();
443     PageFormat format = report.getPaperFormat().getPageFormat();
444     SwingPrintBook book = new SwingPrintBook(pageContents, format);
445     printJob.setPageable(book);
446
447     if (printJob.printDialog()) {
448     final StatusDialog statusDialog =
449         new StatusDialog(frame,
450                  I18N.get("SwingLE.print_report_title"),
451                  true,
452                  I18N.get("SwingLE.print_report_status"));
453     book.setStatusDialog(statusDialog);
454
455     new Thread JavaDoc(new Runnable JavaDoc() {
456         public void run() {
457         try {
458             printJob.print();
459         }
460         catch (UserCancellationException uce) {
461             printJob.cancel();
462         }
463         catch (Exception JavaDoc e) {
464             ErrorHandler.error(e);
465         }
466         finally {
467             if (statusDialog != null)
468             statusDialog.dispose();
469         }
470         }
471         }).start();
472     }
473 }
474
475 }
476
Popular Tags