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.ui.internal.texteditor.rulers;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.Platform;
014: import org.eclipse.core.runtime.content.IContentType;
015:
016: /**
017: * Describes the target of a contribution to the <code>org.eclipse.ui.texteditor.rulerColumns</code>
018: * extension point.
019: *
020: * @since 3.3
021: */
022: public abstract class RulerColumnTarget {
023: public abstract boolean matchesEditorId(String editorId);
024:
025: public abstract boolean matchesContentType(IContentType contentType);
026:
027: public abstract boolean matchesClass(Class clazz);
028:
029: /* package visible */
030: RulerColumnTarget() {
031: }
032:
033: public static RulerColumnTarget createAllTarget() {
034: return new AllTarget();
035: }
036:
037: public static RulerColumnTarget createOrTarget(
038: RulerColumnTarget either, RulerColumnTarget or) {
039: Assert.isLegal(or != null || either != null);
040: if (either == null)
041: return or;
042: if (or == null)
043: return either;
044: return new OrTarget(either, or);
045: }
046:
047: public static RulerColumnTarget createContentTypeTarget(
048: String contentTypeId) {
049: return new ContentTypeTarget(contentTypeId);
050: }
051:
052: public static RulerColumnTarget createEditorIdTarget(String editorId) {
053: return new EditorIdTarget(editorId);
054: }
055:
056: public static RulerColumnTarget createClassTarget(String className) {
057: return new ClassTarget(className);
058: }
059: }
060:
061: final class AllTarget extends RulerColumnTarget {
062: AllTarget() {
063: }
064:
065: public boolean matchesContentType(IContentType contentType) {
066: return true;
067: }
068:
069: public boolean matchesEditorId(String editorId) {
070: return true;
071: }
072:
073: public boolean matchesClass(Class clazz) {
074: return true;
075: }
076:
077: public String toString() {
078: return "All"; //$NON-NLS-1$
079: }
080: }
081:
082: final class OrTarget extends RulerColumnTarget {
083: private final RulerColumnTarget fEither;
084: private final RulerColumnTarget fOr;
085:
086: OrTarget(RulerColumnTarget either, RulerColumnTarget or) {
087: fEither = either;
088: fOr = or;
089: Assert.isLegal(either != null);
090: Assert.isLegal(or != null);
091: }
092:
093: public boolean matchesContentType(IContentType contentType) {
094: return fEither.matchesContentType(contentType)
095: || fOr.matchesContentType(contentType);
096: }
097:
098: public boolean matchesEditorId(String editorId) {
099: return fEither.matchesEditorId(editorId)
100: || fOr.matchesEditorId(editorId);
101: }
102:
103: public boolean matchesClass(Class clazz) {
104: return fEither.matchesClass(clazz) || fOr.matchesClass(clazz);
105: }
106:
107: public String toString() {
108: return fEither.toString() + " || " + fOr.toString(); //$NON-NLS-1$
109: }
110: }
111:
112: final class EditorIdTarget extends RulerColumnTarget {
113: private final String fEditorId;
114:
115: EditorIdTarget(String id) {
116: Assert.isLegal(id != null);
117: fEditorId = id;
118: }
119:
120: public boolean matchesContentType(IContentType contentType) {
121: return false;
122: }
123:
124: public boolean matchesEditorId(String editorId) {
125: return fEditorId.equals(editorId);
126: }
127:
128: public boolean matchesClass(Class clazz) {
129: return false;
130: }
131:
132: public String toString() {
133: return "editorID=" + fEditorId; //$NON-NLS-1$
134: }
135: }
136:
137: final class ClassTarget extends RulerColumnTarget {
138: private final String fClassName;
139:
140: ClassTarget(String className) {
141: Assert.isLegal(className != null);
142: fClassName = className;
143: }
144:
145: public boolean matchesContentType(IContentType contentType) {
146: return false;
147: }
148:
149: public boolean matchesEditorId(String editorId) {
150: return false;
151: }
152:
153: public boolean matchesClass(Class clazz) {
154: Assert.isLegal(clazz != null);
155:
156: do {
157: if (clazz.getName().equals(fClassName))
158: return true;
159: clazz = clazz.getSuperclass();
160: } while (clazz != null);
161:
162: return false;
163: }
164:
165: public String toString() {
166: return "class=" + fClassName; //$NON-NLS-1$
167: }
168: }
169:
170: final class ContentTypeTarget extends RulerColumnTarget {
171: private final IContentType fContentType;
172:
173: ContentTypeTarget(String contentTypeId) {
174: Assert.isLegal(contentTypeId != null);
175: fContentType = Platform.getContentTypeManager().getContentType(
176: contentTypeId);
177: }
178:
179: public boolean matchesContentType(IContentType contentType) {
180: return fContentType != null && contentType != null
181: && contentType.isKindOf(fContentType);
182: }
183:
184: public boolean matchesEditorId(String editorId) {
185: return false;
186: }
187:
188: public boolean matchesClass(Class clazz) {
189: return false;
190: }
191:
192: public String toString() {
193: return "contentType=" + fContentType; //$NON-NLS-1$
194: }
195: }
|