KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > client > AbstractOptionDialog


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.client;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.FlowLayout JavaDoc;
25 import java.awt.Point JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27
28 import javax.swing.AbstractAction JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import javax.swing.Box JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JDialog JavaDoc;
33 import javax.swing.JFrame JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.SwingConstants JavaDoc;
37 import javax.swing.border.CompoundBorder JavaDoc;
38 import javax.swing.border.EmptyBorder JavaDoc;
39 import javax.swing.border.EtchedBorder JavaDoc;
40
41 /**
42  *
43  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
44  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:23 $
45  * @since 4.1
46  */

47 public abstract class AbstractOptionDialog
48     extends JDialog JavaDoc
49 {
50     public static final int BUTTON_OK = 1;
51     public static final int BUTTON_CANCEL = 2;
52     
53     protected int m_action = BUTTON_CANCEL;
54     
55     /*---------------------------------------------------------------
56      * Constructors
57      *-------------------------------------------------------------*/

58     /**
59      * Creates a new AbstractOptionDialog.
60      *
61      * @param frame Frame which owns the dialog.
62      * @param title Title for the dialog.
63      * @param buttons List of buttons to display.
64      */

65     protected AbstractOptionDialog( JFrame JavaDoc frame, String JavaDoc title, int buttons )
66     {
67         super( frame, title, true );
68         
69         JPanel JavaDoc contentPane = (JPanel JavaDoc)getContentPane();
70         contentPane.setLayout( new BorderLayout JavaDoc() );
71         contentPane.setBorder( new EmptyBorder JavaDoc( 5, 5, 5, 5 ) );
72         
73         JPanel JavaDoc backPane = new JPanel JavaDoc();
74         backPane.setLayout( new BorderLayout JavaDoc() );
75         backPane.setBorder(
76             new CompoundBorder JavaDoc(
77                 new EmptyBorder JavaDoc( 0, 0, 5, 0 ),
78                 new CompoundBorder JavaDoc(
79                     new EtchedBorder JavaDoc( EtchedBorder.LOWERED ),
80                     new EmptyBorder JavaDoc( 5, 5, 5, 5 )
81                 )
82             )
83         );
84         contentPane.add( backPane, BorderLayout.CENTER );
85         
86         // Build the message
87
backPane.add( new JLabel JavaDoc( getMessage(), SwingConstants.LEFT ), BorderLayout.NORTH );
88         
89         // Build the main panel
90
JPanel JavaDoc mainPanel = getMainPanel();
91         mainPanel.setBorder( new EmptyBorder JavaDoc( 5, 0, 0, 0 ) );
92         backPane.add( mainPanel, BorderLayout.CENTER );
93         
94         
95         // Build the button panel
96
JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
97         buttonPanel.setLayout( new FlowLayout JavaDoc( FlowLayout.CENTER ) );
98         Box JavaDoc buttonBox = Box.createHorizontalBox();
99         if ( ( buttons & BUTTON_OK ) != 0 )
100         {
101             Action JavaDoc action = new AbstractAction JavaDoc( "OK" )
102             {
103                 public void actionPerformed( ActionEvent JavaDoc event )
104                 {
105                     if ( validateFields() )
106                     {
107                         m_action = BUTTON_OK;
108                         AbstractOptionDialog.this.hide();
109                     }
110                 }
111             };
112             JButton JavaDoc button = new JButton JavaDoc( action );
113             buttonBox.add( button );
114             buttonBox.add( Box.createHorizontalStrut( 5 ) );
115         }
116         if ( ( buttons & BUTTON_CANCEL ) != 0 )
117         {
118             Action JavaDoc action = new AbstractAction JavaDoc( "Cancel" )
119             {
120                 public void actionPerformed( ActionEvent JavaDoc event )
121                 {
122                     m_action = BUTTON_CANCEL;
123                     AbstractOptionDialog.this.hide();
124                 }
125             };
126             JButton JavaDoc button = new JButton JavaDoc( action );
127             buttonBox.add( button );
128             buttonBox.add( Box.createHorizontalStrut( 5 ) );
129         }
130         buttonPanel.add( buttonBox );
131         contentPane.add( buttonPanel, BorderLayout.SOUTH );
132         
133         pack();
134         
135         // Position the dialog.
136
Point JavaDoc frameLocation = frame.getLocation();
137         Dimension JavaDoc frameSize = frame.getSize();
138         Dimension JavaDoc size = getSize();
139         
140         setLocation(
141             (int)( frameLocation.getX() + (frameSize.getWidth() - size.getWidth() ) / 2 ),
142             (int)( frameLocation.getY() + (frameSize.getHeight() - size.getHeight() ) / 2 ) );
143         
144         // Make the dialog a fixed size.
145
setResizable( false );
146     }
147     
148     /*---------------------------------------------------------------
149      * Methods
150      *-------------------------------------------------------------*/

151     /**
152      * Returns the message to show at the top of the dialog.
153      *
154      * @return The text of the message.
155      */

156     protected abstract String JavaDoc getMessage();
157     
158     /**
159      * Returns the main panel which makes up the guts of the dialog.
160      *
161      * @return The main panel.
162      */

163     protected abstract JPanel JavaDoc getMainPanel();
164     
165     /**
166      * Goes through and validates the fields in the dialog.
167      *
168      * @return True if the fields were Ok.
169      */

170     protected boolean validateFields()
171     {
172         return true;
173     }
174     
175     /**
176      * Returns the button which the user selected.
177      */

178     public int getAction()
179     {
180         return m_action;
181     }
182 }
183
184
Popular Tags