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.io.File ; 38 import java.io.FileInputStream ; 39 import java.io.FileOutputStream ; 40 import java.io.InputStream ; 41 import java.io.OutputStream ; 42 43 import javax.swing.DefaultComboBoxModel ; 44 import javax.swing.JButton ; 45 import javax.swing.JDialog ; 46 import javax.swing.JFrame ; 47 import javax.swing.JList ; 48 import javax.swing.JPanel ; 49 import javax.swing.JScrollPane ; 50 import javax.swing.ListSelectionModel ; 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 60 { 61 private static final long serialVersionUID = 1L; 62 private final static String CONTEXT = "logon.context"; 63 64 DjProperties _props = new DjProperties(); 65 66 JPanel panel1 = new JPanel (); 67 BorderLayout borderLayout1 = new BorderLayout (); 68 JPanel jPanel3 = new JPanel (); 69 BorderLayout borderLayout2 = new BorderLayout (); 70 JPanel jPanel4 = new JPanel (); 71 JButton _butOk = new JButton (); 72 JButton _butCancel = new JButton (); 73 boolean _wasCancelled = true; 74 JScrollPane jScrollPane1 = new JScrollPane (); 75 JList _lstContext = new JList (); 76 77 public ContextDialog(JFrame 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 mdl = new DefaultComboBoxModel (contexts); 85 _lstContext.setModel(mdl); 86 pack(); 87 setSize(300, 300); 88 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 89 Dimension frameSize = getSize(); 90 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 91 92 loadProps(); 93 String 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 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 122 { 123 panel1.setLayout(borderLayout1); 124 jPanel3.setLayout(borderLayout2); 125 _butOk.setText(Messages.getString("global.Ok")); 126 _butOk.addKeyListener(new java.awt.event.KeyAdapter () 127 { 128 public void keyPressed(KeyEvent e) 129 { 130 _butOk_keyPressed(e); 131 } 132 }); 133 _butOk.addActionListener(new java.awt.event.ActionListener () 134 { 135 public void actionPerformed(ActionEvent e) 136 { 137 _butOk_actionPerformed(e); 138 } 139 }); 140 _butCancel.setText(Messages.getString("global.Cancel")); 141 _butCancel.addKeyListener(new java.awt.event.KeyAdapter () 142 { 143 public void keyPressed(KeyEvent e) 144 { 145 _butCancel_keyPressed(e); 146 } 147 }); 148 _butCancel.addActionListener(new java.awt.event.ActionListener () 149 { 150 public void actionPerformed(ActionEvent e) 151 { 152 _butCancel_actionPerformed(e); 153 } 154 }); 155 _lstContext.addKeyListener(new java.awt.event.KeyAdapter () 156 { 157 public void keyPressed(KeyEvent 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 e) 174 { 175 _wasCancelled = false; 176 saveProps(); 177 setVisible(false); 178 } 179 180 void _butCancel_actionPerformed(ActionEvent e) 181 { 182 setVisible(false); 183 } 184 185 private void loadProps() 186 { 187 String absFileName = new File (LoginDialog._propertiesFileName).getAbsoluteFile().toString(); 188 189 try 190 { 191 InputStream ios = new FileInputStream (absFileName); 192 _props.load(ios); 193 ios.close(); 194 195 } 196 catch (Exception x) 197 { 198 DjLogger.log(x); 199 } 200 } 201 202 private void saveProps() 203 { 204 String absFileName = new File (LoginDialog._propertiesFileName).getAbsoluteFile().toString(); 205 206 try 207 { 208 OutputStream os = new FileOutputStream (absFileName); 209 210 _props.setProperty(CONTEXT, getContext().getCode()); 211 212 _props.store(os, Messages.getString("global.CachedLoginCredentials")); 213 os.close(); 214 } 215 catch (Exception x) 216 { 217 System.err.println(Messages.getString("global.WarningCouldNotWrite", absFileName)); 218 } 219 } 220 221 void _lstContext_keyPressed(KeyEvent 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 e) 228 { 229 _lstContext_keyPressed(e); 230 } 231 232 void _butOk_keyPressed(KeyEvent e) 233 { 234 _lstContext_keyPressed(e); 235 } 236 } | Popular Tags |