Search in sources :

Example 1 with XmlStructureDialog

use of com.twinsoft.convertigo.eclipse.dialogs.XmlStructureDialog in project convertigo by convertigo.

the class SequenceImportFromXmlAction method run.

public void run() {
    Display display = Display.getDefault();
    Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
    Shell shell = getParentShell();
    shell.setCursor(waitCursor);
    try {
        ProjectExplorerView explorerView = getProjectExplorerView();
        if (explorerView != null) {
            DatabaseObjectTreeObject databaseObjectTreeObject = (DatabaseObjectTreeObject) explorerView.getFirstSelectedTreeObject();
            DatabaseObject databaseObject = databaseObjectTreeObject.getObject();
            SequenceTreeObject sequenceTreeObject = (SequenceTreeObject) ((databaseObject instanceof Sequence) ? databaseObjectTreeObject : databaseObjectTreeObject.getParentDatabaseObjectTreeObject());
            Sequence sequence = (databaseObject instanceof Sequence) ? (Sequence) databaseObject : ((StepWithExpressions) databaseObject).getSequence();
            // Open a file dialog to search a XML file
            FileDialog fileDialog = new FileDialog(shell, SWT.PRIMARY_MODAL | SWT.SAVE);
            fileDialog.setText("Import XML file");
            fileDialog.setFilterExtensions(new String[] { "*.xml" });
            fileDialog.setFilterNames(new String[] { "XML" });
            fileDialog.setFilterPath(Engine.PROJECTS_PATH);
            String filePath = fileDialog.open();
            if (filePath != null) {
                // Get XML content from the file
                File xmlFile = new File(filePath);
                Charset charset = XMLUtils.getEncoding(xmlFile);
                String xmlContent = FileUtils.readFileToString(xmlFile, charset.name());
                // Open and add XML content to the dialog area
                XmlStructureDialog dlg = new XmlStructureDialog(shell, sequence, xmlContent);
                if (dlg.open() == Window.OK) {
                    if (dlg.result instanceof Throwable) {
                        throw (Throwable) dlg.result;
                    } else {
                        Step step = (Step) dlg.result;
                        if (step != null) {
                            if (databaseObject instanceof Sequence) {
                                sequence.addStep(step);
                            } else {
                                StepWithExpressions swe = (StepWithExpressions) databaseObject;
                                swe.addStep(step);
                            }
                            sequence.hasChanged = true;
                            // Reload sequence in tree without updating its schema for faster reload
                            ConvertigoPlugin.logDebug("Reload sequence: start");
                            explorerView.reloadTreeObject(sequenceTreeObject);
                            ConvertigoPlugin.logDebug("Reload sequence: end");
                            // Select target dbo in tree
                            explorerView.objectSelected(new CompositeEvent(databaseObject));
                        }
                    }
                }
            }
        }
    } catch (Throwable e) {
        ConvertigoPlugin.logException(e, "Unable to import step from xml!");
    } finally {
        shell.setCursor(null);
        waitCursor.dispose();
    }
}
Also used : ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) SequenceTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.SequenceTreeObject) StepWithExpressions(com.twinsoft.convertigo.beans.core.StepWithExpressions) Charset(java.nio.charset.Charset) Sequence(com.twinsoft.convertigo.beans.core.Sequence) Step(com.twinsoft.convertigo.beans.core.Step) Cursor(org.eclipse.swt.graphics.Cursor) XmlStructureDialog(com.twinsoft.convertigo.eclipse.dialogs.XmlStructureDialog) Shell(org.eclipse.swt.widgets.Shell) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) CompositeEvent(com.twinsoft.convertigo.eclipse.editors.CompositeEvent) Display(org.eclipse.swt.widgets.Display)

Aggregations

DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)1 Sequence (com.twinsoft.convertigo.beans.core.Sequence)1 Step (com.twinsoft.convertigo.beans.core.Step)1 StepWithExpressions (com.twinsoft.convertigo.beans.core.StepWithExpressions)1 XmlStructureDialog (com.twinsoft.convertigo.eclipse.dialogs.XmlStructureDialog)1 CompositeEvent (com.twinsoft.convertigo.eclipse.editors.CompositeEvent)1 ProjectExplorerView (com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView)1 DatabaseObjectTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject)1 SequenceTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.SequenceTreeObject)1 File (java.io.File)1 Charset (java.nio.charset.Charset)1 Cursor (org.eclipse.swt.graphics.Cursor)1 Display (org.eclipse.swt.widgets.Display)1 FileDialog (org.eclipse.swt.widgets.FileDialog)1 Shell (org.eclipse.swt.widgets.Shell)1