/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fm.ak.server;
import java.util.concurrent.CountDownLatch;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
/**
* Singleton
*
* @author Petre
*/
public final class UI implements Runnable {
static String ENDL = "\n";
Display display;
Shell shell;
//Label status;
String gstatus;
public Text hlog;
Tray tray;
TrayItem itray;
Image lrgcon;
Image smlcon;
boolean gb;
Text command;
String log;
String gcommit = "";
GridData gridData;
CountDownLatch latch;
Font monospace;
boolean endl = false;
int gcolor;
UI(CountDownLatch o) {
latch = o;
}
/**
* Creates the screen.
*/
@Override
public void run() {
display = new Display();
monospace = new Font(display, "Courier new", 9, 0);
shell = new Shell(display);
shell.setText("AK Server");
shell.setSize(500, 300);
center(shell);
shell.setLayout(new GridLayout(3, false));
//Image bg = new Image(display, "bg.png");
//shell.setBackgroundImage(bg);
shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
smlcon = new Image(display, "16.png");
lrgcon = new Image(display, "32.png");
//icong = new Image(display, "icong.gif");
//iconb = new Image(display, "iconb.gif");
gridData = new GridData(SWT.LEFT, SWT.NONE, false, false, 3, 0);
//Label logo = new Label(shell, SWT.IMAGE_PNG);
//logo.setImage(lrgcon);
final Image[] images = new Image[] { smlcon, lrgcon };
shell.setImages(images);
/*status = new Label(shell, SWT.SINGLE ); // SWT.NO_BACKGROUND | SWT.ALPHA | SWT.DRAW_TRANSPARENT | SWT.TRANSPARENT
gridData = new GridData(SWT.LEFT, SWT.NONE, false, false, 3, 0);
status.setLayoutData(gridData);
status.setForeground(display.getSystemColor(SWT.COLOR_DARK_GRAY));
status.setText("Status: Not started");*/
// log
hlog = new Text(shell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY | SWT.BORDER);
gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 0);
hlog.setLayoutData(gridData); // illegal
hlog.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
hlog.setBackground(display.getSystemColor(SWT.COLOR_DARK_RED));
hlog.addListener(SWT.Modify, new Listener() {
@Override
public void handleEvent(Event e) {
hlog.setTopIndex(hlog.getLineCount() - 1);
}
});
// label
Label here = new Label(shell, SWT.SINGLE);
here.setText("Command:");
// input
command = new Text(shell, 0);
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
//command.setToolTipText("If you're lost, try typing /help");
gridData.minimumWidth = 100;
command.setFont(monospace);
command.setLayoutData(gridData);
command.setText("/help");
command.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
// button
Button execute = new Button(shell, SWT.PUSH);
//execute.setToolTipText("Run the command on the left (or press enter).");
execute.setText("Parse Command");
Tray tray = display.getSystemTray();
if(tray != null) {
itray = new TrayItem(tray, SWT.NONE);
itray.setToolTipText("No");
itray.setImage(smlcon);
}
//shell.setImage(iconb);
// release waiting main
latch.countDown();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
itray.dispose();
System.exit(0);
}
public void commit(String quote) {
gcommit += quote;
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
hlog.append(gcommit);
gcommit = "";
}
});
if (endl) {
gcommit += ENDL;
endl = false;
}
}
public void commitln(String quote) {
endl = true;
//quote += Scherm.endl;
commit(quote);
}
public void center(Shell shell) {
Rectangle bds = shell.getDisplay().getBounds();
Point p = shell.getSize();
int nLeft = (bds.width - p.x) / 2;
int nTop = (bds.height - p.y) / 2;
shell.setBounds(nLeft, nTop, p.x, p.y);
}
public void goodbad(final boolean good) {
final int c = (good) ? SWT.COLOR_DARK_GREEN : SWT.COLOR_DARK_RED;
final Image a = smlcon;//(good) ? icong : iconb;
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
hlog.setBackground(display.getSystemColor(c));
}
});
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
itray.setImage(a);
//shell.setImage(a);
}
});
}
public void status(String s) {
final String a = "Status: " + s;
final String b = s;
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
//status.setText(a);
itray.setToolTipText("AK Server: " + b);
}
});
}
}