001: /*
002: * Copyright 2005 Patrick Gotthardt
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.pagosoft.plaf;
017:
018: import javax.swing.*;
019: import javax.swing.text.*;
020: import java.awt.*;
021: import java.awt.datatransfer.*;
022: import java.awt.event.*;
023: import java.util.ResourceBundle;
024:
025: /**
026: * @author Patrick Gotthardt
027: */
028: public class TextComponentPopupHandler extends MouseAdapter {
029: private static TextComponentPopupHandler INSTANCE;
030:
031: public static TextComponentPopupHandler getInstance() {
032: if (INSTANCE == null) {
033: INSTANCE = new TextComponentPopupHandler();
034: }
035: return INSTANCE;
036: }
037:
038: private ResourceBundle bundle;
039:
040: private Action cutAction;
041: private Action copyAction;
042: private Action pasteAction;
043: private Action deleteAction;
044: private Action selectAllAction;
045:
046: private JPopupMenu popupMenu;
047: private JTextComponent comp;
048:
049: private TextComponentPopupHandler() {
050: bundle = ResourceBundle.getBundle("com.pagosoft.plaf.Bundle");
051:
052: cutAction = new CutAction();
053: copyAction = new CopyAction();
054: pasteAction = new PasteAction();
055: deleteAction = new DeleteAction();
056: selectAllAction = new SelectAllAction();
057:
058: }
059:
060: public ImageIcon getIcon(String name) {
061: try {
062: return new ImageIcon(TextComponentPopupHandler.class
063: .getResource("/com/pagosoft/plaf/icons/" + name));
064: } catch (Exception e) {
065: e.printStackTrace();
066: }
067: return null;
068: }
069:
070: public void mousePressed(MouseEvent e) {
071: if (!e.isPopupTrigger()
072: // does the user himself take care about the menu?
073: || MenuSelectionManager.defaultManager()
074: .getSelectedPath().length > 0) {
075: return;
076: }
077:
078: comp = (JTextComponent) e.getComponent();
079:
080: popupMenu = new JPopupMenu();
081: popupMenu.add(cutAction);
082: popupMenu.add(copyAction);
083: popupMenu.add(pasteAction);
084: popupMenu.add(deleteAction);
085: popupMenu.addSeparator();
086: popupMenu.add(selectAllAction);
087:
088: // show the menu
089: popupMenu.show(comp, e.getX(), e.getY());
090: }
091:
092: private class CutAction extends AbstractAction {
093: public CutAction() {
094: super (bundle.getString("textcomponent.cut"),
095: getIcon("editcut.png"));
096: putValue(Action.ACCELERATOR_KEY, KeyStroke
097: .getKeyStroke("control X"));
098: }
099:
100: public void actionPerformed(ActionEvent e) {
101: ((JTextComponent) e.getSource()).cut();
102: }
103:
104: public boolean isEnabled() {
105: return comp.isEditable() && comp.isEnabled()
106: && comp.getSelectedText() != null;
107: }
108: }
109:
110: private class CopyAction extends AbstractAction {
111: public CopyAction() {
112: super (bundle.getString("textcomponent.copy"),
113: getIcon("editcopy.png"));
114: putValue(Action.ACCELERATOR_KEY, KeyStroke
115: .getKeyStroke("control C"));
116: }
117:
118: public void actionPerformed(ActionEvent e) {
119: ((JTextComponent) e.getSource()).copy();
120: }
121:
122: public boolean isEnabled() {
123: return comp.isEnabled() && comp.getSelectedText() != null;
124: }
125: }
126:
127: private class PasteAction extends AbstractAction {
128: public PasteAction() {
129: super (bundle.getString("textcomponent.paste"),
130: getIcon("editpaste.png"));
131: putValue(Action.ACCELERATOR_KEY, KeyStroke
132: .getKeyStroke("control V"));
133: }
134:
135: public void actionPerformed(ActionEvent e) {
136: ((JTextComponent) e.getSource()).paste();
137: }
138:
139: public boolean isEnabled() {
140: if (comp.isEditable() && comp.isEnabled()) {
141: Transferable contents = Toolkit.getDefaultToolkit()
142: .getSystemClipboard().getContents(this );
143: return contents
144: .isDataFlavorSupported(DataFlavor.stringFlavor);
145: } else {
146: return false;
147: }
148: }
149: }
150:
151: private class DeleteAction extends AbstractAction {
152: public DeleteAction() {
153: super (bundle.getString("textcomponent.delete"),
154: getIcon("editdelete.png"));
155: }
156:
157: public void actionPerformed(ActionEvent e) {
158: ((JTextComponent) e.getSource()).replaceSelection(null);
159: }
160:
161: public boolean isEnabled() {
162: return comp.isEditable() && comp.isEnabled()
163: && comp.getSelectedText() != null;
164: }
165: }
166:
167: private class SelectAllAction extends AbstractAction {
168: public SelectAllAction() {
169: super (bundle.getString("textcomponent.selectall"));
170: putValue(Action.ACCELERATOR_KEY, KeyStroke
171: .getKeyStroke("control A"));
172: }
173:
174: public void actionPerformed(ActionEvent e) {
175: ((JTextComponent) e.getSource()).selectAll();
176: }
177:
178: public boolean isEnabled() {
179: return comp.isEnabled() && comp.getText().length() > 0;
180: }
181: }
182: }
|