001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.lenya.config.core;
020:
021: import java.awt.AWTEvent;
022: import java.awt.Component;
023: import java.awt.EventQueue;
024: import java.awt.Point;
025: import java.awt.Toolkit;
026: import java.awt.datatransfer.DataFlavor;
027: import java.awt.datatransfer.Transferable;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.MouseEvent;
030:
031: import javax.swing.AbstractAction;
032: import javax.swing.JPopupMenu;
033: import javax.swing.MenuSelectionManager;
034: import javax.swing.SwingUtilities;
035: import javax.swing.text.JTextComponent;
036:
037: public class ContextEventQueue extends EventQueue {
038:
039: protected void dispatchEvent(AWTEvent event) {
040: super .dispatchEvent(event);
041:
042: if (!(event instanceof MouseEvent))
043: return;
044:
045: MouseEvent me = (MouseEvent) event;
046:
047: if (!me.isPopupTrigger())
048: return;
049:
050: Component comp = SwingUtilities.getDeepestComponentAt(me
051: .getComponent(), me.getX(), me.getY());
052:
053: if (!(comp instanceof JTextComponent))
054: return;
055:
056: if (MenuSelectionManager.defaultManager().getSelectedPath().length > 0)
057: return;
058:
059: JTextComponent tc = (JTextComponent) comp;
060: JPopupMenu menu = new JPopupMenu();
061:
062: menu.add(new CopyAction(tc));
063: menu.add(new CutAction(tc));
064: menu.add(new PasteAction(tc));
065: menu.addSeparator();
066: menu.add(new SelectAllAction(tc));
067:
068: Point pt = SwingUtilities.convertPoint(me.getComponent(), me
069: .getPoint(), tc);
070: menu.show(tc, pt.x, pt.y);
071: }
072: }
073:
074: class CutAction extends AbstractAction {
075: JTextComponent comp;
076:
077: public CutAction(JTextComponent comp) {
078: super ("Cut");
079: this .comp = comp;
080: }
081:
082: public void actionPerformed(ActionEvent e) {
083: comp.cut();
084: }
085:
086: public boolean isEnabled() {
087: return comp.isEditable() && comp.isEnabled()
088: && comp.getSelectedText() != null;
089: }
090: }
091:
092: class PasteAction extends AbstractAction {
093: JTextComponent comp;
094:
095: public PasteAction(JTextComponent comp) {
096: super ("Paste");
097: this .comp = comp;
098: }
099:
100: public void actionPerformed(ActionEvent e) {
101: comp.paste();
102: }
103:
104: public boolean isEnabled() {
105: if (comp.isEditable() && comp.isEnabled()) {
106: Transferable contents = Toolkit.getDefaultToolkit()
107: .getSystemClipboard().getContents(this );
108: return contents
109: .isDataFlavorSupported(DataFlavor.stringFlavor);
110: } else
111: return false;
112: }
113: }
114:
115: class DeleteAction extends AbstractAction {
116: JTextComponent comp;
117:
118: public DeleteAction(JTextComponent comp) {
119: super ("Delete");
120: this .comp = comp;
121: }
122:
123: public void actionPerformed(ActionEvent e) {
124: comp.replaceSelection(null);
125: }
126:
127: public boolean isEnabled() {
128: return comp.isEditable() && comp.isEnabled()
129: && comp.getSelectedText() != null;
130: }
131: }
132:
133: class CopyAction extends AbstractAction {
134: JTextComponent comp;
135:
136: public CopyAction(JTextComponent comp) {
137: super ("Copy");
138: this .comp = comp;
139: }
140:
141: public void actionPerformed(ActionEvent e) {
142: comp.copy();
143: }
144:
145: public boolean isEnabled() {
146: return comp.isEnabled() && comp.getSelectedText() != null;
147: }
148: }
149:
150: class SelectAllAction extends AbstractAction {
151: JTextComponent comp;
152:
153: public SelectAllAction(JTextComponent comp) {
154: super ("Select All");
155: this .comp = comp;
156: }
157:
158: public void actionPerformed(ActionEvent e) {
159: comp.selectAll();
160: }
161:
162: public boolean isEnabled() {
163: return comp.isEnabled() && comp.getText().length() > 0;
164: }
165: }
|