Java/SWT JFace Eclipse/Form
Custom Components
<source lang="java">
/*******************************************************************************
* All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie * * Created on 2004-6-14 10:55:42 by JACK $Id$ * ******************************************************************************/
import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.forms.HyperlinkGroup; import org.eclipse.ui.forms.events.ExpansionAdapter; import org.eclipse.ui.forms.events.ExpansionEvent; import org.eclipse.ui.forms.events.HyperlinkAdapter; import org.eclipse.ui.forms.events.HyperlinkEvent; import org.eclipse.ui.forms.events.IHyperlinkListener; import org.eclipse.ui.forms.widgets.ExpandableComposite; import org.eclipse.ui.forms.widgets.Form; import org.eclipse.ui.forms.widgets.FormText; import org.eclipse.ui.forms.widgets.FormToolkit; import org.eclipse.ui.forms.widgets.Hyperlink; import org.eclipse.ui.forms.widgets.ImageHyperlink; import org.eclipse.ui.forms.widgets.Section; import org.eclipse.ui.forms.widgets.TableWrapLayout; public class CustomWidgets extends ApplicationWindow {
FormToolkit toolkit; Form form; /** * @param parentShell */ public CustomWidgets(Shell parentShell) { super(parentShell); } private void demoSections() { form.getBody().setLayout(new TableWrapLayout()); Section section = toolkit.createSection(form.getBody(), Section.DESCRIPTION | Section.TREE_NODE | Section.EXPANDED); section.setText("This is the title"); toolkit.createCompositeSeparator(section); section.setDescription("-= This is a description -="); FormText text = toolkit.createFormText(section, false); text.setText( "This is a long text. The user can show or hide this text " + "by expanding or collapsing the expandable composite.", false, false); section.setClient(text); } private void demoExpandableComposite() { form.getBody().setLayout(new TableWrapLayout()); ExpandableComposite ec1 = toolkit.createExpandableComposite( form.getBody(), ExpandableComposite.TREE_NODE | ExpandableComposite.EXPANDED); ec1.setText("This is the title"); FormText text = toolkit.createFormText(ec1, false); text.setText( "This is a long text. The user can show or hide this text " + "by expanding or collapsing the expandable composite.", false, false); ec1.setClient(text); ec1.addExpansionListener(new ExpansionAdapter() { public void expansionStateChanged(ExpansionEvent e) { // resizes the application window. getShell().pack(true); } }); } private void demoFormTextXML() { form.getBody().setLayout(new TableWrapLayout()); FormText text = toolkit.createFormText(form.getBody(), true); Image image = new Image(form.getDisplay(), "icons/eclipse0.gif"); text.setImage("eclipse", image); text.setText( "<form>"+ "
<img href=\"eclipse\"/> Eclipse Projects:
" + "+ "</form>", true, false); } private void demoFormTextNormal() { form.getBody().setLayout(new TableWrapLayout()); FormText text = toolkit.createFormText(form.getBody(), true); // text.setLayoutData(new TableWrapData(TableWrapData.FILL)); text.setText( "Eclipse is a kind of universal tool platform - an open extensible " + "IDE for anything and nothing in particular. For more details, please " + "visit http://www.eclipse.org for more details.", false, false); } private void demoFormTextURL() { form.getBody().setLayout(new TableWrapLayout()); FormText text = toolkit.createFormText(form.getBody(), true); HyperlinkGroup group = new HyperlinkGroup(form.getDisplay()); group.setForeground(form.getDisplay().getSystemColor(SWT.COLOR_BLUE)); group.setActiveForeground( form.getDisplay().getSystemColor(SWT.COLOR_BLUE)); text.setHyperlinkSettings(group); text.setText( "Eclipse is a kind of universal tool platform - an open extensible " + "IDE for anything and nothing in particular. For more details, please " + "visit http://www.eclipse.org web site.", false, true); text.addHyperlinkListener(new HyperlinkAdapter() { public void linkActivated(HyperlinkEvent e) { System.out.println("Link activated: " + e.getHref()); } }); } private void demoHyperlinks() { form.getBody().setLayout(new GridLayout()); Hyperlink hyperlink = toolkit.createHyperlink( form.getBody(), "This is a hyperlink to Eclipse.org", SWT.NULL); hyperlink.setHref("http://www.eclipse.org"); hyperlink.setForeground( getShell().getDisplay().getSystemColor(SWT.COLOR_BLUE)); hyperlink.addHyperlinkListener(new IHyperlinkListener() { public void linkEntered(HyperlinkEvent e) { System.out.println("Mouse entered."); } public void linkExited(HyperlinkEvent e) { System.out.println("Mouse left."); } public void linkActivated(HyperlinkEvent e) { System.out.println("Hyperlink activated."); System.out.println("HREF = " + e.getHref()); } }); ImageHyperlink imageHyperlink = toolkit.createImageHyperlink(form.getBody(), SWT.NULL); imageHyperlink.setText("This is an image hyperlink."); imageHyperlink.setForeground( getShell().getDisplay().getSystemColor(SWT.COLOR_BLUE)); imageHyperlink.setImage( new Image(getShell().getDisplay(), "icons/eclipse0.gif")); imageHyperlink.addHyperlinkListener(new HyperlinkAdapter() { public void linkActivated(HyperlinkEvent e) { System.out.println("Image hyperlink activated."); } }); HyperlinkGroup group = new HyperlinkGroup(getShell().getDisplay()); group.add(hyperlink); group.add(imageHyperlink); group.setActiveBackground( getShell().getDisplay().getSystemColor(SWT.COLOR_YELLOW)); group.setActiveForeground( getShell().getDisplay().getSystemColor(SWT.COLOR_RED)); group.setForeground( getShell().getDisplay().getSystemColor(SWT.COLOR_BLUE)); } /* * (non-Javadoc) * * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.ruposite) */ protected Control createContents(Composite parent) { Composite composite = new Composite(parent, SWT.NULL); composite.setLayout(new FillLayout()); // Sets up the toolkit. toolkit = new FormToolkit(getShell().getDisplay()); // Creates a form instance. form = toolkit.createForm(composite); form.setLayoutData(new GridData(GridData.FILL_BOTH)); // Sets title. form.setText("Custom Form Widgets Demo"); // demoHyperlinks(); // demoFormTextNormal(); // demoFormTextURL(); // demoFormTextXML(); // demoExpandableComposite(); demoSections(); return composite; } public static void main(String[] args) { CustomWidgets win = new CustomWidgets(null); win.setBlockOnOpen(true); win.open(); }
}
</source>
Email Form
<source lang="java">
/******************************************************************************
* All Right Reserved. * Copyright (c) 1998, 2004 Jackwind Li Guojie * * Created on 2004-6-13 18:19:20 by JACK * $Id$ * *****************************************************************************/
import org.eclipse.jface.action.Action; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.forms.widgets.FormToolkit; import org.eclipse.ui.forms.widgets.ScrolledForm; public class EmailForm extends ApplicationWindow {
/** * @param parentShell */ public EmailForm(Shell parentShell) { super(parentShell); }
/* (non-Javadoc) * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.ruposite) */ protected Control createContents(Composite parent) { Composite composite = new Composite(parent, SWT.NULL); composite.setLayout(new FillLayout()); // Sets up the toolkit. FormToolkit toolkit = new FormToolkit(getShell().getDisplay()); // Creates a form instance. // Form form = toolkit.createForm(composite); ScrolledForm form = toolkit.createScrolledForm(composite); form.setLayoutData(new GridData(GridData.FILL_BOTH)); // Sets title. form.setText("Composing an Email Message"); // Adds body contents. form.getBody().setLayout(new GridLayout(2, false)); Label label = toolkit.createLabel(form.getBody(), "To: ", SWT.NULL); Text textTo = toolkit.createText(form.getBody(), ""); textTo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); label = toolkit.createLabel(form.getBody(), "Subject: ", SWT.NULL); Text textSubject = toolkit.createText(form.getBody(), ""); textSubject.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); label = toolkit.createLabel(form.getBody(), "Message: ", SWT.NULL); Text textMessage = toolkit.createText(form.getBody(), ""); textMessage.setLayoutData(new GridData(GridData.FILL_BOTH)); label = toolkit.createLabel(form.getBody(), "Option: ", SWT.NULL); Button buttonOption = toolkit.createButton(form.getBody(), "save a copy", SWT.CHECK); Button buttonClose = toolkit.createButton(form.getBody(), "Close", SWT.PUSH); GridData gridData = new GridData(); gridData.horizontalSpan = 2; gridData.horizontalAlignment = GridData.END; buttonClose.setLayoutData(gridData); // Button button = toolkit.createButton(form.getBody(), "Test", SWT.NULL); // Adds tool bar items. form.getToolBarManager().add(new Action("Send") { public void run() { System.out.println("Sending email ..."); } }); form.getToolBarManager().add(new Action("Cancel") { public void run() { System.out.println("Cancelled."); } }); form.updateToolBar(); return composite; } public static void main(String[] args) { EmailForm emailForm = new EmailForm(null); emailForm.setBlockOnOpen(true); emailForm.open(); }
}
</source>
HTML Form
<source lang="java">
/*******************************************************************************
* All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie * * Created on 2004-6-11 15:12:57 by JACK $Id$ * ******************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.forms.widgets.Form; import org.eclipse.ui.forms.widgets.FormText; import org.eclipse.ui.forms.widgets.FormToolkit; import org.eclipse.ui.forms.widgets.Hyperlink; import org.eclipse.ui.forms.widgets.Section; public class SWTTest {
private FormToolkit toolkit; private Form form; private Display display; private Shell shell; private Hyperlink link; private Section section1, section2, section3; private FormText rtext; private Composite client1, client2, client3; private Text text; private Button button2; private Label label; static public void main(String args[]) { new SWTTest().run(); } private void run() { setupShell(); setupToolkit(); createFormStructure(); addLayout(); addHooks(); shell.pack(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } private void createFormStructure() { // form form = toolkit.createForm(shell); form.setText("Eclipse Forms"); Button button = toolkit.createButton(form.getBody(), "Test", SWT.NULL); form.getBody().setLayout(new GridLayout()); //form.setBackgroundImage(new Image(display, "jexp.gif")); } private String getHTML() { StringBuffer buf = new StringBuffer(); buf.append("<form>");buf.append("
"); buf.append("Here is some plain text for the text to render; "); buf.append( "this text is at web site."); buf.append("
"); buf.append(""); buf.append( "This text is in header font and color."); buf.append("
");buf.append("
This line will contain some bold and some source text. "); buf.append("We can also add <img href=\"image\"/> an image. "); buf.append("
"); buf.append("buf.append("
buf.append("
buf.append("
buf.append("
buf.append("
buf.append("</form>"); return buf.toString(); } private void setupToolkit() { toolkit = new FormToolkit(display); } private void setupShell() { display = new Display(); shell = new Shell(display); shell.open(); } private void addLayout() { // shell shell.setLayout(new FillLayout()); //form
// form.getBody().setLayout(new TableWrapLayout()); // // section1.setLayoutData(new TableWrapData(TableWrapData.FILL)); // // section2.setLayoutData(new TableWrapData(TableWrapData.FILL)); // // section3.setLayoutData(new TableWrapData(TableWrapData.FILL)); // // // client1 // // client1.setLayout(new GridLayout()); // // // client2 // // client2.setLayout(new GridLayout()); // // // // client3 // // GridLayout layout = new GridLayout(); // // client3.setLayout(layout); // // layout.numColumns = 2; // // // client3->text // // text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // // // client3->button2 // // GridData gd = new GridData(); // // gd.horizontalSpan = 2; // // button2.setLayoutData(gd);
} private void addHooks() {
// section1.addExpansionListener(new ExpansionAdapter() { // // public void expansionStateChanged(ExpansionEvent e) { // // System.out.println("expansionbutton clicked!"); // // } // // }); // // link.addHyperlinkListener(new HyperlinkAdapter() { // // public void linkActivated(HyperlinkEvent e) { // // System.out.println("Link active: " + e.getLabel()); // // } // // });
}
}
</source>
Simple Form 1
<source lang="java">
/******************************************************************************
* All Right Reserved. * Copyright (c) 1998, 2004 Jackwind Li Guojie * * Created on 2004-6-13 13:32:48 by JACK * $Id$ * *****************************************************************************/
import org.eclipse.jface.action.Action; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.forms.widgets.Form; import org.eclipse.ui.forms.widgets.FormToolkit; public class SimpleForm extends ApplicationWindow {
/** * @param parentShell */ public SimpleForm(Shell parentShell) { super(parentShell); } /* (non-Javadoc) * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.ruposite) */ protected Control createContents(Composite parent) { Composite composite = new Composite(parent, SWT.NULL); composite.setLayout(new GridLayout()); // Sets up the toolkit. FormToolkit toolkit = new FormToolkit(getShell().getDisplay()); // create a form instance. Form form = toolkit.createForm(composite); form.setLayoutData(new GridData(GridData.FILL_BOTH)); form.setText("Eclipse Forms"); form.getBody().setLayout(new GridLayout()); Button button = toolkit.createButton(form.getBody(), "Test", SWT.NULL); // tool bar form.getToolBarManager().add(new Action("TEST") { public void run() { } }); Menu menu = new Menu(form.getBody()); MenuItem item = new MenuItem(menu, SWT.NULL); item.setText("Testing item"); form.setMenu(menu); form.updateToolBar(); return composite; } public static void main(String[] args) { SimpleForm simpleForm = new SimpleForm(null); simpleForm.setBlockOnOpen(true); simpleForm.open(); }
}
</source>