/** Used when the context is switched. Installs the undoMa */ public void update() { try { actualProjectDiagram = ModelerSession.getProjectService().getSelectedDiagram(); actualUCDiagramModel = (UCDiagramModel) actualProjectDiagram.getDiagramModel(); actualUCDiagramModel.installUndoActions( actions.get(UCNotationModel.UNDO_ACTION_KEY), actions.get(UCNotationModel.REDO_ACTION_KEY)); actions .get(UCNotationModel.UNDO_ACTION_KEY) .setEnabled(actualUCDiagramModel.getUndoManager().canUndo()); actions .get(UCNotationModel.REDO_ACTION_KEY) .setEnabled(actualUCDiagramModel.getUndoManager().canRedo()); final ProjectDiagram projectDiagram = ModelerSession.getProjectService().getSelectedDiagram(); projectDiagram.addChangeListener(this); actions.get(UCNotationModel.SAVE_ACTION_KEY).setEnabled(projectDiagram.isChanged()); } catch (ClassCastException exception) { LOG.error("Unable to cast selected diagram model to UCDiagramModel class.", exception); } catch (Exception exception) { LOG.error("An error has occurred during context switch.", exception); } this.setTitleAt(0, actualProjectDiagram.getDisplayName()); ((UCTabParent) this.getComponent(0)).update(); }
/** * Publishes file system item write error. * * @param projectDiagram is the diagram that is supposed to be written * @param projectItemFile is the file path to the not existing file of diagram */ private void publicAddDiagramError( final ProjectDiagram projectDiagram, final File projectItemFile) { syncDialog.appendErrorInfo(DIAGRAM_LABEL + " " + projectDiagram.getDisplayName(), false); syncDialog.appendErrorInfo( NOTATION_LABEL + " " + projectDiagram.getNotationIdentifier(), false); syncDialog.appendErrorInfo(DIAGRAM_WRITE_ERROR_LABEL, false); syncDialog.appendErrorInfo(" >> " + projectItemFile.getAbsolutePath(), true); LOG.error( "Sync PN -> FS: Diagram " + projectItemFile.getAbsolutePath() + " has NOT been saved."); }
/** * Publishes file system item overwrite error. * * @param projectDiagram is the diagram that is supposed to be overwritten * @param projectItemFile is the file path to the existing file of diagram */ private void publicOverwriteError( final ProjectDiagram projectDiagram, final File projectItemFile) { syncDialog.appendErrorInfo(DIAGRAM_LABEL + " " + projectDiagram.getDisplayName(), false); syncDialog.appendErrorInfo( NOTATION_LABEL + " " + projectDiagram.getNotationIdentifier(), false); syncDialog.appendErrorInfo(DIAGRAM_OVERWRITE_ERROR_LABEL, false); syncDialog.appendErrorInfo(" >> " + projectItemFile.getAbsolutePath(), true); LOG.error( "Sync PN -> FS: Diagram " + projectItemFile.getAbsolutePath() + " was NOT possible to overwrite."); }
/** * {@inheritDoc} * * <p>Un-install the last UC notation diagram's listeners, sets the UNDO & REDO action as disable * and makes the actualUCDiagramModel variable null (actual UC notation diagram is none). */ public void over() { if (actualUCDiagramModel != null) { actualUCDiagramModel.uninstallUndoActions(); } else { LOG.error( "over() method of UC notation workspace has been invoked, but there hasn't been set any" + "actual UC notation diagram before."); } if (actualProjectDiagram != null) { actualProjectDiagram.removeChangeListener(this); } }
/** * Synchronize the selected project subtree to the file system. This methods requires the correct * starting level on the file system with respect to the project subtree to exist. * * <p>Important to note that this method synchronize only the SUBTREE 'under' the last tree node * in the treePath variable. * * @param fsLevel is the starting point for the synchronization on the file system * @param treePath is the starting point in the project structure for the synchronization * @param addProjectItems indicates whether new items are supposed to be created on the file * system if they are missing * @param overwriteProjectItems indicates whether diagrams that already exists on the file system * are supposed to be overwritten * @param deleteProjectItems indicates whether not projects items (files or diagrams) are supposed * to be deleted from file system * @return true if and only if no error occurs, false otherwise * @throws InterruptedException when user cancels the synchronization */ private boolean syncSubtreeToFS( final File fsLevel, final TreePath treePath, final boolean addProjectItems, final boolean overwriteProjectItems, final boolean deleteProjectItems) throws InterruptedException { checkInterruption(); boolean retValue = true; final DefaultMutableTreeNode lastTreePathNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); final Object userObject = lastTreePathNode.getUserObject(); final ProjectItem projectItem; projectItem = (ProjectItem) userObject; // sync a project diagram if (userObject instanceof ProjectDiagram) { final ProjectDiagram projectDiagram = (ProjectDiagram) userObject; final String fileExtension = ProjectServiceUtils.getNotationFileExtension(projectDiagram.getNotationIdentifier()); final File projectItemFile = new File(fsLevel.getAbsolutePath(), projectItem.getDisplayName() + fileExtension); NotationLocalIOController.SaveResult successSaveResult = NotationLocalIOController.SaveResult.SUCCESS; if (projectItemFile.exists() && overwriteProjectItems) { successSaveResult = ModelerSession.getNotationService() .getNotation(projectDiagram.getNotationIdentifier()) .getLocalIOController() .saveProjectDiagram(projectDiagram, projectItemFile.getParent(), false); if (successSaveResult == NotationLocalIOController.SaveResult.SUCCESS) { LOG.debug( "Sync PN -> FS: Diagram " + projectItemFile.getAbsolutePath() + " has been overwritten."); } else { publicOverwriteError(projectDiagram, projectItemFile); } } else if (!projectItemFile.exists() && addProjectItems) { successSaveResult = ModelerSession.getNotationService() .getNotation(projectDiagram.getNotationIdentifier()) .getLocalIOController() .saveProjectDiagram(projectDiagram, projectItemFile.getParent(), false); if (successSaveResult == NotationLocalIOController.SaveResult.SUCCESS) { LOG.debug( "Sync PN -> FS: Diagram " + projectItemFile.getAbsolutePath() + " has been saved."); } else { publicAddDiagramError(projectDiagram, projectItemFile); } } if (successSaveResult != NotationLocalIOController.SaveResult.SUCCESS) { retValue = false; } // user object is project root or a subfolder } else if (userObject instanceof ProjectContainer) { final Set<String> synchronizedItems = new HashSet<String>(); if (userObject instanceof ProjectRoot) { // add project file (.pmp) to the list of synchronizedItems = do not delete it synchronizedItems.add( (((ProjectItem) userObject)).getDisplayName() + ProjectService.PROJECT_FILE_EXTENSION); } for (int i = 0; i < lastTreePathNode.getChildCount(); i++) { checkInterruption(); final DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) lastTreePathNode.getChildAt(i); final ProjectItem projectItemOfChildNode = (ProjectItem) defaultMutableTreeNode.getUserObject(); final File projectItemFile; if (projectItemOfChildNode instanceof ProjectContainer) { // project containers are without file extension projectItemFile = new File(fsLevel.getAbsolutePath(), projectItemOfChildNode.getDisplayName()); } else if (projectItemOfChildNode instanceof ProjectDiagram) { // project diagram files do have a file extension final String fileExtension = ProjectServiceUtils.getNotationFileExtension( ((ProjectDiagram) projectItemOfChildNode).getNotationIdentifier()); projectItemFile = new File( fsLevel.getAbsolutePath(), projectItemOfChildNode.getDisplayName() + fileExtension); } else { LOG.error("A project item has to be either project container or project diagram."); syncDialog.appendErrorInfo(INVALID_PARENT_ERROR_LABEL, true); return false; } if (projectItemFile.exists()) { if (projectItemOfChildNode instanceof ProjectDiagram) { retValue &= syncSubtreeToFS( projectItemFile.getParentFile(), treePath.pathByAddingChild(defaultMutableTreeNode), addProjectItems, overwriteProjectItems, deleteProjectItems); } else { /* next child is container */ retValue &= syncSubtreeToFS( projectItemFile, treePath.pathByAddingChild(defaultMutableTreeNode), addProjectItems, overwriteProjectItems, deleteProjectItems); } synchronizedItems.add(projectItemFile.getName()); } else if (!projectItemFile.exists() && addProjectItems) { /* next child is diagram */ if (projectItemOfChildNode instanceof ProjectDiagram) { final String fileExtension = ProjectServiceUtils.getNotationFileExtension( ((ProjectDiagram) projectItemOfChildNode).getNotationIdentifier()); retValue &= syncSubtreeToFS( projectItemFile.getParentFile(), treePath.pathByAddingChild(defaultMutableTreeNode), addProjectItems, overwriteProjectItems, deleteProjectItems); synchronizedItems.add(projectItemOfChildNode.getDisplayName() + fileExtension); } else { /* next child is container */ if (projectItemFile.mkdir()) { // make new subfolder LOG.debug( "Sync PN -> FS: new subfolder has been created, " + projectItemFile.getAbsolutePath() + "."); retValue &= syncSubtreeToFS( projectItemFile, treePath.pathByAddingChild(defaultMutableTreeNode), addProjectItems, overwriteProjectItems, deleteProjectItems); synchronizedItems.add(projectItemOfChildNode.getDisplayName()); } else { publicMkdirError(projectItemFile); } } } } // delete non PN items in this fs level if (deleteProjectItems) { final String[] fsLevelItems = fsLevel.list(); for (final String fsItem : fsLevelItems) { if (!synchronizedItems.contains(fsItem)) { // delete not PN item from FS final File itemToDelete = new File(fsLevel.getAbsolutePath(), fsItem); if (ProjectServiceUtils.removeDirectory(itemToDelete)) { LOG.debug("File system item has been deleted, " + itemToDelete.getAbsolutePath()); } else { publicDeleteError(itemToDelete); retValue = false; } } } } } return retValue; }