📄 utils.java
字号:
}
}
}
}
if (oneChanged) {
parent.layout(true);
if (grandParent instanceof ScrolledComposite) {
((ScrolledComposite)grandParent).setMinSize(parent.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
}
}
}
} // size
} // controlResized
} // class
public static void alternateRowBackground(TableItem item) {
// On linux, table lines are actually alternating background colors
if (Constants.isLinux) {
if (!item.getParent().getLinesVisible())
item.getParent().setLinesVisible(true);
return;
}
if (item == null || item.isDisposed())
return;
Color[] colors = { item.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND),
Colors.colorAltRow };
Color newColor = colors[ item.getParent().indexOf(item) % colors.length];
if (!item.getBackground().equals(newColor)) {
item.setBackground(newColor);
}
}
public static void alternateTableBackground(Table table) {
if (table == null || table.isDisposed())
return;
// On linux, table lines are actually alternating background colors
if (Constants.isLinux) {
if (!table.getLinesVisible())
table.setLinesVisible(true);
return;
}
int iTopIndex = table.getTopIndex();
int iBottomIndex = getTableBottomIndex(table, iTopIndex);
Color[] colors = { table.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND),
Colors.colorAltRow };
int iFixedIndex = iTopIndex;
for (int i = iTopIndex; i <= iBottomIndex; i++) {
TableItem row = table.getItem(i);
// Rows can be disposed!
if (!row.isDisposed()) {
Color newColor = colors[iFixedIndex % colors.length];
iFixedIndex++;
if (!row.getBackground().equals(newColor)) {
// System.out.println("setting "+rows[i].getBackground() +" to " + newColor);
row.setBackground(newColor);
}
}
}
}
/**
* <p>
* Set a MenuItem's image with the given ImageRepository key. In compliance with platform
* human interface guidelines, the images are not set under Mac OS X.
* </p>
* @param item SWT MenuItem
* @param repoKey ImageRepository image key
* @see <a href="http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHIGuidelines/XHIGMenus/chapter_7_section_3.html#//apple_ref/doc/uid/TP30000356/TPXREF116">Apple HIG</a>
*/
public static void setMenuItemImage(final MenuItem item, final String repoKey)
{
if(!Constants.isOSX)
item.setImage(ImageRepository.getImage(repoKey));
}
public static void setMenuItemImage(final MenuItem item, final Image image)
{
if(!Constants.isOSX)
item.setImage(image);
}
/**
* Sets the shell's Icon(s) to the default Azureus icon. OSX doesn't require
* an icon, so they are skipped
*
* @param shell
*/
public static void setShellIcon(Shell shell) {
final String[] sImageNames = { "azureus", "azureus32", "azureus64",
"azureus128" };
if (Constants.isOSX)
return;
try {
ArrayList list = new ArrayList();
Image[] images = new Image[] { ImageRepository.getImage("azureus"),
ImageRepository.getImage("azureus32"),
ImageRepository.getImage("azureus64"),
ImageRepository.getImage("azureus128") };
for (int i = 0; i < images.length; i++) {
Image image = ImageRepository.getImage(sImageNames[i]);
if (image != null)
list.add(image);
}
if (list.size() == 0)
return;
shell.setImages((Image[]) list.toArray(new Image[0]));
} catch (NoSuchMethodError e) {
// SWT < 3.0
Image image = ImageRepository.getImage(sImageNames[0]);
if (image != null)
shell.setImage(image);
}
}
/**
* Execute code in the Runnable object using SWT's thread. If current
* thread it already SWT's thread, the code will run immediately. If the
* current thread is not SWT's, code will be run either synchronously or
* asynchronously on SWT's thread at the next reasonable opportunity.
*
* This method does not catch any exceptions.
*
* @param code code to run
* @param async true if SWT asyncExec, false if SWT syncExec
* @return success
*/
public static boolean execSWTThread(Runnable code,
boolean async) {
SWTThread swt = SWTThread.getInstance();
if (swt == null) {
System.err.println("SWT Thread not started yet");
return false;
}
Display display = swt.getDisplay();
if (display == null || display.isDisposed())
return false;
if (display.getThread() == Thread.currentThread())
code.run();
else if (async)
display.asyncExec(code);
else
display.syncExec(code);
return true;
}
/**
* Execute code in the Runnable object using SWT's thread. If current
* thread it already SWT's thread, the code will run immediately. If the
* current thread is not SWT's, code will be run asynchronously on SWT's
* thread at the next reasonable opportunity.
*
* This method does not catch any exceptions.
*
* @param code code to run
* @return success
*/
public static boolean execSWTThread(Runnable code) {
return execSWTThread(code, true);
}
public static boolean isThisThreadSWT() {
SWTThread swt = SWTThread.getInstance();
if (swt == null) {
System.err.println("SWT Thread not started yet");
return false;
}
Display display = swt.getDisplay();
if (display == null || display.isDisposed())
return false;
return (display.getThread() == Thread.currentThread());
}
/** Open a messagebox using resource keys for title/text
*
* @param parent Parent shell for messagebox
* @param style SWT styles for messagebox
* @param keyPrefix message bundle key prefix used to get title and text.
* Title will be keyPrefix + ".title", and text will be set to
* keyPrefix + ".text"
* @param textParams any parameters for text
*
* @return what the messagebox returns
*/
public static int openMessageBox(Shell parent, int style, String keyPrefix,
String[] textParams) {
MessageBox mb = new MessageBox(parent, style);
mb.setMessage(MessageText.getString(keyPrefix + ".text", textParams));
mb.setText(MessageText.getString(keyPrefix + ".title"));
return mb.open();
}
/** Open a messagebox with actual title and text
*
* @param parent
* @param style
* @param title
* @param text
* @return
*/
public static int openMessageBox(Shell parent, int style, String title,
String text) {
MessageBox mb = new MessageBox(parent, style);
mb.setMessage(text);
mb.setText(title);
return mb.open();
}
/**
* Bottom Index may be negative
*/
public static int getTableBottomIndex(Table table, int iTopIndex) {
// getItemHeight is slow on Linux, use getItem/getClientArea
// getItem(Point) is slow on OSX
if (!table.isVisible())
return -1;
if (Constants.isOSX)
return Math.min(iTopIndex
+ ((table.getClientArea().height - table.getHeaderHeight() - 1) /
table.getItemHeight()), table.getItemCount() - 1);
// getItem will return null if clientArea's height is smaller than
// header height.
int areaHeight = table.getClientArea().height;
if (areaHeight <= table.getHeaderHeight())
return -1;
// 2 offset to be on the safe side
TableItem bottomItem = table.getItem(new Point(2,
table.getClientArea().height - 1));
int iBottomIndex = (bottomItem != null) ? table.indexOf(bottomItem) :
table.getItemCount() - 1;
return iBottomIndex;
}
public static void
openURL(
String url )
{
Program.launch( url );
}
/**
* Sets the checkbox in a Virtual Table while inside a SWT.SetData listener
* trigger. SWT 3.1 has an OSX bug that needs working around.
*
* @param item
* @param checked
*/
public static void setCheckedInSetData(final TableItem item,
final boolean checked) {
if (DIRECT_SETCHECKED) {
item.setChecked(checked);
} else {
item.setChecked(!checked);
item.getDisplay().asyncExec(new AERunnable() {
public void runSupport() {
item.setChecked(checked);
}
});
}
if (Constants.isWindowsXP || isGTK) {
Rectangle r = item.getBounds(0);
Table table = item.getParent();
Rectangle rTable = table.getClientArea();
r.y += VerticalAligner.getTableAdjustVerticalBy(table);
table.redraw(0, r.y, rTable.width, r.height, true);
}
}
public static boolean linkShellMetricsToConfig(final Shell shell,
final String sConfigPrefix) {
boolean isMaximized = COConfigurationManager.getBooleanParameter(
sConfigPrefix + ".maximized", false);
shell.setMaximized(isMaximized);
String windowRectangle = COConfigurationManager.getStringParameter(
sConfigPrefix + ".rectangle", null);
boolean bDidResize = false;
if (null != windowRectangle) {
int i = 0;
int[] values = new int[4];
StringTokenizer st = new StringTokenizer(windowRectangle, ",");
try {
while (st.hasMoreTokens() && i < 4) {
values[i++] = Integer.valueOf(st.nextToken()).intValue();
if (values[i - 1] < 0)
values[i - 1] = 0;
}
if (i == 4) {
shell.setBounds(values[0], values[1], values[2], values[3]);
bDidResize = true;
}
} catch (Exception e) {
}
}
shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
COConfigurationManager.setParameter(sConfigPrefix + ".maximized", shell
.getMaximized());
// unmaximize to get correct window rect
if (shell.getMaximized())
shell.setMaximized(false);
Rectangle windowRectangle = shell.getBounds();
COConfigurationManager.setParameter(sConfigPrefix + ".rectangle",
windowRectangle.x + "," + windowRectangle.y + ","
+ windowRectangle.width + "," + windowRectangle.height);
}
});
return bDidResize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -