Example usage for org.eclipse.swt.custom StyledText StyledText

List of usage examples for org.eclipse.swt.custom StyledText StyledText

Introduction

In this page you can find the example usage for org.eclipse.swt.custom StyledText StyledText.

Prototype

public StyledText(Composite parent, int style) 

Source Link

Document

Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

Usage

From source file:StyleRangeTest.java

/**
 * Creates the main window contents//from  w w w .  j  a v a2 s .c  om
 * 
 * @param shell the main window
 */
private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());

    // Create the StyledText
    StyledText styledText = new StyledText(shell, SWT.BORDER);

    // Set the text
    styledText.setText("Go Gators");

    /*
     * The multiple setStyleRange() method // Turn all of the text orange, with
     * the default background color styledText.setStyleRange(new StyleRange(0, 9,
     * orange, null));
     *  // Turn "Gators" blue styledText.setStyleRange(new StyleRange(3, 6, blue,
     * null));
     */

    /*
     * The setStyleRanges() method // Create the array to hold the StyleRanges
     * StyleRange[] ranges = new StyleRange[2];
     *  // Create the first StyleRange, making sure not to overlap. Include the
     * space. ranges[0] = new StyleRange(0, 3, orange, null);
     *  // Create the second StyleRange ranges[1] = new StyleRange(3, 6, blue,
     * null);
     *  // Replace all the StyleRanges for the StyledText
     * styledText.setStyleRanges(ranges);
     */

    /* The replaceStyleRanges() method */
    // Create the array to hold the StyleRanges
    StyleRange[] ranges = new StyleRange[2];

    // Create the first StyleRange, making sure not to overlap. Include the
    // space.
    ranges[0] = new StyleRange(0, 3, orange, null);

    // Create the second StyleRange
    ranges[1] = new StyleRange(3, 6, blue, null);

    // Replace only the StyleRanges in the affected area
    styledText.replaceStyleRanges(0, 9, ranges);
}

From source file:Ch5Persistent.java

protected void buildControls() {
    this.setLayout(new FillLayout());
    styledText = new StyledText(this, SWT.MULTI | SWT.V_SCROLL);
    load();/*from   w  w w.  j  a  v a 2 s  .co m*/

    styledText.addExtendedModifyListener(new ExtendedModifyListener() {
        public void modifyText(ExtendedModifyEvent event) {
            if (doBold) {
                StyleRange style = new StyleRange(event.start, event.length, null, null, SWT.BOLD);
                styledText.setStyleRange(style);
            }
        }
    });

    styledText.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            switch (e.keyCode) {
            case SWT.F1:
                toggleBold();
                break;
            default:
                //ignore everything else
            }
        }
    });
}

From source file:SyntaxTest.java

/**
 * Creates the main window contents/*from   w  w w .j  av  a2  s.  c  om*/
 * 
 * @param shell the main window
 */
private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());

    // Create the StyledText
    final StyledText styledText = new StyledText(shell, SWT.BORDER);

    // Add the syntax coloring handler
    styledText.addExtendedModifyListener(new ExtendedModifyListener() {
        public void modifyText(ExtendedModifyEvent event) {
            // Determine the ending offset
            int end = event.start + event.length - 1;

            // If they typed something, get it
            if (event.start <= end) {
                // Get the text
                String text = styledText.getText(event.start, end);

                // Create a collection to hold the StyleRanges
                java.util.List ranges = new java.util.ArrayList();

                // Turn any punctuation red
                for (int i = 0, n = text.length(); i < n; i++) {
                    if (PUNCTUATION.indexOf(text.charAt(i)) > -1) {
                        ranges.add(new StyleRange(event.start + i, 1, red, null, SWT.BOLD));
                    }
                }

                // If we have any ranges to set, set them
                if (!ranges.isEmpty()) {
                    styledText.replaceStyleRanges(event.start, event.length,
                            (StyleRange[]) ranges.toArray(new StyleRange[0]));
                }
            }
        }
    });
}

From source file:JavaViewer.java

void createStyledText() {
    text = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
    GridData spec = new GridData();
    spec.horizontalAlignment = GridData.FILL;
    spec.grabExcessHorizontalSpace = true;
    spec.verticalAlignment = GridData.FILL;
    spec.grabExcessVerticalSpace = true;
    text.setLayoutData(spec);/* www . j  a v  a  2s .  c om*/
    text.addLineStyleListener(lineStyler);
    text.setEditable(false);
    Color bg = Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
    text.setBackground(bg);
}

From source file:org.jcryptool.visual.verifiablesecretsharing.views.ReconstructionChartComposite.java

/**
 * Generates the head of the tab. The head has a title and a description.
 *//*w w w  .j av  a  2 s.co m*/
private void createHead() {
    final Composite head = new Composite(this, SWT.NONE);
    head.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    head.setLayout(new GridLayout());

    final Label label = new Label(head, SWT.NONE);
    label.setFont(FontService.getHeaderFont());
    label.setText(Messages.VerifiableSecretSharingComposite_tab_title);
    stDescription = new StyledText(head, SWT.READ_ONLY | SWT.MULTI | SWT.WRAP);
    stDescription.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    if (reconstructedPolynom.toString().compareTo("0") == 0) {
        stDescription.setText(Messages.ChartComposite_noGraph);
    } else {
        stDescription.setText(reconstructedPolynom.toString());
    }
}

From source file:BasicEditor.java

public BasicEditor() {
    shell.setLayout(new GridLayout());

    // Add a tool bar. 
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
    ToolItem tiNew = new ToolItem(toolBar, SWT.PUSH);
    tiNew.setText("&New");
    tiNew.setImage(getImage("java2s.gif"));
    tiNew.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            if (handleChangesBeforeDiscard()) {
                file = null;// w w w.j  av a  2  s . c  o m
                text.setText("");
            }
        }
    });

    ToolItem tiOpen = new ToolItem(toolBar, SWT.PUSH);
    tiOpen.setText("&Open");
    tiOpen.setImage(getImage("open.gif"));
    tiOpen.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            if (handleChangesBeforeDiscard())
                loadTextFromFile();
        }
    });

    ToolItem tiSave = new ToolItem(toolBar, SWT.PUSH);
    tiSave.setText("&Save");
    tiSave.setImage(getImage("save.gif"));
    tiSave.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            saveTextToFile();
        }
    });

    new ToolItem(toolBar, SWT.SEPARATOR);

    ToolItem tiCopy = new ToolItem(toolBar, SWT.PUSH);
    tiCopy.setText("&Copy");
    tiCopy.setImage(getImage("copy.gif"));
    tiCopy.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            text.copy();
        }
    });

    ToolItem tiCut = new ToolItem(toolBar, SWT.PUSH);
    tiCut.setText("Cu&t");
    tiCut.setImage(getImage("cut.gif"));
    tiCut.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            text.cut();
        }
    });

    ToolItem tiPaste = new ToolItem(toolBar, SWT.PUSH);
    tiPaste.setText("&Paste");
    tiPaste.setImage(getImage("paste.gif"));
    tiPaste.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            text.paste();
        }
    });

    new ToolItem(toolBar, SWT.SEPARATOR);

    final ToolItem tiWrap = new ToolItem(toolBar, SWT.CHECK);
    tiWrap.setText("&Wrap");
    tiWrap.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            text.setWordWrap(tiWrap.getSelection());
            miWrap.setSelection(tiWrap.getSelection());
        }
    });

    toolBar.pack();

    System.out.println("Client area: " + shell.getClientArea());

    text = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    text.setLayoutData(new GridData(GridData.FILL_BOTH));

    Font font = new Font(shell.getDisplay(), "Courier New", 10, SWT.NORMAL);
    text.setFont(font);

    text.setText("BasicEditor version 1.0\r\nWriten by Jack Li Guojie. ");
    text.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {
            hasUnsavedChanges = true;
        }
    });

    // Add menus. 
    Menu menuBar = new Menu(shell, SWT.BAR);

    // --- sub menu: File
    MenuItem fileMenuItem = new MenuItem(menuBar, SWT.CASCADE);
    fileMenuItem.setText("&File");
    Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);

    MenuItem miNew = new MenuItem(fileMenu, SWT.PUSH);
    miNew.setText("&New\tCtrl+N");
    miNew.setImage(getImage("new.gif"));
    miNew.setAccelerator(SWT.CTRL + 'N');
    miNew.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            if (handleChangesBeforeDiscard()) {
                file = null;
                text.setText("");
            }
        }
    });

    MenuItem miOpen = new MenuItem(fileMenu, SWT.PUSH);
    miOpen.setText("&Open\tCtrl+O");
    miOpen.setAccelerator(SWT.CTRL + 'O');
    miOpen.setImage(getImage("open.gif"));
    miOpen.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            if (handleChangesBeforeDiscard())
                loadTextFromFile();
        }
    });

    MenuItem miSave = new MenuItem(fileMenu, SWT.PUSH);
    miSave.setText("&Save\tCtrl+S");
    miSave.setImage(getImage("save.gif"));
    miSave.setAccelerator(SWT.CTRL + 'S');
    miSave.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            saveTextToFile();
        }
    });

    new MenuItem(fileMenu, SWT.SEPARATOR);

    MenuItem miExit = new MenuItem(fileMenu, SWT.PUSH);
    miExit.setText("&Exit");
    miExit.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            if (handleChangesBeforeDiscard())
                shell.dispose();
        }
    });

    fileMenuItem.setMenu(fileMenu);

    // --- sub menu: Edit.
    MenuItem editMenuItem = new MenuItem(menuBar, SWT.CASCADE);
    editMenuItem.setText("&Edit");

    Menu editMenu = new Menu(shell, SWT.DROP_DOWN);

    MenuItem miCopy = new MenuItem(editMenu, SWT.PUSH);
    miCopy.setText("&Copy\tCtrl+C");
    miCopy.setImage(getImage("copy.gif"));
    miCopy.setAccelerator(SWT.CTRL + 'C');
    miCopy.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            text.copy();
        }
    });

    MenuItem miCut = new MenuItem(editMenu, SWT.PUSH);
    miCut.setText("Cu&t\tCtrl+X");
    miCut.setImage(getImage("cut.gif"));
    miCut.setAccelerator(SWT.CTRL + 'X');
    miCut.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            text.cut();
        }
    });

    MenuItem miPaste = new MenuItem(editMenu, SWT.PUSH);
    miPaste.setText("&Paste\tCtrl+P");
    miPaste.setImage(getImage("paste.gif"));
    miPaste.setAccelerator(SWT.CTRL + 'P');
    miPaste.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            text.paste();
        }
    });

    editMenuItem.setMenu(editMenu);

    // --- sub menu: Format.
    MenuItem formatMenuItem = new MenuItem(menuBar, SWT.CASCADE);
    formatMenuItem.setText("&Format");

    Menu formatMenu = new Menu(shell, SWT.DROP_DOWN);

    miWrap = new MenuItem(formatMenu, SWT.CHECK);
    miWrap.setText("&Wrap\tCtrl+W");
    miWrap.setAccelerator(SWT.CTRL + 'W');
    miWrap.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            text.setWordWrap(miWrap.getSelection());
            tiWrap.setSelection(miWrap.getSelection());
        }
    });

    formatMenuItem.setMenu(formatMenu);

    // Add the menu bar to the shell.
    shell.setMenuBar(menuBar);

    shell.setSize(400, 200);
    shell.open();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:org.eclipse.swt.examples.controlexample.StyledTextTab.java

/**
 * Creates the "Example" widgets.//from  w w w. jav  a  2s.  c  o  m
 */
@Override
void createExampleWidgets() {

    /* Compute the widget style */
    int style = getDefaultStyle();
    if (singleButton.getSelection())
        style |= SWT.SINGLE;
    if (multiButton.getSelection())
        style |= SWT.MULTI;
    if (horizontalButton.getSelection())
        style |= SWT.H_SCROLL;
    if (verticalButton.getSelection())
        style |= SWT.V_SCROLL;
    if (wrapButton.getSelection())
        style |= SWT.WRAP;
    if (readOnlyButton.getSelection())
        style |= SWT.READ_ONLY;
    if (borderButton.getSelection())
        style |= SWT.BORDER;
    if (fullSelectionButton.getSelection())
        style |= SWT.FULL_SELECTION;

    /* Create the example widgets */
    styledText = new StyledText(styledTextGroup, style);
    styledText.setText(ControlExample.getResourceString("Example_string"));
    styledText.append("\n");
    styledText.append(ControlExample.getResourceString("One_Two_Three"));

    if (text != null) {
        styledText.setText(text);
        text = null;
    }
    if (styleRanges != null) {
        styledText.setStyleRanges(styleRanges);
        styleRanges = null;
    }
}

From source file:BasicEditor2.java

public BasicEditor2() {

    // Action: create new text.
    Action actionNew = new Action("&New", ImageDescriptor.createFromFile(null, "icons/new.gif")) {
        public void run() {
            if (handleChangesBeforeDiscard()) {
                file = null;//from ww w  . j a  va  2 s .com
                text.setText("");
            }
        }
    };
    actionNew.setAccelerator(SWT.CTRL + 'N');

    // Action: open a text file.
    Action actionOpen = new Action("&Open", ImageDescriptor.createFromFile(null, "icons/open.gif")) {
        public void run() {
            if (handleChangesBeforeDiscard())
                loadTextFromFile();
        }
    };
    actionOpen.setAccelerator(SWT.CTRL + 'O');

    // Action: save the text to a file. 
    Action actionSave = new Action("&Save\tCtrl+S", ImageDescriptor.createFromFile(null, "icons/save.gif")) {
        public void run() {
            saveTextToFile();
        }
    };
    //actionSave.setAccelerator(SWT.CTRL + 'S');

    // Action: copy selected text.
    Action actionCopy = new Action("&Copy", ImageDescriptor.createFromFile(null, "icons/copy.gif")) {
        public void run() {
            text.copy();
        }
    };
    actionCopy.setAccelerator(SWT.CTRL + 'C');

    // Separator. 

    // Action: cut the selected text. 
    Action actionCut = new Action("Cu&t", ImageDescriptor.createFromFile(null, "icons/cut.gif")) {
        public void run() {
            text.cut();
        }
    };
    actionCut.setAccelerator(SWT.CTRL + 'X');

    // Action: paste the text on clipboard. 
    Action actionPaste = new Action("&Paste", ImageDescriptor.createFromFile(null, "icons/paste.gif")) {
        public void run() {
            text.paste();
        }
    };
    actionPaste.setAccelerator(SWT.CTRL + 'P');

    // Separator. 

    // Action: set wrap property. 
    Action actionWrap = new Action("&Wrap", IAction.AS_CHECK_BOX) {
        public void run() {
            text.setWordWrap(isChecked());
        }
    };
    actionWrap.setAccelerator(SWT.CTRL + 'W');

    // Action: exit. 
    Action actionExit = new Action("&Exit@Ctrl+X") {
        public void run() {
            if (handleChangesBeforeDiscard())
                shell.dispose();
        }
    };

    System.out.println(actionWrap.getText());

    // Add a tool bar.
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
    ToolBarManager toolBarManager = new ToolBarManager(toolBar);

    toolBarManager.add(actionNew);
    toolBarManager.add(actionOpen);
    toolBarManager.add(actionSave);
    toolBarManager.add(new Separator());
    toolBarManager.add(actionCopy);
    toolBarManager.add(actionCut);
    toolBarManager.add(actionPaste);

    toolBarManager.add(new Separator());

    toolBarManager.add(actionWrap);

    toolBarManager.update(true);

    shell.setLayout(new GridLayout());

    System.out.println("Client area: " + shell.getClientArea());

    text = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    text.setLayoutData(new GridData(GridData.FILL_BOTH));

    Font font = new Font(shell.getDisplay(), "Courier New", 10, SWT.NORMAL);
    text.setFont(font);

    text.setText("BasicEditor version 1.0\r\nWriten by Jack Li Guojie. ");
    text.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {
            hasUnsavedChanges = true;
        }
    });

    // Add menus.
    MenuManager barMenuManager = new MenuManager();

    MenuManager fileMenuManager = new MenuManager("&File");
    MenuManager editMenuManager = new MenuManager("&Edit");
    MenuManager formatMenuManager = new MenuManager("&Format");

    barMenuManager.add(fileMenuManager);
    barMenuManager.add(editMenuManager);
    barMenuManager.add(formatMenuManager);

    fileMenuManager.add(actionNew);
    fileMenuManager.add(actionOpen);
    fileMenuManager.add(actionSave);
    fileMenuManager.add(new Separator());

    fileMenuManager.add(actionExit);

    editMenuManager.add(actionCopy);
    editMenuManager.add(actionCut);
    editMenuManager.add(actionPaste);

    formatMenuManager.add(actionWrap);

    // Add the menu bar to the shell.
    // shell.setMenuBar(menuBar);
    barMenuManager.updateAll(true);
    shell.setMenuBar(barMenuManager.createMenuBar((Decorations) shell));

    shell.setSize(400, 200);
    shell.open();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:PmpEditor.java

/**
 * Creates the main window's contents//from  w  ww .  jav  a2  s  .co m
 * 
 * @param shell
 *          the main window
 */
private void createContents(Shell shell) {
    // Set the layout and the menu bar
    shell.setLayout(new FormLayout());
    shell.setMenuBar(new PmpEditorMenu(shell).getMenu());

    // Create the status bar
    status = new Label(shell, SWT.BORDER);
    FormData data = new FormData();
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    data.bottom = new FormAttachment(100, 0);
    data.height = status.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
    status.setLayoutData(data);

    // Create the styled text
    st = new StyledText(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    data = new FormData();
    data.left = new FormAttachment(0);
    data.right = new FormAttachment(100);
    data.top = new FormAttachment(0);
    data.bottom = new FormAttachment(status);
    st.setLayoutData(data);

    // Set the font
    st.setFont(font);

    // Add Brief delete next word
    // Use SWT.MOD1 instead of SWT.CTRL for portability
    st.setKeyBinding('k' | SWT.MOD1, ST.DELETE_NEXT);

    // Add vi end of line (kind of)
    // Use SWT.MOD1 instead of SWT.CTRL for portability
    // Use SWT.MOD2 instead of SWT.SHIFT for portability
    // Shift+4 is $
    st.setKeyBinding('4' | SWT.MOD1 | SWT.MOD2, ST.LINE_END);

    // Handle key presses
    st.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent event) {
            // Update the status bar
            updateStatus();
        }
    });

    // Handle text modifications
    st.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent event) {
            // Update the status bar
            updateStatus();

            // Update the comments
            if (lineStyleListener != null) {
                lineStyleListener.refreshMultilineComments(st.getText());
                st.redraw();
            }
        }
    });

    // Store undo information
    st.addExtendedModifyListener(new ExtendedModifyListener() {
        public void modifyText(ExtendedModifyEvent event) {
            if (!ignoreUndo) {
                // Push this change onto the changes stack
                changes.push(new TextChange(event.start, event.length, event.replacedText));
                if (changes.size() > UNDO_LIMIT)
                    changes.remove(0);
            }
        }
    });

    // Update the title bar and the status bar
    updateTitle();
    updateStatus();
}

From source file:BasicEditor3.java

public BasicEditor3() {

    // Action: create new text.
    Action actionNew = new Action("&New", ImageDescriptor.createFromFile(null, "java2s.gif")) {
        public void run() {
            if (handleChangesBeforeDiscard()) {
                file = null;/*from w w w .  j  a  va 2 s  .co m*/
                text.setText("");
            }
        }
    };
    actionNew.setAccelerator(SWT.CTRL + 'N');

    // Action: open a text file.
    Action actionOpen = new Action("&Open", ImageDescriptor.createFromFile(null, "icons/open.gif")) {
        public void run() {
            if (handleChangesBeforeDiscard())
                loadTextFromFile();
        }
    };
    actionOpen.setAccelerator(SWT.CTRL + 'O');

    // Action: save the text to a file. 
    Action actionSave = new Action("&Save\tCtrl+S", ImageDescriptor.createFromFile(null, "icons/save.gif")) {
        public void run() {
            saveTextToFile();
        }
    };
    //actionSave.setAccelerator(SWT.CTRL + 'S');

    // Action: copy selected text.
    Action actionCopy = new Action("&Copy", ImageDescriptor.createFromFile(null, "icons/copy.gif")) {
        public void run() {
            text.copy();
        }
    };
    actionCopy.setAccelerator(SWT.CTRL + 'C');

    // Separator. 

    // Action: cut the selected text. 
    Action actionCut = new Action("Cu&t", ImageDescriptor.createFromFile(null, "icons/cut.gif")) {
        public void run() {
            text.cut();
        }
    };
    actionCut.setAccelerator(SWT.CTRL + 'X');

    // Action: paste the text on clipboard. 
    Action actionPaste = new Action("&Paste", ImageDescriptor.createFromFile(null, "icons/paste.gif")) {
        public void run() {
            text.paste();
        }
    };
    actionPaste.setAccelerator(SWT.CTRL + 'P');

    // Separator. 

    // Action: set wrap property. 
    Action actionWrap = new Action("&Wrap", IAction.AS_CHECK_BOX) {
        public void run() {
            text.setWordWrap(isChecked());
        }
    };
    actionWrap.setAccelerator(SWT.CTRL + 'W');

    // Action: exit. 
    Action actionExit = new Action("&Exit@Ctrl+X") {
        public void run() {
            if (handleChangesBeforeDiscard())
                shell.dispose();
        }
    };

    Action actionPrint = new Action("&Print@Ctrl+P") {
        public void run() {
            printText(text.getText());
        }
    };

    Action actionPrint2 = new Action("Print (StyledText)") {
        public void run() {
            StyledTextPrintOptions options = new StyledTextPrintOptions();
            options.header = "SWT";
            options.footer = "Page <page>";
            options.jobName = "Text";

            Runnable runnable = text.print(new Printer(), options);
            runnable.run();
        }
    };

    // Add a tool bar.
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
    ToolBarManager toolBarManager = new ToolBarManager(toolBar);

    toolBarManager.add(actionNew);
    toolBarManager.add(actionOpen);
    toolBarManager.add(actionSave);
    toolBarManager.add(new Separator());
    toolBarManager.add(actionCopy);
    toolBarManager.add(actionCut);
    toolBarManager.add(actionPaste);

    toolBarManager.add(new Separator());

    toolBarManager.add(actionWrap);

    toolBarManager.add(new Separator());
    toolBarManager.add(actionPrint);
    toolBarManager.add(actionPrint2);

    toolBarManager.update(true);

    shell.setText(APP_NAME);
    shell.setLayout(new GridLayout());

    text = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    text.setLayoutData(new GridData(GridData.FILL_BOTH));

    Font font = new Font(shell.getDisplay(), "Courier New", 10, SWT.NORMAL);
    text.setFont(font);

    text.setText("BasicEditor version 3.0\r\nWriten by Jack Li Guojie. ");
    text.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {
            hasUnsavedChanges = true;
        }
    });

    // Add menus.
    MenuManager barMenuManager = new MenuManager();

    MenuManager fileMenuManager = new MenuManager("&File");
    MenuManager editMenuManager = new MenuManager("&Edit");
    MenuManager formatMenuManager = new MenuManager("&Format");

    barMenuManager.add(fileMenuManager);
    barMenuManager.add(editMenuManager);
    barMenuManager.add(formatMenuManager);

    fileMenuManager.add(actionNew);
    fileMenuManager.add(actionOpen);
    fileMenuManager.add(actionSave);
    fileMenuManager.add(actionPrint);
    fileMenuManager.add(new Separator());

    fileMenuManager.add(actionExit);

    editMenuManager.add(actionCopy);
    editMenuManager.add(actionCut);
    editMenuManager.add(actionPaste);

    formatMenuManager.add(actionWrap);

    // Add the menu bar to the shell.
    // shell.setMenuBar(menuBar);
    barMenuManager.updateAll(true);
    shell.setMenuBar(barMenuManager.createMenuBar((Decorations) shell));

    shell.setSize(400, 200);
    shell.open();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}