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

📄 ebookmaker.java

📁 JAVA手机的电子书编辑器以及阅读器
💻 JAVA
字号:
/*
 * Created on 2004-10-22
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.zmc.ebook.maker;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Vector;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
 *EBookMaker <br>
 * Copyright (c) 2000, 2004 e-Channels Corporation<br>
 * <br>
 * Created on 2004-10-22<br>
 * @author ZHONGMC<br>
 *<br>
 * Last modified on 2004-10-22<br>
 * Last modified by ZHONGMC<br>
 *
 *<p>Comment here</p>
 *
 */
public class EBookMaker extends Composite
{

    private Text chapterNameText;

    private Text contentText;

    private List chapterList;

    private Text bookNameText;

    private Vector contents = new Vector(10);

    public EBookMaker(Composite parent, int style)
    {
        super(parent, style);
        final GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        setLayout(gridLayout);

        final Button button_3 = new Button(this, SWT.NONE);
        button_3.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                saveFile();
            }
        });
        button_3.setText("SaveFile");

        final Button button_4 = new Button(this, SWT.NONE);
        button_4.setText("OpenFile");

        final Label label = new Label(this, SWT.NONE);
        label.setText("BookName:");

        bookNameText = new Text(this, SWT.BORDER);
        final GridData gridData_1 = new GridData();
        gridData_1.widthHint = 235;
        bookNameText.setLayoutData(gridData_1);

        final SashForm sashForm = new SashForm(this, SWT.NONE);

        final Composite composite = new Composite(sashForm, SWT.NONE);
        final GridLayout gridLayout_1 = new GridLayout();
        gridLayout_1.numColumns = 2;
        composite.setLayout(gridLayout_1);

        final Label label_1 = new Label(composite, SWT.NONE);
        final GridData gridData_4 = new GridData();
        gridData_4.horizontalSpan = 2;
        label_1.setLayoutData(gridData_4);
        label_1.setText("Chapaters");

        chapterList = new List(composite, SWT.BORDER);
        chapterList.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e)
            {
                setActivateChapter();
            }
        });
        final GridData gridData_5 = new GridData(GridData.FILL_BOTH);
        gridData_5.horizontalSpan = 2;
        chapterList.setLayoutData(gridData_5);

        final Composite composite_1 = new Composite(sashForm, SWT.NONE);
        composite_1.setLayout(new GridLayout());

        final Label label_3 = new Label(composite_1, SWT.NONE);
        label_3.setText("ChapterName:");

        chapterNameText = new Text(composite_1, SWT.BORDER);
        chapterNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        final Composite composite_2 = new Composite(composite_1, SWT.NONE);
        final GridLayout gridLayout_2 = new GridLayout();
        gridLayout_2.numColumns = 4;
        composite_2.setLayout(gridLayout_2);

        final Button button_1 = new Button(composite_2, SWT.NONE);
        final GridData gridData_2 = new GridData();
        gridData_2.widthHint = 80;
        button_1.setLayoutData(gridData_2);
        button_1.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e)
            {
                addChapter();
            }
        });
        button_1.setText("add");

        final Button button = new Button(composite_2, SWT.NONE);
        final GridData gridData_3 = new GridData();
        gridData_3.widthHint = 80;
        button.setLayoutData(gridData_3);
        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e)
            {
                deleteChapter();
            }
        });
        button.setText("delete");

        final Button button_2 = new Button(composite_2, SWT.NONE);
        final GridData gridData_6 = new GridData();
        gridData_6.widthHint = 80;
        button_2.setLayoutData(gridData_6);
        button_2.setText("update");

        final Label label_2 = new Label(composite_1, SWT.NONE);
        label_2.setText("Chapater Content:");

        contentText = new Text(composite_1, SWT.BORDER | SWT.H_SCROLL | SWT.MULTI | SWT.V_SCROLL);
        contentText.setLayoutData(new GridData(GridData.FILL_BOTH));
        sashForm.setWeights(new int[] { 1, 1 });
        final GridData gridData = new GridData(GridData.FILL_BOTH);
        gridData.horizontalSpan = 2;
        sashForm.setLayoutData(gridData);
        //
    }

    private void addChapter()
    {
        String chapterName = this.chapterNameText.getText();
        if (chapterName.length() == 0)
            return;

        String content = this.contentText.getText();
        if (content.length() == 0)
            return;

        String[] items = chapterList.getItems();
        this.chapterList.add(chapterName);

        this.contents.addElement(content);

    }

    private void deleteChapter()
    {
        int idx = this.chapterList.getSelectionIndex();
        if (idx == -1)
            return;

        this.chapterList.remove(idx);
        this.contents.removeElementAt(idx);
    }

    private void setActivateChapter()
    {
        int idx = this.chapterList.getSelectionIndex();
        if (idx == -1)
            return;

        String chapterName = chapterList.getItem(idx);
        String content = (String) contents.elementAt(idx);

        this.chapterNameText.setText(chapterName);
        this.contentText.setText(content);
    }

    public void saveToFile(String path)
    {
        try
        {
            String fileName;
            fileName = path + "/meta.txt";
            FileOutputStream fo = new FileOutputStream(fileName);
            for (int i = 0; i < contents.size(); i++)
            {
                fileName = path + "/Chapter" + i + ".txt";
                //Chapter-1: /chapter1.txt
                String chapter = "Chapter-" + i + ": /Chapter" + i + ".txt\n";
                fo.write(chapter.getBytes());
                String tmp = "ChapterTitle-" + i + ": ";
                fo.write(tmp.getBytes());
                String chapterName = this.chapterList.getItem(i);
                fo.write(chapterName.getBytes("UTF-8"));
                fo.write("\n".getBytes());
                //ChapterTitle-2: JPEG BW
                String content = (String) contents.elementAt(i);
                FileOutputStream fo1 = new FileOutputStream(fileName);
                fo1.write(content.getBytes("UTF-8"));
                fo1.close();
            }

            fo.close();

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

	public	static void main(String[] argv )
	{

		Display display;
		display = new Display ();
        final Shell shell = new Shell (display);
        shell.setLayout(new FillLayout());
       
//        shell.setText("Test Frame");


        EBookMaker editor = new EBookMaker( shell, SWT.BORDER );
        
        
    
         shell.open ();

		   // start the event loop. We stop when the user has done
		   // something to dispose our window.
		   while (!shell.isDisposed ()) {
			  if (!display.readAndDispatch ())
				 display.sleep ();
		   }
		   display.dispose ();
	    
	    
	}

	
	private void openFile()
	{
	    
	    
	}
	
	private void saveFile()
	{
		try{
			FileDialog fileDlg = new FileDialog( this.getShell() );
			String afileName = fileDlg.open();
			if( afileName == null )
				return;

			String fileName = fileDlg.getFileName();
			
			saveToFile("e:\\temp\\ebook");


		}catch(Exception e)
		{
			e.printStackTrace();
		}
	    
	}
	
    
    public void dispose()
    {
        super.dispose();
    }

    protected void checkSubclass()
    {
    }
}

⌨️ 快捷键说明

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