KickJava   Java API By Example, From Geeks To Geeks.

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


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