1 30 package com.genimen.djeneric.tools.common; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.Toolkit ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.KeyEvent ; 37 import java.awt.event.MouseEvent ; 38 import java.awt.event.WindowEvent ; 39 import java.io.File ; 40 import java.io.FileInputStream ; 41 import java.io.FileOutputStream ; 42 import java.io.InputStream ; 43 import java.io.OutputStream ; 44 45 import javax.swing.DefaultComboBoxModel ; 46 import javax.swing.JButton ; 47 import javax.swing.JDialog ; 48 import javax.swing.JFrame ; 49 import javax.swing.JList ; 50 import javax.swing.JPanel ; 51 import javax.swing.JScrollPane ; 52 import javax.swing.ListSelectionModel ; 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 62 { 63 private static final long serialVersionUID = 1L; 64 private final static String REPOSITORY = "logon.repository"; 65 66 DjProperties _props = new DjProperties(); 67 68 JPanel panel1 = new JPanel (); 69 BorderLayout borderLayout1 = new BorderLayout (); 70 JPanel jPanel3 = new JPanel (); 71 BorderLayout borderLayout2 = new BorderLayout (); 72 JPanel jPanel4 = new JPanel (); 73 JButton _butOk = new JButton (); 74 JButton _butCancel = new JButton (); 75 boolean _wasCancelled = true; 76 JScrollPane jScrollPane1 = new JScrollPane (); 77 JList _lstRepository = new JList (); 78 79 public RepositoryDialog(JFrame 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 mdl = new DefaultComboBoxModel (repositories); 87 _lstRepository.setModel(mdl); 88 pack(); 89 setSize(300, 300); 90 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 91 Dimension frameSize = getSize(); 92 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 93 94 loadProps(); 95 String 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 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 124 { 125 panel1.setLayout(borderLayout1); 126 jPanel3.setLayout(borderLayout2); 127 _butOk.setText(Messages.getString("global.Ok")); 128 _butOk.addKeyListener(new java.awt.event.KeyAdapter () 129 { 130 public void keyPressed(KeyEvent e) 131 { 132 handleKeys(e); 133 } 134 }); 135 _butOk.addActionListener(new java.awt.event.ActionListener () 136 { 137 public void actionPerformed(ActionEvent e) 138 { 139 _butOk_actionPerformed(e); 140 } 141 }); 142 _butCancel.setText(Messages.getString("global.Cancel")); 143 _butCancel.addKeyListener(new java.awt.event.KeyAdapter () 144 { 145 public void keyPressed(KeyEvent e) 146 { 147 handleKeys(e); 148 } 149 }); 150 _butCancel.addActionListener(new java.awt.event.ActionListener () 151 { 152 public void actionPerformed(ActionEvent e) 153 { 154 _butCancel_actionPerformed(e); 155 } 156 }); 157 _lstRepository.addKeyListener(new java.awt.event.KeyAdapter () 158 { 159 public void keyPressed(KeyEvent e) 160 { 161 handleKeys(e); 162 } 163 }); 164 _lstRepository.addMouseListener(new java.awt.event.MouseAdapter () 165 { 166 public void mouseClicked(MouseEvent e) 167 { 168 _lstRepository_mouseClicked(e); 169 } 170 }); 171 this.addWindowListener(new java.awt.event.WindowAdapter () 172 { 173 public void windowOpened(WindowEvent 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 e) 190 { 191 _wasCancelled = false; 192 saveProps(); 193 setVisible(false); 194 } 195 196 void _butCancel_actionPerformed(ActionEvent e) 197 { 198 setVisible(false); 199 } 200 201 private void loadProps() 202 { 203 String absFileName = new File (LoginDialog._propertiesFileName).getAbsoluteFile().toString(); 204 205 try 206 { 207 InputStream ios = new FileInputStream (absFileName); 208 _props.load(ios); 209 ios.close(); 210 211 } 212 catch (Exception x) 213 { 214 } 216 } 217 218 private void saveProps() 219 { 220 String absFileName = new File (LoginDialog._propertiesFileName).getAbsoluteFile().toString(); 221 222 try 223 { 224 OutputStream os = new FileOutputStream (absFileName); 225 226 _props.setProperty(REPOSITORY, getRepository().getName()); 227 228 _props.store(os, Messages.getString("global.CachedLoginCredentials")); 229 os.close(); 230 } 231 catch (Exception x) 232 { 233 System.err.println(Messages.getString("global.WarningCouldNotWrite", absFileName)); 234 } 235 } 236 237 void handleKeys(KeyEvent 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 e) 244 { 245 if (e.getClickCount() > 1) _butOk_actionPerformed(null); 246 } 247 248 void this_windowOpened(WindowEvent e) 249 { 250 _lstRepository.requestFocus(); 251 252 } 253 254 } | Popular Tags |