SWT Patterns.
SWT Widgets.
/*******************************************************************
* Rosella Software.
* Data Mining, Database & Analytics: www.roselladb.com
*******************************************************************/
package rosella.swtexample;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.printing.*;
import org.eclipse.swt.browser.*;
import rosella.swtpatterns.*;
/*
* This shows example usage of SWT Design Patterns.
*
*/
public class SWTExample extends SWTFrame {
// location of gif files in the jar file.
static String gifbase = "/rosella/swtexample/";
public static void main(String[] args) {
// here your housekeeping codes!
/*
* All SWT Pattern programs must invoke program as follows
* after substituting "SWTExample" to your program class name
* and application and the window icon file.
*/
SWTDevice.setup(SWTExample.class,
"Example Patterns",
gifbase+"rosellatte.gif");
Shell shell = new SWTExample();
SWTDevice.runShell(shell);
// here your cleanup codes!
System.exit(0);
}
// widget anchors
SWTTabFolder tabfolder;
SWTList list;
SWTTextArea text;
SWTLabel xmsg;
SWTTable table;
SWTTree tree;
SWTProgressBar bar = new SWTProgressBar(this, SWT.NONE);
SWTCanvas1 db;
SWTBrowser browser;
SWTButton forward, backward;
// menu and menuitems
SWTMenu mfile, medit;
SWTMenuItem xfile, xdir, xprint, xhover, xdiag, xexit;
SWTMenuItem xcut, xpaste;
SWTToolBar toolbar;
public SWTExample() {
super("SWT Design Pattern Example");
SWTMenu sub;
SWTSash sash1, sash2;
SWTTabItem item;
/*
* create menubar and add menus.
*/
setupMenuBar();
mfile = addMainMenu("File");
// cascade sub-menu
sub = addSubMenu(mfile, "Choose ...");
xfile = addMenuItem(sub, "file", this);
xdir = addMenuItem(sub, "directory", this);
xprint = addMenuItem(mfile, "print", this);
xhover = addMenuItem(mfile, "hover", this);
xdiag = addMenuItem(mfile, "dialog", this);
addMenuItemSeparator(mfile);
xexit = addMenuItem(mfile, "Exit", this);
// second menu.
medit = addMainMenu("Edit");
xcut = addMenuItem(medit, "Cut", gifbase+"cut.gif", this);
xpaste = addMenuItem(medit, "Paste", gifbase+"paste.gif", this);
/*
* create toolbar and add items.
*/
toolbar = setupToolBar(SWT.HORIZONTAL | SWT.WRAP |
SWT.FLAT | SWT.SHADOW_OUT);
addToolItem("Copy", gifbase+"copy.gif", SWT.FLAT, this);
addToolItem("Cut", gifbase+"cut.gif", SWT.FLAT, this);
addToolItem("Paste", gifbase+"paste.gif", SWT.FLAT, this);
/*
* create list selection and add font names.
*/
list = new SWTList(this, SWT.NONE, this);
list.setItems(SWTDevice.getFontNames(true));
/*
* create list selection and add items.
*/
tabfolder = new SWTTabFolder(this, SWT.BOTTOM, this);
item = tabfolder.addItem("Empty Tab"); // empty tab!
// temporary variables.
SWTTreeItem sitem;
SWTTableItem titem;
SWTTableColumn col1, col2, col3;
String[] data1 = {"dd1", "dd2", "dd3"};
String[] data2 = {"ee1", "ee2", "ee3"};
/*
* second tab containing a table.
*/
item = tabfolder.addItem("Table");
table = new SWTTable(tabfolder, SWT.BORDER |
SWT.CHECK | SWT.FULL_SELECTION, this);
item.setControl(table);
// column definitions.
col1 = table.addColumn("first");
col2 = table.addColumn("second");
col3 = table.addColumn("third");
// add three rows! first two with data. third without data.
titem = table.addItem(data1);
titem = table.addItem(data2);
titem = table.addItem(null, null);
titem.setText(0, "rr1"); // add manually!
titem.setText(1, "rr2"); // add manually!
titem.setText(2, "rr3"); // add manually!
// assemble and setup properties.
col1.pack();
col2.pack();
col3.pack();
table.pack();
table.setHeaderVisible(true);
/*
* second tab containing a tree.
*/
item = tabfolder.addItem("Tree");
tree = new SWTTree(tabfolder, SWT.BORDER | SWT.SINGLE | SWT.CHECK,
this);
item.setControl(tree);
// fill the tree with sub items.
sitem = tree.addItem("level1", gifbase+"rosellatte.gif"); // 1st item.
sitem.addItem("level1a");
sitem.addItem("level1b");
sitem.addItem("level1c");
sitem = tree.addItem("level2", gifbase+"rosellatte.gif"); // 2nd item.
sitem.addItem("level2a");
sitem.addItem("level2b");
sitem.addItem("level2c");
/*
* next tab containing a group.
*/
item = tabfolder.addItem("Group", gifbase+"rosellatte.gif");
SWTGroup group = new SWTGroup(tabfolder, SWT.NONE, "Rosella Group");
item.setControl(group);
// add group content using RowLayout.
group.setLayout(new RowLayout(SWT.VERTICAL));
new SWTLabel(group, SWT.NONE, "group example");
new SWTLabel(group, SWT.NONE, "second label");
new SWTLabel(group, SWT.NONE, "third label");
group.pack();
/*
* next tab containing a canvas painting example.
* see the implementation of the class.
*/
item = tabfolder.addItem("Paint");
db = new SWTCanvas1(tabfolder, this);
item.setControl(db);
/*
* next tab containing a browser.
*/
int lp = 2;
int mp = 3;
SWTComposite comp;
item = tabfolder.addItem("Browser");
comp = new SWTComposite(tabfolder, SWT.NONE);
item.setControl(comp);
try {
// create browser with forward and backward buttons.
browser = new SWTBrowser(comp, SWT.NONE);
backward = browser.getBackwardButton(comp,
SWT.PUSH | SWT.FLAT,
"BACKWARD", null);
forward = browser.getForwardButton(comp,
SWT.PUSH | SWT.FLAT,
"FORWARD", null);
browser.setSize(500, 500);
// place buttons and browser on the tab panel.
// see the examples later on!
attachFormLayout(comp);
layForm(forward, DEFAULT,DEFAULT,DEFAULT,FULL, 0, 0, 0, 0);
layForm(backward, DEFAULT,DEFAULT,DEFAULT,forward, 0, 0, 0, 0);
layForm(browser, forward,PERCENT,FULL,FULL, mp, 0, 0, 0);
// place initial content!
browser.setUrl("http://localhost/Working/roselladb/index.htm");
// browser.setText("This us a example");
} catch (Throwable t1) {
t1.printStackTrace();
}
/*
* two sashes are used!
*/
sash1 = new SWTSash(this, SWT.HORIZONTAL);
sash2 = new SWTSash(this, SWT.VERTICAL);
/*
* the followings are extra control element.
*/
text = new SWTTextArea(this, SWT.NONE, "loggin\n", 3, 50);
xmsg = new SWTLabel(this, SWT.NONE, "Ready.");
/*
* Finally assemble the main components using form layout.
*/
FormLayout layout = attachFormLayout(this);
layout.marginWidth = lp;
layout.marginHeight = mp;
layForm(toolbar, PERCENT,PERCENT,DEFAULT,FULL, 0, 0, 0, 0);
layForm(list, toolbar, PERCENT,sash1,sash2, lp, 0, 0, 0);
layForm(sash1, OFFSET,PERCENT,DEFAULT,sash2, 200,0,0,0);
layForm(tabfolder, sash1, PERCENT,bar,sash2, 0, 0, -mp, 0);
layForm(sash2, toolbar,OFFSET,bar,DEFAULT, lp,400,-lp,0);
layForm(text, toolbar,sash2,bar,FULL, lp, 0, -mp, 0);
layForm(xmsg, DEFAULT,PERCENT,FULL,bar, 0, 0, 0, 0);
layForm(bar, DEFAULT,DEFAULT,FULL,FULL, 0, 0, 0, 0);
pack();
setSize(800, 500);
setLocation(10, 20);
show();
// set initial focus.
list.setFocus();
}
/**
* Implements SelectionListener
* SWT.DefaultSelection, SWT.Selection,
*/
public void widgetSelected(SelectionEvent evt) {
if (evt.widget==null) return;
try {
text.append("\nS="+evt);
} catch (Throwable t) {
t.printStackTrace();
try {
text.append("\nW=");
} catch (Throwable t1) {
}
}
/*
* Exit programs.
*/
if (evt.widget==xexit) {
dispose();
return;
}
/*
* List event handling.
*/
if (evt.widget==list) {
db.repaint();
return;
}
/*
* Hover. Not used!
*/
if (evt.widget==xhover) {
SWTHover hover = new SWTHover();
hover.setSize(200, 300);
hover.setLocation(1, 10);
hover.open();
return;
}
/*
* File chooser example.
*/
if (evt.widget==xfile) {
FileDialog dialog = new FileDialog(this, SWT.SAVE);
dialog.setText("Select save file");
dialog.setFilterPath(System.getProperty("user.dir"));
dialog.setFilterExtensions(new String[]{"*.txt","*.*"});
dialog.setFilterNames(new String[]{"*.txt","*.*"});
dialog.setFileName("unnown.txt");
String path = dialog.open(); // returns path name
if (path==null) {
return;
}
text.append("\n"+path);
text.append("\n"+dialog.getFileName());
return;
}
/*
* Directory chooser example.
*/
if (evt.widget==xdir) {
DirectoryDialog dialog = new DirectoryDialog(this);
dialog.setText("Select save file");
dialog.setMessage("Select directory to sabe");
// dialog.setFilterPath(System.getProperty("user.dir"));
String path = dialog.open(); // returns path name
if (path==null) {
return;
}
text.append("\n"+path);
text.append("\n"+dialog.getFilterPath());
xmsg.setText("done...");
return;
}
/*
* Dialog event handling example.
*/
if (evt.widget==xdiag) {
SWTDialog1 dialog = new SWTDialog1(this, this);
dialog.open();
return;
}
/*
* Printing example.
*/
if (evt.widget==xprint) {
PrinterData[] pd = Printer.getPrinterList();
for (int i=0; i < pd.length; i++) {
text.append("\n"+pd[i].toString());
}
PrintDialog dialog = new PrintDialog(this, SWT.NONE);
dialog.setText("Select print device");
dialog.setScope(PrinterData.ALL_PAGES);
dialog.setPrintToFile(false);
PrinterData pdata = dialog.open();
if (pdata==null) {
return;
}
Printer printer = new Printer(pdata);
GC gc = new GC(printer);
Rectangle dm = printer.getClientArea();
Point dpi = printer.getDPI();
text.append("\ndpi="+dpi);
text.append("\ndm="+dm);
try {
while (true) { // a strange way of doing!
if (!printer.startJob("Jobname")) break;
if (!printer.startPage()) break;
gc.drawString("TTTTT", 30, 30);
printer.endPage();
if (!printer.startPage()) break;
gc.drawString("KKK", 30, 30);
printer.endPage();
printer.endJob();
break;
}
} catch (Throwable t) {
}
gc.dispose();
xmsg.setText("done...");
return;
}
}
}
/*
* SWTDialog implementation example. This has OK button!
*/
public class SWTDialog1 extends SWTDialog {
SWTExample m;
SWTButton button;
SWTDialog1(Shell parent, SWTExample m0) {
super(parent, SWT.NONE, "Dialog Example");
button = new SWTButton(this, SWT.NONE, this, "OK");
FormLayout layout = attachFormLayout(this);
layout.marginWidth = 3;
layout.marginHeight = 3;
layForm(button, PERCENT,PERCENT,PERCENT,PERCENT, 0, 0, 100, 100);
pack();
setSize(200, 300);
m = m0;
}
/*
* Event handling occuring inside this dialog.
*/
public void widgetSelected(SelectionEvent evt) {
if (evt.widget==null) return;
try {
m.text.append("\nK="+evt);
} catch (Throwable t) {
t.printStackTrace();
try {
m.text.append("\nK=");
} catch (Throwable t1) {
}
}
/*
* When the buttone is pressed, dispose item.
*/
if (evt.widget==button) {
// set return object before returning if needed!
// returnObject = yourobject;
dispose();
return;
}
}
}
/*
* SWTCanvas example.
*/
public class SWTCanvas1 extends SWTCanvas {
SWTExample m;
SWTCanvas1(Composite parent, SWTExample m0) {
super(parent, SWT.NONE);
m = m0;
}
/*
* Note that you must extend and implement this method!
*/
protected void painted(GC gc, Rectangle dm) {
Color c1 = createColor(0,0,0);
Color c2 = createColor(255,0,0);
Font f1 = null;
String st = null;
int k = m.list.getSelectionIndex();
if (k < 0) {
f1 = createFont("Arial", 24, SWT.BOLD);
st = "+default";
} else {
st = m.list.getItem(k);
f1 = createFont(st, 24, SWT.BOLD);
}
gc.setBackground(c1);
gc.setFont(f1);
// gc.fillRectangle(dm.x, dm.y, dm.width, dm.height);
gc.setForeground(c2);
gc.drawString(st+dm, 10, 10);
gc.drawLine(10, 70, 100, 70);
}
}
SWT Patterns.
SWT Widgets.
|