📄 swtimagehelper.java
字号:
/* This file is part of ui2swt.
*
* $Revision: 1.5 $
* $Date: 2007/01/20 10:19:31 $
* $Name: $
*
* Copyright (C) 2006-2007 James Forbes, All Rights Reserved.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the
* use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in
* a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
package ui2swt.swt;
import java.util.HashSet;
import java.util.Set;
import ui2swt.ClassDefinition;
import ui2swt.Image;
import ui2swt.UIDefinition;
import ui2swt.Widget;
public class SWTImageHelper extends SWTHelper
{
private static final String UI_IMAGE_NAME = "name";
private static final String UI_IMAGE_DATA = "data";
private static final String UI_IMAGE_FORMAT = "data.format";
private static final String UI_IMAGE_FORMAT_XPM_GZ = "XPM.GZ";
private static final String UI_IMAGE_PIXMAP = "pixmap.pixmap";
private static final String UI_IMAGE_ICON_SET = "icon.iconset";
private static final String SWT_IMAGE_DATA_CONSTANT = "swt.imageData.constant";
private static final Set translatedImages = new HashSet();
//
//
//
public static boolean hasImage(
UIDefinition iUIDefinition,
Widget iWidget )
{
boolean result = false;
if ( iWidget.containsProperty(UI_IMAGE_PIXMAP) )
{
result = true;
}
else if ( iWidget.containsProperty(UI_IMAGE_ICON_SET) )
{
result = true;
}
return result;
}
public static String getImage(
UIDefinition iUIDefinition,
Widget iWidget )
{
String result = null;
if ( iWidget.containsProperty(UI_IMAGE_PIXMAP) )
{
result = iWidget.getProperty(UI_IMAGE_PIXMAP);
}
else if ( iWidget.containsProperty(UI_IMAGE_ICON_SET) )
{
result = iWidget.getProperty(UI_IMAGE_ICON_SET);
}
return result;
}
//
//
//
public static boolean isCompressed(
UIDefinition iUIDefinition,
Image iImage )
{
String imageFormat = iImage.getProperty(UI_IMAGE_FORMAT);
if ( UI_IMAGE_FORMAT_XPM_GZ.equals(imageFormat) )
{
return true;
}
else
{
return false;
}
}
//
//
//
public static void generateSetImageCode(
UIDefinition iUIDefinition,
Widget iWidget,
String iPixmap,
ClassDefinition iClassDefinition )
{
switch (iUIDefinition.getVersion())
{
case UIDefinition.VERSION_2:
case UIDefinition.VERSION_3:
{
Image image = iUIDefinition.getImage(UI_IMAGE_NAME, iPixmap);
if ( image == null )
{
System.err.println("WARNING: Could not find image: "+iPixmap);
}
else
{
translateImage(iUIDefinition, image, iClassDefinition);
if ( isCompressed(iUIDefinition, image) )
{
String widgetVar = iWidget.getProperty(SWT_VAR);
String imageDataConstant = image.getProperty(SWT_IMAGE_DATA_CONSTANT);
iClassDefinition
.addImport("org.eclipse.swt.graphics.Image")
.addImport("java.io.ByteArrayInputStream")
.addImport("java.util.zip.InflaterInputStream")
.getConstructor().addMethodCall1(
widgetVar,
"setImage",
"new Image("+widgetVar+".getDisplay(),new InflaterInputStream(new ByteArrayInputStream("+imageDataConstant+")))");
}
else
{
String widgetVar = iWidget.getProperty(SWT_VAR);
String imageDataConstant = image.getProperty(SWT_IMAGE_DATA_CONSTANT);
iClassDefinition
.addImport("org.eclipse.swt.graphics.Image")
.addImport("java.io.ByteArrayInputStream")
.getConstructor().addMethodCall1(
widgetVar,
"setImage",
"new Image("+widgetVar+".getDisplay(),new ByteArrayInputStream("+imageDataConstant+"))");
}
}
}
break;
case UIDefinition.VERSION_4:
{
String widgetVar = iWidget.getProperty(SWT_VAR);
iClassDefinition
.addImport("org.eclipse.swt.graphics.Image")
.getConstructor().addMethodCall1(
widgetVar,
"setImage",
"new Image("+widgetVar+".getDisplay(),\""+iPixmap+"\")");
}
break;
}
}
//
//
//
private static void translateImage(
UIDefinition iUIDefinition,
Image iImage,
ClassDefinition iClassDefinition )
{
if ( ! translatedImages.contains(iImage) )
{
String name = iImage.getProperty(UI_IMAGE_NAME);
String data = iImage.getProperty(UI_IMAGE_DATA);
String constant = name.toUpperCase()+"_DATA";
StringBuffer value = new StringBuffer();
int length = data.length();
value.append("new byte[]{");
if ( length > 0 )
{
value.append(convertHexByte(data.charAt(0), data.charAt(1)));
// value.append("(byte)0x");
// value.append(data.charAt(0));
// value.append(data.charAt(1));
for ( int i = 2; i < length; i+=2 )
{
value.append(",");
value.append(convertHexByte(data.charAt(i), data.charAt(i+1)));
// value.append(",(byte)0x");
// value.append(data.charAt(i));
// value.append(data.charAt(i+1));
}
}
value.append("}");
iClassDefinition.addConstant("public", "byte[]", constant, value);
iImage.setProperty(SWT_IMAGE_DATA_CONSTANT, constant);
translatedImages.add(iImage);
}
}
private static byte convertHexByte( char ch1, char ch2 )
{
return (byte)((convertHexChar(ch1) << 4) | convertHexChar(ch2));
}
private static int convertHexChar( char ch )
{
if ( (ch >= '0') && (ch <= '9') )
{
return (ch - '0');
}
else if ((ch >= 'a') && (ch <= 'f') )
{
return (ch - 'a' + 10);
}
else if ((ch >= 'A') && (ch <= 'F') )
{
return (ch - 'A' + 10);
}
else
{
return 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -