All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mxgraph.swing.handler.mxKeyboardHandler Maven / Gradle / Ivy

There is a newer version: 2.2.18
Show newest version
/**
 * $Id: mxKeyboardHandler.java,v 1.1 2012/11/15 13:26:44 gaudenz Exp $
 * Copyright (c) 2008, Gaudenz Alder
 */
package com.mxgraph.swing.handler;

import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.yaoqiang.graph.action.GraphActions;
import org.yaoqiang.util.Constants;

import com.mxgraph.swing.mxGraphComponent;

/**
 * @author Administrator
 * 
 */
public class mxKeyboardHandler
{

	/**
	 * 
	 * @param graphComponent
	 */
	public mxKeyboardHandler(mxGraphComponent graphComponent)
	{
		installKeyboardActions(graphComponent);
	}

	/**
	 * Invoked as part from the boilerplate install block.
	 */
	protected void installKeyboardActions(mxGraphComponent graphComponent)
	{
		InputMap inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		SwingUtilities.replaceUIInputMap(graphComponent,
				JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);

		inputMap = getInputMap(JComponent.WHEN_FOCUSED);
		SwingUtilities.replaceUIInputMap(graphComponent,
				JComponent.WHEN_FOCUSED, inputMap);
		SwingUtilities.replaceUIActionMap(graphComponent, createActionMap());
	}

	/**
	 * Return JTree's input map.
	 */
	protected InputMap getInputMap(int condition)
	{
		InputMap map = null;

		if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
		{
			map = (InputMap) UIManager.get("ScrollPane.ancestorInputMap");
		}
		else if (condition == JComponent.WHEN_FOCUSED)
		{
			map = new InputMap();

			map.put(KeyStroke.getKeyStroke("F2"), "edit");

			map.put(KeyStroke.getKeyStroke("HOME"), "home");
			map.put(KeyStroke.getKeyStroke("PAGE_DOWN"), "enterGroup");
			map.put(KeyStroke.getKeyStroke("PAGE_UP"), "exitGroup");
			map.put(KeyStroke.getKeyStroke("control G"), "group");
			map.put(KeyStroke.getKeyStroke("control U"), "ungroup");

			map.put(KeyStroke.getKeyStroke("control D"), "duplicate");
			map.put(KeyStroke.getKeyStroke("A"), "autoLayout");
			map.put(KeyStroke.getKeyStroke("V"), "verticallyAlign");
			map.put(KeyStroke.getKeyStroke("H"), "horizontallyAlign");

			map.put(KeyStroke.getKeyStroke("UP"), "moveUp");
			map.put(KeyStroke.getKeyStroke("DOWN"), "moveDown");
			map.put(KeyStroke.getKeyStroke("RIGHT"), "moveRight");
			map.put(KeyStroke.getKeyStroke("LEFT"), "moveLeft");
			map.put(KeyStroke.getKeyStroke("control UP"), "fastMoveUp");
			map.put(KeyStroke.getKeyStroke("control DOWN"), "fastMoveDown");
			map.put(KeyStroke.getKeyStroke("control RIGHT"), "fastMoveRight");
			map.put(KeyStroke.getKeyStroke("control LEFT"), "fastMoveLeft");

			map.put(KeyStroke.getKeyStroke("control shift V"), "selectVertices");
			map.put(KeyStroke.getKeyStroke("control shift E"), "selectEdges");
			if (Constants.OS.startsWith("Mac OS")) {
				map.put(KeyStroke.getKeyStroke("DELETE"), "expand_collapse");
			} else {
				map.put(KeyStroke.getKeyStroke("BACK_SPACE"), "expand_collapse");
			}
		}

		return map;
	}

	/**
	 * Return the mapping between JTree's input map and JGraph's actions.
	 */
	protected ActionMap createActionMap()
	{
		ActionMap map = (ActionMap) UIManager.get("ScrollPane.actionMap");

		map.put("edit", GraphActions.getAction(GraphActions.EDIT));

		map.put("home", GraphActions.getAction(GraphActions.HOME));
		map.put("enterGroup", GraphActions.getAction(GraphActions.ENTER_GROUP));
		map.put("exitGroup", GraphActions.getAction(GraphActions.EXIT_GROUP));
		map.put("group", GraphActions.getAction(GraphActions.GROUP));
		map.put("ungroup", GraphActions.getAction(GraphActions.UNGROUP));

		map.put("duplicate", GraphActions.getAction(GraphActions.DUPLICATE));
		map.put("autoLayout", GraphActions.getAction(GraphActions.AUTO_LAYOUT));
		map.put("verticallyAlign", GraphActions.getAction(GraphActions.ALIGN_CENTER));
		map.put("horizontallyAlign", GraphActions.getAction(GraphActions.ALIGN_MIDDLE));

		map.put("moveUp", GraphActions.getAction(GraphActions.MOVE_UP));
		map.put("moveDown", GraphActions.getAction(GraphActions.MOVE_DOWN));
		map.put("moveRight", GraphActions.getAction(GraphActions.MOVE_RIGHT));
		map.put("moveLeft", GraphActions.getAction(GraphActions.MOVE_LEFT));
		map.put("fastMoveUp", GraphActions.getFastMoveAction(GraphActions.MOVE_UP));
		map.put("fastMoveDown", GraphActions.getFastMoveAction(GraphActions.MOVE_DOWN));
		map.put("fastMoveRight", GraphActions.getFastMoveAction(GraphActions.MOVE_RIGHT));
		map.put("fastMoveLeft", GraphActions.getFastMoveAction(GraphActions.MOVE_LEFT));
		map.put("selectVertices", GraphActions.getAction(GraphActions.SELECT_VERTICES));
		map.put("selectEdges", GraphActions.getAction(GraphActions.SELECT_EDGES));
		map.put("expand_collapse", GraphActions.getAction(GraphActions.FOLD_CELLS));

		return map;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy