⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swtframe.java

📁 Java开发图文混排的编辑器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * Created on 2004-7-24
 * Author: Xuefeng, Copyright (C) 2004, Xuefeng.
 */
package jexi.ui.swt;

import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.program.*;

/**
 * The implementation of Frame. And this is the real window 
 * displayed on the screen! <br>
 * <b>NOTE</b>: The UI was created by ui-design-tool, the swt-designer, 
 * (see: <a href="www.swt-designer.com">www.swt-designer.com</a>), 
 * so modify carefully! 
 * 
 * @author Xuefeng
 */
public class SWTFrame implements jexi.ui.Frame {

    // store the view:
    private jexi.ui.View view = null;

    // the device:
    private Display display = null;
    // the main window:
    private Shell shell = null;

    // the default graphics:
    private SWTGraphics defaultGraphics = null;

    private ToolBar toolBarCommon;
    private ToolBar toolBarFormat;

    private Combo cmbFontName;
    private Combo cmbFontSize;

    private Composite ScrollableView;
    private Canvas textView;

    private Slider sliderV;
    private Slider sliderH;

    // caret:
    private Caret caret;

    // color-select menu:
    private Menu mnuColorSelect;
    private MenuItem[] mnuColor = new MenuItem[16];

    // image factory:
    private SWTImageFactory imageFactory;

    /**
     * Get the display object. 
     * 
     * @return The display object.
     */
    public Display getDisplay() {
        return this.display;
    }

    /**
     * Get the shell object.
     * 
     * @return The shell object.
     */
    public Shell getShell() {
        return this.shell;
    }

    /**
     * Initialize the frame. 
     * 
     * @see jexi.ui.Frame#init()
     */
    public void init(){
        // create device:
        this.display = new Display();
        this.imageFactory = new SWTImageFactory(this.display);
        // create window:
        this.shell = new Shell(display);
        this.shell.setText("Jexi 1.0 beta");

        // create default graphics:
        org.eclipse.swt.graphics.GC gc = new org.eclipse.swt.graphics.GC(shell);
        this.defaultGraphics = new jexi.ui.swt.SWTGraphics(gc);

        // now layout:
        final GridLayout gridLayoutForShell = new GridLayout();
        gridLayoutForShell.verticalSpacing = 1;
        gridLayoutForShell.marginWidth = 0;
        gridLayoutForShell.marginHeight = 0;
        gridLayoutForShell.makeColumnsEqualWidth = true;
        shell.setLayout(gridLayoutForShell);

        // create menu bar:
        createMenu();

        // create pop-up menu of "select color":
        createColorSelectMenu();

        // create tool bar - Common:
        createToolBarCommon();

        // create tool bar - Format:
        createToolBarFormat();

        // init fonts:
        initFont();

        // create "view":
        createComposite();

        ///////////////////////////////////////////////////////////////////////
        // ok, create the view:
        //this.view = new SWTTextView(this.textView);
        this.view = new SWTScrollableViewDecorator(new SWTTextView(this.textView),
            this.sliderH, this.sliderV);
        jexi.core.Document document = jexi.core.Document.createEmptyDocument(view);
        view.init(document);
        //this.document.updateCaret();
        ///////////////////////////////////////////////////////////////////////

        // notify view when size changed:
        textView.addControlListener(new ControlAdapter () {
            public void controlResized(ControlEvent e) {
                // NOTE: the size is textView(Canvas):
                org.eclipse.swt.graphics.Rectangle r = textView.getBounds();
                view.onSizeChanged(r.width, r.height);
            }
        });

        // notify view when need repaint:
        textView.addPaintListener(new PaintListener () {
            public void paintControl(PaintEvent e) {
                view.update();
            }
        });

        // notify view when mouse moved:
        textView.addMouseMoveListener(new MouseMoveListener() {
            public void mouseMove(MouseEvent e) {
                view.onMouseMove(e.x, e.y);
            }
        });

        // nofity view when mouse clicked:
        textView.addMouseListener(new MouseListener() {
            public void mouseDown(MouseEvent e) {
                if(e.button==1)
                    view.onLButtonDown(e.x, e.y);
                if(e.button==3)
                    view.onRButtonDown(e.x, e.y);
            }
            public void mouseUp(MouseEvent e) {
                if(e.button==1)
                    view.onLButtonUp(e.x, e.y);
                if(e.button==3)
                    view.onRButtonUp(e.x, e.y);
            }
            public void mouseDoubleClick(MouseEvent e) {
                if(e.button==1)
                    view.onLButtonDblClick(e.x, e.y);
            }
        });

        // nofity view when key pressed:
        textView.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent e) {
                System.out.println(e.toString());
                boolean shift = (e.stateMask & SWT.SHIFT) == SWT.SHIFT;
                boolean ctrl = (e.stateMask & SWT.CTRL) == SWT.CTRL;
                boolean alt = (e.stateMask & SWT.ALT) == SWT.ALT;
                if(e.character!='\0') {
                    if(e.character==8 || e.character==13 || e.character==127)
                        view.onFunctionKeyPressed(e.character, shift, ctrl, alt);
                    else if(!ctrl && !alt)
                        view.onKeyPressed(e.character);
                }
                else
                    view.onFunctionKeyPressed(e.keyCode, shift, ctrl, alt);
            }
            public void keyReleased(KeyEvent e) {
                //System.out.println(e.toString());
            }
        });

        // nofity view when select font, size and color:
        cmbFontName.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                System.out.println(e.toString());
                view.onFormatChanged(cmbFontName.getText(),
                    null, null, null, null, null
                );
            }
        });
        cmbFontSize.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                System.out.println(e.toString());
                view.onFormatChanged(null,
                    Integer.valueOf(cmbFontSize.getText()),
                    null, null, null, null
                );
            }
        });

        for(int i=0; i<16; i++) {
            mnuColor[i].addSelectionListener(new SelectColorHandler(view, i));
        }
    }


    /**
     * Ready to show the window and run message loop. 
     * 
     * @see jexi.ui.Frame#show()
     */
    public void show() {
        shell.pack();
        shell.open();
        this.textView.setFocus();

        while(!shell.isDisposed())
            if(!display.readAndDispatch())
                display.sleep();
    }

    /**
     * Clean up. 
     * 
     * @see jexi.ui.Frame#dispose()
     */
    public void dispose() {
        // dispose fonts & colors:
        jexi.ui.FontFactory.instance().clearAllFonts();
        jexi.ui.ColorFactory.instance().clearAllColors();

        // dispose the display:
        this.display.dispose();
    }

    // create menu bar:
    private void createMenu() {
        Menu menubar = new Menu(shell, SWT.BAR);

        // File menu:
        final MenuItem mnuFile = new MenuItem(menubar, SWT.CASCADE);
        mnuFile.setText("&File");

        Menu popupmenu = new Menu(mnuFile);
        mnuFile.setMenu(popupmenu);

        final MenuItem mnuFileNew = new MenuItem(popupmenu, SWT.NONE);
        mnuFileNew.setEnabled(false);
        mnuFileNew.setText("&New");

        final MenuItem mnuFileOpen = new MenuItem(popupmenu, SWT.NONE);
        mnuFileOpen.setEnabled(false);
        mnuFileOpen.setText("&Open...\tCtrl+O");

        final MenuItem mnuFileClose = new MenuItem(popupmenu, SWT.NONE);
        mnuFileClose.setEnabled(false);
        mnuFileClose.setText("&Close");

        new MenuItem(popupmenu, SWT.SEPARATOR);

        final MenuItem mnuFileSave = new MenuItem(popupmenu, SWT.NONE);
        mnuFileSave.setEnabled(false);
        mnuFileSave.setText("&Save\tCtrl+S");

        final MenuItem mnuFileSaveAs = new MenuItem(popupmenu, SWT.NONE);
        mnuFileSaveAs.setEnabled(false);
        mnuFileSaveAs.setText("Save &As...");

        new MenuItem(popupmenu, SWT.SEPARATOR);

        final MenuItem mnuFilePageSetup = new MenuItem(popupmenu, SWT.NONE);
        mnuFilePageSetup.setEnabled(false);
        mnuFilePageSetup.setText("Page Set&up...");

        final MenuItem mnuFilePrintPreview = new MenuItem(popupmenu, SWT.NONE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -