001: /*
002: * $Id: JGraphpadMouseAdapter.java,v 1.1.1.1 2005/08/04 11:21:58 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.pad.util;
011:
012: import java.awt.event.MouseAdapter;
013: import java.awt.event.MouseEvent;
014:
015: import javax.swing.JPopupMenu;
016: import javax.swing.SwingUtilities;
017:
018: import org.w3c.dom.Node;
019:
020: import com.jgraph.JGraphEditor;
021: import com.jgraph.JGraphpad;
022: import com.jgraph.editor.JGraphEditorSettings;
023:
024: /**
025: * Simpify the creation of popup menus.
026: *
027: */
028: public class JGraphpadMouseAdapter extends MouseAdapter {
029:
030: /**
031: * References the enclosing editor.
032: */
033: protected JGraphEditor editor;
034:
035: /**
036: * Holds the name of the configuration to be returned from
037: * {@link JGraphpad#NAME_UICONFIG}.
038: */
039: protected String configName;
040:
041: /**
042: * Constructs a new mouse adapter for the specified enclosing editor and
043: * popup menu configuration name.
044: *
045: * @param editor
046: * The enclosing editor.
047: * @param configName
048: * The name of the popup menu configuration.
049: */
050: public JGraphpadMouseAdapter(JGraphEditor editor, String configName) {
051: this .editor = editor;
052: this .configName = configName;
053: }
054:
055: /**
056: * Hook for subclassers to return a configuration for the popup menu.
057: *
058: * @param event
059: * The object that describes the event.
060: * @return Returns the configuration for the popup menu.
061: */
062: public Node getPopupMenuConfiguration(MouseEvent event) {
063: return JGraphEditorSettings.getNodeByName(editor.getSettings()
064: .getDocument(JGraphpad.NAME_UICONFIG)
065: .getDocumentElement().getChildNodes(), getConfigName());
066: }
067:
068: /**
069: * Displays a popup menu.
070: *
071: * @param event
072: * The object that describes the event.
073: */
074: public void mouseReleased(MouseEvent event) {
075: if (SwingUtilities.isRightMouseButton(event)) {
076: Node configuration = getPopupMenuConfiguration(event);
077: if (configuration != null) {
078: JPopupMenu popupMenu = editor.getFactory()
079: .createPopupMenu(configuration);
080: popupMenu.setFocusable(false);
081: popupMenu.show(event.getComponent(), event.getX(),
082: event.getY());
083: event.consume();
084: }
085: }
086: }
087:
088: /**
089: * @return Returns the configName.
090: */
091: public String getConfigName() {
092: return configName;
093: }
094:
095: /**
096: * @param configName
097: * The configName to set.
098: */
099: public void setConfigName(String configName) {
100: this.configName = configName;
101: }
102:
103: }
|