view java/src/uk/ac/qmul/eecs/ccmi/gui/NodePopupMenu.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.diagrammodel.NodeProperties;
import uk.ac.qmul.eecs.ccmi.utils.InteractionLog;

/**
 * A pop up menu displaying the possible operations to perform on an node on from a visual representation
 * of a diagram.
 *
 */
@SuppressWarnings("serial")
public class NodePopupMenu extends JPopupMenu {
	public NodePopupMenu(Node node, Component parentComponent, DiagramModelUpdater modelUpdater){
		nodeRef = node;
		this.modelUpdater = modelUpdater;
		this.parentComponent = parentComponent;
		addMenuItems();
	}
	
	
	private void addMenuItems(){
		/* add set name menu item*/
		JMenuItem setNameMenuItem = new JMenuItem(resources.getString("menu.set_name"));
		setNameMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(!modelUpdater.getLock(nodeRef, Lock.NAME)){
					iLog("Could not get the lock on node for renaming",DiagramElement.toLogString(nodeRef));
					JOptionPane.showMessageDialog(parentComponent, resources.getString("dialog.lock_failure.name"));
					return;
				}
				iLog("open rename node dialog",DiagramElement.toLogString(nodeRef));
				String name = JOptionPane.showInputDialog(parentComponent, MessageFormat.format(resources.getString("dialog.input.name"),nodeRef.getName()), nodeRef.getName());
				if(name == null)
					iLog("cancel rename node dialog",DiagramElement.toLogString(nodeRef)); 
				else
					/* node has been locked at selection time */
					modelUpdater.setName(nodeRef, name);
				modelUpdater.yieldLock(nodeRef, Lock.NAME);
			}
			
		});
		add(setNameMenuItem);
		
		/* if the node has no properties defined, then don't add the menu item */
		if(nodeRef.getProperties().isNull())
			return;
		/* add set property menu item*/
		JMenuItem setPropertiesMenuItem = new JMenuItem(resources.getString("menu.set_properties"));
		setPropertiesMenuItem.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(!modelUpdater.getLock(nodeRef, Lock.PROPERTIES)){
					iLog("Could not get the lock on node for properties",DiagramElement.toLogString(nodeRef));
					JOptionPane.showMessageDialog(parentComponent, resources.getString("dialog.lock_failure.properties"));
					modelUpdater.yieldLock(nodeRef, Lock.PROPERTIES);
					return;
				}
				iLog("open edit properties dialog",DiagramElement.toLogString(nodeRef));
				NodeProperties properties = PropertyEditorDialog.showDialog(JOptionPane.getFrameForComponent(parentComponent),nodeRef.getPropertiesCopy()); 
				if(properties == null){ // user clicked on cancel 
					iLog("cancel edit properties dialog",DiagramElement.toLogString(nodeRef));
					modelUpdater.yieldLock(nodeRef, Lock.PROPERTIES);
					return;
				}
				if(!properties.isNull())
					modelUpdater.setProperties(nodeRef,properties);
				modelUpdater.yieldLock(nodeRef, Lock.PROPERTIES);
			}
		});
		add(setPropertiesMenuItem);
	}
	
	private void iLog(String action, String args){
		InteractionLog.log("GRAPH",action,args);
	}
	
	public Node nodeRef; 
	private DiagramModelUpdater modelUpdater;
	private Component parentComponent;
	private static ResourceBundle resources = ResourceBundle.getBundle(NodePopupMenu.class.getName());
}