package pyUML.views; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorPart; import org.eclipse.uml2.uml.Model; import pyUML.actions.ManageViewsAction; import pyUML.backend.GlobalConstants; import pyUML.backend.JavaHelperMethods; import pyUML.backend.UMLToolsHelperMethods; import pyUML.listeners.ViewChangeListener; /** * This is the dialog for managing pyUML.views. * Views can be selected, deleted, edited and created. * A listener is implemented with this class, so here * is defined what happens when you press buttons in the dialog. */ public class ManageViewsPage extends Dialog implements SelectionListener{ Composite composite = null; Group viewsBoxGroup = null; List viewsBox = null; Button openButton = null; Button editButton = null; Button deleteButton = null; Button createNewButton = null; Model model; IProject project; Shell shell=null; public ManageViewsPage(Shell parent, Model model, IProject project){ super(parent); this.model= model; this.project=project; } protected Control createDialogArea(Composite parent) { this.composite = (Composite) super.createDialogArea(parent); setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX); createControls(composite); initializeDialog(); //add controls to composite as necessary return composite; } public void createControls (Composite composite) { // create graphical components shell = composite.getShell(); shell.setText("Manage UML Views"); viewsBoxGroup = new Group(composite, SWT.None); viewsBoxGroup.setText("Available Views"); viewsBoxGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); viewsBoxGroup.setLayout(new GridLayout(1, true)); viewsBox = new List( viewsBoxGroup, SWT.BORDER|SWT.SINGLE|SWT.V_SCROLL|SWT.H_SCROLL ); viewsBox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); Composite buttonGroup = new Composite(viewsBoxGroup, SWT.NONE); buttonGroup.setLayoutData(new GridData(GridData.FILL_BOTH)); buttonGroup.setLayout(new GridLayout(3, true)); viewsBox.setSize(300,300); openButton = new Button(buttonGroup, SWT.PUSH); openButton.setText("Open View"); openButton.addSelectionListener(this); editButton = new Button(buttonGroup, SWT.PUSH); editButton.setText("Edit View"); editButton.addSelectionListener(this); deleteButton = new Button(buttonGroup, SWT.PUSH); deleteButton.setText("Delete View"); deleteButton.addSelectionListener(this); buttonGroup.pack(); viewsBoxGroup.pack(); setElementEnabled(false, viewsBoxGroup); createNewButton = new Button(composite, SWT.PUSH); createNewButton.setText("Create New View"); createNewButton.setLayoutData(new GridData(GridData.FILL_BOTH)); createNewButton.addSelectionListener(this); composite.pack(); } /** * Sets a composite element and all children enabled/disabled * @param enabled * @param element */ public static void setElementEnabled(boolean enabled, Control element) { element.setEnabled(enabled); if (element instanceof Composite) { Composite comp = (Composite) element; for (Control child : comp.getChildren()) { setElementEnabled(enabled, child); } } } public void widgetDefaultSelected(SelectionEvent e) { // Auto-generated method stub } /** * Handles pressed-button events and runs appropriate pyUML.actions */ public void widgetSelected(SelectionEvent e) { // "CREATE NEW" BUTTON if (e.getSource() == this.createNewButton) { this.createDialog(); // re-open View management window this.cancelPressed(); } //DELETE BUTTON else if (e.getSource() == this.deleteButton) { // return if nothing was selected if (this.viewsBox.getSelection().length <= 0) return; // warn before deletion if (MessageDialog.openConfirm(null, "Confirm deletion of view", "Do you really want to delete this view?") == false) return; // delete view configuration, UML model and diagram String selectedElement = this.viewsBox.getSelection()[0]; IPath conFilePath = project.getFullPath().append(GlobalConstants.getPyUmlDir()) .append(selectedElement+GlobalConstants.getViewConfExtension()); IFile confFile = this.project.getWorkspace().getRoot().getFile(conFilePath); IPath umlFilePath = project.getFullPath().append(GlobalConstants.getPyUmlDir()) .append(selectedElement+GlobalConstants.getViewUmlExtension()); IFile umlFile = this.project.getWorkspace().getRoot().getFile(umlFilePath); IPath diagramFilePath = project.getFullPath().append(GlobalConstants.getPyUmlDir()) .append(selectedElement+GlobalConstants.getDiagramExtension()); IFile diagramFile = this.project.getWorkspace().getRoot().getFile(diagramFilePath); try { if (confFile.exists()) confFile.delete(false, null); if (umlFile.exists()) umlFile.delete(false, null); if (diagramFile.exists()) diagramFile.delete(false, null); } catch (CoreException err) { MessageDialog.openError(null, "Error deleting element", "Could not delete view. Reason:\n"+err.getMessage()); } this.cancelPressed(); ManageViewsAction.run(project); // OPEN BUTTON } else if (e.getSource() == this.openButton) { if (this.viewsBox.getSelection().length <= 0) return; String ViewUmlFileName = this.viewsBox.getSelection()[0]+GlobalConstants.getViewUmlExtension(); String ViewConfFileName = this.viewsBox.getSelection()[0]+GlobalConstants.getViewConfExtension(); String selectedElement = this.viewsBox.getSelection()[0]+GlobalConstants.getDiagramExtension(); IFile viewDiagramFile = this.project.getWorkspace().getRoot(). getFile(project.getFullPath().append(GlobalConstants.getPyUmlDir()). append(selectedElement)); IFile viewConfFile = this.project.getWorkspace().getRoot(). getFile(project.getFullPath().append(GlobalConstants.getPyUmlDir()). append(ViewConfFileName)); if (! viewDiagramFile.exists()) { MessageDialog.openError(null, "Error opening view", "The selected view could not be opened;\n" + "The UML diagram does not seem to exist!"); } else { UMLToolsHelperMethods.updateViewModel(project, viewConfFile); IEditorPart newPage = null; try { newPage = UMLToolsHelperMethods.refreshDiagramEditor(this.project, viewDiagramFile, false); } catch (Throwable t) { t.printStackTrace(); } if (newPage == null) newPage = UMLToolsHelperMethods.openDiagram(this.project, viewDiagramFile, ViewUmlFileName, true); if ( newPage == null) { MessageDialog.openError(null, "Error opening view", "The selected view could not be opened!"); } else { newPage.addPropertyListener(new ViewChangeListener(this.project)); this.close(); } } } // EDIT BUTTON else if (e.getSource() == this.editButton) { String selectedViewName = this.viewsBox.getSelection()[0]; IFile confFile = this.project.getWorkspace().getRoot().getFile(project.getFullPath().append(GlobalConstants.getPyUmlDir()).append(selectedViewName + GlobalConstants.getViewConfExtension())); IFile viewDiagramFile = this.project.getWorkspace().getRoot().getFile(project.getFullPath().append(GlobalConstants.getPyUmlDir()).append(selectedViewName + GlobalConstants.getDiagramExtension())); // open view editor new EditView(this.project, confFile, selectedViewName, true); // reload View, if it is already opened UMLToolsHelperMethods.refreshDiagramEditor(project, viewDiagramFile, true); // re-open View management window this.cancelPressed(); ManageViewsAction.run(project); } } /** * creates a "new View" dialog * creates an empty view config file and runs "edit" on this file */ public void createDialog() { String viewName = "newView"; String newViewName = viewName; IPath filePath=this.project.getLocation().append(GlobalConstants.getPyUmlDir()); // find initial filename that is not already used int i=0; java.io.File f; do { newViewName = viewName+(i==0?"":i); String fileName=filePath.append(newViewName +GlobalConstants.getViewConfExtension()).toOSString(); f= new java.io.File(fileName); i++; } while (f.exists()); // create initial config file try{ IFile confFile = this.project.getWorkspace().getRoot().getFile(project.getFullPath().append(GlobalConstants.getPyUmlDir()).append(newViewName + GlobalConstants.getViewConfExtension())); confFile.create(JavaHelperMethods.stringToStream(""), false, null); //open "edit view" editor new EditView(this.project, confFile, newViewName, true); this.close(); } catch (CoreException err) { MessageDialog.openError(null, "Cannot create View", "View could not be created.\n" + "Reason:\n"+err.getMessage()); return; } } /** * re-reads available pyUML.views from hard disk */ protected void initializeDialog() { boolean viewFound = false; String pyUmlDir = project.getLocation().append(GlobalConstants.getPyUmlDir()).toOSString(); java.io.File umlDir = new java.io.File(pyUmlDir); if (umlDir.list() == null) { MessageDialog.openError(null, "Error Managing Views", "No UML Model was found.\n" + "Please create a Model before creating a View!"); this.close(); return; } for (String file : umlDir.list()) { if (file.matches(".*.pyUmlView.uml$")) { viewFound=true; this.viewsBox.add(file.replace(".pyUmlView.uml", "")); this.viewsBox.add("a"); this.viewsBox.remove(this.viewsBox.getItemCount()-1); } } if (viewFound) setElementEnabled(true, viewsBoxGroup); else setElementEnabled(false, viewsBoxGroup); } }