📄 utils.java
字号:
break;
if (!display.readAndDispatch())
display.sleep();
} while (true);
}
public static GridData getWrappableLabelGridData(int hspan, int styles)
{
GridData gridData = new GridData(0x100 | styles);
gridData.horizontalSpan = hspan;
gridData.widthHint = 0;
return gridData;
}
private static Image createAlphaImage(Device device, int width, int height)
{
return createAlphaImage(device, width, height, (byte)0);
}
public static Image createAlphaImage(Device device, int width, int height, byte defaultAlpha)
{
byte alphaData[] = new byte[width * height];
Arrays.fill(alphaData, 0, alphaData.length, defaultAlpha);
ImageData imageData = new ImageData(width, height, 24, new PaletteData(255, 65280, 0xff0000));
Arrays.fill(imageData.data, 0, imageData.data.length, (byte)0);
imageData.alphaData = alphaData;
Image image = new Image(device, imageData);
return image;
}
public static Image blitImage(Device device, Image srcImage, Rectangle srcArea, Image dstImage, Point dstPos)
{
if (srcArea == null)
srcArea = srcImage.getBounds();
Rectangle dstBounds = dstImage.getBounds();
if (dstPos == null)
{
dstPos = new Point(dstBounds.x, dstBounds.y);
} else
{
dstBounds.x = dstPos.x;
dstBounds.y = dstPos.y;
}
ImageData dstImageData = dstImage.getImageData();
ImageData srcImageData = srcImage.getImageData();
int yPos = dstPos.y;
int pixels[] = new int[srcArea.width];
byte alphas[] = new byte[srcArea.width];
for (int y = 0; y < srcArea.height; y++)
{
srcImageData.getPixels(srcArea.x, y + srcArea.y, srcArea.width, pixels, 0);
dstImageData.setPixels(dstPos.x, yPos, srcArea.width, pixels, 0);
srcImageData.getAlphas(srcArea.x, y + srcArea.y, srcArea.width, alphas, 0);
dstImageData.setAlphas(dstPos.x, yPos, srcArea.width, alphas, 0);
yPos++;
}
return new Image(device, dstImageData);
}
public static void drawStriped(GC gcImg, int x, int y, int width, int height, int lineDist, int leftshift, boolean fallingLines)
{
lineDist += 2;
int xm = x + width;
int ym = y + height;
for (int i = x; i < xm; i++)
{
for (int j = y; j < ym; j++)
if ((i + leftshift + (fallingLines ? -j : j)) % lineDist == 0)
gcImg.drawPoint(i, j);
}
}
public static Image renderTransparency(Display display, Image background, Image foreground, Point foregroundOffsetOnBg, int modifyForegroundAlpha)
{
if (display == null || display.isDisposed() || background == null || background.isDisposed() || foreground == null || foreground.isDisposed())
return null;
Rectangle backgroundArea = background.getBounds();
Rectangle foregroundDrawArea = foreground.getBounds();
foregroundDrawArea.x += foregroundOffsetOnBg.x;
foregroundDrawArea.y += foregroundOffsetOnBg.y;
foregroundDrawArea.intersect(backgroundArea);
if (foregroundDrawArea.isEmpty())
return null;
Image image = new Image(display, backgroundArea);
ImageData backData = background.getImageData();
ImageData foreData = foreground.getImageData();
ImageData imgData = image.getImageData();
PaletteData backPalette = backData.palette;
ImageData backMask = backData.getTransparencyType() == 1 ? null : backData.getTransparencyMask();
PaletteData forePalette = foreData.palette;
ImageData foreMask = foreData.getTransparencyType() == 1 ? null : foreData.getTransparencyMask();
PaletteData imgPalette = imgData.palette;
image.dispose();
for (int x = 0; x < backgroundArea.width; x++)
{
for (int y = 0; y < backgroundArea.height; y++)
{
RGB cBack = backPalette.getRGB(backData.getPixel(x, y));
int aBack = backData.getAlpha(x, y);
if (backMask != null && backMask.getPixel(x, y) == 0)
aBack = 0;
int aFore = 0;
if (foregroundDrawArea.contains(x, y))
{
int fx = x - foregroundDrawArea.x;
int fy = y - foregroundDrawArea.y;
RGB cFore = forePalette.getRGB(foreData.getPixel(fx, fy));
aFore = foreData.getAlpha(fx, fy);
if (foreMask != null && foreMask.getPixel(fx, fy) == 0)
aFore = 0;
aFore = (aFore * modifyForegroundAlpha) / 255;
cBack.red *= aBack * (255 - aFore);
cBack.red /= 255;
cBack.red += aFore * cFore.red;
cBack.red /= 255;
cBack.green *= aBack * (255 - aFore);
cBack.green /= 255;
cBack.green += aFore * cFore.green;
cBack.green /= 255;
cBack.blue *= aBack * (255 - aFore);
cBack.blue /= 255;
cBack.blue += aFore * cFore.blue;
cBack.blue /= 255;
}
imgData.setAlpha(x, y, aFore + (aBack * (255 - aFore)) / 255);
imgData.setPixel(x, y, imgPalette.getPixel(cBack));
}
}
return new Image(display, imgData);
}
public static Control findBackgroundImageControl(Control control)
{
Image image = control.getBackgroundImage();
if (image == null)
return control;
Composite parent = control.getParent();
Composite lastParent = parent;
for (; parent != null; parent = parent.getParent())
{
Image parentImage = parent.getBackgroundImage();
if (!image.equals(parentImage))
return lastParent;
lastParent = parent;
}
return control;
}
public static boolean anyShellHaveStyle(int styles)
{
Display display = Display.getCurrent();
if (display != null)
{
Shell shells[] = display.getShells();
for (int i = 0; i < shells.length; i++)
{
Shell shell = shells[i];
int style = shell.getStyle();
if ((style & styles) == styles)
return true;
}
}
return false;
}
public static int[] colorToIntArray(Color color)
{
if (color == null || color.isDisposed())
return null;
else
return (new int[] {
color.getRed(), color.getGreen(), color.getBlue()
});
}
public static void centerRelativeTo(Rectangle target, Rectangle reference)
{
target.x = (reference.x + reference.width / 2) - target.width / 2;
target.y = (reference.y + reference.height / 2) - target.height / 2;
}
public static void makeVisibleOnCursor(Rectangle rect)
{
if (null == rect)
return;
Display display = Display.getCurrent();
if (null == display)
{
Debug.out("No current display detected. This method [Utils.makeVisibleOnCursor()] must be called from a display thread.");
return;
}
try
{
Point cursorLocation = display.getCursorLocation();
makeVisibleOnMonitor(rect, getMonitor(cursorLocation));
}
catch (Throwable t) { }
}
public static void makeVisibleOnMonitor(Rectangle rect, Monitor monitor)
{
if (null == rect || null == monitor)
return;
try
{
Rectangle monitorBounds = monitor.getClientArea();
int bottomDiff = (monitorBounds.y + monitorBounds.height) - (rect.y + rect.height);
if (bottomDiff < 0)
rect.y += bottomDiff;
int rightDiff = (monitorBounds.x + monitorBounds.width) - (rect.x + rect.width);
if (rightDiff < 0)
rect.x += rightDiff;
if (rect.x < monitorBounds.x)
rect.x = monitorBounds.x;
if (rect.y < monitorBounds.y)
rect.y = monitorBounds.y;
}
catch (Throwable t) { }
}
private static Monitor getMonitor(int x, int y)
{
return getMonitor(new Point(x, y));
}
public static Monitor getMonitor(Point location)
{
Display display;
display = Display.getCurrent();
if (null == display)
{
Debug.out("No current display detected. This method [Utils.makeVisibleOnCursor()] must be called from a display thread.");
return null;
}
Monitor monitors[];
Rectangle monitorBounds;
int i;
monitors = display.getMonitors();
monitorBounds = null;
i = 0;
_L1:
if (i >= monitors.length)
break MISSING_BLOCK_LABEL_66;
monitorBounds = monitors[i].getClientArea();
if (monitorBounds.contains(location))
return monitors[i];
i++;
goto _L1
Throwable t;
t;
return null;
}
public static int getInitialBrowserStyle(int style)
{
if (!gotBrowserStyle)
{
browserStyle = COConfigurationManager.getBooleanParameter("swt.forceMozilla") ? 32768 : 0;
gotBrowserStyle = true;
}
return style | browserStyle;
}
public static void setFontHeight(Control control, int height, int style)
{
FontData fDatas[] = control.getFont().getFontData();
for (int i = 0; i < fDatas.length; i++)
{
fDatas[i].height = height;
fDatas[i].setStyle(style);
}
Font newFont = new Font(control.getDisplay(), fDatas);
control.setFont(newFont);
control.addDisposeListener(new DisposeListener(newFont) {
final Font val$newFont;
public void widgetDisposed(DisposeEvent e)
{
if (null != newFont && !newFont.isDisposed())
newFont.dispose();
}
{
newFont = font;
super();
}
});
}
public static boolean addDataSourceAggregated(addDataSourceCallback callback)
{
if (callback == null)
return true;
boolean processQueueImmediately = false;
List to_do_now = null;
synchronized (timerProcessDataSources)
{
if (timerEventProcessDS != null && !timerEventProcessDS.hasRun())
{
long now = SystemTime.getCurrentTime();
if (now - timerEventProcessDS.getCreatedTime() < 2000L)
{
long lNextTime = now + 150L;
timerProcessDataSources.adjustAllBy(lNextTime - timerEventProcessDS.getWhen());
if (!processDataSourcesOutstanding.contains(callback))
processDataSourcesOutstanding.add(callback);
} else
{
timerEventProcessDS.cancel();
timerEventProcessDS = null;
processQueueImmediately = true;
to_do_now = processDataSourcesOutstanding;
processDataSourcesOutstanding = new ArrayList();
}
} else
{
if (!processDataSourcesOutstanding.contains(callback))
processDataSourcesOutstanding.add(callback);
timerEventProcessDS = timerProcessDataSources.addEvent(SystemTime.getCurrentTime() + 150L, new TimerEventPerformer() {
public void perform(TimerEvent event)
{
List to_do;
synchronized (Utils.timerProcessDataSources)
{
Utils.timerEventProcessDS = null;
to_do = Utils.processDataSourcesOutstanding;
Utils.processDataSourcesOutstanding = new ArrayList();
}
for (int i = 0; i < to_do.size(); i++)
try
{
addDataSourceCallback this_callback = (addDataSourceCallback)to_do.get(i);
if (TableViewSWT.DEBUGADDREMOVE)
this_callback.debug((new StringBuilder()).append("processDataSourceQueue after ").append(SystemTime.getCurrentTime() - event.getCreatedTime()).append("ms").toString());
this_callback.process();
}
catch (Throwable e)
{
Debug.printStackTrace(e);
}
}
});
}
if (to_do_now != null)
{
to_do_now.remove(callback);
for (int i = 0; i < to_do_now.size(); i++)
try
{
addDataSourceCallback this_callback = (addDataSourceCallback)to_do_now.get(i);
if (TableViewSWT.DEBUGADDREMOVE)
this_callback.debug("Over immediate delay limit, processing queue now");
this_callback.process();
}
catch (Throwable e)
{
Debug.printStackTrace(e);
}
}
}
return processQueueImmediately;
}
public static synchronized String truncateText(GC gc, String text, int maxWidth, boolean cache)
{
if (cache)
{
TruncatedTextResult result = (TruncatedTextResult)truncatedTextCache.get(text);
if (result != null && result.maxWidth == maxWidth)
return result.text;
}
StringBuffer sb = new StringBuffer(text);
String append = "...";
int appendWidth = gc.textExtent(append).x;
boolean needsAppend = false;
do
{
if (gc.textExtent(sb.toString()).x <= maxWidth)
break;
sb.deleteCharAt(sb.length() - 1);
needsAppend = true;
} while (sb.length() != 1);
if (needsAppend)
{
do
{
if (gc.textExtent(sb.toString()).x + appendWidth <= maxWidth)
break;
sb.deleteCharAt(sb.length() - 1);
needsAppend = true;
} while (sb.length() != 1);
sb.append(append);
}
if (cache)
{
TruncatedTextResult ttR = new TruncatedTextResult();
ttR.text = sb.toString();
ttR.maxWidth = maxWidth;
truncatedTextCache.put(text, ttR);
}
return sb.toString();
}
public static String toColorHexString(Color bg)
{
StringBuffer sb = new StringBuffer();
twoHex(sb, bg.getRed());
twoHex(sb, bg.getGreen());
twoHex(sb, bg.getBlue());
return sb.toString();
}
private static void twoHex(StringBuffer sb, int h)
{
if (h <= 15)
sb.append('0');
sb.append(Integer.toHexString(h));
}
public static String getWidgetBGColorURLParam()
{
Color bg = findAnyShell().getDisplay().getSystemColor(22);
byte color[] = new byte[3];
color[0] = (byte)bg.getRed();
color[1] = (byte)bg.getGreen();
color[2] = (byte)bg.getBlue();
return (new StringBuilder()).append("bg_color=").append(ByteFormatter.nicePrint(color)).toString();
}
public static void reportError(Throwable e)
{
MessageBoxShell mb = new MessageBoxShell(findAnyShell(), MessageText.getString("ConfigView.section.security.op.error.title"), MessageText.getString("ConfigView.section.security.op.error", new String[] {
Debug.getNestedExceptionMessage(e)
}), new String[] {
MessageText.getString("Button.ok")
}, 0);
mb.open();
}
static
{
isGTK = SWT.getPlatform().equals("gtk");
LAST_TABLECOLUMN_EXPANDS = isGTK;
TABLE_GRIDLINE_IS_ALTERNATING_COLOR = isGTK;
DIRECT_SETCHECKED = !Constants.isOSX || SWT.getVersion() >= 3212;
DEBUG_SWTEXEC = System.getProperty("debug.swtexec", "0").equals("1");
if (DEBUG_SWTEXEC)
{
queue = new ArrayList();
diag_logger = AEDiagnostics.getLogger("swt");
diag_logger.log("\n\nSWT Logging Starts");
} else
{
queue = null;
diag_logger = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -