directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stefan Seelmann <m...@stefan-seelmann.de>
Subject Re: [Studio] org.eclipse.swt.graphics.* disposal : a methodology
Date Wed, 27 May 2015 21:18:34 GMT
Thanks Emmanuel for fixing the issue and the advices. Now that the code
is 'clean' I hope we don't just allocate any new SWT resource.

In the UI integration tests we also have a test called SwtResourcesTest
that was created to check the number of created SWT resources is not too
high (less than 1000). But I think it is hard to also test that they are
being dieposed, we'd need to stop the plugins.

Kind Regards,
Stefan


On 05/27/2015 05:56 PM, Emmanuel L├ęcharny wrote:
> Hi guys,
> 
> I finally closed DIRSTUDIO_1038. We now dispose all the
> org.eclipse.swt.graphics.* elements.
> 
> For the upcoming developemnts, I'd like to give a bit of guidelines :
> 
> Font
> ----
> 
> Font must be disposed. It's then easier to use a FontRegistry to avoid
> creating a font everytime it's needed. One way to do that is :
> 
>         FontData[] fontData =
> Display.getDefault().getSystemFont().getFontData();
>         FontData fontDataBoldItalic = new FontData(
> fontData[0].getName(), fontData[0].getHeight(), SWT.BOLD | SWT.ITALIC );
> 
>         FontRegistry fontRegistry = new FontRegistry(
> getWorkbench().getDisplay() );
> 
>         if ( !fontRegistry.hasValueFor( fontData[0].toString() ) )
>         {
>             fontRegistry.put( fontData[0].toString(), fontData );
>         }
> 
>         Font newFont = fontRegistry.get( fontData[0].toString() );
> 
> GC
> --
> 
> Not frequently used. One exemple :
> 
>         GC gc = new GC( control );
>        
>         try
>         {
>             gc.setFont( JFaceResources.getDialogFont() );
>             FontMetrics fontMetrics = gc.getFontMetrics();
>    
>             int width = Dialog.convertHorizontalDLUsToPixels(
> fontMetrics, IDialogConstants.BUTTON_WIDTH );
> 
>             return width;
>         }
>         finally
>         {
>             gc.dispose();
>         }
> 
> The disposal is done in a finally clause, to be sure we dispose the GC,
> no matter what
> 
> Image
> -----
> Even simpler : when you create an Image, be sure to call the dispose()
> method when done. Otherwise, each plugin activator (ie the class which
> extends AbstractUIPlugin) has a method getImage( String ) :
> 
>     /**
>      * Use this method to get SWT images. A ImageRegistry is used
>      * to manage the the path->Image mapping.
>      * <p>
>      * Note: Don't dispose the returned SWT Image. It is disposed
>      * automatically when the plugin is stopped.
>      *
>      * @param path the path
>      * @return The SWT Image or null
>      */
>     public Image getImage( String path )
>     {
>         Image image = getImageRegistry().get( path );
>        
>         if ( image == null )
>         {
>             ImageDescriptor id = getImageDescriptor( path );
>            
>             if ( id != null )
>             {
>                 image = id.createImage();
>                 getImageRegistry().put( path, image );
>             }
>         }
>        
>         return image;
>     }
> 
> Color
> -----
> 
> Colors are quite frequently used. Hopefully, we mostly used quite common
> colors, like Red, Black, or various greys. I have moved all the Color
> creations and disposals into the CommonUiConstants/CommonUiPlugins classes :
> 
>     // To see the world in black and white
>     public static final RGB BLACK = new RGB( 0, 0, 0 );
>     public static Color BLACK_COLOR;
>     public static final RGB WHITE = new RGB( 255, 255, 255 );
>     public static Color WHITE_COLOR;
>    
>     // 7 shade of greys... From dark to light, with combinaisons
>     public static final RGB BD_GREY = new RGB( 31, 31, 31 );    // Black
> Dark grey
>     public static Color BD_GREY_COLOR;
>     public static final RGB D_GREY = new RGB( 63, 63, 63 );     // Dark grey
>     public static Color D_GREY_COLOR;
>     public static final RGB MD_GREY = new RGB( 95, 95, 95 );    //
> Medium Dark grey
>     public static Color MD_GREY_COLOR;
>     public static final RGB M_GREY = new RGB( 127, 127, 127 );  // Grey
>     public static Color M_GREY_COLOR;
>     public static final RGB ML_GREY = new RGB( 159, 159, 159 ); //
> Medium Light grey
>     public static Color ML_GREY_COLOR;
>     public static final RGB L_GREY = new RGB( 191, 191, 191 );  // Light
> grey
>     public static Color L_GREY_COLOR;
>     public static final RGB WL_GREY = new RGB( 224, 224, 224 ); // White
> Light grey
>     public static Color WL_GREY_COLOR;
>    
>     // Red
>     public static final RGB M_RED = new RGB( 127, 0, 0 );       //
> Medium red
>     public static Color M_RED_COLOR;
>     public static final RGB ML_RED = new RGB( 159, 0, 0 );      //
> Medium light red
>     public static Color ML_RED_COLOR;
>     public static final RGB RED = new RGB( 255, 0, 0 );         // Full red
>     public static Color RED_COLOR;
>    
>     // Green
>     public static final RGB M_GREEN = new RGB( 0, 127, 0 );     //
> Medium green
>     public static Color M_GREEN_COLOR;
>     public static final RGB ML_GREEN = new RGB( 0, 159, 0 );    //
> Medium Light green
>     public static Color ML_GREEN_COLOR;
>     public static final RGB GREEN = new RGB( 0, 255, 0 );       // Full
> green
>     public static Color GREEN_COLOR;
>    
>     // Blue
>     public static final RGB M_BLUE = new RGB( 0, 0, 127 );      //
> Medium blue
>     public static Color M_BLUE_COLOR;
>     public static final RGB L_BLUE = new RGB( 0, 0, 191 );      // Light
> blue
>     public static Color L_BLUE_COLOR;
>     public static final RGB BLUE = new RGB( 0, 0, 255 );        // Full blue
>     public static Color BLUE_COLOR;
>    
>     // Purple
>     public static final RGB M_PURPLE = new RGB( 127, 0, 127 );  //
> Middle purple
>     public static Color M_PURPLE_COLOR;
>     public static final RGB PURPLE = new RGB( 255, 0, 255 );    // Full
> purple
>     public static Color PURPLE_COLOR;
> 
>     // Other colors
>     public static final RGB R0_G127_B255 = new RGB( 0, 127, 255 );
>     public static Color R0_G127_B255_COLOR;
>     public static final RGB R95_G63_B159 = new RGB( 95, 63, 159 );
>     public static Color R95_G63_B159_COLOR;
>     public static final RGB R63_G127_B63 = new RGB( 63, 127, 63 );
>     public static Color R63_G127_B63_COLOR;
> 
> Each color is declared as a RGB instance, and a Color instance is
> created in the CommonUIPlugins class start's method :
> 
>     public void start( BundleContext context ) throws Exception
>     {
>         super.start( context );
>        
>         // Create the colors we use
>         CommonUIConstants.BLACK_COLOR = new Color( null,
> CommonUIConstants.BLACK );
>         CommonUIConstants.WHITE_COLOR = new Color( null,
> CommonUIConstants.WHITE );
>         ...
> 
> and the dispose() method is called in the stop method :
> 
>     public void stop( BundleContext context ) throws Exception
>     {
>         plugin = null;
>        
>         // Dispose the colors
>         CommonUIConstants.BLACK_COLOR.dispose();
>         CommonUIConstants.WHITE_COLOR.dispose();
>        
> 
> 
> Would you want to create a new color, it's enough to add the RGB
> instance and declare the Color instance in the CommonUiConstants class,
> and to instanciate it and dispose it in the CommonUIPlugins class.
> 
> 
> Comments ?
> 
> 


Mime
View raw message