001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.commands;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014:
015: import org.eclipse.pde.internal.ui.PDEUIMessages;
016:
017: public abstract class CommandCopyFilter {
018:
019: private static final ArrayList fFilters = new ArrayList();
020:
021: private CommandCopyFilter() {
022: fFilters.add(this );
023: }
024:
025: public final String filter(String serializedCommand,
026: boolean surroundWithMarkup, String markupLabel) {
027: if (surroundWithMarkup)
028: return markup(escape(serializedCommand), markupLabel);
029: return escape(serializedCommand);
030: }
031:
032: protected abstract String escape(String serializedCommand);
033:
034: protected abstract String markup(String escapedSerializedCommand,
035: String markupLabel);
036:
037: public abstract String getLabelText();
038:
039: public abstract String getToolTipText();
040:
041: public static CommandCopyFilter[] getFilters() {
042: return (CommandCopyFilter[]) fFilters
043: .toArray(new CommandCopyFilter[fFilters.size()]);
044: }
045:
046: public static CommandCopyFilter getFilter(int index) {
047: return (CommandCopyFilter) fFilters.get(index);
048: }
049:
050: public static int indexOf(CommandCopyFilter filter) {
051:
052: int index = 0;
053: for (Iterator i = fFilters.iterator(); i.hasNext();) {
054: CommandCopyFilter f = (CommandCopyFilter) i.next();
055: if (f == filter)
056: return index;
057: index++;
058: }
059: return -1;
060: }
061:
062: public static final CommandCopyFilter NONE = new CommandCopyFilter() {
063: public String getLabelText() {
064: return PDEUIMessages.CommandCopyFilter_noFilter;
065: }
066:
067: public String getToolTipText() {
068: return PDEUIMessages.CommandCopyFilter_noFilterDesc;
069: }
070:
071: protected String escape(String serializedCommand) {
072: return serializedCommand;
073: }
074:
075: protected String markup(String escapedSerializedCommand,
076: String markupLabel) {
077: return escapedSerializedCommand;
078: }
079:
080: };
081:
082: public static final CommandCopyFilter HELP = new CommandCopyFilter() {
083: public String getLabelText() {
084: return PDEUIMessages.CommandCopyFilter_help;
085: }
086:
087: public String getToolTipText() {
088: return PDEUIMessages.CommandCopyFilter_helpDesc;
089: }
090:
091: protected String escape(String serializedCommand) {
092: // TODO: escape for Help
093: return serializedCommand;
094: }
095:
096: protected String markup(String escapedSerializedCommand,
097: String markupLabel) {
098: StringBuffer sb = new StringBuffer();
099:
100: sb.append("<a href='javascript:executeCommand(\""); //$NON-NLS-1$
101: sb.append(escapedSerializedCommand);
102: sb.append("\")'>"); //$NON-NLS-1$
103: if (markupLabel != null)
104: sb.append(markupLabel);
105: sb.append("</a>"); //$NON-NLS-1$
106:
107: return sb.toString();
108: }
109:
110: };
111:
112: public static final CommandCopyFilter CHEATSHEET = new CommandCopyFilter() {
113: public String getLabelText() {
114: return PDEUIMessages.CommandCopyFilter_cheatsheet;
115: }
116:
117: public String getToolTipText() {
118: return PDEUIMessages.CommandCopyFilter_cheatsheetDesc;
119: }
120:
121: protected String escape(String serializedCommand) {
122: // TODO: escape for Cheatsheets
123: return serializedCommand;
124: }
125:
126: protected String markup(String escapedSerializedCommand,
127: String markupLabel) {
128: StringBuffer sb = new StringBuffer();
129:
130: sb.append("<command serialization=\""); //$NON-NLS-1$
131: sb.append(escapedSerializedCommand);
132: sb.append("\"/>"); //$NON-NLS-1$
133:
134: return sb.toString();
135: }
136:
137: };
138:
139: public static final CommandCopyFilter INTRO = new CommandCopyFilter() {
140: public String getLabelText() {
141: return PDEUIMessages.CommandCopyFilter_intro;
142: }
143:
144: public String getToolTipText() {
145: return PDEUIMessages.CommandCopyFilter_introDesc;
146: }
147:
148: protected String escape(String serializedCommand) {
149: // TODO: escape for Intro
150: return serializedCommand;
151: }
152:
153: protected String markup(String escapedSerializedCommand,
154: String markupLabel) {
155: StringBuffer sb = new StringBuffer();
156:
157: sb.append("<link\n"); //$NON-NLS-1$
158: if (markupLabel != null) {
159: sb.append("label=\""); //$NON-NLS-1$
160: sb.append(markupLabel);
161: sb.append("\"\n"); //$NON-NLS-1$
162: }
163:
164: sb.append("id=\"TODO\"\n"); //$NON-NLS-1$
165:
166: sb.append("url=\""); //$NON-NLS-1$
167: sb.append(escapedSerializedCommand);
168: sb.append("\"\n"); //$NON-NLS-1$
169:
170: sb.append("<text>TODO</text>\n"); //$NON-NLS-1$
171:
172: sb.append("</link>"); //$NON-NLS-1$
173:
174: return sb.toString();
175: }
176:
177: };
178:
179: }
|