001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Component;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.MouseEvent;
024:
025: import javax.swing.Action;
026: import javax.swing.JMenuItem;
027: import javax.swing.text.JTextComponent;
028:
029: import net.sourceforge.squirrel_sql.fw.gui.action.BaseAction;
030: import net.sourceforge.squirrel_sql.fw.util.StringManager;
031: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
032:
033: public class TextPopupMenu extends BasePopupMenu {
034: /** Internationalized strings for this class. */
035: private static final StringManager s_stringMgr = StringManagerFactory
036: .getStringManager(TextPopupMenu.class);
037:
038: public interface IOptionTypes {
039: int CUT = 0;
040: int COPY = 1;
041: int PASTE = 2;
042: int SELECT_ALL = 3;
043: int LAST_ENTRY = 3;
044: }
045:
046: private JTextComponent _comp;
047:
048: private final JMenuItem[] _menuItems = new JMenuItem[IOptionTypes.LAST_ENTRY + 1];
049:
050: private CutAction _cut = new CutAction();
051: private CopyAction _copy = new CopyAction();
052: private PasteAction _paste = new PasteAction();
053: private SelectAllAction _select = new SelectAllAction();
054:
055: /**
056: * Default constructor.
057: */
058: public TextPopupMenu() {
059: super ();
060: addMenuEntries();
061: }
062:
063: protected void setItemAction(int optionType, Action action) {
064: if (optionType < 0 || optionType > IOptionTypes.LAST_ENTRY) {
065: throw new IllegalArgumentException("Invalid option type: "
066: + optionType);
067: }
068: if (action == null) {
069: throw new IllegalArgumentException("Action == null");
070: }
071:
072: final int idx = getComponentIndex(_menuItems[optionType]);
073: remove(idx);
074: insert(action, idx);
075: _menuItems[optionType] = (JMenuItem) getComponent(idx);
076: }
077:
078: private void addMenuEntries() {
079: _menuItems[IOptionTypes.CUT] = add(_cut);
080: _menuItems[IOptionTypes.COPY] = add(_copy);
081: _menuItems[IOptionTypes.PASTE] = add(_paste);
082: addSeparator();
083: _menuItems[IOptionTypes.SELECT_ALL] = add(_select);
084: }
085:
086: public void setTextComponent(JTextComponent value) {
087: _comp = value;
088: }
089:
090: /**
091: * Show the menu.
092: */
093: public void show(Component invoker, int x, int y) {
094: updateActions();
095: super .show(invoker, x, y);
096: }
097:
098: public void show(MouseEvent evt) {
099: updateActions();
100: super .show(evt);
101: }
102:
103: protected void updateActions() {
104: final boolean isEditable = _comp != null && _comp.isEditable();
105: _cut.setEnabled(isEditable);
106: _paste.setEnabled(isEditable);
107: }
108:
109: protected JTextComponent getTextComponent() {
110: return _comp;
111: }
112:
113: public void dispose() {
114: // Menues that are also shown in the main window Session menu might
115: // be in this popup. If we don't remove them, the Session won't be Garbage Collected.
116: removeAll();
117: setInvoker(null);
118: _comp = null;
119: }
120:
121: protected class CutAction extends BaseAction {
122: CutAction() {
123: super (s_stringMgr.getString("TextPopupMenu.cut"));
124: }
125:
126: public void actionPerformed(ActionEvent evt) {
127: if (_comp != null) {
128: _comp.cut();
129: }
130: }
131: }
132:
133: protected class CopyAction extends BaseAction {
134: CopyAction() {
135: super (s_stringMgr.getString("TextPopupMenu.copy"));
136: }
137:
138: public void actionPerformed(ActionEvent evt) {
139: if (_comp != null) {
140: _comp.copy();
141: }
142: }
143: }
144:
145: protected class PasteAction extends BaseAction {
146: PasteAction() {
147: super (s_stringMgr.getString("TextPopupMenu.paste"));
148: }
149:
150: public void actionPerformed(ActionEvent evt) {
151: if (_comp != null) {
152: _comp.paste();
153: }
154: }
155: }
156:
157: protected class SelectAllAction extends BaseAction {
158: SelectAllAction() {
159: super (s_stringMgr.getString("TextPopupMenu.selectall"));
160: }
161:
162: public void actionPerformed(ActionEvent evt) {
163: if (_comp != null) {
164: _comp.selectAll();
165: }
166: }
167: }
168: }
|