javamanager/javainstaller/installerui/javasrc/com/nokia/mj/impl/installer/ui/eswt/InstallerUiEswt.java
changeset 56 abc41079b313
parent 49 35baca0e7a2e
child 72 1f0034e370aa
equal deleted inserted replaced
50:023eef975703 56:abc41079b313
    33 import com.nokia.mj.impl.utils.StartUpTrace;
    33 import com.nokia.mj.impl.utils.StartUpTrace;
    34 import com.nokia.mj.impl.utils.exception.InstallerExceptionBase;
    34 import com.nokia.mj.impl.utils.exception.InstallerExceptionBase;
    35 
    35 
    36 import java.io.InputStream;
    36 import java.io.InputStream;
    37 import java.io.IOException;
    37 import java.io.IOException;
       
    38 import java.util.Enumeration;
    38 import java.util.Hashtable;
    39 import java.util.Hashtable;
    39 
    40 
    40 import org.eclipse.ercp.swt.midp.UIThreadSupport;
    41 import org.eclipse.ercp.swt.midp.UIThreadSupport;
    41 import org.eclipse.swt.SWT;
    42 import org.eclipse.swt.SWT;
    42 import org.eclipse.swt.events.ControlEvent;
    43 import org.eclipse.swt.events.ControlEvent;
    46 import org.eclipse.swt.graphics.Image;
    47 import org.eclipse.swt.graphics.Image;
    47 import org.eclipse.swt.graphics.ImageData;
    48 import org.eclipse.swt.graphics.ImageData;
    48 import org.eclipse.swt.graphics.ImageLoader;
    49 import org.eclipse.swt.graphics.ImageLoader;
    49 import org.eclipse.swt.graphics.Point;
    50 import org.eclipse.swt.graphics.Point;
    50 import org.eclipse.swt.graphics.Rectangle;
    51 import org.eclipse.swt.graphics.Rectangle;
       
    52 import org.eclipse.swt.internal.extension.DisplayExtension;
       
    53 import org.eclipse.swt.internal.extension.ImageUtil;
    51 import org.eclipse.swt.widgets.Display;
    54 import org.eclipse.swt.widgets.Display;
    52 import org.eclipse.swt.widgets.Event;
    55 import org.eclipse.swt.widgets.Event;
    53 import org.eclipse.swt.widgets.Composite;
    56 import org.eclipse.swt.widgets.Composite;
    54 import org.eclipse.swt.widgets.Listener;
    57 import org.eclipse.swt.widgets.Listener;
    55 import org.eclipse.swt.widgets.Shell;
    58 import org.eclipse.swt.widgets.Shell;
   199                 if (!display.readAndDispatch())
   202                 if (!display.readAndDispatch())
   200                 {
   203                 {
   201                     display.sleep();
   204                     display.sleep();
   202                 }
   205                 }
   203             }
   206             }
   204             if (iBoldFont != null && !iBoldFont.isDisposed())
   207             disposeResources();
   205             {
       
   206                 iBoldFont.dispose();
       
   207             }
       
   208             display.dispose();
   208             display.dispose();
   209             log("uiMain: display disposed");
   209             log("uiMain: display disposed");
   210             synchronized (iExitWaitObject)
   210             synchronized (iExitWaitObject)
   211             {
   211             {
   212                 // Notify that UI main thread has been terminated.
   212                 // Notify that UI main thread has been terminated.
  1045     {
  1045     {
  1046         if (iSecurityIcon != null)
  1046         if (iSecurityIcon != null)
  1047         {
  1047         {
  1048             return iSecurityIcon;
  1048             return iSecurityIcon;
  1049         }
  1049         }
  1050         String iconFilename = ResourceUtil.UNTRUSTED_ICON_NAME;
  1050         int id = ImageUtil.THEME_IMAGE_SECURITY_UNTRUSTED;
  1051         if (aIdentified)
  1051         if (aIdentified)
  1052         {
  1052         {
  1053             iconFilename = ResourceUtil.TRUSTED_ICON_NAME;
  1053             id = ImageUtil.THEME_IMAGE_SECURITY_TRUSTED;
  1054         }
  1054         }
  1055         String resourceDir = ResourceUtil.getResourceDir(0);
  1055         
  1056         for (int i = 1; iSecurityIcon == null && resourceDir != null; i++)
  1056         try 
  1057         {
  1057         {
  1058             iSecurityIcon = loadImage(aDisplay, resourceDir + iconFilename, false);
  1058             iSecurityIcon = ImageUtil.createImageFromTheme(aDisplay, id);
  1059             resourceDir = ResourceUtil.getResourceDir(i);
  1059         }
  1060         }
  1060         catch (Throwable t)
       
  1061         {
       
  1062             log("Can not load security icon: " + t);
       
  1063         }
       
  1064         
  1061         return iSecurityIcon;
  1065         return iSecurityIcon;
  1062     }
  1066     }
  1063 
  1067 
  1064     /**
  1068     /**
  1065      * Loads image from specified InputStream. This method scales the image
  1069      * Loads image from specified InputStream. This method scales the image
  1145             return result;
  1149             return result;
  1146         }
  1150         }
  1147         try
  1151         try
  1148         {
  1152         {
  1149             long startTime = System.currentTimeMillis();
  1153             long startTime = System.currentTimeMillis();
  1150             ImageData[] imageDatas = new ImageLoader().load(aInputStream);
  1154             
  1151             ImageData imageData = imageDatas[0];
  1155             Image image = new Image(aDisplay, aInputStream);
  1152             if (aScaleImage)
  1156             if (aScaleImage)
  1153             {
  1157             {
  1154                 Point bestSize = getBestImageSize(
  1158                 int maxWidth = DisplayExtension.getBestImageWidth(DisplayExtension.LIST_ELEMENT);
  1155                                      imageData.width, imageData.height);
  1159                 int maxHeight = DisplayExtension.getBestImageHeight(DisplayExtension.LIST_ELEMENT);
  1156                 if (bestSize.x != imageData.width ||
  1160                 Rectangle rect = image.getBounds();
  1157                         bestSize.y != imageData.height)
  1161                 if (maxWidth != rect.width || maxHeight != rect.height)
  1158                 {
  1162                 {
  1159                     imageData = imageData.scaledTo(bestSize.x, bestSize.y);
  1163                     // Copy and scale natively preserving the aspect ratio
  1160                     log("Image " + aImageName + " scaled from " +
  1164                     result = ImageUtil.scaleImage(aDisplay, image, new Point(maxWidth, maxHeight), true);
  1161                         imageDatas[0].width + "x" + imageDatas[0].height +
  1165                     image.dispose();
  1162                         " to " + bestSize.x + "x" + bestSize.y);
  1166                     image = null;
  1163                 }
  1167                 }
  1164             }
  1168             }
  1165             result = new Image(aDisplay, imageData);
  1169             if (result == null)
       
  1170             {
       
  1171                 result = image;
       
  1172             }
       
  1173             
  1166             long endTime = System.currentTimeMillis();
  1174             long endTime = System.currentTimeMillis();
  1167             log("Loaded image " + aImageName + " (load time " +
  1175             log("Loaded image " + aImageName + " (load time " +
  1168                 (endTime - startTime) + " ms)");
  1176                 (endTime - startTime) + " ms)");
  1169             iImageTable.put(aImageName, result);
  1177             iImageTable.put(aImageName, result);
  1170         }
  1178         }
  1174             //logError("Exception while loading image " + aImageName, t);
  1182             //logError("Exception while loading image " + aImageName, t);
  1175         }
  1183         }
  1176         return result;
  1184         return result;
  1177     }
  1185     }
  1178 
  1186 
  1179     /**
       
  1180      * Determines the best image size for the image of given size.
       
  1181      */
       
  1182     private static Point getBestImageSize(int aWidth, int aHeight)
       
  1183     {
       
  1184         // Actually maximum image width and height should be obtained with
       
  1185         // org.eclipse.swt.internal.extension.DisplayExtension
       
  1186         // getBestImageWidth() and getBestImageHeight().
       
  1187         final int MAX_WIDTH = 50; // max width in pixels
       
  1188         final int MAX_HEIGHT = 50; // max height in pixels
       
  1189         Point result = new Point(aWidth, aHeight);
       
  1190         if (result.x > MAX_WIDTH || result.y > MAX_HEIGHT)
       
  1191         {
       
  1192             if (result.x >= MAX_WIDTH)
       
  1193             {
       
  1194                 result.x = MAX_WIDTH;
       
  1195                 result.y = MAX_WIDTH * aHeight / aWidth;
       
  1196             }
       
  1197             if (result.y >= MAX_HEIGHT)
       
  1198             {
       
  1199                 result.x = MAX_HEIGHT * aWidth / aHeight;
       
  1200                 result.y = MAX_HEIGHT;
       
  1201             }
       
  1202         }
       
  1203         return result;
       
  1204     }
       
  1205 
       
  1206     /** Returns true if UI has been created and can be used. */
  1187     /** Returns true if UI has been created and can be used. */
  1207     protected boolean isUiReady()
  1188     protected boolean isUiReady()
  1208     {
  1189     {
  1209         if (iProgressView == null)
  1190         if (iProgressView == null)
  1210         {
  1191         {
  1296             }
  1277             }
  1297             iBoldFont = new Font(iParent.getDisplay(), boldFontDatas);
  1278             iBoldFont = new Font(iParent.getDisplay(), boldFontDatas);
  1298         }
  1279         }
  1299         return iBoldFont;
  1280         return iBoldFont;
  1300     }
  1281     }
       
  1282     
       
  1283     int iconLabelTopMargin()
       
  1284     {
       
  1285         // Aproximating the space at the top of a Label.
       
  1286         // eSWT's Label adds extra space around texts but not around images.
       
  1287         return getBoldFont().getFontData()[0].getHeight();
       
  1288     }
  1301 
  1289 
  1302     void setActiveView(ViewBase aView)
  1290     void setActiveView(ViewBase aView)
  1303     {
  1291     {
  1304         if (iActiveView != null && iActiveView != aView &&
  1292         if (iActiveView != null && iActiveView != aView &&
  1305                 !iActiveView.isDisposed())
  1293                 !iActiveView.isDisposed())
  1311 
  1299 
  1312     ViewBase getActiveView()
  1300     ViewBase getActiveView()
  1313     {
  1301     {
  1314         return iActiveView;
  1302         return iActiveView;
  1315     }
  1303     }
       
  1304     
       
  1305     private void disposeResources() {
       
  1306         if (iBoldFont != null && !iBoldFont.isDisposed())
       
  1307         {
       
  1308             iBoldFont.dispose();
       
  1309         }
       
  1310         if (iSecurityIcon != null && !iSecurityIcon.isDisposed())
       
  1311         {
       
  1312             iSecurityIcon.dispose();
       
  1313         }
       
  1314         Enumeration e = iImageTable.elements();
       
  1315         while (e.hasMoreElements())
       
  1316         {
       
  1317             Image img = (Image)e.nextElement();
       
  1318             if (img != null && !img.isDisposed())
       
  1319             {
       
  1320                 img.dispose();
       
  1321             }
       
  1322         }
       
  1323     }
  1316 }
  1324 }