001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 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.quickaccess;
011:
012: import org.eclipse.jface.resource.ImageDescriptor;
013:
014: /**
015: * @since 3.3
016: *
017: */
018: public abstract class QuickAccessElement {
019:
020: private static final int[][] EMPTY_INDICES = new int[0][0];
021: private QuickAccessProvider provider;
022:
023: /**
024: * @param provider
025: */
026: public QuickAccessElement(QuickAccessProvider provider) {
027: super ();
028: this .provider = provider;
029: }
030:
031: /**
032: * Returns the label to be displayed to the user.
033: *
034: * @return the label
035: */
036: public abstract String getLabel();
037:
038: /**
039: * Returns the image descriptor for this element.
040: *
041: * @return an image descriptor, or null if no image is available
042: */
043: public abstract ImageDescriptor getImageDescriptor();
044:
045: /**
046: * Returns the id for this element. The id has to be unique within the
047: * QuickAccessProvider that provided this element.
048: *
049: * @return the id
050: */
051: public abstract String getId();
052:
053: /**
054: * Executes the associated action for this element.
055: */
056: public abstract void execute();
057:
058: /**
059: * Return the label to be used for sorting and matching elements.
060: *
061: * @return the sort label
062: */
063: public String getSortLabel() {
064: return getLabel();
065: }
066:
067: /**
068: * @return Returns the provider.
069: */
070: public QuickAccessProvider getProvider() {
071: return provider;
072: }
073:
074: /**
075: * @param filter
076: * @return
077: */
078: public QuickAccessEntry match(String filter,
079: QuickAccessProvider providerForMatching) {
080: String sortLabel = getLabel();
081: int index = sortLabel.toLowerCase().indexOf(filter);
082: if (index != -1) {
083: return new QuickAccessEntry(
084: this ,
085: providerForMatching,
086: new int[][] { { index, index + filter.length() - 1 } },
087: EMPTY_INDICES);
088: }
089: String combinedLabel = (providerForMatching.getName() + " " + getLabel()); //$NON-NLS-1$
090: index = combinedLabel.toLowerCase().indexOf(filter);
091: if (index != -1) {
092: int lengthOfElementMatch = index + filter.length()
093: - providerForMatching.getName().length() - 1;
094: if (lengthOfElementMatch > 0) {
095: return new QuickAccessEntry(
096: this ,
097: providerForMatching,
098: new int[][] { { 0, lengthOfElementMatch - 1 } },
099: new int[][] { { index,
100: index + filter.length() - 1 } });
101: }
102: return new QuickAccessEntry(this , providerForMatching,
103: EMPTY_INDICES, new int[][] { { index,
104: index + filter.length() - 1 } });
105: }
106: String camelCase = CamelUtil.getCamelCase(sortLabel);
107: index = camelCase.indexOf(filter);
108: if (index != -1) {
109: int[][] indices = CamelUtil.getCamelCaseIndices(sortLabel,
110: index, filter.length());
111: return new QuickAccessEntry(this , providerForMatching,
112: indices, EMPTY_INDICES);
113: }
114: String combinedCamelCase = CamelUtil
115: .getCamelCase(combinedLabel);
116: index = combinedCamelCase.indexOf(filter);
117: if (index != -1) {
118: String providerCamelCase = CamelUtil
119: .getCamelCase(providerForMatching.getName());
120: int lengthOfElementMatch = index + filter.length()
121: - providerCamelCase.length();
122: if (lengthOfElementMatch > 0) {
123: return new QuickAccessEntry(this , providerForMatching,
124: CamelUtil.getCamelCaseIndices(sortLabel, 0,
125: lengthOfElementMatch), CamelUtil
126: .getCamelCaseIndices(
127: providerForMatching.getName(),
128: index, filter.length()
129: - lengthOfElementMatch));
130: }
131: return new QuickAccessEntry(this, providerForMatching,
132: EMPTY_INDICES, CamelUtil.getCamelCaseIndices(
133: providerForMatching.getName(), index,
134: filter.length()));
135: }
136: return null;
137: }
138: }
|