1 package com.genimen.djeneric.tools.common; 2 3 import java.awt.Color ; 4 import java.awt.Dimension ; 5 import java.awt.Toolkit ; 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileOutputStream ; 9 import java.io.InputStream ; 10 import java.io.OutputStream ; 11 import java.sql.SQLException ; 12 13 import javax.swing.JFrame ; 14 import javax.swing.JLabel ; 15 import javax.swing.JOptionPane ; 16 import javax.swing.RepaintManager ; 17 import javax.swing.UIManager ; 18 import javax.swing.plaf.ColorUIResource ; 19 20 import com.genimen.djeneric.language.Messages; 21 import com.genimen.djeneric.repository.DjContext; 22 import com.genimen.djeneric.repository.DjCredentials; 23 import com.genimen.djeneric.repository.DjMessenger; 24 import com.genimen.djeneric.repository.DjRepositoryDescriptor; 25 import com.genimen.djeneric.repository.exceptions.DjenericException; 26 import com.genimen.djeneric.repository.exceptions.LogonException; 27 import com.genimen.djeneric.util.DjLogger; 28 import com.genimen.djeneric.util.DjProperties; 29 import com.genimen.djeneric.util.DjStatusDisplayer; 30 import com.genimen.djeneric.util.DjStringReplacer; 31 import com.incors.plaf.kunststoff.KunststoffLookAndFeel; 32 import com.incors.plaf.kunststoff.KunststoffTheme; 33 34 public class DjenericTool extends JFrame implements DjStatusDisplayer, DjMessenger 35 { 36 37 private static final long serialVersionUID = 1L; 38 private DjProperties _props = new DjProperties(); 39 private String _loadedPropertiesFileName = null; 40 41 JLabel _lblStatus = null; 42 Color _normalColor = new Color (102, 102, 153); 43 44 public String getProperty(String key, String dflt) 45 { 46 return _props.getProperty(key, dflt); 47 } 48 49 public void setProperty(String key, String value) 50 { 51 _props.setProperty(key, value); 52 } 53 54 protected void loadProps(String fileName) 55 { 56 _loadedPropertiesFileName = fileName; 57 getPropsFrom(fileName); 58 } 59 60 protected void getPropsFrom(String fileName) 61 { 62 String absFileName = new File (fileName).getAbsoluteFile().toString(); 63 64 try 65 { 66 InputStream ios = new FileInputStream (absFileName); 67 _props.load(ios); 68 ios.close(); 69 70 } 71 catch (Exception x) 72 { 73 DjLogger.log(Messages.getString("DjenericTool.CouldNotOpenProps", absFileName)); 74 } 75 } 76 77 protected void saveProps() 78 { 79 saveProps(_loadedPropertiesFileName); 80 } 81 82 protected void saveProps(String filename) 83 { 84 String absFileName = new File (filename).getAbsoluteFile().toString(); 85 86 try 87 { 88 OutputStream os = new FileOutputStream (absFileName); 89 90 updateProperties(); 91 92 _props.store(os, Messages.getString("DjenericTool.PropertiesFor", this.getClass().getName())); 93 os.close(); 94 } 95 catch (Exception x) 96 { 97 JOptionPane.showMessageDialog(this, Messages.getString("DjenericTool.CouldNotWriteProps", absFileName)); 98 } 99 } 100 101 protected void exitProgram() 102 { 103 saveProps(); 104 105 System.exit(0); 106 } 107 108 protected void updateProperties() 109 { 110 System.err.println(Messages.getString("DjenericTool.NotUpdated", this.getClass().getName())); 111 } 112 113 public void setStatusLabel(JLabel lbl) 114 { 115 _lblStatus = lbl; 116 _normalColor = _lblStatus.getForeground(); 117 } 118 119 public void setStatusMessage(String msg, boolean informative) 120 { 121 if ((msg == null) && !informative) msg = Messages.getString("global.Nullpointer"); 122 123 if (_lblStatus == null) 124 { 125 System.err.println(msg); 126 return; 127 } 128 129 if (!informative) _lblStatus.setForeground(Color.red); 130 else _lblStatus.setForeground(_normalColor); 131 if (msg == null || msg.trim().length() == 0) msg = " "; 132 133 if (msg.indexOf("\n") != -1) 134 { 135 DjStringReplacer sr = new DjStringReplacer(msg); 136 msg = sr.replace("\n", " "); 137 } 138 139 _lblStatus.setText(msg); 140 } 141 142 public void setStatusMessageNow(String msg, boolean informative) 143 { 144 setStatusMessage(msg, informative); 145 if (_lblStatus == null) return; 146 RepaintManager rm = RepaintManager.currentManager(_lblStatus); 147 if (rm != null) 148 { 149 rm.addInvalidComponent(_lblStatus); 150 rm.paintDirtyRegions(); 151 } 152 } 153 154 public void setStatusMessage(String msg) 155 { 156 setStatusMessage(msg, true); 157 } 158 159 public void setStatusMessage(Throwable e) 160 { 161 String msg = e.getMessage(); 162 if (!(e instanceof DjenericException)) msg = e.getClass().getName() + ": " + msg; 163 164 e.printStackTrace(); 165 setStatusMessage(msg, false); 166 } 167 168 public void handleException(Exception ex) 169 { 170 String msg = ex.getMessage(); 171 if (ex instanceof DjenericException) 172 { 173 DjenericException de = (DjenericException) ex; 174 175 if (de.getRootException() instanceof SQLException ) 176 { 177 msg = Messages.getString("DjenericTool.CouldNotConnect", de.getRootException().getMessage()); 178 } 179 if (de.getRootException() instanceof ClassNotFoundException ) 180 { 181 msg = Messages.getString("DjenericTool.CouldNotLoadClass", de.getRootException().getMessage()); 182 } 183 } 184 else 185 { 186 msg = ex.getClass().getName() + ": " + msg; 187 } 188 189 setStatusMessage(msg, false); 190 JOptionPane.showMessageDialog(this, msg, Messages.getString("DjenericTool.CouldNotConnect1"), 191 JOptionPane.WARNING_MESSAGE); 192 193 ex.printStackTrace(); 194 } 195 196 public DjProperties getProperties() 197 { 198 return _props; 199 } 200 201 public void startApp() 202 { 203 int BORDER_SIZE = 55; 204 205 validate(); 206 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 207 Dimension frameSize = getSize(); 208 if (frameSize.height > screenSize.height - BORDER_SIZE) 209 { 210 frameSize.height = screenSize.height - BORDER_SIZE; 211 } 212 if (frameSize.width > screenSize.width - BORDER_SIZE) 213 { 214 frameSize.width = screenSize.width - BORDER_SIZE; 215 } 216 setSize(frameSize); 217 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 218 checkVmVersion(); 219 setVisible(true); 220 } 221 222 protected void checkVmVersion() 223 { 224 String version = System.getProperty("java.version"); 225 if (version.startsWith("1.0") || version.startsWith("1.1") || version.startsWith("1.2") 226 || version.startsWith("1.3")) 227 { 228 JOptionPane.showMessageDialog(this, Messages.getString("DjenericTool.WrongVersion", version), "JDK " + version, 229 JOptionPane.WARNING_MESSAGE); 230 } 231 } 232 233 public static void setLookAndFeel() 234 { 235 try 236 { 237 KunststoffLookAndFeel klaf = new com.incors.plaf.kunststoff.KunststoffLookAndFeel(); 238 KunststoffLookAndFeel.setCurrentTheme(new MyTheme()); 239 UIManager.setLookAndFeel(klaf); 240 } 242 catch (Exception x) 243 { 244 x.printStackTrace(); 245 } 246 247 } 248 249 public DjCredentials getCredentials(DjRepositoryDescriptor repository) throws LogonException 250 { 251 LoginDialog ld = new LoginDialog(this, repository); 252 DjCredentials credentials = new DjCredentials(); 253 credentials.setUserid(ld.getUserId()); 254 credentials.setPassword(ld.getPassword()); 255 return credentials; 256 } 257 258 public DjContext selectContext(DjContext[] ctxts) throws LogonException 259 { 260 if(ctxts.length == 1) return ctxts[0]; 261 262 ContextDialog cd = new ContextDialog(this, ctxts); 263 return cd.getContext(); 264 } 265 266 public DjRepositoryDescriptor selectRepository(DjRepositoryDescriptor[] descriptors) throws DjenericException 267 { 268 RepositoryDialog dlg = new RepositoryDialog(this, descriptors); 269 return dlg.getRepository(); 270 } 271 272 public void showMessage(String title, String msg) 273 { 274 JOptionPane.showMessageDialog(this, msg, title, JOptionPane.INFORMATION_MESSAGE); 275 } 276 277 public void showWarning(String title, String msg) 278 { 279 JOptionPane.showMessageDialog(this, msg, title, JOptionPane.WARNING_MESSAGE); 280 } 281 282 } 283 284 class MyTheme extends KunststoffTheme 285 { 286 287 private final ColorUIResource primary1 = new ColorUIResource (32, 32, 32); 288 289 private final ColorUIResource primary2 = new ColorUIResource (160, 160, 180); 290 291 private final ColorUIResource primary3 = new ColorUIResource (200, 200, 224); 292 293 private final ColorUIResource secondary1 = new ColorUIResource (130, 130, 130); 294 295 private final ColorUIResource secondary2 = new ColorUIResource (180, 180, 180); 296 297 private final ColorUIResource secondary3 = new ColorUIResource (214, 214, 214); 298 299 public String getName() 300 { 301 return "Default Kunststoff Theme"; 302 } 303 304 protected ColorUIResource getPrimary1() 305 { 306 return primary1; 307 } 308 309 protected ColorUIResource getPrimary2() 310 { 311 return primary2; 312 } 313 314 protected ColorUIResource getPrimary3() 315 { 316 return primary3; 317 } 318 319 protected ColorUIResource getSecondary1() 320 { 321 return secondary1; 322 } 323 324 protected ColorUIResource getSecondary2() 325 { 326 return secondary2; 327 } 328 329 protected ColorUIResource getSecondary3() 330 { 331 return secondary3; 332 } 333 334 } | Popular Tags |