📄 messageslideshell.java
字号:
public void runSupport() {
if (shell == null || shell.isDisposed())
return;
if (bSlide) {
new SlideShell(shell, SWT.UP, endBounds).run();
} else {
Utils.execSWTThread(new AERunnable() {
public void runSupport() {
shell.setBounds(endBounds);
shell.open();
}
});
}
int delayLeft = COConfigurationManager
.getIntParameter("Message Popup Autoclose in Seconds") * 1000;
final boolean autohide = (delayLeft != 0);
long lastDelaySecs = 0;
int lastNumPopups = -1;
while ((!autohide || bDelayPaused || delayLeft > 0) && !shell.isDisposed()) {
int delayPausedOfs = (bDelayPaused ? 1 : 0);
final long delaySecs = Math.round(delayLeft / 1000.0)
+ delayPausedOfs;
final int numPopups = historyList.size();
if (lastDelaySecs != delaySecs || lastNumPopups != numPopups) {
lastDelaySecs = delaySecs;
lastNumPopups = numPopups;
shell.getDisplay().asyncExec(new AERunnable() {
public void runSupport() {
String sText = "";
if (lblCloseIn == null || lblCloseIn.isDisposed())
return;
lblCloseIn.setRedraw(false);
if (!bDelayPaused && autohide)
sText += MessageText.getString("popup.closing.in",
new String[] { String.valueOf(delaySecs) });
int numPopupsAfterUs = numPopups - idx - 1;
boolean bHasMany = numPopupsAfterUs > 0;
if (bHasMany) {
sText += "\n";
sText += MessageText.getString("popup.more.waiting",
new String[] { String.valueOf(numPopupsAfterUs) });
}
lblCloseIn.setText(sText);
if (btnHideAll.getVisible() != bHasMany) {
cShell.setRedraw(false);
btnHideAll.setVisible(bHasMany);
lblCloseIn.getParent().layout(true);
cShell.setRedraw(true);
}
setButtonNextText(numPopupsAfterUs);
// Need to redraw to cause a paint
lblCloseIn.setRedraw(true);
}
});
}
if (!bDelayPaused)
delayLeft -= PAUSE;
try {
Thread.sleep(PAUSE);
} catch (InterruptedException e) {
delayLeft = 0;
}
}
if (this.isInterrupted()) {
// App closedown likely, boot out ASAP
disposeShell(shell);
return;
}
// Assume that if the shell was disposed during loop, it's on purpose
// and that it has handled whether to show the next popup or not
if (shell != null && !shell.isDisposed()) {
if (idx + 1 < historyList.size()) {
showPopup(display, (PopupParams) historyList.get(idx + 1), true);
}
// slide out current popup
if (bSlide)
new SlideShell(shell, SWT.RIGHT).run();
disposeShell(shell);
}
}
};
thread.start();
}
private void disposeShell(final Shell shell) {
if (shell == null || shell.isDisposed())
return;
Utils.execSWTThread(new AERunnable() {
public void runSupport() {
shell.dispose();
}
});
}
/**
* Waits until all slideys are closed before returning to caller.
*/
public static void waitUntilClosed() {
if (currentPopupIndex < 0)
return;
Display display = Display.getCurrent();
while (currentPopupIndex >= 0) {
if (!display.readAndDispatch())
display.sleep();
}
}
/**
* XXX This could/should be its own class
*/
private class SlideShell {
private int STEP = 8;
private int PAUSE = 30;
private Shell shell;
private Rectangle shellBounds = null;
private Rectangle endBounds;
private final int direction;
private final boolean slideIn;
/**
* Slide In
*
* @param shell
* @param direction
* @param endBounds
*/
public SlideShell(final Shell shell, int direction,
final Rectangle endBounds) {
this.shell = shell;
this.endBounds = endBounds;
this.slideIn = true;
this.direction = direction;
if (shell == null || shell.isDisposed())
return;
Display display = shell.getDisplay();
display.syncExec(new Runnable() {
public void run() {
if (shell == null || shell.isDisposed())
return;
switch (SlideShell.this.direction) {
case SWT.UP:
default:
shell.setLocation(endBounds.x, endBounds.y);
Rectangle displayBounds = null;
try {
boolean ok = false;
Monitor[] monitors = shell.getDisplay().getMonitors();
for (int i = 0; i < monitors.length; i++) {
Monitor monitor = monitors[i];
displayBounds = monitor.getBounds();
int x = endBounds.x;
int y = endBounds.y;
if (displayBounds.contains(endBounds.x, endBounds.y)) {
ok = true;
break;
}
}
if (!ok) {
displayBounds = shell.getMonitor().getBounds();
}
} catch (Throwable t) {
displayBounds = shell.getDisplay().getBounds();
}
shellBounds = new Rectangle(endBounds.x, displayBounds.y
+ displayBounds.height, endBounds.width, 0);
break;
}
shell.setBounds(shellBounds);
shell.setVisible(true);
if (DEBUG)
System.out.println("Slide In: " + shell.getText());
}
});
}
/**
* Slide Out
*
* @param shell
* @param direction
*/
public SlideShell(final Shell shell, int direction) {
this.shell = shell;
this.slideIn = false;
this.direction = direction;
if (DEBUG && canContinue())
shell.getDisplay().syncExec(new Runnable() {
public void run() {
System.out.println("Slide Out: " + shell.getText());
}
});
}
private boolean canContinue() {
if (shell == null || shell.isDisposed())
return false;
if (shellBounds == null)
return true;
//System.out.println((slideIn ? "In" : "Out") + ";" + direction + ";S:" + shellBounds + ";" + endBounds);
if (slideIn) {
if (direction == SWT.UP) {
return shellBounds.y > endBounds.y;
}
// TODO: Other directions
} else {
if (direction == SWT.RIGHT) {
// stop early, because some OSes have trim, and won't allow the window
// to go smaller than it.
return shellBounds.width > 10;
}
}
return false;
}
public void run() {
while (canContinue()) {
long lStartedAt = System.currentTimeMillis();
shell.getDisplay().syncExec(new AERunnable() {
public void runSupport() {
if (shell == null || shell.isDisposed()) {
return;
}
if (shellBounds == null) {
shellBounds = shell.getBounds();
}
int delta;
if (slideIn) {
switch (direction) {
case SWT.UP:
delta = Math.min(endBounds.height - shellBounds.height, STEP);
shellBounds.height += delta;
delta = Math.min(shellBounds.y - endBounds.y, STEP);
shellBounds.y -= delta;
break;
default:
break;
}
} else {
switch (direction) {
case SWT.RIGHT:
delta = Math.min(shellBounds.width, STEP);
shellBounds.width -= delta;
shellBounds.x += delta;
if (shellBounds.width == 0) {
shell.dispose();
return;
}
break;
default:
break;
}
}
shell.setBounds(shellBounds);
shell.update();
}
});
try {
long lDrawTime = System.currentTimeMillis() - lStartedAt;
long lSleepTime = PAUSE - lDrawTime;
if (lSleepTime < 15) {
double d = (lDrawTime + 15.0) / PAUSE;
PAUSE *= d;
STEP *= d;
lSleepTime = 15;
}
Thread.sleep(lSleepTime);
} catch (Exception e) {
}
}
}
}
private class PopupParams {
int iconID;
String title;
String text;
String details;
long addedOn;
/**
* @param iconID
* @param title
* @param text
* @param details
*/
public PopupParams(int iconID, String title, String text, String details) {
this.iconID = iconID;
this.title = title;
this.text = text;
this.details = details;
addedOn = System.currentTimeMillis();
}
}
/**
* Test
*
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
Shell shell = new Shell(display, SWT.DIALOG_TRIM);
shell.setLayout(new FillLayout());
Button btn = new Button(shell, SWT.PUSH);
btn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
test(display);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public static void test(Display display) {
ImageRepository.loadImages(display);
String title = "This is the title that never ends, never ends!";
String text = "This is a very long message with lots of information and "
+ "stuff you really should read. Are you still reading? Good, because "
+ "reading <a href=\"http://moo.com\">stimulates</a> the mind and grows "
+ "hair on your chest.\n\n Unless you are a girl, then it makes you want "
+ "to read more. It's an endless cycle of reading that will never "
+ "end. Cursed is the long text that is in this test and may it fill"
+ "every last line of the shell until there is no more.";
// delay before running, to give eclipse time to finish up it's work
// Otherwise, Mr Slidey is jumpy
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// MessagePopupShell shell = new MessagePopupShell(display,
// MessagePopupShell.ICON_INFO, "Title", text, "Details");
new MessageSlideShell(display, SWT.ICON_INFORMATION,
"Simple. . . . . . . . . . . . . . . . . . .", "Simple", (String) null);
new MessageSlideShell(display, SWT.ICON_INFORMATION, title + "1", text,
"Details: " + text);
new MessageSlideShell(display, SWT.ICON_INFORMATION, "ShortTitle2",
"ShortText", "Details");
MessageSlideShell.waitUntilClosed();
new MessageSlideShell(display, SWT.ICON_INFORMATION, "ShortTitle3",
"ShortText", (String) null);
for (int x = 0; x < 10; x++)
text += "\n\n\n\n\n\n\n\nWow";
new MessageSlideShell(display, SWT.ICON_INFORMATION, title + "4", text,
"Details");
new MessageSlideShell(display, SWT.ICON_ERROR, title + "5", text,
(String) null);
MessageSlideShell.waitUntilClosed();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -