ui demo: CustomComponents

yidabu yidabu.spam at gmail.com
Thu Jun 12 18:17:56 PDT 2008


TODO: FormText demo not works.

code:

module ui.CustomComponents;

/*******************************************************************************
 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on 2004-6-14 10:55:42 by JACK $Id$
 *  
 * Port to the D programming language:
 *     yidabu at gmail dot com  ( D China http://www.d-programming-language-china.org/ ) 
 ******************************************************************************/
import dwtx.jface.window.ApplicationWindow;
import dwt.DWT;
import dwt.graphics.Image;
import dwt.layout.FillLayout;
import dwt.layout.GridData;
import dwt.layout.GridLayout;
import dwt.widgets.Composite;
import dwt.widgets.Control;
import dwt.widgets.Display;
import dwt.widgets.Shell;
import dwtx.ui.forms.HyperlinkGroup;
import dwtx.ui.forms.events.ExpansionAdapter;
import dwtx.ui.forms.events.ExpansionEvent;
import dwtx.ui.forms.events.HyperlinkAdapter;
import dwtx.ui.forms.events.HyperlinkEvent;
import dwtx.ui.forms.events.IHyperlinkListener;
import dwtx.ui.forms.widgets.ExpandableComposite;
import dwtx.ui.forms.widgets.Form;
import dwtx.ui.forms.widgets.FormText;
import dwtx.ui.forms.widgets.FormToolkit;
import dwtx.ui.forms.widgets.Hyperlink;
import dwtx.ui.forms.widgets.ImageHyperlink;
import dwtx.ui.forms.widgets.Section;
import dwtx.ui.forms.widgets.TableWrapLayout;

import dwt.graphics.ImageData;
import dwt.dwthelper.utils;
import dwt.dwthelper.ByteArrayInputStream;

import tango.util.log.Trace;

void main(String[] args) {
    CustomWidgets win = new CustomWidgets(null);
    win.setBlockOnOpen(true);
    win.open();
    //Display.getCurrent().dispose();
}

public class CustomWidgets : ApplicationWindow {
    FormToolkit toolkit;
    Form form;

    /**
    * @param parentShell
    */
    public this(Shell parentShell) {
        super(parentShell);
    }

    /// TODO : tango.core.Exception.NoSuchElementException: no matching key
    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 class(getShell()) ExpansionAdapter {
            Shell shell;
            this(Shell shell_)
            {
                shell = shell_;
            }
            public void expansionStateChanged(ExpansionEvent e) {
            // resizes the application window.
                shell.pack(true);
            }
        });
    }

    private void demoFormTextXML() {
        form.getBody().setLayout(new TableWrapLayout());
        FormText text = toolkit.createFormText(form.getBody(), true);

        Image image = new Image(form.getDisplay(), new ImageData(new ByteArrayInputStream(cast(byte[]) import("eclipse.png"))));
        text.setImage("eclipse", image);
        text.setText(
          "<form>"
            ~ "<p><img href=\"eclipse\"/> Eclipse Projects: </p>"
            ~ "<li><b>Platform</b> - Eclipse frameworks</li>"
            ~ "<li><b>JDT</b> - Java development tools</li>"
            ~ "<li><b>PDE</b> - Plug-in development environment</li>"
            ~ "</form>",
          true,
          false);

    }

    // TODO: tango.core.Exception.NoSuchElementException: no matching key
    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);
        
    }

    // TODO: tango.core.Exception.NoSuchElementException: no matching key
    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(DWT.COLOR_BLUE));
        group.setActiveForeground(
          form.getDisplay().getSystemColor(DWT.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 class() HyperlinkAdapter {
          public void linkActivated(HyperlinkEvent e) {
            //Trace.formatln("Link activated: {}", stringcast(e.getHref()));
          }
        });

    }

    private void demoHyperlinks() {
        form.getBody().setLayout(new GridLayout());

        Hyperlink hyperlink =
          toolkit.createHyperlink(
            form.getBody(),
            "This is a hyperlink to Eclipse.org",
            DWT.NULL);
        hyperlink.setHref(new ArrayWrapperString("http://www.eclipse.org"));
        hyperlink.setForeground(
          getShell().getDisplay().getSystemColor(DWT.COLOR_BLUE));
        hyperlink.addHyperlinkListener(new class() IHyperlinkListener {
          public void linkEntered(HyperlinkEvent e) {
            Trace.formatln("Mouse entered.");
          }

          public void linkExited(HyperlinkEvent e) {
            Trace.formatln("Mouse left.");
          }

          public void linkActivated(HyperlinkEvent e) {
              Trace.formatln("Hyperlink activated.");
              Trace.formatln("HREF = {}", stringcast(e.getHref()));
          }
        });

        ImageHyperlink imageHyperlink =
          toolkit.createImageHyperlink(form.getBody(), DWT.NULL);
        imageHyperlink.setText("This is an image hyperlink.");
        imageHyperlink.setForeground(
          getShell().getDisplay().getSystemColor(DWT.COLOR_BLUE));
        imageHyperlink.setImage(
          new Image(getShell().getDisplay(), new ImageData(new ByteArrayInputStream(cast(byte[]) import("eclipse.png")))));
        imageHyperlink.addHyperlinkListener(new class() HyperlinkAdapter {
          public void linkActivated(HyperlinkEvent e) {
              Trace.formatln("Image hyperlink activated.");
          }
        });

        HyperlinkGroup group = new HyperlinkGroup(getShell().getDisplay());
        group.add(hyperlink);
        group.add(imageHyperlink);

        group.setActiveBackground(
          getShell().getDisplay().getSystemColor(DWT.COLOR_YELLOW));
        group.setActiveForeground(
          getShell().getDisplay().getSystemColor(DWT.COLOR_RED));
        group.setForeground(
          getShell().getDisplay().getSystemColor(DWT.COLOR_BLUE));
    }

    /*
    * (non-Javadoc)
    * 
    * @see dwtx.jface.window.Window#createContents(dwt.widgets.Composite)
    */
    protected Control createContents(Composite parent) {
        Composite composite = new Composite(parent, DWT.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");

        
        /// only works demo
        demoHyperlinks();
        
        /// BUG: tango.core.Exception.NoSuchElementException: no matching key
        //demoSections();  
        //demoFormTextNormal();
        //demoFormTextURL();
        //demoFormTextXML();
        //demoExpandableComposite();
        
        return composite;
    }

}

           
       

-- 
yidabu <yidabu.spam at gmail.com>
http://www.dsource.org/projects/dwin

D 语言-中文(D Chinese):
http://www.d-programming-language-china.org/
http://bbs.d-programming-language-china.org/
http://dwin.d-programming-language-china.org/
http://scite4d.d-programming-language-china.org/




More information about the Digitalmars-d-dwt mailing list