diff options
author | Silenio Quarti <silenio> | 2004-04-08 18:47:47 +0000 |
---|---|---|
committer | Silenio Quarti <silenio> | 2004-04-08 18:47:47 +0000 |
commit | c57e6709ba1f97c13687499427427413c1eb3b48 (patch) | |
tree | 888d14424b03f71c66d8b3edf11bdb66ee6a2c38 | |
parent | 08734f1d9217f5c1e4c2264ddc5ef8916987021c (diff) | |
download | eclipse.platform.swt-c57e6709ba1f97c13687499427427413c1eb3b48.tar.gz eclipse.platform.swt-c57e6709ba1f97c13687499427427413c1eb3b48.tar.xz eclipse.platform.swt-c57e6709ba1f97c13687499427427413c1eb3b48.zip |
53632
3 files changed, 68 insertions, 17 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java index 7f737fc885..381ea42407 100755 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java @@ -2581,6 +2581,38 @@ public static void error (int code) { * @see IllegalArgumentException */ public static void error (int code, Throwable throwable) { + error (code, throwable, null); +} + +/** + * Throws an appropriate exception based on the passed in error code. + * The <code>throwable</code> argument should be either null, or the + * throwable which caused SWT to throw an exception. + * <p> + * In SWT, errors are reported by throwing one of three exceptions: + * <dl> + * <dd>java.lang.IllegalArgumentException</dd> + * <dt>thrown whenever one of the API methods is invoked with an illegal argument</dt> + * <dd>org.eclipse.swt.SWTException (extends java.lang.RuntimeException)</dd> + * <dt>thrown whenever a recoverable error happens internally in SWT</dt> + * <dd>org.eclipse.swt.SWTError (extends java.lang.Error)</dd> + * <dt>thrown whenever a <b>non-recoverable</b> error happens internally in SWT</dt> + * </dl> + * This method provides the logic which maps between error codes + * and one of the above exceptions. + * </p> + * + * @param code the SWT error code. + * @param throwable the exception which caused the error to occur. + * @param detail more information about error. + * + * @see SWTError + * @see SWTException + * @see IllegalArgumentException + * + * @since 3.0 + */ +public static void error (int code, Throwable throwable, String detail) { /* * This code prevents the creation of "chains" of SWTErrors and @@ -2595,7 +2627,9 @@ public static void error (int code, Throwable throwable) { */ if (throwable instanceof SWTError) throw (SWTError) throwable; if (throwable instanceof SWTException) throw (SWTException) throwable; - + + String message = findErrorText (code); + if (detail != null) message += detail; switch (code) { /* Illegal Arguments (non-fatal) */ @@ -2608,7 +2642,7 @@ public static void error (int code, Throwable throwable) { case ERROR_MENUITEM_NOT_CASCADE: case ERROR_INVALID_PARENT: case ERROR_INVALID_RANGE: { - throw new IllegalArgumentException (findErrorText (code)); + throw new IllegalArgumentException (message); } /* SWT Errors (non-fatal) */ @@ -2622,7 +2656,7 @@ public static void error (int code, Throwable throwable) { case ERROR_UNSUPPORTED_FORMAT: case ERROR_FAILED_EXEC: case ERROR_IO: { - SWTException exception = new SWTException (code); + SWTException exception = new SWTException (code, message); exception.throwable = throwable; throw exception; } @@ -2648,14 +2682,14 @@ public static void error (int code, Throwable throwable) { case ERROR_NO_MORE_CALLBACKS: case ERROR_NOT_IMPLEMENTED: case ERROR_UNSPECIFIED: { - SWTError error = new SWTError (code); + SWTError error = new SWTError (code, message); error.throwable = throwable; throw error; } } /* Unknown/Undefined Error */ - SWTError error = new SWTError (code); + SWTError error = new SWTError (code, message); error.throwable = throwable; throw error; } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java index b748fea7f1..51ca43b34b 100755 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java @@ -450,6 +450,25 @@ public FontData [] getFontList (String faceName, boolean scalable) { return result; } +String getLastError () { + int error = OS.GetLastError(); + if (error == 0) return ""; + return " [GetLastError=0x" + Integer.toHexString(error) + "]"; +} + +String getLastErrorText () { + int error = OS.GetLastError(); + if (error == 0) return ""; + int[] buffer = new int[1]; + int dwFlags = OS.FORMAT_MESSAGE_ALLOCATE_BUFFER | OS.FORMAT_MESSAGE_FROM_SYSTEM | OS.FORMAT_MESSAGE_IGNORE_INSERTS; + int length = OS.FormatMessage(dwFlags, 0, error, OS.LANG_USER_DEFAULT, buffer, 0, 0); + if (length == 0) return " [GetLastError=0x" + Integer.toHexString(error) + "]"; + TCHAR buffer1 = new TCHAR(0, length); + OS.MoveMemory(buffer1, buffer[0], length * TCHAR.sizeof); + if (buffer[0] != 0) OS.LocalFree(buffer[0]); + return buffer1.toString(0, length); +} + /** * Returns the matching standard color for the given * constant, which should be one of the color constants diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java index 82b342e963..fd636e8100 100755 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java @@ -1352,21 +1352,19 @@ void init(Device device, int width, int height) { } this.device = device; type = SWT.BITMAP; - - /* Get the HDC for the device */ int hDC = device.internal_new_GC(null); - - /* Fill the bitmap with the current background color */ handle = OS.CreateCompatibleBitmap(hDC, width, height); - if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); - int memDC = OS.CreateCompatibleDC(hDC); - int hOldBitmap = OS.SelectObject(memDC, handle); - OS.PatBlt(memDC, 0, 0, width, height, OS.PATCOPY); - OS.SelectObject(memDC, hOldBitmap); - OS.DeleteDC(memDC); - - /* Release the HDC for the device */ + if (handle != 0) { + int memDC = OS.CreateCompatibleDC(hDC); + int hOldBitmap = OS.SelectObject(memDC, handle); + OS.PatBlt(memDC, 0, 0, width, height, OS.PATCOPY); + OS.SelectObject(memDC, hOldBitmap); + OS.DeleteDC(memDC); + } device.internal_dispose_GC(hDC, null); + if (handle == 0) { + SWT.error(SWT.ERROR_NO_HANDLES, null, device.getLastError()); + } } /** |