KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > application > swt > ResourceManager


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ResourceManager.java,v 1.2 2007/01/07 06:14:22 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.core.application.swt;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Cursor;
26 import org.eclipse.swt.graphics.Font;
27 import org.eclipse.swt.graphics.FontData;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Combo;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.List;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.TabFolder;
37 import org.eclipse.swt.widgets.Table;
38 import org.eclipse.swt.widgets.Text;
39 import org.opensubsystems.core.util.GlobalConstants;
40
41 /**
42  * Class, which manages SWT resources so they can be properly released.
43  *
44  * @version $Id: ResourceManager.java,v 1.2 2007/01/07 06:14:22 bastafidli Exp $
45  * @author Miro Halas
46  * @code.reviewer Miro Halas
47  * @code.reviewed Initial revision
48  */

49 public class ResourceManager
50 {
51    // Constants ////////////////////////////////////////////////////////////////
52

53    /**
54     * Lock used in synchronized sections.
55     */

56    private static final String JavaDoc IMPL_LOCK = "IMPL_LOCK";
57    
58    /**
59     * Height of font used for middle size buttons.
60     */

61    public static final int MIDDLE_BUTTON_FONT_HEIGHT = 14;
62    
63    /**
64     * Height of font used for big size buttons.
65     */

66    public static final int BIG_BUTTON_FONT_HEIGHT = 22;
67    
68    /**
69     * Height of font used for middle size texts.
70     */

71    public static final int MIDDLE_TEXT_FONT_HEIGHT = 14;
72
73    /**
74     * Height of font used for big size texts.
75     */

76    public static final int BIG_TEXT_FONT_HEIGHT = 22;
77
78    /**
79     * Height of font used for big size tables.
80     */

81    public static final int BIG_TABLE_FONT_HEIGHT = 20;
82
83    /**
84     * Height of font used for big size combo boxes.
85     */

86    public static final int BIG_COMBO_FONT_HEIGHT = 20;
87    
88    /**
89     * Height of font used for big size list boxes.
90     */

91    public static final int BIG_LIST_FONT_HEIGHT = 20;
92    
93    /**
94     * Height of font used for big size list boxes.
95     */

96    public static final int BIG_TAB_FOLDER_FONT_HEIGHT = 20;
97    
98    // Cached values ////////////////////////////////////////////////////////////
99

100    /**
101     * Reference to the instance actually in use.
102     */

103    private static ResourceManager s_defaultInstance;
104    
105    // Attributes ///////////////////////////////////////////////////////////////
106

107    /**
108     * Font used for middle size buttons.
109     */

110    protected Font m_middleButtonFont = null;
111
112    /**
113     * Font used for big size buttons.
114     */

115    protected Font m_bigButtonFont = null;
116    
117    /**
118     * Font used for big size texts.
119     */

120    protected Font m_bigTextFont = null;
121    
122    /**
123     * Font used for big size tables.
124     */

125    protected Font m_bigTableFont = null;
126
127    /**
128     * Font used for big size combos.
129     */

130    protected Font m_bigComboFont = null;
131    
132    /**
133     * Font used for big size lists.
134     */

135    protected Font m_bigListFont = null;
136    
137    /**
138     * Font used for middle size labels.
139     */

140    protected Font m_middleLabelFont = null;
141
142    /**
143     * Font used for big size labels.
144     */

145    protected Font m_bigLabelFont = null;
146
147    /**
148     * Font used for big size tabs in tab folder.
149     */

150    protected Font m_bigTabFolderFont = null;
151
152    /**
153     * Cursor displayed when user waits.
154     */

155    protected Cursor m_waitCursor = null;
156
157    /**
158     * Cursor displayed by default.
159     */

160    protected Cursor m_defaultCursor = null;
161    
162    /**
163     * Image to user for "hidden" cursor.
164     */

165    protected Image m_blankImage = null;
166
167    // Factory methods //////////////////////////////////////////////////////////
168

169    /**
170     * Get the default instance.
171     *
172     * @return ResourceManager
173     */

174    public static ResourceManager getInstance(
175    )
176    {
177       if (s_defaultInstance == null)
178       {
179          // Only if the default instance wasn't set by other means create a new one
180
// Synchronize just for the creation
181
synchronized (IMPL_LOCK)
182          {
183             if (s_defaultInstance == null)
184             {
185                setInstance(new ResourceManager());
186             }
187          }
188       }
189       
190       return s_defaultInstance;
191    }
192    
193    /**
194     * Set default instance. This instance will be returned by getInstance
195     * method until it is changed.
196     *
197     * @param defaultInstance - new default controller instance
198     * @see #getInstance
199     */

200    public static void setInstance(
201       ResourceManager defaultInstance
202    )
203    {
204       if (GlobalConstants.ERROR_CHECKING)
205       {
206          
207          assert defaultInstance != null : "Default instance cannot be null";
208       }
209       
210       synchronized (IMPL_LOCK)
211       {
212          s_defaultInstance = defaultInstance;
213       }
214    }
215    
216    /**
217     * Release all allocated resources.
218     */

219    public void releaseResources(
220    )
221    {
222       if ((m_middleButtonFont != null) && (!m_middleButtonFont.isDisposed()))
223       {
224          m_middleButtonFont.dispose();
225          m_middleButtonFont = null;
226       }
227       if ((m_bigButtonFont != null) && (!m_bigButtonFont.isDisposed()))
228       {
229          m_bigButtonFont.dispose();
230          m_bigButtonFont = null;
231       }
232       if ((m_bigTextFont != null) && (!m_bigTextFont.isDisposed()))
233       {
234          m_bigTextFont.dispose();
235          m_bigTextFont = null;
236       }
237       if ((m_bigTableFont != null) && (!m_bigTableFont.isDisposed()))
238       {
239          m_bigTableFont.dispose();
240          m_bigTableFont = null;
241       }
242       if ((m_bigComboFont != null) && (!m_bigComboFont.isDisposed()))
243       {
244          m_bigComboFont.dispose();
245          m_bigComboFont = null;
246       }
247       if ((m_bigListFont != null) && (!m_bigListFont.isDisposed()))
248       {
249          m_bigListFont.dispose();
250          m_bigListFont = null;
251       }
252       if ((m_middleLabelFont != null) && (!m_middleLabelFont.isDisposed()))
253       {
254          m_middleLabelFont.dispose();
255          m_middleLabelFont = null;
256       }
257       if ((m_bigLabelFont != null) && (!m_bigLabelFont.isDisposed()))
258       {
259          m_bigLabelFont.dispose();
260          m_bigLabelFont = null;
261       }
262       if ((m_bigTabFolderFont != null) && (!m_bigTabFolderFont.isDisposed()))
263       {
264          m_bigTabFolderFont.dispose();
265          m_bigTabFolderFont = null;
266       }
267       if ((m_waitCursor != null) && (!m_waitCursor.isDisposed()))
268       {
269          m_waitCursor.dispose();
270          m_waitCursor = null;
271       }
272       if ((m_defaultCursor != null) && (!m_defaultCursor.isDisposed()))
273       {
274          m_defaultCursor.dispose();
275          m_defaultCursor = null;
276       }
277       if ((m_blankImage != null) && (!m_blankImage.isDisposed()))
278       {
279          m_blankImage.dispose();
280          m_blankImage = null;
281       }
282    }
283    
284    /**
285     * Set middle font for specified button.
286     *
287     * @param middleButton - button to set font for
288     */

289    public void setMiddleButtonFont(
290       Button middleButton
291    )
292    {
293       if (m_middleButtonFont == null)
294       {
295          Font initialFont = middleButton.getFont();
296          FontData[] fontData = initialFont.getFontData();
297          for (int i = 0; i < fontData.length; i++)
298          {
299             fontData[i].setHeight(MIDDLE_BUTTON_FONT_HEIGHT);
300          }
301          m_middleButtonFont = new Font(middleButton.getDisplay(), fontData);
302       }
303       middleButton.setFont(m_middleButtonFont);
304    }
305
306    /**
307     * Set big font for specified tab folder
308     *
309     * @param bigTabFolder - tab folder to set font for
310     */

311    public void setBigTabFolderFont(
312       TabFolder bigTabFolder
313    )
314    {
315       if (m_bigTabFolderFont == null)
316       {
317          Font initialFont = bigTabFolder.getFont();
318          FontData[] fontData = initialFont.getFontData();
319          for (int i = 0; i < fontData.length; i++)
320          {
321             fontData[i].setHeight(BIG_TAB_FOLDER_FONT_HEIGHT);
322          }
323          m_bigTabFolderFont = new Font(bigTabFolder.getDisplay(), fontData);
324       }
325       bigTabFolder.setFont(m_bigTabFolderFont);
326    }
327    
328    /**
329     * Set big font for specified button.
330     *
331     * @param bigButton - button to set font for
332     */

333    public void setBigButtonFont(
334       Button bigButton
335    )
336    {
337       if (m_bigButtonFont == null)
338       {
339          Font initialFont = bigButton.getFont();
340          FontData[] fontData = initialFont.getFontData();
341          for (int i = 0; i < fontData.length; i++)
342          {
343             fontData[i].setHeight(BIG_BUTTON_FONT_HEIGHT);
344          }
345          m_bigButtonFont = new Font(bigButton.getDisplay(), fontData);
346       }
347       bigButton.setFont(m_bigButtonFont);
348    }
349
350    /**
351     * Set big font for specified text.
352     *
353     * @param bigText - text to set font for
354     */

355    public void setBigTextFont(
356       Text bigText
357    )
358    {
359       if (m_bigTextFont == null)
360       {
361          Font initialFont = bigText.getFont();
362          FontData[] fontData = initialFont.getFontData();
363          for (int i = 0; i < fontData.length; i++)
364          {
365             fontData[i].setHeight(BIG_TEXT_FONT_HEIGHT);
366          }
367          m_bigTextFont = new Font(bigText.getDisplay(), fontData);
368       }
369       bigText.setFont(m_bigTextFont);
370    }
371
372    /**
373     * Set table like font for specified text.
374     *
375     * @param bigText - text to set font for
376     */

377    public void setTableTextFont(
378       Text bigText
379    )
380    {
381       if (m_bigTableFont == null)
382       {
383          Font initialFont = bigText.getFont();
384          FontData[] fontData = initialFont.getFontData();
385          for (int i = 0; i < fontData.length; i++)
386          {
387             fontData[i].setHeight(BIG_TABLE_FONT_HEIGHT);
388          }
389          m_bigTableFont = new Font(bigText.getDisplay(), fontData);
390       }
391       bigText.setFont(m_bigTableFont);
392    }
393
394    /**
395     * Set big font for specified combo.
396     *
397     * @param bigCombo - combo to set font for
398     */

399    public void setBigComboTextFont(
400      Combo bigCombo
401    )
402    {
403       if (m_bigComboFont == null)
404       {
405          Font initialFont = bigCombo.getFont();
406          FontData[] fontData = initialFont.getFontData();
407          for (int i = 0; i < fontData.length; i++)
408          {
409             fontData[i].setHeight(BIG_COMBO_FONT_HEIGHT);
410          }
411          m_bigComboFont = new Font(bigCombo.getDisplay(), fontData);
412       }
413       bigCombo.setFont(m_bigComboFont);
414    }
415    
416    /**
417     * Set big font for specified list.
418     *
419     * @param bigList - combo to set font for
420     */

421    public void setBigListTextFont(
422       List bigList
423    )
424    {
425       if (m_bigListFont == null)
426       {
427          Font initialFont = bigList.getFont();
428          FontData[] fontData = initialFont.getFontData();
429          for (int i = 0; i < fontData.length; i++)
430          {
431             fontData[i].setHeight(BIG_LIST_FONT_HEIGHT);
432          }
433          m_bigListFont = new Font(bigList.getDisplay(), fontData);
434       }
435       bigList.setFont(m_bigListFont);
436    }
437
438    /**
439     * Set big font for specified table.
440     *
441     * @param bigTable - table to set font for
442     */

443    public void setBigTableFont(
444       Table bigTable
445    )
446    {
447       if (m_bigTableFont == null)
448       {
449          Font initialFont = bigTable.getFont();
450          FontData[] fontData = initialFont.getFontData();
451          for (int i = 0; i < fontData.length; i++)
452          {
453             fontData[i].setHeight(BIG_TABLE_FONT_HEIGHT);
454          }
455          m_bigTableFont = new Font(bigTable.getDisplay(), fontData);
456       }
457       bigTable.setFont(m_bigTableFont);
458    }
459
460    /**
461     * Set middle font for specified label.
462     *
463     * @param middleLabel - label to set font for
464     */

465    public void setMiddleLabelFont(
466       Label middleLabel
467    )
468    {
469       if (m_middleLabelFont == null)
470       {
471          Font initialFont = middleLabel.getFont();
472          FontData[] fontData = initialFont.getFontData();
473          for (int i = 0; i < fontData.length; i++)
474          {
475             fontData[i].setHeight(MIDDLE_TEXT_FONT_HEIGHT);
476          }
477          m_middleLabelFont = new Font(middleLabel.getDisplay(), fontData);
478       }
479       middleLabel.setFont(m_middleLabelFont);
480    }
481    
482    /**
483     * Set big font for specified label.
484     *
485     * @param bigLabel - label to set font for
486     */

487    public void setBigLabelFont(
488       Label bigLabel
489    )
490    {
491       if (m_bigLabelFont == null)
492       {
493          Font initialFont = bigLabel.getFont();
494          FontData[] fontData = initialFont.getFontData();
495          for (int i = 0; i < fontData.length; i++)
496          {
497             fontData[i].setHeight(BIG_TEXT_FONT_HEIGHT);
498          }
499          m_bigLabelFont = new Font(bigLabel.getDisplay(), fontData);
500       }
501       bigLabel.setFont(m_bigLabelFont);
502    }
503    
504    /**
505     * Set the wait cursor for given control
506     *
507     * @param ctlControl - control to set the cursor for
508     */

509    public void setWaitCursor(
510       Control ctlControl
511    )
512    {
513       if (m_waitCursor == null)
514       {
515          m_waitCursor = new Cursor(ctlControl.getDisplay(), SWT.CURSOR_WAIT);
516       }
517       ctlControl.setCursor(m_waitCursor);
518    }
519
520    /**
521     * Set the default cursor for given control
522     *
523     * @param ctlControl - control to set the cursor for
524     */

525    public void setDefaultCursor(
526       Control ctlControl
527    )
528    {
529       if (m_defaultCursor == null)
530       {
531          // This doesn't work on Linux so we have to do our own cursor
532
m_defaultCursor = new Cursor(ctlControl.getDisplay(), SWT.CURSOR_ARROW);
533       }
534       ctlControl.setCursor(m_defaultCursor);
535    }
536
537    /**
538     * Set the default cursor for given shell
539     *
540     * @param window - shell to set the cursor for
541     */

542    public void setDefaultCursor(
543       Shell window
544    )
545    {
546       if (m_defaultCursor == null)
547       {
548          // This doesn't work on Linux so we have to do our own cursor
549
m_defaultCursor = new Cursor(window.getDisplay(), SWT.CURSOR_ARROW);
550       }
551       window.setCursor(m_defaultCursor);
552    }
553    
554    /**
555     * Set the default cursor to be hidden to make it easier for people on two
556     * touchscreen system to do not find for the cursor. This will make it very
557     * small so it is almost invisible.
558     *
559     * @param defaultDisplay - display which is used
560     */

561    public void setHiddenDefaultCursor(
562       Display defaultDisplay
563    )
564    {
565       if (m_blankImage == null)
566       {
567          if (m_defaultCursor != null)
568          {
569             // TODO: Improve: Review if this dispose is also disposing the
570
// image?
571
m_defaultCursor.dispose();
572          }
573
574          // This doesn't work on Linux so we have to do our own cursor
575
// m_defaultCursor = new Cursor(window.getDisplay(), SWT.CURSOR_NO);
576
m_blankImage = new Image(defaultDisplay, 1, 1);
577          m_defaultCursor = new Cursor(defaultDisplay, m_blankImage.getImageData(),
578                                       0, 0);
579       }
580    }
581 }
582
Popular Tags