001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: package com.ibm.richtext.uiimpl;
014:
015: import java.util.EventObject;
016:
017: import com.ibm.richtext.textpanel.MTextPanel;
018: import com.ibm.richtext.textpanel.TextPanelEvent;
019: import com.ibm.richtext.uiimpl.resources.MenuData;
020:
021: public abstract class CommandMenuItem extends MenuItemSet {
022:
023: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
024: private/*final*/MItem fItem;
025:
026: protected abstract boolean isEnabled();
027:
028: protected abstract void performAction();
029:
030: protected CommandMenuItem(MenuData data) {
031:
032: this (data, false);
033: }
034:
035: protected CommandMenuItem(MenuData data, boolean enableByDefault) {
036:
037: fItem = MItem.createItem(data);
038: fItem.addListener(new EventListener() {
039: public void eventOccurred(EventObject event) {
040: performAction();
041: }
042: });
043: fItem.setEnabled(enableByDefault);
044: }
045:
046: protected void textPanelChanged() {
047:
048: MTextPanel textPanel = getTextPanel();
049: if (textPanel == null) {
050: fItem.setEnabled(false);
051: } else {
052: fItem.setEnabled(isEnabled());
053: }
054: }
055:
056: public final void textEventOccurred(TextPanelEvent event) {
057:
058: fItem.setEnabled(isEnabled());
059: }
060:
061: public static final class CutCopyClear extends CommandMenuItem {
062:
063: public static final int CUT = 0;
064: public static final int COPY = 1;
065: public static final int CLEAR = 2;
066:
067: private final int fKind;
068:
069: public CutCopyClear(MenuData menuData, int kind) {
070:
071: super (menuData);
072: if (kind != CUT && kind != COPY && kind != CLEAR) {
073: throw new IllegalArgumentException("Invalid menu kind");
074: }
075: fKind = kind;
076: }
077:
078: protected boolean isEnabled() {
079:
080: MTextPanel panel = getTextPanel();
081: return panel.getSelectionStart() != panel.getSelectionEnd();
082: }
083:
084: public boolean respondsToEventType(int type) {
085:
086: return type == TextPanelEvent.SELECTION_EMPTY_CHANGED;
087: }
088:
089: protected void performAction() {
090:
091: MTextPanel panel = getTextPanel();
092: switch (fKind) {
093: case CUT:
094: panel.cut();
095: break;
096: case COPY:
097: panel.copy();
098: break;
099: case CLEAR:
100: panel.clear();
101: break;
102: }
103: }
104: }
105:
106: public static final class UndoRedo extends CommandMenuItem {
107:
108: public static final boolean UNDO = true;
109: public static final boolean REDO = false;
110:
111: private boolean fKind;
112:
113: public UndoRedo(MenuData menuData, boolean kind) {
114:
115: super (menuData);
116: fKind = kind;
117: }
118:
119: protected boolean isEnabled() {
120:
121: MTextPanel panel = getTextPanel();
122: if (fKind == UNDO) {
123: return panel.canUndo();
124: } else {
125: return panel.canRedo();
126: }
127: }
128:
129: public boolean respondsToEventType(int type) {
130:
131: return type == TextPanelEvent.UNDO_STATE_CHANGED;
132: }
133:
134: protected void performAction() {
135:
136: MTextPanel panel = getTextPanel();
137: if (fKind == UNDO) {
138: panel.undo();
139: } else {
140: panel.redo();
141: }
142: }
143: }
144:
145: public static final class Paste extends CommandMenuItem {
146:
147: public Paste(MenuData menuData) {
148:
149: super (menuData);
150: }
151:
152: protected boolean isEnabled() {
153:
154: return getTextPanel().clipboardNotEmpty();
155: }
156:
157: public boolean respondsToEventType(int type) {
158:
159: return type == TextPanelEvent.CLIPBOARD_CHANGED;
160: }
161:
162: protected void performAction() {
163:
164: getTextPanel().paste();
165: }
166: }
167:
168: public static final class SelectAll extends CommandMenuItem {
169:
170: public SelectAll(MenuData menuData) {
171:
172: super (menuData);
173: }
174:
175: protected boolean isEnabled() {
176:
177: return true;
178: }
179:
180: public boolean respondsToEventType(int type) {
181:
182: return false;
183: }
184:
185: protected void performAction() {
186:
187: getTextPanel().selectAll();
188: }
189: }
190: }
|