view java/src/uk/ac/qmul/eecs/ccmi/gui/DiagramPanel.java @ 0:9418ab7b7f3f

Initial import
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Fri, 16 Dec 2011 17:35:51 +0000
parents
children 9e67171477bc
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package uk.ac.qmul.eecs.ccmi.gui;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * It's the panel which displays a diagram. It contains a {@link GraphPanel}, a {@link DiagramTree} 
 * and a {@link GraphToolbar} 
 *
 */
@SuppressWarnings("serial")
public class DiagramPanel extends JPanel{
   
   public DiagramPanel(Diagram diagram, EditorTabbedPane tabbedPane){
	  this.diagram = diagram;
	  this.tabbedPane = tabbedPane;
	  setName(diagram.getLabel());
	  setLayout(new BorderLayout());
	  
	  modelChangeListener = new ChangeListener(){
		  @Override
		  public void stateChanged(ChangeEvent e) {
			  setModified(true);
		  }
	  };
	  
      toolbar = new GraphToolbar(diagram);
      graphPanel = new GraphPanel(diagram, toolbar);
      /* the focus must be hold by the tree and the tab panel only */
      toolbar.setFocusable(false);
      graphPanel.setFocusable(false);
      
      tree = new DiagramTree(diagram);
      
      /* the panel containing the graph and the toolbar */
      JPanel graphAndToolbarPanel = new JPanel(new BorderLayout());
      graphAndToolbarPanel.add(toolbar, BorderLayout.NORTH);
      graphAndToolbarPanel.add(new JScrollPane(graphPanel),BorderLayout.CENTER);
      
      JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
            new JScrollPane(tree),
      		graphAndToolbarPanel);
      splitPane.setDividerLocation((int)tree.getPreferredSize().width*2);
      add(splitPane, BorderLayout.CENTER);
      
      diagram.getCollectionModel().addChangeListener(modelChangeListener);
   }

   public String getFilePath(){
      return filePath;
   }

   public void setFilePath(String newValue){
      filePath = newValue;
   }
   
   public Diagram getDiagram(){
	   return diagram;
   }
   
   public void setDiagram(Diagram diagram){
	   /* remove the listener from the old model  */
	   this.diagram.getCollectionModel().removeChangeListener(modelChangeListener);
	   diagram.getCollectionModel().addChangeListener(modelChangeListener);
	   
	   this.diagram = diagram;
	   tree.setDiagram(diagram);
	   graphPanel.setModelUpdater(diagram.getModelUpdater());
	   setName(diagram.getLabel());
	   /* set the * according to the new diagram's model modification status */
	   setModified(isModified());
   }
   
   public GraphPanel getGraphPanel(){
	   return graphPanel;
   }
   
   public DiagramTree getTree(){
	   return tree;
   }
   
   /** This method is for changing the 'modified' status of the diagram.       * 
    * When called passing false as argument listeners are notified that the   *
    * diagram has been saved. 											      */
   public void setModified(boolean modified){
	   if(!modified)
		   diagram.getCollectionModel().setUnmodified();
	   /* add an asterisk to notify that the diagram has changed */
       if(modified)
     	  setName(getName()+"*");
       else 
    	   setName(diagram.getLabel());
       tabbedPane.refreshComponentTabTitle(this);
   }
   
   public boolean isModified(){
	   return diagram.getCollectionModel().isModified();
   }

   private Diagram diagram;
   private GraphPanel graphPanel;
   private DiagramTree tree;
   private GraphToolbar toolbar;
   private String filePath;
   private ChangeListener modelChangeListener;
   private EditorTabbedPane tabbedPane;
}