algsymboleditor.wizards.NewFencedWizard.java Source code

Java tutorial

Introduction

Here is the source code for algsymboleditor.wizards.NewFencedWizard.java

Source

//$$strtCprt
/**
* OEEE (Oversimplified [MathML] Expression Editor for Eclipse)
* 
* Copyright (C) 2016 Thornton Green
* 
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program; if not, 
* see <http://www.gnu.org/licenses>.
* Additional permission under GNU GPL version 3 section 7
*
*/
//$$endCprt

package algsymboleditor.wizards;

import meta.FlexString;
import meta.HighLevelList;
import meta.StdLowLevelList;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.operation.*;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;

import java.io.*;

import org.eclipse.ui.*;
import org.eclipse.ui.ide.IDE;

import algsymboleditor.editors.FencedEditorCanvas;

/**
 * Wizard for generating new algfenced files.
 *
 */
public class NewFencedWizard extends Wizard implements INewWizard {

    /**
     * Enclosed wizard page.
     */
    private NewFencedWizardPage page;

    /**
     * Enclosed selection.
     */
    private ISelection selection;

    /**
     * Constructor for NewFencedWizard.
     */
    public NewFencedWizard() {
        super();
        setNeedsProgressMonitor(true);
    }

    @Override
    public void addPages() {
        page = new NewFencedWizardPage(selection);
        addPage(page);
    }

    @Override
    public boolean performFinish() {
        final String containerName = page.getContainerName();
        final String fileName = page.getFileName();
        IRunnableWithProgress op = new IRunnableWithProgress() {
            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException {
                try {
                    doFinish(containerName, fileName, monitor);
                } catch (CoreException e) {
                    throw new InvocationTargetException(e);
                } finally {
                    monitor.done();
                }
            }
        };
        try {
            getContainer().run(true, false, op);
        } catch (InterruptedException e) {
            return false;
        } catch (InvocationTargetException e) {
            Throwable realException = e.getTargetException();
            MessageDialog.openError(getShell(), "Error", realException.getMessage());
            return false;
        }
        return true;
    }

    /**
     * Generates the result file in response to a request to finish the wizard.
     * @param containerName The containing path in which to generate the file.
     * @param fileName The name of the file to be created.
     * @param monitor Progress monitor for the creation operation.
     * @throws CoreException
     */
    private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException {
        // create a sample file
        monitor.beginTask("Creating " + fileName, 2);
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IResource resource = root.findMember(new Path(containerName));
        if (!resource.exists() || !(resource instanceof IContainer)) {
            throwCoreException("Container \"" + containerName + "\" does not exist.");
        }
        IContainer container = (IContainer) resource;
        final IFile file = container.getFile(new Path(fileName));
        try {
            InputStream stream = openContentStream();
            if (file.exists()) {
                file.setContents(stream, true, true, monitor);
            } else {
                file.create(stream, true, monitor);
            }
            stream.close();
        } catch (IOException e) {
        }
        monitor.worked(1);
        monitor.setTaskName("Opening file for editing...");
        getShell().getDisplay().asyncExec(new Runnable() {
            @Override
            public void run() {
                IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                try {
                    IDE.openEditor(page, file, true);
                } catch (PartInitException e) {
                }
            }
        });
        monitor.worked(1);
    }

    /**
     * Returns an InputStream containing the contents to be placed in the destination file.
     * @return The InputStream containing the contents to be placed in the destination file.
     */
    private InputStream openContentStream() {
        try {
            final HighLevelList<StdLowLevelList<FlexString>, FlexString> fencedscriptLst = new HighLevelList<StdLowLevelList<FlexString>, FlexString>();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            FencedEditorCanvas.writeAlgFencedEditor(fencedscriptLst, FencedEditorCanvas.InsertModes.CIRC_MODE,
                    baos);
            ByteArrayInputStream stream = new ByteArrayInputStream(baos.toByteArray());
            return (stream);
        } catch (IOException ex) {
            ex.printStackTrace(System.out);
            throw (new RuntimeException(ex.toString()));
        }
    }

    /**
     * Throws a CoreException.
     * @param message The message string to be placed in the exception.
     * @throws CoreException
     */
    private void throwCoreException(String message) throws CoreException {
        IStatus status = new Status(IStatus.ERROR, "Algebra Fenced Editor", IStatus.OK, message, null);
        throw new CoreException(status);
    }

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {
        this.selection = selection;
    }

}