Skip to content

Commit

Permalink
opt linux platform notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamling committed Apr 10, 2018
1 parent 31292af commit 8861058
Showing 1 changed file with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package cn.ieclipse.smartim.common;

import java.awt.Insets;
import java.awt.Toolkit;
import java.lang.reflect.Method;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;

import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
Expand Down Expand Up @@ -33,34 +40,45 @@ public class Notifications extends Shell {
public static void main(String args[]) {
try {
final Display display = Display.getDefault();
Notifications.getInstance(new Shell(display)).setMock(true)
.setMessage(null, "test");
IMPlugin.runOnUI(new Runnable() {

@Override
public void run() {
System.out.println("UI Thread");
// Notifications.getInstance(new Shell(display)).setMock(true)
// .setMessage(null, "test");
// Notifications.notify("title", "message");
Notifications instance = new Notifications(display);
instance.open();
instance.layout();
instance.setLocation(100, 100);
instance.setVisible(true);
instance.setMessage("title", "message");
while (!instance.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
});
if (!display.readAndDispatch()) {
display.sleep();
}
} catch (Exception e) {
e.printStackTrace();
}
}

private Label fIcon;
private Label fTitle;
private Label fText;
private TranslateAnimation ta;
private Timer timer;

/**
* @wbp.parser.constructor
*/
public Notifications(Display display) {
this(display, SWT.ON_TOP | SWT.CLOSE | SWT.TITLE);
}

/**
* Create the shell.
*
* @param display
*/
public Notifications(Display display) {
super(display, SWT.ON_TOP | SWT.CLOSE | SWT.TITLE);
public Notifications(Display display, int style) {
super(display, style);
try {
Class<?> clazz = Class.forName("org.eclipse.swt.internal.win32.OS");
if (clazz != null) {
Expand Down Expand Up @@ -89,6 +107,24 @@ protected void createContents() {
setSize(300, 150);

setLayout(new GridLayout(2, false));
if (Platform.OS_LINUX.equals(Platform.getOS()))
{
fIcon = new Label(this, SWT.NONE);
fIcon.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
fIcon.setImage(SWTResourceManager.getImage(org.eclipse.ui.IWorkbench.class,
"/icons/full/etool16/delete_edit.png"));
fIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
setVisible(false);
}
});

fTitle = new Label(this, SWT.NONE);
fTitle.setText("通知");
fTitle.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
}

fText = new Label(this, SWT.WRAP);
// fText.setEditable(false);
Expand Down Expand Up @@ -121,7 +157,10 @@ public void widgetSelected(SelectionEvent e) {
}
});

Insets sd = Toolkit.getDefaultToolkit().getScreenInsets(
new JFrame().getGraphicsConfiguration());
Rectangle screen = Display.getDefault().getClientArea();
screen.height = screen.height - sd.bottom;
ta = new TranslateAnimation(screen.width, screen.width - getSize().x,
screen.height, screen.height - getSize().y).setTarget(this)
.setDuration(300);
Expand Down Expand Up @@ -179,6 +218,9 @@ private void doSetMessage(String title, CharSequence text) {
}
if (title != null) {
this.setText(title);
if (fTitle != null) {
fTitle.setText(title);
}
}
this.fText.setText(text == null ? "" : text.toString());
}
Expand Down

0 comments on commit 8861058

Please sign in to comment.