1 29 38 39 package org.planetamessenger.mos.engine; 40 41 import org.planetamessenger.io.*; 42 43 import java.sql.Connection ; 44 import java.sql.PreparedStatement ; 45 import java.sql.ResultSet ; 46 import java.sql.SQLException ; 47 import java.util.jar.*; 48 import java.util.*; 49 import java.io.*; 50 import java.net.*; 51 52 53 public class JLookAndFeelManager { 54 55 static private final String META_INF_PATH = "META-INF/services/"; 56 static private final String LOOKANDFEEL_FILE = "javax.swing.LookAndFeel"; 57 58 59 private String strLastError; 60 private URLClassLoader lookAndFeelLoader = null; 61 private org.planetamessenger.db.JDatabase database = null; 62 private JLookAndFeelInfo currentPlafInfo = null; 63 64 65 66 70 public JLookAndFeelManager() { 71 72 init(); 73 loadDefaultLookAndFeel(); 74 } 75 76 79 private void init() { 80 81 database = JSharedObjects.getDatabase(); 82 } 83 84 88 public void destroy() { 89 90 lookAndFeelLoader = null; 91 database = null; 92 } 93 94 98 public void loadDefaultLookAndFeel() { 99 100 int nProfileId = JSharedObjects.getProfileManager().getDefaultProfile(); 101 JLookAndFeelInfo plafInfo; 102 String strClassName; 103 String strFileName; 104 ResultSet resultSet = null; 105 Connection conn = null; 106 PreparedStatement st = null; 107 108 try{ 109 conn = database.getConnection(); 110 st = conn.prepareStatement( "SELECT class_name, file_name FROM profile WHERE profile_id = ?" ); 111 st.setLong( 1, nProfileId ); 112 113 resultSet = st.executeQuery(); 114 115 if( resultSet == null ) { 116 strFileName = ""; 117 strClassName = javax.swing.UIManager.getSystemLookAndFeelClassName(); 118 } 119 120 resultSet.next(); 121 strClassName = resultSet.getString( "class_name" ); 122 strFileName = resultSet.getString( "file_name" ); 123 124 if( "".compareTo( ( strClassName == null ? "" : strClassName ) ) == 0 ) 125 strClassName = javax.swing.UIManager.getSystemLookAndFeelClassName(); 126 127 if( strFileName == null ) 128 strFileName = ""; 129 130 resultSet.close(); 131 132 } catch( java.sql.SQLException e ) { 133 System.err.println( "JLookAndFeelManager.loadDefaultLookAndFeel() - SQLException " + e ); 134 strFileName = ""; 135 strClassName = javax.swing.UIManager.getSystemLookAndFeelClassName(); 136 }finally{ 137 if( resultSet != null ) 138 try{ 139 resultSet.close(); 140 } catch( SQLException e ) { 141 System.err.println( "JLookAndFeelManager.loadDefaultLookAndFeel() - " + e ); 142 } 143 if( st != null ) 144 try{ 145 st.close(); 146 } catch( SQLException e ) { 147 System.err.println( "JLookAndFeelManager.loadDefaultLookAndFeel() - " + e ); 148 } 149 } 150 151 plafInfo = new JLookAndFeelInfo(); 152 153 plafInfo.setFileName( strFileName ); 154 plafInfo.setClassName( strClassName ); 155 156 if( !loadLookAndFeel( plafInfo ) ) { 158 plafInfo.setFileName( "" ); 159 plafInfo.setClassName( javax.swing.UIManager.getSystemLookAndFeelClassName() ); 160 loadLookAndFeel( plafInfo ); 161 } 162 } 163 164 171 public ArrayList<JLookAndFeelInfo> getLookAndFeelList() { 172 173 JarFile plafJarFile; 174 JarEntry jarEntry; 175 JLookAndFeelInfo item = null; 176 JFilenameFilter filter = new JFilenameFilter(); 177 java.io.File plafDir = new java.io.File ( JSharedObjects.getConfiguration().getLookAndFeelPath() ); 178 ArrayList<JLookAndFeelInfo> plafVector = new ArrayList<JLookAndFeelInfo>(); 179 java.lang.String [] strDirList; 180 javax.swing.UIManager.LookAndFeelInfo lookAndFeelInfo[]; 181 182 183 184 filter.add( "jar" ); 185 filter.add( "zip" ); 186 187 strDirList = plafDir.list( filter ); 188 189 lookAndFeelInfo = javax.swing.UIManager.getInstalledLookAndFeels(); 191 192 for( int nCount = 0; nCount < lookAndFeelInfo.length; nCount++ ) { 193 item = new JLookAndFeelInfo(); 194 item.setName( lookAndFeelInfo[nCount].getName() ); 195 item.setClassName( lookAndFeelInfo[nCount].getClassName() ); 196 plafVector.add( item ); 197 } 198 199 for( int nCount = 0; nCount < strDirList.length; nCount++ ) { 201 202 try { 203 plafJarFile = new JarFile( JSharedObjects.getConfiguration().getLookAndFeelPath() + strDirList[nCount] ); 204 jarEntry = plafJarFile.getJarEntry( META_INF_PATH + LOOKANDFEEL_FILE ); 205 206 if( jarEntry != null ) { 207 BufferedReader inStream = new BufferedReader( ( new InputStreamReader( plafJarFile.getInputStream( jarEntry ) ) ) ); 208 String strLine; 209 210 try { 211 while( true ) { 212 strLine = inStream.readLine(); 213 214 if( strLine == null ) 215 strLine = ""; 216 217 if( strLine.compareTo( "" ) != 0 ) { 218 219 if( strLine.indexOf( '#' ) >= 0 ) { 220 item = new JLookAndFeelInfo(); 221 item.setName( strLine.substring( 1 ) ); 222 item.setFileName( strDirList[nCount] ); 223 } 224 else { 225 item.setClassName( strLine ); 226 plafVector.add( item ); 227 } 228 } 229 else 230 try { 231 plafJarFile.close(); 232 inStream.close(); 233 break; 234 } catch( IOException ioex ) { 235 System.err.println( "JLookAndFeelManager.getLookAndFeelList()" + ioex ); 236 break; 237 } 238 } 239 } catch( IOException ioe ) { 240 try { 241 System.err.println( "JLookAndFeelManager.getLookAndFeelList()" + ioe ); 242 plafJarFile.close(); 243 inStream.close(); 244 } catch( IOException ioex ) { 245 System.err.println( "JLookAndFeelManager.getLookAndFeelList()" + ioex ); 246 } 247 } 248 } 249 else 250 continue; 251 252 } catch( java.io.IOException e ) { 253 System.err.println( "JLookAndFeelManager.getLookAndFeelList() - " + e ); 254 continue; 255 } 256 } 257 258 return plafVector; 259 } 260 261 268 public JLookAndFeelInfo getLookAndFeel( String strFileName, String strClassName ) { 269 270 ArrayList<JLookAndFeelInfo> plafVector = getLookAndFeelList(); 271 JLookAndFeelInfo plafInfo; 272 273 274 for( int nCount = 0; nCount < plafVector.size(); nCount++ ) { 275 276 plafInfo = ( JLookAndFeelInfo ) plafVector.get( nCount ); 277 278 if( ( plafInfo.getFileName().compareTo( ( strFileName == null ? "" : ( strFileName.compareTo( "" ) == 0 ? plafInfo.getFileName() : strFileName ) ) ) == 0 ) && 279 ( plafInfo.getClassName().compareTo( ( strClassName == null ? "" : ( strClassName.compareTo( "" ) == 0 ? plafInfo.getClassName() : strClassName ) ) ) == 0 ) ) 280 return plafInfo; 281 } 282 283 return null; 284 } 285 286 290 public String getLastError() { 291 292 return strLastError; 293 } 294 295 300 public boolean loadLookAndFeel( JLookAndFeelInfo plafInfo ) { 301 302 javax.swing.LookAndFeel plaf = null; 303 304 305 if( currentPlafInfo != null ) 307 if( currentPlafInfo.equals( plafInfo ) ) 308 return true; 309 310 try { 311 if( plafInfo.getFileName().compareTo( "" ) != 0 ) { 312 String strURL = "jar:file:" + JSharedObjects.getConfiguration().getLookAndFeelPath() + plafInfo.getFileName() + "!/"; 313 URL[] urlPlaf = new URL[1]; 314 315 urlPlaf[0] = new URL( strURL ); 316 lookAndFeelLoader = URLClassLoader.newInstance( urlPlaf, ( lookAndFeelLoader == null ? ClassLoader.getSystemClassLoader() : lookAndFeelLoader ) ) ; 317 javax.swing.UIManager.put( "ClassLoader", lookAndFeelLoader ); 318 } 319 320 plaf = ( javax.swing.LookAndFeel ) java.beans.Beans.instantiate( ( lookAndFeelLoader == null ? ClassLoader.getSystemClassLoader() : lookAndFeelLoader ), plafInfo.getClassName() ); 321 322 if( plaf.isSupportedLookAndFeel() ) { 323 javax.swing.LookAndFeel oldPlaf = javax.swing.UIManager.getLookAndFeel(); 324 325 try { 326 javax.swing.UIManager.setLookAndFeel( plaf ) ; 327 JSharedObjects.getMainWindow().windowRefresh( org.planetamessenger.mos.forms.JMOSWindow.LOOK_AND_FEEL_REFRESH ); 328 currentPlafInfo = plafInfo; 329 } catch( Throwable te ) { 330 strLastError = te.getMessage(); 331 System.err.println( "JLookAndFeelManager.loadLookAndFeel() - " + strLastError ); 332 System.err.println( "JLookAndFeelManager.loadLookAndFeel() - Restoring old Look and Feel" ); 333 334 try { 335 javax.swing.UIManager.setLookAndFeel( oldPlaf ) ; 336 } catch( Throwable tte ) { 337 System.err.println( "JLookAndFeelManager.loadLookAndFeel() - Failed to restore old Look and Feel" ); 338 return false; 339 } 340 } 341 } 342 else { 343 strLastError = JSharedObjects.getLanguageManager().getStringEx( "LAF_NOT_SUPPORTED" ); 344 return false; 345 } 346 347 } catch( java.io.IOException ioe ) { 348 System.err.println( "JLookAndFeelManager.loadLookAndFeel() - " + ioe ); 349 strLastError = "Internal error - " + ioe; 350 return false; 351 } catch( java.lang.ClassNotFoundException ce ) { 352 System.err.println( "JLookAndFeelManager.loadLookAndFeel() - " + ce ); 353 strLastError = "Internal error - " + ce; 354 return false; 355 } catch( java.lang.Exception e ) { 356 System.err.println( "JLookAndFeelManager.loadLookAndFeel() - " + e ); 357 strLastError = "Internal error - " + e; 358 return false; 359 } 360 361 return true; 362 } 363 364 369 public boolean isSupportedLookAndFeel( JLookAndFeelInfo plafInfo ) { 370 371 URLClassLoader classLoader = null; 372 javax.swing.LookAndFeel plaf; 373 boolean bRet; 374 375 try { 376 if( plafInfo.getFileName().compareTo( "" ) != 0 ) { 377 378 String strURL = "jar:file:" + JSharedObjects.getConfiguration().getLookAndFeelPath() + plafInfo.getFileName() + "!/"; 379 URL[] urlPlaf = new URL[1]; 380 381 urlPlaf[0] = new URL( strURL ); 382 classLoader = new URLClassLoader( urlPlaf ); 383 } 384 385 plaf = ( javax.swing.LookAndFeel ) java.beans.Beans.instantiate( classLoader, plafInfo.getClassName() ); 386 bRet = plaf.isSupportedLookAndFeel(); 387 388 if( !bRet ) 389 strLastError = JSharedObjects.getLanguageManager().getStringEx( "LAF_NOT_SUPPORTED" ); 390 391 return bRet; 392 393 } catch( java.lang.ClassNotFoundException ce ) { 394 System.err.println( "JLookAndFeelManager.isSupportedLookAndFeel() - " + ce ); 395 strLastError = "Internal error - " + ce; 396 return false; 397 } catch( IOException ioe ) { 398 System.err.println( "JLookAndFeelManager.isSupportedLookAndFeel() - " + ioe ); 399 strLastError = "Internal error - " + ioe; 400 return false; 401 } 402 } 403 404 410 public boolean updateProfile( long nProfileId, JLookAndFeelInfo plafInfo ) { 411 412 Connection conn = null; 413 PreparedStatement st = null; 414 415 416 if( nProfileId == -1 ) 417 return false; 418 419 try { 420 conn = JSharedObjects.getDatabase().getConnection(); 421 st = conn.prepareStatement( "UPDATE profile SET class_name=?, file_name=? WHERE profile_id=?" ); 422 st.setString( 1, plafInfo.getClassName() ); 423 st.setString( 2, plafInfo.getFileName() ); 424 st.setLong( 3, nProfileId ); 425 426 if( st.executeUpdate() <= 0 ) 427 return false; 428 429 } catch( SQLException e ) { 430 System.err.println( "JLookAndFeelManager.updateProfile() - " + e ); 431 return false; 432 } finally { 433 if( st != null ) 434 try { 435 st.close(); 436 } catch( SQLException e ) { 437 System.err.println( "JLookAndFeelManager.updateProfile() - " + e ); 438 } 439 } 440 441 return true; 442 } 443 } 444 445 | Popular Tags |