package pyUML.listeners; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IPropertyListener; import org.eclipse.ui.PlatformUI; import pyUML.backend.EclipseHelperMethods; import pyUML.backend.GlobalConstants; import pyUML.backend.UMLToolsHelperMethods; import pyUML.views.SynchronizeModelByView; /** * This listener synchronizes the global model and * all other pyUML.views whenever a view is saved */ public abstract class ViewChangeListener2 implements IPropertyListener { private IProject project; private static boolean listenerEnabled = true; public ViewChangeListener2(IProject project) { super(); this.project = project; } /** * Enables or disables all Listeners; this can be * useful to avoid circular listener calls * (model, view...) on a document change * @param enabled */ public static void setListenersEnabled(boolean enabled) { listenerEnabled = enabled; } /** @return true, if the View listeners are currently enabled, false otehrwise. */ public static boolean getListenersEnabled() { return listenerEnabled; } public void propertyChanged(Object source, int propId) { if (! ViewChangeListener2.listenerEnabled) return; if (source instanceof DiagramEditor) { // Wait until the Model is saved in file system try { Thread.sleep(1000); }catch (Exception e) {} DiagramEditor umlEditor = (DiagramEditor) source; if (! umlEditor.isDirty()) { try { umlEditor.getDiagram().eResource().save(null); } catch (Exception e){e.printStackTrace();} ViewChangeListener2.listenerEnabled = false; ModelChangeListener.setEnabled(false); // editor was just saved, otherwise it would be dirty. // -> synchronize model every time the view is saved String openEditorName=umlEditor.getTitle(); String viewPath = this.project.getLocation() .append(GlobalConstants.getPyUmlDir()) .append(openEditorName.replace(GlobalConstants.getDiagramExtension(), "") + GlobalConstants.getViewUmlExtension()).toOSString(); SynchronizeModelByView.synchronizeGlobalModel(viewPath, this.project); IFile globalModelDiagramFile = this.project.getWorkspace() .getRoot().getFile(this.project.getFullPath() .append(GlobalConstants.getPyUmlDir()) .append(project.getName()+GlobalConstants .getDiagramExtension())); // close all sub-package pyUML.views of global model diagram for ( org.eclipse.ui.IEditorReference editor : EclipseHelperMethods.lookForOpenEditorByName(project.getName()+"::.*")) { PlatformUI.getWorkbench().getActiveWorkbenchWindow() .getActivePage().closeEditor(editor.getEditor(true), true); } // after creating model, be sure the diagram view is reloaded /*boolean diagramOpened = null != EclipseHelperMethods .lookForOpenEditorByName(project.getName()+GlobalConstants.getDiagramExtension()); if (diagramOpened) { // set focus to updated diagram only if no diagram is currently active boolean keepFocus = false; try { keepFocus = PlatformUI.getWorkbench().getActiveWorkbenchWindow() .getActivePage().getActiveEditor().getTitle() .matches(".*"+GlobalConstants.getDiagramExtension()); } catch (Exception e) { e.printStackTrace(); } IEditorPart newPage = UMLToolsHelperMethods.refreshDiagramEditor(project, globalModelDiagramFile, keepFocus); if (newPage != null) newPage.addPropertyListener(new ViewChangeListener2(this.project)); }*/ UMLToolsHelperMethods.updateModelAndViewPages(this.project, openEditorName.replace(GlobalConstants.getDiagramExtension(), ""), false); // enable Listeners only after 4 seconds, when all diagrams are reloaded Thread t = new Thread( new Runnable() { public void run() { try { Thread.sleep(4000); System.out.println("finish"); } catch (InterruptedException e) { e.printStackTrace(); } ViewChangeListener2.listenerEnabled = true; ModelChangeListener.setEnabled(true); System.out.println(ViewChangeListener2.listenerEnabled); } }); t.start(); } } } }