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

Initial import
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Fri, 16 Dec 2011 17:35:51 +0000
parents
children 4b2f975e35fa
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 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.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.MessageFormat;
import java.util.ResourceBundle;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement;
import uk.ac.qmul.eecs.ccmi.utils.InteractionLog;

/**
 * A pop up menu displaying the possible operations to perform on an edge on from a visual representation
 * of a diagram.
 *
 */
@SuppressWarnings("serial")
public class EdgePopupMenu extends JPopupMenu {
	
	/**
	 * Creates a pop up menu, showing set name, set end label and select arrow head menu items.  
	 * @param edge the edge that will be edited 
	 * @param node the node whose end label and arrow head that will be edited
	 * @param parentComponent the component where the pop up will appear 
	 * @param modelUpdater the model updater for applying changes 
	 */
	public EdgePopupMenu( Edge edge, Node node, Component parentComponent, DiagramModelUpdater modelUpdater){
		this.edgeRef = edge;
		this.nodeRef = node;
		this.parentComponent = parentComponent;
		this.modelUpdater = modelUpdater;
		arrowHeads = new Object[edgeRef.getAvailableEndDescriptions().length + 1];
		for(int i=0;i<edgeRef.getAvailableEndDescriptions().length;i++){
			arrowHeads[i] = edgeRef.getAvailableEndDescriptions()[i].toString();
		}
		arrowHeads[arrowHeads.length-1] = Edge.NO_ARROW_STRING;
		addMenuItems(false);
	}
	
	/**
	 * creates a pop up menu, showing set name menu item only 
	 * @param edge the edge being edited 
	 * @param parentComponent the component where the pop up will appear
	 * @param modelUpdater the model updater for applying changes 
	 */
	public EdgePopupMenu( Edge edge, Component parentComponent,DiagramModelUpdater modelUpdater){
		this.edgeRef = edge;
		this.parentComponent = parentComponent;
		this.modelUpdater = modelUpdater;
		addMenuItems(true);
	}
	
	private void addMenuItems(boolean showSetNameMenuItemOnly){
		
		JMenuItem setNameMenuItem = new JMenuItem(resources.getString("menu.set_name"));
		setNameMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(!modelUpdater.getLock(edgeRef, Lock.NAME)){
					iLog("Could not get the lock on edge  for name",DiagramElement.toLogString(edgeRef));
					JOptionPane.showMessageDialog(parentComponent, resources.getString("dialog.lock_failure.name"));
					return;
				}
				iLog("open rename edge dialog",DiagramElement.toLogString(edgeRef));
				String name = JOptionPane.showInputDialog(parentComponent, MessageFormat.format(resources.getString("dialog.input.name"),edgeRef.getName()), edgeRef.getName());
  	    	   	if(name == null)
  	    	   		iLog("cancel rename edge dialog",DiagramElement.toLogString(edgeRef)); 
  	    	   	else
  	    	   		/* edge has been locked at selection time */
  	    	   		modelUpdater.setName(edgeRef,name);
  	    	   	modelUpdater.yieldLock(edgeRef, Lock.NAME);
			}
		});
		
		add(setNameMenuItem);
		if(!showSetNameMenuItemOnly){
			/* Label menu item */
			JMenuItem setLabelMenuItem = new JMenuItem(resources.getString("menu.set_label"));
			setLabelMenuItem.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent evt) {
					if(!modelUpdater.getLock(edgeRef, Lock.EDGE_END)){
						iLog("Could not get lock on edge for end label",DiagramElement.toLogString(edgeRef)+" end node:"+DiagramElement.toLogString(nodeRef));
						JOptionPane.showMessageDialog(parentComponent, resources.getString("dialog.lock_failure.end"));
						return;
					}
					iLog("open edge label dialog",DiagramElement.toLogString(edgeRef)+" end node:"+DiagramElement.toLogString(nodeRef));
					String text = JOptionPane.showInputDialog(parentComponent,resources.getString("dialog.input.label"));
					if(text != null)
						modelUpdater.setEndLabel(edgeRef,nodeRef,text);
					else
						iLog("cancel edge label dialog",DiagramElement.toLogString(edgeRef)+" end node:"+DiagramElement.toLogString(nodeRef));
					modelUpdater.yieldLock(edgeRef, Lock.EDGE_END);
				}
			});
			add(setLabelMenuItem);

			if(arrowHeads.length > 1){
				/* arrow head menu item */
				JMenuItem selectArrowHeadMenuItem = new JMenuItem(resources.getString("menu.choose_arrow_head"));
				selectArrowHeadMenuItem.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e) {
						if(!modelUpdater.getLock(edgeRef, Lock.EDGE_END)){
							iLog("Could not get lock on edge for arrow head",DiagramElement.toLogString(edgeRef)+" end node:"+DiagramElement.toLogString(nodeRef));
							JOptionPane.showMessageDialog(parentComponent, resources.getString("dialog.lock_failure.end"));
							return;
						}
						iLog("open select arrow head dialog",DiagramElement.toLogString(edgeRef)+" end node:"+DiagramElement.toLogString(nodeRef));
						String arrowHead = (String)JOptionPane.showInputDialog(
								parentComponent, 
								resources.getString("dialog.input.arrow"), 
								resources.getString("dialog.input.arrow.title"), 
								JOptionPane.PLAIN_MESSAGE, 
								null, 
								arrowHeads, 
								arrowHeads);
						if(arrowHead == null){
							iLog("cancel select arrow head dialog",DiagramElement.toLogString(edgeRef)+" end node:"+DiagramElement.toLogString(nodeRef));
							modelUpdater.yieldLock(edgeRef, Lock.EDGE_END);
							return;
						}
						for(int i=0; i<edgeRef.getAvailableEndDescriptions().length;i++){
							if(edgeRef.getAvailableEndDescriptions()[i].toString().equals(arrowHead)){
								modelUpdater.setEndDescription(edgeRef, nodeRef, i);
								modelUpdater.yieldLock(edgeRef, Lock.EDGE_END);
								return;
							}
						}
						/* the user selected the none menu item */
						modelUpdater.setEndDescription(edgeRef,nodeRef, Edge.NO_END_DESCRIPTION_INDEX);
						modelUpdater.yieldLock(edgeRef, Lock.EDGE_END);
					}
				});
				add(selectArrowHeadMenuItem);
			}
		}
	}
	
	private void iLog(String action, String args){
		InteractionLog.log("GRAPH",action,args);
	}
	
	public Edge edgeRef;
	private Node nodeRef;
	private Component parentComponent;
	private Object[] arrowHeads;
	private DiagramModelUpdater modelUpdater;
	private static ResourceBundle resources = ResourceBundle.getBundle(EdgePopupMenu.class.getName()); 
}