package com.codeaffine.extras.jdt.internal.junitstatus;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class StatusComposite extends Composite {
  private int defaultTrimHeight;

  public StatusComposite( Composite parent, int style ) {
    super( parent, style );
  }

  @Override
  public Point computeSize( int wHint, int hHint, boolean changed ) {
    Point textSize = measureText( "MMMMMMMMMM" );
    int height = Math.max( getDefaultTrimHeight(), textSize.y + 4 );
    int width = textSize.x + 15;
    return new Point( width, height );
  }

  private int getDefaultTrimHeight() {
    if( defaultTrimHeight == 0 ) {
      Shell shell = new Shell( getDisplay(), SWT.NONE );
      shell.setLayout( new GridLayout() );
      ToolBar toolBar = new ToolBar( shell, SWT.NONE );
      toolBar.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
      ToolItem toolItem = new ToolItem( toolBar, SWT.PUSH );
      toolItem.setImage( JFaceResources.getImageRegistry().get( Dialog.DLG_IMG_MESSAGE_INFO ) );
      shell.layout();
      int toolItemHeight = toolBar.computeSize( SWT.DEFAULT, SWT.DEFAULT ).y;
      GC gc = new GC( shell );
      Point fontSize = gc.textExtent( "Wg" );
      gc.dispose();
      defaultTrimHeight = Math.max( toolItemHeight, fontSize.y );
      shell.dispose();
    }
    return defaultTrimHeight;
  }

  private Point measureText( String string ) {
    GC gc = new GC( this );
    Point result = gc.textExtent( string );
    gc.dispose();
    return result;
  }
}
@@ -248,90 +248,8 @@
 nt;%0A
-import org.eclipse.swt.layout.GridData;%0Aimport org.eclipse.swt.layout.GridLayout;%0A
 impo
@@ -934,16 +934,168 @@
 0 ) %7B%0A  
+    defaultTrimHeight = computeDefaultTrimHeight();%0A    %7D%0A    return defaultTrimHeight;%0A  %7D%0A%0A  private int computeDefaultTrimHeight() %7B%0A    int result;%0A
     Shel
@@ -1149,46 +1149,13 @@
     
-  shell.setLayout( new GridLayout() );
+try %7B
 %0A   
@@ -1211,87 +1211,8 @@
  );%0A
-      toolBar.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );%0A
     
@@ -1369,30 +1369,8 @@
  );%0A
-      shell.layout();%0A
     
@@ -1453,164 +1453,83 @@
     
-GC gc = new GC( shell );%0A      Point fontSize = gc.textExtent( %22Wg%22 );%0A      gc.dispose();%0A      defaultTrimHeight = Math.max( toolItemHeight, fontSize.y );
+result = Math.max( toolItemHeight, measureText( %22Wg%22 ).y );%0A    %7D finally %7B
 %0A   
@@ -1565,32 +1565,21 @@
  return 
-defaultTrimHeigh
+resul
 t;%0A  %7D%0A%0A

Simplify default-trim-height computation