directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pamarce...@apache.org
Subject svn commit: r1398743 - /directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/image/ImageDialog.java
Date Tue, 16 Oct 2012 11:38:20 GMT
Author: pamarcelot
Date: Tue Oct 16 11:38:20 2012
New Revision: 1398743

URL: http://svn.apache.org/viewvc?rev=1398743&view=rev
Log:
Fixed for DIRSTUDIO-775 (Display issues in Image Editor)

Modified:
    directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/image/ImageDialog.java

Modified: directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/image/ImageDialog.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/image/ImageDialog.java?rev=1398743&r1=1398742&r2=1398743&view=diff
==============================================================================
--- directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/image/ImageDialog.java
(original)
+++ directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/image/ImageDialog.java
Tue Oct 16 11:38:20 2012
@@ -44,6 +44,7 @@ import org.eclipse.swt.events.ModifyEven
 import org.eclipse.swt.events.ModifyListener;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.GC;
 import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.graphics.ImageData;
 import org.eclipse.swt.graphics.ImageLoader;
@@ -52,6 +53,7 @@ import org.eclipse.swt.layout.GridLayout
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
@@ -68,65 +70,55 @@ import org.eclipse.swt.widgets.Text;
  */
 public class ImageDialog extends Dialog
 {
+    /** The dialog setting key for the currently selected tab item */
+    private static final String SELECTED_TAB_DIALOGSETTINGS_KEY = ImageDialog.class.getName()
+ ".tab"; //$NON-NLS-1$
 
-    private static final int MAX_WIDTH = 250;
-
-    private static final int MAX_HEIGHT = 250;
+    /** The maximum width for the image */
+    private static final int MAX_WIDTH = 400;
+    /** The maximum height for the image */
+    private static final int MAX_HEIGHT = 400;
 
+    /** The current image tab item */
     private static final int CURRENT_TAB = 0;
-
+    /** The new image tab item */
     private static final int NEW_TAB = 1;
 
-    private static final String SELECTED_TAB_DIALOGSETTINGS_KEY = ImageDialog.class.getName()
+ ".tab"; //$NON-NLS-1$
-
-    private TabFolder tabFolder;
+    /** The current image bytes */
+    private byte[] currentImageRawData;
 
-    private TabItem currentTab;
+    /** The required image type */
+    private int requiredImageType;
 
-    private TabItem newTab;
+    /** The new image bytes */
+    private byte[] newImageRawData;
 
-    private byte[] currentImageRawData;
+    /** The new image bytes in the required image format */
+    private byte[] newImageRawDataInRequiredFormat;
 
-    private Image currentImage;
+    // UI widgets
+    private TabFolder tabFolder;
 
+    private TabItem currentTab;
     private Composite currentImageContainer;
-
+    private Image currentImage;
     private Label currentImageLabel;
-
     private Text currentImageTypeText;
-
     private Text currentImageWidthText;
-
     private Text currentImageHeightText;
-
     private Text currentImageSizeText;
-
     private Button currentImageSaveButton;
 
-    private byte[] newImageRawData;
-
-    private Image newImage;
-
+    private TabItem newTab;
     private Composite newImageContainer;
-
+    private Image newImage;
     private Label newImageLabel;
-
     private Text newImageTypeText;
-
     private Text newImageWidthText;
-
     private Text newImageHeightText;
-
     private Text newImageSizeText;
-
     private Text newImageFilenameText;
-
     private Button newImageBrowseButton;
 
-    private int requiredImageType;
-
-    private byte[] newImageRawDataInRequiredFormat;
-
     private Button okButton;
 
 
@@ -153,16 +145,19 @@ public class ImageDialog extends Dialog
      */
     public boolean close()
     {
+        // Disposing the current image
         if ( currentImage != null && !currentImage.isDisposed() )
         {
             currentImage.dispose();
         }
+
+        // Disposing the new image
         if ( newImage != null && !newImage.isDisposed() )
         {
             newImage.dispose();
         }
 
-        // save selected tab to dialog settings
+        // Saving the selected tab item to dialog settings
         ValueEditorsActivator.getDefault().getDialogSettings().put( SELECTED_TAB_DIALOGSETTINGS_KEY,
             tabFolder.getSelectionIndex() );
 
@@ -179,11 +174,13 @@ public class ImageDialog extends Dialog
         {
             if ( newImageRawData != null )
             {
+                // Preparing the new image bytes for the required format
                 try
                 {
                     ImageData imageData = new ImageData( new ByteArrayInputStream( newImageRawData
) );
                     if ( imageData.type != requiredImageType )
                     {
+                        // Converting the new image in the required format
                         ImageLoader imageLoader = new ImageLoader();
                         imageLoader.data = new ImageData[]
                             { imageData };
@@ -193,6 +190,7 @@ public class ImageDialog extends Dialog
                     }
                     else
                     {
+                        // Directly using the new image bytes
                         newImageRawDataInRequiredFormat = newImageRawData;
                     }
                 }
@@ -239,9 +237,10 @@ public class ImageDialog extends Dialog
         }
         catch ( Exception e )
         {
+            // Nothing to do
         }
 
-        // update on load
+        // Updating the tab folder on load
         updateTabFolder();
     }
 
@@ -252,17 +251,13 @@ public class ImageDialog extends Dialog
     protected Control createDialogArea( Composite parent )
     {
         Composite composite = ( Composite ) super.createDialogArea( parent );
-        GridData gd1 = new GridData( GridData.FILL_BOTH );
-        gd1.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH
);
-        gd1.heightHint = convertVerticalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH
);
-        composite.setLayoutData( gd1 );
+        //        GridData compositeGridData = new GridData( SWT.FILL, SWT.FILL, true, true
);
+        //        compositeGridData.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH
);
+        //        compositeGridData.heightHint = convertVerticalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH
);
+        //        composite.setLayoutData( compositeGridData );
 
         tabFolder = new TabFolder( composite, SWT.TOP );
-        GridLayout mainLayout = new GridLayout();
-        mainLayout.marginWidth = 0;
-        mainLayout.marginHeight = 0;
-        tabFolder.setLayout( mainLayout );
-        tabFolder.setLayoutData( new GridData( GridData.FILL_BOTH ) );
+        tabFolder.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
         tabFolder.addSelectionListener( new SelectionAdapter()
         {
             public void widgetSelected( SelectionEvent e )
@@ -274,15 +269,10 @@ public class ImageDialog extends Dialog
         // current image
         if ( currentImageRawData != null && currentImageRawData.length > 0 )
         {
-            currentImageContainer = new Composite( tabFolder, SWT.NONE );
-            GridLayout currentLayout = new GridLayout();
-            currentLayout.marginHeight = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_MARGIN
);
-            currentLayout.marginWidth = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_MARGIN
);
-            currentLayout.verticalSpacing = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_SPACING
);
-            currentLayout.horizontalSpacing = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_SPACING
);
-            currentImageContainer.setLayout( currentLayout );
-            currentImageContainer.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true,
false ) );
+            currentTab = new TabItem( tabFolder, SWT.NONE );
+            currentTab.setText( Messages.getString( "ImageDialog.CurrentImage" ) ); //$NON-NLS-1$
 
+            currentImageContainer = createTabItemComposite();
             currentImageLabel = createImageLabel( currentImageContainer );
 
             Composite currentImageInfoContainer = createImageInfoContainer( currentImageContainer
);
@@ -321,7 +311,6 @@ public class ImageDialog extends Dialog
                         }
                         catch ( FileNotFoundException e )
                         {
-
                             ConnectionUIPlugin.getDefault().getExceptionHandler().handleException(
                                 new Status( IStatus.ERROR, ValueEditorsConstants.PLUGIN_ID,
IStatus.ERROR, Messages
                                     .getString( "ImageDialog.CantWriteFile" ), e ) ); //$NON-NLS-1$
@@ -336,20 +325,14 @@ public class ImageDialog extends Dialog
                 }
             } );
 
-            currentTab = new TabItem( tabFolder, SWT.NONE );
-            currentTab.setText( Messages.getString( "ImageDialog.CurrentImage" ) ); //$NON-NLS-1$
             currentTab.setControl( currentImageContainer );
         }
 
         // new image
-        newImageContainer = new Composite( tabFolder, SWT.NONE );
-        GridLayout newLayout = new GridLayout();
-        newLayout.marginHeight = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_MARGIN
);
-        newLayout.marginWidth = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_MARGIN
);
-        newLayout.verticalSpacing = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_SPACING
);
-        newLayout.horizontalSpacing = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_SPACING
);
-        newImageContainer.setLayout( newLayout );
-        newImageContainer.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false )
);
+        newTab = new TabItem( tabFolder, SWT.NONE );
+        newTab.setText( Messages.getString( "ImageDialog.NewImage" ) ); //$NON-NLS-1$
+
+        newImageContainer = createTabItemComposite();
 
         newImageLabel = createImageLabel( newImageContainer );
 
@@ -377,9 +360,9 @@ public class ImageDialog extends Dialog
             {
                 FileDialog fileDialog = new FileDialog( ImageDialog.this.getShell(), SWT.OPEN
);
                 fileDialog.setText( Messages.getString( "ImageDialog.SelectImage" ) ); //$NON-NLS-1$
-                // fileDialog.setFilterExtensions(IMAGE_FILE_EXTENSIONS);
                 fileDialog.setFileName( new File( newImageFilenameText.getText() ).getName()
);
                 fileDialog.setFilterPath( new File( newImageFilenameText.getText() ).getParent()
);
+
                 String returnedFileName = fileDialog.open();
                 if ( returnedFileName != null )
                 {
@@ -388,8 +371,6 @@ public class ImageDialog extends Dialog
             }
         } );
 
-        newTab = new TabItem( tabFolder, SWT.NONE );
-        newTab.setText( Messages.getString( "ImageDialog.NewImage" ) ); //$NON-NLS-1$
         newTab.setControl( newImageContainer );
 
         applyDialogFont( composite );
@@ -398,6 +379,51 @@ public class ImageDialog extends Dialog
 
 
     /**
+     * Creates a tab item composite.
+     *
+     * @return a tab item composite
+     */
+    private Composite createTabItemComposite()
+    {
+        Composite composite = new Composite( tabFolder, SWT.NONE );
+
+        GridLayout compositeLayout = new GridLayout( 1, false );
+        compositeLayout.marginHeight = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_MARGIN
);
+        compositeLayout.marginWidth = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_MARGIN
);
+        compositeLayout.verticalSpacing = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_SPACING
);
+        compositeLayout.horizontalSpacing = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_SPACING
);
+        composite.setLayout( compositeLayout );
+
+        composite.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
+
+        return composite;
+    }
+
+
+    /**
+     * Creates the image label.
+     * 
+     * @param parent the parent
+     * @return the image label
+     */
+    private Label createImageLabel( Composite parent )
+    {
+        Composite labelComposite = new Composite( parent, SWT.BORDER );
+        labelComposite.setLayout( new GridLayout() );
+        GridData gd = new GridData( SWT.FILL, SWT.FILL, true, true );
+        //        gd.widthHint = MAX_WIDTH;
+        //        gd.heightHint = MAX_HEIGHT;
+        labelComposite.setLayoutData( gd );
+        labelComposite.setBackground( getShell().getDisplay().getSystemColor( SWT.COLOR_WIDGET_NORMAL_SHADOW
) );
+
+        Label imageLabel = new Label( labelComposite, SWT.CENTER );
+        gd = new GridData( SWT.CENTER, SWT.CENTER, true, true );
+        imageLabel.setLayoutData( gd );
+        return imageLabel;
+    }
+
+
+    /**
      * Update current image tab.
      */
     private void updateCurrentImageGroup()
@@ -418,6 +444,10 @@ public class ImageDialog extends Dialog
                     currentImage = new Image( getShell().getDisplay(), resizeImage( imageData
) );
                     currentImageLabel.setText( "" ); //$NON-NLS-1$
                     currentImageLabel.setImage( currentImage );
+                    GridData currentImageLabelGridData = new GridData( SWT.CENTER, SWT.CENTER,
true, true );
+                    currentImageLabelGridData.widthHint = currentImage.getBounds().width;
+                    currentImageLabelGridData.heightHint = currentImage.getBounds().height;
+                    currentImageLabel.setLayoutData( currentImageLabelGridData );
                     currentImageTypeText.setText( getImageType( imageData.type ) );
                     currentImageSizeText.setText( getSizeString( currentImageRawData.length
) );
                     currentImageWidthText.setText( NLS
@@ -586,55 +616,52 @@ public class ImageDialog extends Dialog
      */
     private ImageData resizeImage( ImageData imageData )
     {
+        // Computing the width scale factor
         double widthScaleFactor = 1.0;
         if ( imageData.width > MAX_WIDTH )
         {
             widthScaleFactor = ( double ) MAX_WIDTH / imageData.width;
         }
+
+        // Computing the height scale factor
         double heightScaleFactor = 1.0;
         if ( imageData.height > MAX_HEIGHT )
         {
             heightScaleFactor = ( double ) MAX_HEIGHT / imageData.height;
         }
 
-        if ( heightScaleFactor < widthScaleFactor )
-        {
-            imageData = imageData.scaledTo(
-                convertHorizontalDLUsToPixels( ( int ) ( imageData.width * heightScaleFactor
) ),
-                convertHorizontalDLUsToPixels( ( int ) ( imageData.height * heightScaleFactor
) ) );
-        }
-        else
-        {
-            imageData = imageData.scaledTo(
-                convertHorizontalDLUsToPixels( ( int ) ( imageData.width * widthScaleFactor
) ),
-                convertHorizontalDLUsToPixels( ( int ) ( imageData.height * widthScaleFactor
) ) );
-        }
+        // Taking the minimum of both
+        double minScalefactor = Math.min( heightScaleFactor, widthScaleFactor );
 
-        return imageData;
+        // Resizing the image data
+        return resize( imageData, ( int ) ( imageData.width * minScalefactor ),
+            ( int ) ( imageData.height * minScalefactor ) );
     }
 
 
     /**
-     * Creates the image label.
-     * 
-     * @param parent the parent
-     * 
-     * @return the image label
+     * Resizes an image using the GC (for better quality).
+     *
+     * @param imageData the image data
+     * @param width the width
+     * @param height the height
+     * @return the resized image
      */
-    private Label createImageLabel( Composite parent )
+    private ImageData resize( ImageData imageData, int width, int height )
     {
-        Composite labelComposite = new Composite( parent, SWT.BORDER );
-        labelComposite.setLayout( new GridLayout() );
-        GridData gd = new GridData( SWT.FILL, SWT.FILL, true, true );
-        gd.widthHint = MAX_WIDTH;
-        gd.heightHint = MAX_HEIGHT;
-        labelComposite.setLayoutData( gd );
-        labelComposite.setBackground( getShell().getDisplay().getSystemColor( SWT.COLOR_WIDGET_NORMAL_SHADOW
) );
+        Image image = new Image( Display.getDefault(), imageData );
 
-        Label imageLabel = new Label( labelComposite, SWT.CENTER );
-        gd = new GridData( SWT.CENTER, SWT.CENTER, true, true );
-        imageLabel.setLayoutData( gd );
-        return imageLabel;
+        Image resizedImage = new Image( Display.getDefault(), width, height );
+
+        GC gc = new GC( resizedImage );
+        gc.setAntialias( SWT.ON );
+        gc.setInterpolation( SWT.HIGH );
+        gc.drawImage( image, 0, 0, image.getBounds().width, image.getBounds().height, 0,
0, width, height );
+        gc.dispose();
+
+        image.dispose();
+
+        return resizedImage.getImageData();
     }
 
 



Mime
View raw message