KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > mos > engine > JLookAndFeelManager


1 /*
2     =========================================================================
3     Package engine - Implements the engine package.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JLookAndFeelManager.java,v 1.16 2007/01/28 17:39:20 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.16 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.mos.engine;
40
41 import org.planetamessenger.io.*;
42
43 import java.sql.Connection JavaDoc;
44 import java.sql.PreparedStatement JavaDoc;
45 import java.sql.ResultSet JavaDoc;
46 import java.sql.SQLException JavaDoc;
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 JavaDoc META_INF_PATH = "META-INF/services/";
56   static private final String JavaDoc LOOKANDFEEL_FILE = "javax.swing.LookAndFeel";
57
58
59   private String JavaDoc strLastError;
60   private URLClassLoader lookAndFeelLoader = null;
61   private org.planetamessenger.db.JDatabase database = null;
62   private JLookAndFeelInfo currentPlafInfo = null;
63   
64   
65   
66   /**
67    * Constructor. Initialiaze all class data and
68    * load the default look and feel.
69    */

70   public JLookAndFeelManager() {
71
72     init();
73     loadDefaultLookAndFeel();
74   }
75   
76   /**
77    * Initializes the Look and feel manager.
78    */

79   private void init() {
80
81     database = JSharedObjects.getDatabase();
82   }
83   
84   /**
85    * Destroy the object.
86    * Releases all allocated resources.
87    */

88   public void destroy() {
89     
90     lookAndFeelLoader = null;
91     database = null;
92   }
93
94   /**
95    * Load the default look and feel
96    * assigned to system.
97    */

98   public void loadDefaultLookAndFeel() {
99
100     int nProfileId = JSharedObjects.getProfileManager().getDefaultProfile();
101     JLookAndFeelInfo plafInfo;
102     String JavaDoc strClassName;
103     String JavaDoc strFileName;
104     ResultSet JavaDoc resultSet = null;
105     Connection JavaDoc conn = null;
106     PreparedStatement JavaDoc 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 JavaDoc 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 JavaDoc e ) {
141           System.err.println( "JLookAndFeelManager.loadDefaultLookAndFeel() - " + e );
142         }
143       if( st != null )
144         try{
145           st.close();
146         } catch( SQLException JavaDoc 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     // Try the system default look and feel
157
if( !loadLookAndFeel( plafInfo ) ) {
158       plafInfo.setFileName( "" );
159       plafInfo.setClassName( javax.swing.UIManager.getSystemLookAndFeelClassName() );
160       loadLookAndFeel( plafInfo );
161     }
162   }
163   
164   /**
165    * Return the look and feel list.
166    * This list is acquired from plaf
167    * directory where PlanetaMessenger
168    * lives in and from JVM availables
169    * look and feel.
170    */

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 JavaDoc plafDir = new java.io.File JavaDoc( JSharedObjects.getConfiguration().getLookAndFeelPath() );
178     ArrayList<JLookAndFeelInfo> plafVector = new ArrayList<JLookAndFeelInfo>();
179     java.lang.String JavaDoc[] 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     // Get all installed look and feels
190
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     // Get all look and feel from PlanetaMessenger's plaf dir
200
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 JavaDoc 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 JavaDoc e ) {
253         System.err.println( "JLookAndFeelManager.getLookAndFeelList() - " + e );
254         continue;
255       }
256     }
257     
258     return plafVector;
259   }
260   
261   /**
262    * Return a look and feel based on
263    * file name and class name passed as
264    * parameter.
265    * @param strFileName File name to search;
266    * @param strClassName The class name to search;
267    */

268   public JLookAndFeelInfo getLookAndFeel( String JavaDoc strFileName, String JavaDoc 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   /**
287    * Return the last error nessage occured for any
288    * operation.
289    */

290   public String JavaDoc getLastError() {
291     
292     return strLastError;
293   }
294   
295   /**
296    * Load and instantiate the Look and feel.
297    * @param plafInfo The Look and feel info object
298    * with all information about Look and Feel to start;
299    */

300   public boolean loadLookAndFeel( JLookAndFeelInfo plafInfo ) {
301
302     javax.swing.LookAndFeel JavaDoc plaf = null;
303
304     
305     // Verify if L&F is already loaded
306
if( currentPlafInfo != null )
307       if( currentPlafInfo.equals( plafInfo ) )
308         return true;
309
310     try {
311       if( plafInfo.getFileName().compareTo( "" ) != 0 ) {
312         String JavaDoc 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 JavaDoc ) java.beans.Beans.instantiate( ( lookAndFeelLoader == null ? ClassLoader.getSystemClassLoader() : lookAndFeelLoader ), plafInfo.getClassName() );
321       
322       if( plaf.isSupportedLookAndFeel() ) {
323         javax.swing.LookAndFeel JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc ioe ) {
348       System.err.println( "JLookAndFeelManager.loadLookAndFeel() - " + ioe );
349       strLastError = "Internal error - " + ioe;
350       return false;
351     } catch( java.lang.ClassNotFoundException JavaDoc ce ) {
352       System.err.println( "JLookAndFeelManager.loadLookAndFeel() - " + ce );
353       strLastError = "Internal error - " + ce;
354       return false;
355     } catch( java.lang.Exception JavaDoc e ) {
356       System.err.println( "JLookAndFeelManager.loadLookAndFeel() - " + e );
357       strLastError = "Internal error - " + e;
358       return false;
359     }
360     
361     return true;
362   }
363   
364   /**
365    * Check if a look and feel is supported.
366    * @param plafInfo The Look and feel info object
367    * with all information about Look and Feel to check;
368    */

369   public boolean isSupportedLookAndFeel( JLookAndFeelInfo plafInfo ) {
370
371     URLClassLoader classLoader = null;
372     javax.swing.LookAndFeel JavaDoc plaf;
373     boolean bRet;
374
375     try {
376       if( plafInfo.getFileName().compareTo( "" ) != 0 ) {
377         
378         String JavaDoc 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 JavaDoc ) 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 JavaDoc 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   /**
405    * Save a Look and feel information for a specified
406    * profile;
407    * @param plafInfo The Look and feel info object
408    * with all information about Look and Feel to save;
409    */

410   public boolean updateProfile( long nProfileId, JLookAndFeelInfo plafInfo ) {
411     
412     Connection JavaDoc conn = null;
413     PreparedStatement JavaDoc 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 JavaDoc 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 JavaDoc e ) {
437           System.err.println( "JLookAndFeelManager.updateProfile() - " + e );
438         }
439     }
440
441     return true;
442   }
443 }
444
445 // JLookAndFeelManager class
446
Popular Tags