KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > common > DialogHandler


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.common;
25
26 // ToolBox imports
27
import org.enhydra.tool.ToolBoxInfo;
28 import org.enhydra.tool.common.AboutBox;
29 import org.enhydra.tool.common.ButtonPanel;
30 import org.enhydra.tool.common.InnerPanel;
31 import org.enhydra.tool.common.ProgressMeter;
32 import org.enhydra.tool.common.SwingUtil;
33 import org.enhydra.tool.common.event.HelpEvent;
34 import org.enhydra.tool.common.event.HelpListener;
35
36 // Standard imports
37
import java.awt.event.ActionEvent JavaDoc;
38 import java.awt.event.ActionListener JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Arrays JavaDoc;
41 import java.util.ResourceBundle JavaDoc;
42 import java.awt.Component JavaDoc;
43 import java.awt.Container JavaDoc;
44 import java.awt.Dialog JavaDoc;
45 import java.awt.Frame JavaDoc;
46 import java.awt.GridBagLayout JavaDoc;
47 import java.awt.GridBagConstraints JavaDoc;
48 import java.awt.Insets JavaDoc;
49 import java.awt.Point JavaDoc;
50 import java.awt.Window JavaDoc;
51 import javax.swing.JComponent JavaDoc;
52 import javax.swing.JOptionPane JavaDoc;
53 import javax.swing.JPanel JavaDoc;
54 import javax.swing.JDialog JavaDoc;
55
56 //
57
abstract public class DialogHandler {
58
59     //
60
public static ResourceBundle JavaDoc res =
61         ResourceBundle.getBundle("org.enhydra.tool.common.Res"); // nores
62

63     //
64
private boolean standalone = false;
65     private int option = JOptionPane.CANCEL_OPTION;
66     private ProgressMeter progress = null;
67     private HelpListener[] helpListeners = new HelpListener[0];
68
69     public DialogHandler() {
70         pmInit();
71     }
72
73     abstract public InnerPanel getInnerPanel();
74     abstract public ButtonPanel getButtonPanel();
75     abstract public ActionListener JavaDoc createButtonListener();
76     abstract public String JavaDoc getTitle();
77
78     public int getOption() {
79       return option;
80     }
81
82     public void setOption(int op) {
83       option = op;
84     }
85
86     //
87
public synchronized void addHelpListener(HelpListener l) {
88         ArrayList JavaDoc list = null;
89
90         list = new ArrayList JavaDoc(Arrays.asList(helpListeners));
91         if (!list.contains(l)) {
92             list.add(l);
93             list.trimToSize();
94             helpListeners = new HelpListener[list.size()];
95             helpListeners = (HelpListener[]) list.toArray(helpListeners);
96         }
97         list.clear();
98     }
99
100     public synchronized void removeHelpListener(HelpListener l) {
101         ArrayList JavaDoc list = null;
102
103         list = new ArrayList JavaDoc(Arrays.asList(helpListeners));
104         if (list.contains(l)) {
105             list.remove(l);
106             list.trimToSize();
107             helpListeners = new HelpListener[list.size()];
108             helpListeners = (HelpListener[]) list.toArray(helpListeners);
109         }
110         list.clear();
111     }
112
113     public void notifyHelpListeners(Object JavaDoc source) {
114         HelpEvent event = null;
115
116         if (source instanceof JComponent JavaDoc) {
117             JComponent JavaDoc comp = (JComponent JavaDoc) source;
118
119             event = new HelpEvent(comp.getTopLevelAncestor());
120         } else {
121             event = new HelpEvent(source);
122         }
123         for (int i = 0; i < helpListeners.length; i++) {
124             helpListeners[i].onHelp(event);
125         }
126     }
127
128
129
130     public void showAbout() {
131         StringBuffer JavaDoc textBuf = new StringBuffer JavaDoc();
132         StringBuffer JavaDoc titleBuf = new StringBuffer JavaDoc();
133         AboutBox about = null;
134         Container JavaDoc ancestor = null;
135
136         ancestor = getInnerPanel().getTopLevelAncestor();
137         textBuf.append("Enhydra Application Server 6.5");
138 // textBuf.append(' ');
139
// textBuf.append(ToolBoxInfo.getEnhydraVersion());
140
textBuf.append('\n');
141         textBuf.append(Constants.XMLC);
142         textBuf.append(' ');
143         textBuf.append(ToolBoxInfo.getXMLCVersion());
144         if (ancestor instanceof Frame JavaDoc) {
145             about = new AboutBox((Frame JavaDoc) ancestor);
146         } else if (ancestor instanceof Dialog JavaDoc) {
147             about = new AboutBox((Dialog JavaDoc) ancestor);
148         } else {
149             about = new AboutBox(new Frame JavaDoc());
150         }
151         titleBuf.append(Constants.KELP);
152         titleBuf.append(' ');
153         titleBuf.append(ToolBoxInfo.getToolBoxVersion());
154         about.setTitleText(titleBuf.toString());
155         about.setMediumText(textBuf.toString());
156 // about.setSmallText(ToolBoxInfo.getCopyright());
157
about.show();
158         about.removeAll();
159         about.dispose();
160     }
161
162     public int showDialog(Component JavaDoc parent) {
163         JPanel JavaDoc panelBottom = null;
164         JDialog JavaDoc dialog = null;
165         Point JavaDoc cPoint;
166
167         getInnerPanel().initPreferredSize();
168         if (parent == null) {
169             dialog = new JDialog JavaDoc();
170         } else if (parent instanceof Frame JavaDoc) {
171             dialog = new JDialog JavaDoc((Frame JavaDoc) parent);
172         } else if (parent instanceof Dialog JavaDoc) {
173             dialog = new JDialog JavaDoc((Dialog JavaDoc) parent);
174         } else {
175             dialog = new JDialog JavaDoc();
176         }
177         getButtonPanel().addActionListener(createButtonListener());
178         if (helpListeners.length == 0) {
179             getButtonPanel().removeHelp();
180         }
181         panelBottom = new JPanel JavaDoc();
182         panelBottom.setLayout(new GridBagLayout JavaDoc());
183         dialog.setTitle(getTitle());
184         dialog.setModal(true);
185         dialog.getContentPane().setLayout(new GridBagLayout JavaDoc());
186         dialog.getContentPane().add(getInnerPanel(),
187                                     new GridBagConstraints JavaDoc(0, 0, 1, 1, 0.75,
188                                     0.75, GridBagConstraints.CENTER,
189                                     GridBagConstraints.BOTH,
190                                     new Insets JavaDoc(0, 0, 0, 0), 0, 0));
191         dialog.getContentPane().add(getButtonPanel(),
192                                     new GridBagConstraints JavaDoc(0, 1, 1, 1, 0.25,
193                                     0.25, GridBagConstraints.CENTER,
194                                     GridBagConstraints.BOTH,
195                                     new Insets JavaDoc(0, 0, 0, 0), 0, 0));
196         dialog.invalidate();
197         dialog.doLayout();
198         dialog.pack();
199         cPoint = SwingUtil.getCenteringPoint(dialog.getSize());
200         dialog.setLocation(cPoint);
201         getInnerPanel().preShow();
202         dialog.show();
203         return getOption();
204     }
205
206     /**
207      * Close the dialog and exit the VM if this wizard
208      * was run as a standalone application.
209      */

210     public void closeWindow() {
211         Window JavaDoc window = null;
212
213         try {
214             getInnerPanel().save();
215         } catch (Exception JavaDoc e) {
216             e.printStackTrace(System.err);
217         }
218         if (getInnerPanel().getTopLevelAncestor() instanceof Window JavaDoc) {
219             window = (Window JavaDoc) getInnerPanel().getTopLevelAncestor();
220         }
221         if (window != null) {
222             window.removeAll();
223             window.dispose();
224             clearAll();
225         }
226         if (standalone) {
227             System.exit(0);
228         }
229     }
230
231     /**
232      * Remove all object references.
233      */

234     protected void clearAll() {
235         getInnerPanel().clearAll();
236         getButtonPanel().clearAll();
237         helpListeners = new HelpListener[0];
238     }
239
240      public ProgressMeter getProgressMeter() {
241         return progress;
242     }
243
244     public void openProgress() {
245         progress.setModal(true);
246         progress.setTitle(getProgressTitle());
247         progress.setOwner((Window JavaDoc) getInnerPanel().getTopLevelAncestor());
248         progress.startDialogThread();
249     }
250
251     //
252
// ABSTRACT PROTECTED
253
//
254
abstract protected String JavaDoc getProgressTitle();
255
256     //
257
// PRIVATE
258
//
259
private void pmInit() {
260         progress = new ProgressMeter();
261         progress.setDisposeOnClose(false);
262         progress.setTitle(getProgressTitle());
263     }
264
265 }
266
Popular Tags