KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ixenon > free > swing > PagerDialog


1 /* $Id$
2  *
3  * Copyright (c) 1998 Xenonsoft Limited. All Rights Reserved.
4  *
5  * This software is protected by copyright. You are hereby notified from
6  * now by reading this message. This software is also the confidential
7  * and proprietary information of Xenonsoft Limited. ("Confidential
8  * Information").
9  *
10  * This software is distributed under the Xenonsoft Public end user
11  * License ("XPeL"), where the machine-readable source code is provided
12  * under the "Open Source" model.
13  * For more information, please read the file "LICENSE-XPL.txt"
14  */

15
16 // PagerDialog.java
17

18 // Description:
19
// Pager Component Dialog
20
//
21
// Author:
22
// Peter Pilgrim
23
// Thu Feb 03 20:10:45 GMT 1999
24
//
25
// RCS HEADER
26
//
27
// $Author$
28
// $Date$
29
// $Source$
30
// $Revision$ $State$ $Locker$
31
//
32
// History
33
// ================================================================================
34
// $Log$
35

36 package ixenon.free.swing;
37
38 import java.awt.*;
39 import java.awt.event.*;
40 import java.util.*;
41 import java.io.*; // for File
42
import java.net.*; // for URL, MalformedUrlException
43

44 import javax.swing.*; // for JDialog, JFrame, JOptionPane
45
import javax.swing.event.*; // HyperlinkEvent, HyperlinkLister
46
import javax.swing.border.*; // for BevelBorder
47

48
49 /**
50  *
51  * Implementation of a PagerDialog custom JFC/Swing pseudo component
52  *
53  * *INCOMPLETE*
54  *
55  * @author Peter Pilgrim Thu Feb 03 20:11:27 GMT 1999
56  * @version $Id$
57  */

58 public class PagerDialog extends JDialog
59 implements ActionListener, WindowListener
60 {
61     /** the approve button response enumeration */
62     public final static int APPROVE_BUTTON = 2;
63     /** the cancel button response enumeration */
64     public final static int CANCEL_BUTTON = 1;
65     
66     /** the default approve button text */
67     public final static String JavaDoc DEFAULT_APPROVE_TEXT="Done";
68     /** the default approve button tooltip text */
69     public final static String JavaDoc DEFAULT_APPROVE_TOOLTIP_TEXT="dispose dialog";
70
71     /** the default approve button text */
72     public final static String JavaDoc DEFAULT_CANCEL_TEXT="Cancel";
73     /** the default approve button tooltip text */
74     public final static String JavaDoc DEFAULT_CANCEL_TOOLTIP_TEXT="cancel";
75
76     /** the approve button */
77     protected JButton bt_approve;
78     /** the cancel button */
79     protected JButton bt_cancel;
80
81     /** the pager component pane embedded in the dialog */
82     protected Pager pagerPane;
83
84     protected int response=-1;
85     
86     /**
87      * Create a PagerDialog with default configuration
88      * @param frame the parent frame
89      * @param title the title of the dialog
90      */

91     public PagerDialog( Frame frame, String JavaDoc title)
92     {
93     this( frame, title, DEFAULT_APPROVE_TEXT, DEFAULT_CANCEL_TEXT );
94     }
95     
96     /**
97      * Create a PagerDialog
98      * @param frame the parent frame
99      * @param title the title of the dialog
100      * @param approveText the text of the approve button
101      * @param cancelText the text of the cancel button
102      */

103     public PagerDialog( Frame frame, String JavaDoc title,
104                  String JavaDoc approveText, String JavaDoc cancelText )
105     {
106     super( frame, title, true /*modal*/ );
107     getContentPane().setLayout( new BorderLayout( 2,2 ));
108
109     // ======================================================================
110
// Create the central pager component work area panel
111
// ======================================================================
112
pagerPane = new Pager();
113     getContentPane().add( pagerPane, BorderLayout.CENTER );
114
115     // Add two get buttons to the pager's button container.
116
bt_cancel = new JButton( cancelText );
117     bt_cancel.addActionListener(this);
118     pagerPane.addExtraComponent( bt_cancel);
119     
120     bt_approve = new JButton( approveText );
121     bt_approve.addActionListener(this);
122     pagerPane.addExtraComponent( bt_approve);
123     
124     response = -1; /* ! */
125     addWindowListener(this);
126     pack();
127     }
128     
129     /** Return the Pager object reference */
130     public Pager getPagerPane()
131     {
132     return (pagerPane);
133     }
134     
135     // ======================================================================
136
// ACCESSORS & MODIFIERS
137
// ======================================================================
138

139     /** Convenience method: gets the number of pages in the pager */
140     public int getNumPages()
141     {
142     return pagerPane.getNumPages();
143     }
144     
145     /** Convenience method: gets the pager's page index */
146     public int getPageIndex()
147     {
148     return pagerPane.getPageIndex();
149     }
150     
151     /**
152      * Convenience method: sets the pager's page index
153      * @see #getNumPages
154      * @exception Illegal argument exception if the page limits exceeded
155      */

156     public void setPageIndex( int newIndex )
157     {
158     pagerPane.setPageIndex( newIndex );
159     }
160
161     /** Causes the pager to goto the previous page if available */
162     public void gotoPrevPage()
163     {
164     pagerPane.gotoPrevPage();
165     }
166     
167     /** Convenience method: causes the pager to goto the next page if available */
168     public void gotoNextPage()
169     {
170     pagerPane.gotoNextPage();
171     }
172     
173     /** Convenience method: adds a component as a new page */
174     public Component addPage( Component page )
175     {
176     return pagerPane.addPage( page );
177     }
178     
179     /** Convenience method: removes a component as a new page */
180     public void removePageAt( int index )
181     {
182     pagerPane.removePageAt( index );
183     }
184     
185     /** Convenience method: removes a component as a new page */
186     public void removePage( Component page )
187     {
188     pagerPane.removePage( page );
189     }
190     
191     /** Convenience method: remove remove all page components */
192     public void removeAllPages()
193     {
194     pagerPane.removeAllPages();
195     }
196
197     /** convenience method to get a reference to approve button object */
198     public JButton getApproveButton()
199     {
200     return bt_approve;
201     }
202
203     /** convenience method to get approve button text */
204     public String JavaDoc getApproveButtonText()
205     {
206     return bt_approve.getText();
207     }
208
209     /** convenience method to set approve button text */
210     public void setApproveButtonText( String JavaDoc text )
211     {
212     bt_approve.setText(text);
213     }
214
215     /** convenience method to get approve button tool tip text */
216     public String JavaDoc getApproveButtonToolTipText()
217     {
218     return bt_approve.getToolTipText();
219     }
220
221     /** convenience method to set approve button tool tip text */
222     public void setApproveButtonToolTipText( String JavaDoc tooltip_text )
223     {
224     bt_approve.setToolTipText(tooltip_text);
225     }
226     
227
228     /** convenience method to get a reference to cancel button object */
229     public JButton getCancelButton()
230     {
231     return bt_cancel;
232     }
233
234     /** convenience method to get cancel button text */
235     public String JavaDoc getCancelButtonText()
236     {
237     return bt_cancel.getText();
238     }
239
240     /** convenience method to set cancel button text */
241     public void setCancelButtonText( String JavaDoc text )
242     {
243     bt_cancel.setText(text);
244     }
245
246     /** convenience method to get cancel button tool tip text */
247     public String JavaDoc getCancelButtonToolTipText()
248     {
249     return bt_cancel.getToolTipText();
250     }
251
252     /** convenience method to set cancel button tool tip text */
253     public void setCancelButtonToolTipText( String JavaDoc tooltip_text )
254     {
255     bt_cancel.setToolTipText(tooltip_text);
256     }
257
258     /** returns the last user response of the dialog or
259      * -1 if there was none at all
260      */

261     public int getResponse()
262     {
263     return response;
264     }
265     
266     /** method to show the dialog and return a response */
267     public int showDialog()
268     {
269     setVisible(true);
270     return getResponse();
271     }
272     
273     // ======================================================================
274
// EVENT HANDLING
275
// ======================================================================
276

277
278     /** This method is public by way of implementation */
279     public void actionPerformed( ActionEvent e )
280     {
281     Object JavaDoc src = e.getSource();
282     if ( src == bt_approve ) {
283         response = APPROVE_BUTTON;
284         setVisible(false);
285         dispose();
286     }
287     else if ( src == bt_cancel ) {
288         response = CANCEL_BUTTON;
289         setVisible(false);
290         dispose();
291     }
292     }
293
294     public void windowClosed(WindowEvent event) {}
295     public void windowDeiconified(WindowEvent event) {}
296     public void windowIconified(WindowEvent event) {}
297     public void windowActivated(WindowEvent event) {}
298     public void windowDeactivated(WindowEvent event) {}
299     public void windowOpened(WindowEvent event) {}
300     public void windowClosing(WindowEvent event) { setVisible(false); dispose(); }
301
302     public static void main( String JavaDoc [] args )
303     {
304     JFrame frame = new JFrame("Pager Demonstration");
305     frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
306
307     PagerDialog dialog = new PagerDialog( frame, "PagerDialog");
308     Pager pager = dialog.getPagerPane();
309
310     pager.setPrevButtonToolTipText("go to previous item");
311     pager.setNextButtonToolTipText("go to next item");
312     pager.setHelpButtonToolTipText("call the lifeguard");
313     dialog.setApproveButtonToolTipText("your approval");
314     dialog.setCancelButtonToolTipText("dismiss this dialog");
315
316     Font font = new Font( "Dialog", Font.BOLD, 24 );
317     for (int q=0; q<10; ++q ) {
318         JPanel panel = new JPanel();
319         panel.setLayout( new BorderLayout() );
320         panel.setPreferredSize( new Dimension( 325, 200 ));
321         JButton button = new JButton("Page #"+q );
322         panel.add(button, BorderLayout.CENTER );
323         dialog.addPage(panel);
324     }
325     
326     frame.pack();
327     // frame.setVisible(true);
328
dialog.pack();
329     int answer = dialog.showDialog();
330     System.out.println("dialog response:"+answer);
331     System.exit(0);
332     }
333
334
335
336
337 }
338
339 // fini
340
Popular Tags