001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.swing;
014:
015: import java.awt.event.ActionEvent;
016:
017: import javax.swing.AbstractAction;
018: import javax.swing.JPopupMenu;
019: import javax.swing.event.PopupMenuEvent;
020: import javax.swing.event.PopupMenuListener;
021: import javax.swing.text.JTextComponent;
022:
023: import com.eviware.soapui.support.components.Undoable;
024:
025: public final class JTextComponentPopupMenu extends JPopupMenu implements
026: PopupMenuListener {
027: private final JTextComponent textComponent;
028: private CutAction cutAction;
029: private CopyAction copyAction;
030: private PasteAction pasteAction;
031: private ClearAction clearAction;
032: private SelectAllAction selectAllAction;
033: private UndoAction undoAction;
034: private RedoAction redoAction;
035:
036: public static JTextComponentPopupMenu add(
037: JTextComponent textComponent) {
038: // double-check
039: if (textComponent.getComponentPopupMenu() instanceof JTextComponentPopupMenu)
040: return (JTextComponentPopupMenu) textComponent
041: .getComponentPopupMenu();
042:
043: JTextComponentPopupMenu popupMenu = new JTextComponentPopupMenu(
044: textComponent);
045: textComponent.setComponentPopupMenu(popupMenu);
046: return popupMenu;
047: }
048:
049: private JTextComponentPopupMenu(JTextComponent textComponent) {
050: super ("Edit");
051: this .textComponent = textComponent;
052:
053: if (textComponent instanceof Undoable) {
054: undoAction = new UndoAction();
055: add(undoAction);
056:
057: redoAction = new RedoAction();
058: add(redoAction);
059:
060: addSeparator();
061: }
062:
063: cutAction = new CutAction();
064: add(cutAction);
065: copyAction = new CopyAction();
066: add(copyAction);
067: pasteAction = new PasteAction();
068: add(pasteAction);
069: clearAction = new ClearAction();
070: add(clearAction);
071: addSeparator();
072: selectAllAction = new SelectAllAction();
073: add(selectAllAction);
074:
075: addPopupMenuListener(this );
076: }
077:
078: private final class CutAction extends AbstractAction {
079: public CutAction() {
080: super ("Cut");
081: }
082:
083: public void actionPerformed(ActionEvent e) {
084: textComponent.cut();
085: }
086: }
087:
088: private final class CopyAction extends AbstractAction {
089: public CopyAction() {
090: super ("Copy");
091: }
092:
093: public void actionPerformed(ActionEvent e) {
094: textComponent.copy();
095: }
096: }
097:
098: private final class PasteAction extends AbstractAction {
099: public PasteAction() {
100: super ("Paste");
101: }
102:
103: public void actionPerformed(ActionEvent e) {
104: textComponent.paste();
105: }
106: }
107:
108: private final class ClearAction extends AbstractAction {
109: public ClearAction() {
110: super ("Clear");
111: }
112:
113: public void actionPerformed(ActionEvent e) {
114: textComponent.setText("");
115: }
116: }
117:
118: private final class SelectAllAction extends AbstractAction {
119: public SelectAllAction() {
120: super ("Select All");
121: }
122:
123: public void actionPerformed(ActionEvent e) {
124: textComponent.selectAll();
125: }
126: }
127:
128: private final class UndoAction extends AbstractAction {
129: public UndoAction() {
130: super ("Undo");
131: }
132:
133: public void actionPerformed(ActionEvent e) {
134: ((Undoable) textComponent).undo();
135: }
136: }
137:
138: private final class RedoAction extends AbstractAction {
139: public RedoAction() {
140: super ("Redo");
141: }
142:
143: public void actionPerformed(ActionEvent e) {
144: ((Undoable) textComponent).redo();
145: }
146: }
147:
148: public void popupMenuCanceled(PopupMenuEvent e) {
149: }
150:
151: public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
152: }
153:
154: public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
155: if (textComponent instanceof Undoable) {
156: undoAction.setEnabled(((Undoable) textComponent).canUndo());
157: redoAction.setEnabled(((Undoable) textComponent).canRedo());
158: }
159:
160: cutAction
161: .setEnabled(textComponent.getSelectionEnd() != textComponent
162: .getSelectionStart());
163: copyAction.setEnabled(cutAction.isEnabled());
164: clearAction.setEnabled(cutAction.isEnabled());
165: selectAllAction
166: .setEnabled(textComponent.getText().length() > 0);
167: }
168: }
|