Thank you, all committers and contributors, for your work on the Nebula project in 2013. We wish you a Merry Christmas and a Happy New Year.
Here is a little Christmas gift that enables you to display a mini message. Enjoy.
/*******************************************************************************
* Copyright (c) 2013 The Eclipse Nebula Project
* All rights reserved. This program is made available under the terms
* of the Eclipse Public License v1.0 which does not accompany this
*
* Contributors:
*******************************************************************************/
package nl.remain.td.core.ui;
import java.awt.MouseInfo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* Shows a mini message just above the current mouse pointer. It is for those
* cases when you want to supply a quick feedback to the user without going
* through the workflow of showing a dialog and letting the user press Ok.
* <p/>
* Usage:
* <p/>
* <code>
* new MiniMessage(2000).open("Saved!");
* </code>
*
* @author Wim Jongman
*
*/
public class MiniMessage {
private StyledText fStyledText;
private Shell fShell;
private int fWaitBeforeFade = 0;
/**
* Constructs a {@link MiniMessage}.
*
* @param waitBeforeFade
* waits n milliseconds before the message starts to fade. Fading
* itself takes about a second.
*/
public MiniMessage(int waitBeforeFade) {
this.fWaitBeforeFade = waitBeforeFade;
}
/**
* Main method for testing.
*
* @param args
*/
public static void main(String[] args) {
MiniMessage miniMessage = new MiniMessage(2000);
miniMessage.openWait("Thanks to all Nebula committers and contributors ..");
miniMessage.openWait("for all your work in 2013.");
miniMessage.openWait("Merry Christmas and a Happy New Year!");
}
/**
* Opens the {@link MiniMessage} just above the current cursor position
* showing the passed text. It does not wait until the message has faded.
*
* @param text
* the text to show as the mini message.
*
* @see #openWait(String)
*/
public void openWait(String message) {
doOpen(message, true);
}
/**
* Opens the {@link MiniMessage} just above the current cursor position
* showing the passed text. It does not wait until the message has faded.
*
* @param text
* the text to show as the mini message.
*
* @see #openWait(String)
*/
public void open(String text) {
doOpen(text, false);
}
private void doOpen(String text, boolean wait) {
try {
Display display = Display.getDefault();
fShell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP | SWT.NO_FOCUS);
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.marginWidth = 1;
gridLayout.marginHeight = 1;
gridLayout.verticalSpacing = 0;
gridLayout.horizontalSpacing = 0;
createContents(fShell, SWT.NONE);
setText(text);
Control focusControl = display.getFocusControl();
fShell.setLayout(gridLayout);
fShell.pack();
fShell.setLocation(new Point(MouseInfo.getPointerInfo().getLocation().x, MouseInfo
.getPointerInfo().getLocation().y - fShell.getSize().y));
fShell.open();
fShell.layout();
if (focusControl != null)
focusControl.forceFocus();
Thread thread = new Thread(new Fader(fWaitBeforeFade));
thread.run();
while (wait && !fShell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void setText(String string) {
fStyledText.setText(string);
}
/**
* Create the composite.
*
* @param parent
* @param style
*/
private void createContents(Composite parent, int style) {
parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 2;
gridLayout.marginHeight = 2;
parent.setLayout(gridLayout);
fStyledText = new StyledText(parent, SWT.BORDER);
fStyledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
fStyledText.setEditable(false);
fStyledText.setEnabled(false);
}
private class Fader implements Runnable {
int fAlpha = 255;
private int fTimerWait = 10;
private int fWaitBeforeFade = 0;
public Fader(int waitBeforeFade) {
this.fWaitBeforeFade = waitBeforeFade;
}
public void run() {
if (fWaitBeforeFade > 0) {
fShell.getDisplay().timerExec(fWaitBeforeFade, this);
fWaitBeforeFade = 0;
} else if (fAlpha <= 5) {
fShell.dispose();
} else {
fAlpha -= 5;
if (fAlpha < 255) {
fShell.setAlpha(fAlpha);
}
fShell.getDisplay().timerExec(fTimerWait, this);
}
}
};
}