KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > common > ContextDialog


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.common;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Toolkit JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.KeyEvent JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.FileInputStream JavaDoc;
39 import java.io.FileOutputStream JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.io.OutputStream JavaDoc;
42
43 import javax.swing.DefaultComboBoxModel JavaDoc;
44 import javax.swing.JButton JavaDoc;
45 import javax.swing.JDialog JavaDoc;
46 import javax.swing.JFrame JavaDoc;
47 import javax.swing.JList JavaDoc;
48 import javax.swing.JPanel JavaDoc;
49 import javax.swing.JScrollPane JavaDoc;
50 import javax.swing.ListSelectionModel JavaDoc;
51
52 import com.genimen.djeneric.language.Messages;
53 import com.genimen.djeneric.repository.DjContext;
54 import com.genimen.djeneric.repository.exceptions.LogonException;
55 import com.genimen.djeneric.ui.Util;
56 import com.genimen.djeneric.util.DjLogger;
57 import com.genimen.djeneric.util.DjProperties;
58
59 public class ContextDialog extends JDialog JavaDoc
60 {
61   private static final long serialVersionUID = 1L;
62   private final static String JavaDoc CONTEXT = "logon.context";
63
64   DjProperties _props = new DjProperties();
65
66   JPanel JavaDoc panel1 = new JPanel JavaDoc();
67   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
68   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
69   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
70   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
71   JButton JavaDoc _butOk = new JButton JavaDoc();
72   JButton JavaDoc _butCancel = new JButton JavaDoc();
73   boolean _wasCancelled = true;
74   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
75   JList JavaDoc _lstContext = new JList JavaDoc();
76
77   public ContextDialog(JFrame JavaDoc mainFrame, DjContext[] contexts) throws LogonException
78   {
79     super(mainFrame, Messages.getString("ContextDialog.PleaseSelectContext"), true);
80     try
81     {
82       if (contexts.length == 0) throw new LogonException(Messages.getString("ContextDialog.NoAccess"));
83       jbInit();
84       DefaultComboBoxModel JavaDoc mdl = new DefaultComboBoxModel JavaDoc(contexts);
85       _lstContext.setModel(mdl);
86       pack();
87       setSize(300, 300);
88       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
89       Dimension JavaDoc frameSize = getSize();
90       setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
91
92       loadProps();
93       String JavaDoc contextCode = _props.getProperty(CONTEXT, "");
94
95       _lstContext.setSelectedIndex(0);
96       for (int i = 0; i < mdl.getSize(); i++)
97       {
98         DjContext r = (DjContext) mdl.getElementAt(i);
99         if (r.getCode().equals(contextCode))
100         {
101           _lstContext.setSelectedIndex(i);
102           break;
103         }
104       }
105
106       setVisible(true);
107       if (_wasCancelled) throw new LogonException(Messages.getString("ContextDialog.ContextSelectionCancelled"));
108     }
109     catch (Exception JavaDoc ex)
110     {
111       throw new LogonException(ex);
112     }
113   }
114
115   public DjContext getContext()
116   {
117     if (_lstContext.getSelectedIndex() == -1) return null;
118     return (DjContext) _lstContext.getSelectedValue();
119   }
120
121   void jbInit() throws Exception JavaDoc
122   {
123     panel1.setLayout(borderLayout1);
124     jPanel3.setLayout(borderLayout2);
125     _butOk.setText(Messages.getString("global.Ok"));
126     _butOk.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
127     {
128       public void keyPressed(KeyEvent JavaDoc e)
129       {
130         _butOk_keyPressed(e);
131       }
132     });
133     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
134     {
135       public void actionPerformed(ActionEvent JavaDoc e)
136       {
137         _butOk_actionPerformed(e);
138       }
139     });
140     _butCancel.setText(Messages.getString("global.Cancel"));
141     _butCancel.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
142     {
143       public void keyPressed(KeyEvent JavaDoc e)
144       {
145         _butCancel_keyPressed(e);
146       }
147     });
148     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
149     {
150       public void actionPerformed(ActionEvent JavaDoc e)
151       {
152         _butCancel_actionPerformed(e);
153       }
154     });
155     _lstContext.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
156     {
157       public void keyPressed(KeyEvent JavaDoc e)
158       {
159         _lstContext_keyPressed(e);
160       }
161     });
162     _lstContext.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
163     getContentPane().add(panel1);
164     panel1.add(jPanel3, BorderLayout.SOUTH);
165     jPanel3.add(jPanel4, BorderLayout.EAST);
166     jPanel4.add(_butCancel, null);
167     jPanel4.add(_butOk, null);
168     panel1.add(jScrollPane1, BorderLayout.CENTER);
169     jScrollPane1.getViewport().add(_lstContext, null);
170     Util.sizeButtons(jPanel4);
171   }
172
173   void _butOk_actionPerformed(ActionEvent JavaDoc e)
174   {
175     _wasCancelled = false;
176     saveProps();
177     setVisible(false);
178   }
179
180   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
181   {
182     setVisible(false);
183   }
184
185   private void loadProps()
186   {
187     String JavaDoc absFileName = new File JavaDoc(LoginDialog._propertiesFileName).getAbsoluteFile().toString();
188
189     try
190     {
191       InputStream JavaDoc ios = new FileInputStream JavaDoc(absFileName);
192       _props.load(ios);
193       ios.close();
194
195     }
196     catch (Exception JavaDoc x)
197     {
198       DjLogger.log(x);
199     }
200   }
201
202   private void saveProps()
203   {
204     String JavaDoc absFileName = new File JavaDoc(LoginDialog._propertiesFileName).getAbsoluteFile().toString();
205
206     try
207     {
208       OutputStream JavaDoc os = new FileOutputStream JavaDoc(absFileName);
209
210       _props.setProperty(CONTEXT, getContext().getCode());
211
212       _props.store(os, Messages.getString("global.CachedLoginCredentials"));
213       os.close();
214     }
215     catch (Exception JavaDoc x)
216     {
217       System.err.println(Messages.getString("global.WarningCouldNotWrite", absFileName));
218     }
219   }
220
221   void _lstContext_keyPressed(KeyEvent JavaDoc e)
222   {
223     if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null);
224     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null);
225   }
226
227   void _butCancel_keyPressed(KeyEvent JavaDoc e)
228   {
229     _lstContext_keyPressed(e);
230   }
231
232   void _butOk_keyPressed(KeyEvent JavaDoc e)
233   {
234     _lstContext_keyPressed(e);
235   }
236 }
Popular Tags