Search in sources :

Example 1 with TreeObjectListener

use of com.twinsoft.convertigo.eclipse.views.projectexplorer.TreeObjectListener in project convertigo by convertigo.

the class DatabaseObjectDeleteAction method run.

public void run() {
    Display display = Display.getDefault();
    Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
    Shell shell = getParentShell();
    shell.setCursor(waitCursor);
    try {
        treeNodesToUpdate = new ArrayList<>();
        ProjectExplorerView explorerView = getProjectExplorerView();
        if (explorerView != null) {
            TreeObject[] selectedTreeObjects = explorerView.getSelectedTreeObjects();
            if (selectedTreeObjects != null) {
                Collection<DatabaseObjectTreeObject> treeObjects = new HashSet<>(selectedTreeObjects.length);
                for (TreeObject t : Arrays.asList(selectedTreeObjects)) {
                    if (t instanceof DatabaseObjectTreeObject) {
                        treeObjects.add((DatabaseObjectTreeObject) t);
                    }
                }
                ;
                if (treeObjects.size() > 1) {
                    for (DatabaseObjectTreeObject t : new ArrayList<>(treeObjects)) {
                        TreeObject parent = t.getParent();
                        while (parent != null) {
                            if (treeObjects.contains(parent)) {
                                treeObjects.remove(t);
                                parent = null;
                            } else {
                                parent = parent.getParent();
                            }
                        }
                    }
                    ;
                }
                if (treeObjects.size() > 1) {
                    List<DatabaseObjectTreeObject> list = new ArrayList<>(treeObjects);
                    Collections.sort(list, new Comparator<DatabaseObjectTreeObject>() {

                        @Override
                        public int compare(DatabaseObjectTreeObject o1, DatabaseObjectTreeObject o2) {
                            if (o1 instanceof ProjectTreeObject) {
                                if (o2 instanceof ProjectTreeObject) {
                                    return o1.getName().compareTo(o2.getName());
                                }
                                return -1;
                            } else if (o2 instanceof ProjectTreeObject) {
                                return 1;
                            }
                            return o1.getName().compareTo(o2.getName());
                        }
                    });
                    treeObjects = list;
                }
                MultipleDeletionDialog dialog = new MultipleDeletionDialog(shell, "Object Deletion", treeObjects.size() != 1);
                for (DatabaseObjectTreeObject treeObject : treeObjects) {
                    String message = java.text.MessageFormat.format("Do you really want to delete the {0} \"{1}\" and all its sub-objects?", treeObject instanceof ProjectTreeObject ? "project" : "object", treeObject.getName());
                    if (treeObject instanceof ProjectTreeObject) {
                        message += "\nProject location: " + ((Project) treeObject.getObject()).getDirPath();
                        dialog.setToggle("Delete project content on disk (cannot be undone)", false);
                    } else {
                        dialog.removeToggle();
                    }
                    if (!dialog.shouldBeDeleted(message)) {
                        continue;
                    }
                    try {
                        if (treeObject instanceof ProjectTreeObject) {
                            ((ProjectTreeObject) treeObject).closeAllEditors();
                        } else if (treeObject instanceof SequenceTreeObject) {
                            ((ProjectTreeObject) ((SequenceTreeObject) treeObject).getParent().getParent()).closeSequenceEditors((Sequence) treeObject.getObject());
                        } else if (treeObject instanceof ConnectorTreeObject) {
                            ((ProjectTreeObject) ((ConnectorTreeObject) treeObject).getParent().getParent()).closeConnectorEditors((Connector) treeObject.getObject());
                        } else if (treeObject instanceof StepTreeObject) {
                            // We close the editor linked with the SimpleStep (=SequenceJsStep)
                            if (treeObject.getObject() instanceof SimpleStep) {
                                boolean find = false;
                                SimpleStep simpleStep = (SimpleStep) treeObject.getObject();
                                IWorkbenchPage page = this.getActivePage();
                                IEditorReference[] editors = page.getEditorReferences();
                                int _i = 0;
                                while (find != true && _i < editors.length) {
                                    IEditorReference editor = editors[_i];
                                    IEditorInput input = editor.getEditorInput();
                                    if (input instanceof JScriptEditorInput) {
                                        if (simpleStep.equals(((JScriptEditorInput) input).getDatabaseObject())) {
                                            find = true;
                                            IEditorPart editorPart = page.findEditor(input);
                                            if (editorPart != null) {
                                                page.activate(editorPart);
                                                page.closeEditor(editorPart, false);
                                            }
                                        }
                                    }
                                    ++_i;
                                }
                            }
                        } else if (treeObject instanceof MobileComponentTreeObject) {
                            ((MobileComponentTreeObject) treeObject).closeAllEditors(false);
                        }
                        if (treeObject instanceof ProjectTreeObject) {
                            explorerView.removeProjectTreeObject(treeObject);
                            final Project project = (Project) treeObject.getObject();
                            Job rmProject = new Job("Remove '" + project.getName() + "' project") {

                                @Override
                                protected IStatus run(IProgressMonitor monitor) {
                                    try {
                                        delete(project, dialog.getToggleState());
                                    } catch (Exception e) {
                                        ConvertigoPlugin.logException(e, "Unable to delete the '" + project.getName() + "' project.");
                                        return new MultiStatus(ConvertigoPlugin.PLUGIN_UNIQUE_ID, IStatus.ERROR, "Failed to remove the '" + project.getName() + "' project.", e);
                                    }
                                    return Status.OK_STATUS;
                                }
                            };
                            rmProject.schedule();
                        } else {
                            delete(treeObject);
                            // prevents treeObject and its childs to receive further TreeObjectEvents
                            if (treeObject instanceof TreeObjectListener) {
                                explorerView.removeTreeObjectListener(treeObject);
                            }
                            treeObject.removeAllChildren();
                        }
                        explorerView.fireTreeObjectRemoved(new TreeObjectEvent(treeObject));
                    } catch (Exception e) {
                        ConvertigoPlugin.logException(e, "Unable to delete the current selected object.");
                    }
                }
                ;
                // Updating the tree and the properties panel
                Enumeration<DatabaseObjectTreeObject> enumeration = Collections.enumeration(treeNodesToUpdate);
                DatabaseObjectTreeObject parentTreeObject;
                while (enumeration.hasMoreElements()) {
                    parentTreeObject = enumeration.nextElement();
                    if (parentTreeObject != null) {
                        explorerView.reloadTreeObject(parentTreeObject);
                        explorerView.setSelectedTreeObject(parentTreeObject);
                    }
                }
                // Refresh tree to show potential 'broken' steps
                explorerView.refreshTree();
            }
        }
    } catch (Throwable e) {
        ConvertigoPlugin.logException(e, "Unable to delete object!");
    } finally {
        shell.setCursor(null);
        waitCursor.dispose();
    }
}
Also used : JScriptEditorInput(com.twinsoft.convertigo.eclipse.editors.jscript.JScriptEditorInput) DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) SimpleStep(com.twinsoft.convertigo.beans.steps.SimpleStep) StepTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.StepTreeObject) ArrayList(java.util.ArrayList) MultiStatus(org.eclipse.core.runtime.MultiStatus) MultipleDeletionDialog(com.twinsoft.convertigo.eclipse.dialogs.MultipleDeletionDialog) Cursor(org.eclipse.swt.graphics.Cursor) Shell(org.eclipse.swt.widgets.Shell) IEditorReference(org.eclipse.ui.IEditorReference) TreeObjectEvent(com.twinsoft.convertigo.eclipse.views.projectexplorer.TreeObjectEvent) MobileComponentTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.MobileComponentTreeObject) Job(org.eclipse.core.runtime.jobs.Job) HashSet(java.util.HashSet) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) SequenceTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.SequenceTreeObject) Sequence(com.twinsoft.convertigo.beans.core.Sequence) IEditorPart(org.eclipse.ui.IEditorPart) CoreException(org.eclipse.core.runtime.CoreException) EngineException(com.twinsoft.convertigo.engine.EngineException) ConvertigoException(com.twinsoft.convertigo.engine.ConvertigoException) IOException(java.io.IOException) ConnectorTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.ConnectorTreeObject) Project(com.twinsoft.convertigo.beans.core.Project) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TreeObjectListener(com.twinsoft.convertigo.eclipse.views.projectexplorer.TreeObjectListener) SequenceTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.SequenceTreeObject) ProjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.ProjectTreeObject) StepTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.StepTreeObject) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) DatabaseObjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject) ConnectorTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.ConnectorTreeObject) MobileComponentTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.MobileComponentTreeObject) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ProjectTreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.ProjectTreeObject) IEditorInput(org.eclipse.ui.IEditorInput) Display(org.eclipse.swt.widgets.Display)

Aggregations

Project (com.twinsoft.convertigo.beans.core.Project)1 Sequence (com.twinsoft.convertigo.beans.core.Sequence)1 SimpleStep (com.twinsoft.convertigo.beans.steps.SimpleStep)1 MultipleDeletionDialog (com.twinsoft.convertigo.eclipse.dialogs.MultipleDeletionDialog)1 JScriptEditorInput (com.twinsoft.convertigo.eclipse.editors.jscript.JScriptEditorInput)1 ProjectExplorerView (com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView)1 TreeObjectEvent (com.twinsoft.convertigo.eclipse.views.projectexplorer.TreeObjectEvent)1 TreeObjectListener (com.twinsoft.convertigo.eclipse.views.projectexplorer.TreeObjectListener)1 ConnectorTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.ConnectorTreeObject)1 DatabaseObjectTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.DatabaseObjectTreeObject)1 MobileComponentTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.MobileComponentTreeObject)1 ProjectTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.ProjectTreeObject)1 SequenceTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.SequenceTreeObject)1 StepTreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.StepTreeObject)1 TreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject)1 ConvertigoException (com.twinsoft.convertigo.engine.ConvertigoException)1 EngineException (com.twinsoft.convertigo.engine.EngineException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1