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

📄 swtframe.java

📁 Java开发图文混排的编辑器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        tbNew.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                System.out.println(e.toString());
            }
        });
        tbNew.setToolTipText("Create a new document.");
        img = this.imageFactory.loadToolbarImage("new");
        if(img!=null) tbNew.setImage(img);
        else tbNew.setText("New");

        final ToolItem tbOpen = new ToolItem(toolBarCommon, SWT.PUSH);
        tbOpen.setToolTipText("Open an existing document.");
        img = this.imageFactory.loadToolbarImage("open");
        if(img!=null) tbOpen.setImage(img);
        else tbOpen.setText("Open");

        final ToolItem tbSave = new ToolItem(toolBarCommon, SWT.PUSH);
        tbSave.setToolTipText("Save current document.");
        img = this.imageFactory.loadToolbarImage("save");
        if(img!=null) tbSave.setImage(img);
        else tbSave.setText("Save");

        final ToolItem toolItem_2 = new ToolItem(toolBarCommon, SWT.SEPARATOR);

        final ToolItem tbCut = new ToolItem(toolBarCommon, SWT.NONE);
        tbCut.setToolTipText("Cut the selected glyphs.");
        img = this.imageFactory.loadToolbarImage("cut");
        if(img!=null) tbCut.setImage(img);
        else tbCut.setText("Cut");

        final ToolItem tbCopy = new ToolItem(toolBarCommon, SWT.PUSH);
        tbCopy.setToolTipText("Copy the selected glyphs.");
        img = this.imageFactory.loadToolbarImage("copy");
        if(img!=null) tbCopy.setImage(img);
        else tbCopy.setText("Copy");

        final ToolItem tbPaste = new ToolItem(toolBarCommon, SWT.PUSH);
        tbPaste.setToolTipText("Paste from the clipboard.");
        img = this.imageFactory.loadToolbarImage("paste");
        if(img!=null) tbPaste.setImage(img);
        else tbPaste.setText("Paste");

        final ToolItem toolItem_5 = new ToolItem(toolBarCommon, SWT.SEPARATOR);

        final ToolItem tbUndo = new ToolItem(toolBarCommon, SWT.PUSH);
        tbUndo.setToolTipText("Undo the last command.");
        img = this.imageFactory.loadToolbarImage("undo");
        if(img!=null) tbUndo.setImage(img);
        else tbUndo.setText("Undo");

        final ToolItem tbRedo = new ToolItem(toolBarCommon, SWT.PUSH);
        tbRedo.setToolTipText("Redo the previous command.");
        img = this.imageFactory.loadToolbarImage("redo");
        if(img!=null) tbRedo.setImage(img);
        else tbRedo.setText("Redo");
    }

    // create pop-up menu of "select color":
    private void createColorSelectMenu() {
        String[] color_names = {
            "Black",  "Maroon",  "Green", "Olive",
            "Navy",   "Purple",  "Teal",  "Gray",
            "Silver", "Red",     "Lime",  "Yellow",
            "Blue",   "Fuchsia", "Aqua",  "White"
        };

        mnuColorSelect = new Menu (shell, SWT.POP_UP);
        Image image = null;

        for(int i=0; i<16; i++) {
            mnuColor[i] = new MenuItem(mnuColorSelect, SWT.PUSH);
            mnuColor[i].setText(color_names[i]);
            image = imageFactory.loadMenuColorImage(""+i);
            if(image!=null)
                mnuColor[i].setImage(image);
        }
    }

    // create tool bar - Format:
    private void createToolBarFormat() {
        Image img = null;

        toolBarFormat = new ToolBar(shell, SWT.FLAT);
        toolBarFormat.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        final ToolItem tbFontName = new ToolItem(toolBarFormat, SWT.SEPARATOR);

        final ToolItem tbFontSize = new ToolItem(toolBarFormat, SWT.SEPARATOR);

        final ToolItem tbBold = new ToolItem(toolBarFormat, SWT.CHECK);
        tbBold.addListener (SWT.Selection, new Listener () {
            public void handleEvent (Event event) {
                view.onFormatChanged(null, null,
                    tbBold.getSelection()? Boolean.TRUE : Boolean.FALSE,
                    null, null, null);
            }
        });
        img = this.imageFactory.loadToolbarImage("bold");
        if(img!=null) tbBold.setImage(img);
        else tbBold.setText("B");

        final ToolItem tbItalic = new ToolItem(toolBarFormat, SWT.CHECK);
        tbItalic.setEnabled(false);
        img = this.imageFactory.loadToolbarImage("italic");
        if(img!=null) tbItalic.setImage(img);
        else tbItalic.setText("I");

        final ToolItem tbUnderlined = new ToolItem(toolBarFormat, SWT.CHECK);
        tbUnderlined.setEnabled(false);
        img = this.imageFactory.loadToolbarImage("underlined");
        if(img!=null) tbUnderlined.setImage(img);
        else tbUnderlined.setText("U");

        new ToolItem(toolBarFormat, SWT.SEPARATOR);

        final ToolItem tbAlignLeft = new ToolItem(toolBarFormat, SWT.RADIO);
        tbAlignLeft.setSelection(true);
        img = this.imageFactory.loadToolbarImage("left");
        if(img!=null) tbAlignLeft.setImage(img);
        else tbAlignLeft.setText("left");

        final ToolItem tbAlignCenter = new ToolItem(toolBarFormat, SWT.RADIO);
        tbAlignCenter.setEnabled(false);
        img = this.imageFactory.loadToolbarImage("center");
        if(img!=null) tbAlignCenter.setImage(img);
        else tbAlignCenter.setText("center");

        final ToolItem tbAlignRight = new ToolItem(toolBarFormat, SWT.RADIO);
        tbAlignRight.setEnabled(false);
        img = this.imageFactory.loadToolbarImage("right");
        if(img!=null) tbAlignRight.setImage(img);
        else tbAlignRight.setText("right");

        new ToolItem(toolBarFormat, SWT.SEPARATOR);

        final ToolItem tbIndentLeft = new ToolItem(toolBarFormat, SWT.PUSH);
        tbIndentLeft.setEnabled(false);
        img = this.imageFactory.loadToolbarImage("indent_left");
        if(img!=null) tbIndentLeft.setImage(img);
        else tbIndentLeft.setText("<-");

        final ToolItem tbIndentRight = new ToolItem(toolBarFormat, SWT.PUSH);
        tbIndentRight.setEnabled(false);
        img = this.imageFactory.loadToolbarImage("indent_right");
        if(img!=null) tbIndentRight.setImage(img);
        else tbIndentRight.setText("->");

        new ToolItem(toolBarFormat, SWT.SEPARATOR);

        final ToolItem tbColor = new ToolItem(toolBarFormat, SWT.DROP_DOWN);
        img = this.imageFactory.loadToolbarImage("color");
        if(img!=null) tbColor.setImage(img);
        else tbColor.setText("Color");
        tbColor.addListener (SWT.Selection, new Listener () {
            public void handleEvent (Event event) {
                if (event.detail == SWT.ARROW) {
                    Rectangle rect = tbColor.getBounds ();
                    Point pt = new Point (rect.x, rect.y + rect.height);
                    pt = toolBarFormat.toDisplay (pt);
                    mnuColorSelect.setLocation (pt.x, pt.y);
                    mnuColorSelect.setVisible (true);
                }
            }
        });

        // font name list:
        cmbFontName = new Combo(toolBarFormat, SWT.READ_ONLY);

        // place it on the tool bar:
        cmbFontName.pack();
        tbFontName.setWidth(120);
        tbFontName.setControl(cmbFontName);

        // font size list:
        cmbFontSize = new Combo(toolBarFormat, SWT.READ_ONLY);
        cmbFontSize.setItems(new String[] { "6", "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36", "48", "72" });
        cmbFontSize.select(5);

        // place it on the tool bar:
        cmbFontSize.pack();
        tbFontSize.setWidth(50);
        tbFontSize.setControl(cmbFontSize);

        toolBarFormat.pack();
    }

    // enumerate all fonts:
    private void initFont() {
        ArrayList all_fonts = new ArrayList(10);
        // TODO: enumerate all fonts:
        all_fonts.add("Arial");
        all_fonts.add("Comic Sans MS");
        all_fonts.add("Courier New");
        all_fonts.add("MS Sans Serif");
        all_fonts.add("Tahoma");
        all_fonts.add("Times New Roman");
        String[] fonts = (String[])all_fonts.toArray(new String[0]);
        cmbFontName.setItems(fonts);
    }

    // create "view":
    private void createComposite() {
        // scrollable view:
        ScrollableView = new Composite(shell, SWT.NONE);
        final GridLayout gridLayoutForScrollableView = new GridLayout();
        gridLayoutForScrollableView.horizontalSpacing = 0;
        gridLayoutForScrollableView.verticalSpacing = 0;
        gridLayoutForScrollableView.marginWidth = 0;
        gridLayoutForScrollableView.marginHeight = 0;
        gridLayoutForScrollableView.numColumns = 2;
        ScrollableView.setLayout(gridLayoutForScrollableView);
        ScrollableView.setLayoutData(new GridData(GridData.FILL_BOTH));

        // text view:
        textView = new Canvas(ScrollableView, SWT.NONE);
        textView.setLayoutData(new GridData(GridData.FILL_BOTH));
        textView.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));

        sliderV = new Slider(ScrollableView, SWT.VERTICAL);
        sliderV.setLayoutData(new GridData(GridData.FILL_VERTICAL));

        sliderH = new Slider(ScrollableView, SWT.NONE);
        sliderH.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));

        // IMPORTANT:
        // add listener:
        textView.addListener(SWT.KeyDown, new Listener () {
            public void handleEvent(Event event) {
                //
            }
        });
        textView.addListener (SWT.MouseDown, new Listener () {
            public void handleEvent(Event event) {
            }
        });

        // and set caret:
        caret = new Caret(textView, 0);
        caret.setBounds(1,1,2,24); // TODO:
        textView.setCaret(caret);
    }

    /**
     * Get the default graphics. DO NOT dispose it after use!!! 
     * 
     * @see jexi.ui.Frame#getDefaultGraphics()
     */
    public jexi.ui.Graphics getDefaultGraphics() {
        return this.defaultGraphics;
    }

    /**
     * Get the view. 
     * 
     * @see jexi.ui.Frame#getView()
     */
    public jexi.ui.View getView() {
        return this.view;
    }
}

class SelectColorHandler extends SelectionAdapter {

    private static int[] COLORS = {
        0x000000, 0x800000, 0x008000, 0x808000,
        0x000080, 0x800080, 0x008080, 0x808080,
        0xc0c0c0, 0xff0000, 0x00ff00, 0xffff00,
        0x0000ff, 0xff00ff, 0x00ffff, 0xffffff
    };

    private jexi.ui.View view;
    private int index;
    private jexi.ui.Color color;

    public SelectColorHandler(jexi.ui.View view, int index) {
        this.view = view;
        this.index = index;
        this.color = jexi.ui.ColorFactory.instance().createColor(COLORS[index]);
    }

    // when select color:
    public void widgetSelected(SelectionEvent e) {
        view.onFormatChanged(null, null, null, null, null, color);
    }
}

⌨️ 快捷键说明

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