KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > quickaccess > QuickAccessElement


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.quickaccess;
13
14
15 import org.eclipse.jface.resource.ImageDescriptor;
16
17 /**
18  * @since 3.3
19  *
20  */

21 public abstract class QuickAccessElement {
22
23     private static final int[][] EMPTY_INDICES = new int[0][0];
24     private QuickAccessProvider provider;
25
26     /**
27      * @param provider
28      */

29     public QuickAccessElement(QuickAccessProvider provider) {
30         super();
31         this.provider = provider;
32     }
33
34     /**
35      * Returns the label to be displayed to the user.
36      *
37      * @return the label
38      */

39     public abstract String JavaDoc getLabel();
40
41     /**
42      * Returns the image descriptor for this element.
43      *
44      * @return an image descriptor, or null if no image is available
45      */

46     public abstract ImageDescriptor getImageDescriptor();
47
48     /**
49      * Returns the id for this element. The id has to be unique within the
50      * QuickAccessProvider that provided this element.
51      *
52      * @return the id
53      */

54     public abstract String JavaDoc getId();
55
56     /**
57      * Executes the associated action for this element.
58      */

59     public abstract void execute();
60
61     /**
62      * Return the label to be used for sorting and matching elements.
63      *
64      * @return the sort label
65      */

66     public String JavaDoc getSortLabel() {
67         return getLabel();
68     }
69
70     /**
71      * @return Returns the provider.
72      */

73     public QuickAccessProvider getProvider() {
74         return provider;
75     }
76
77     /**
78      * @param filter
79      * @return
80      */

81     public QuickAccessEntry match(String JavaDoc filter,
82             QuickAccessProvider providerForMatching) {
83         String JavaDoc sortLabel = getLabel();
84         int index = sortLabel.toLowerCase().indexOf(filter);
85         if (index != -1) {
86             return new QuickAccessEntry(this, providerForMatching,
87                     new int[][] { { index, index + filter.length() - 1 } },
88                     EMPTY_INDICES);
89         }
90         String JavaDoc combinedLabel = (providerForMatching.getName() + " " + getLabel()); //$NON-NLS-1$
91
index = combinedLabel.toLowerCase().indexOf(filter);
92         if (index != -1) {
93             int lengthOfElementMatch = index + filter.length()
94                     - providerForMatching.getName().length() - 1;
95             if (lengthOfElementMatch > 0) {
96                 return new QuickAccessEntry(this, providerForMatching,
97                         new int[][] { { 0, lengthOfElementMatch - 1 } },
98                         new int[][] { { index, index + filter.length() - 1 } });
99             }
100             return new QuickAccessEntry(this, providerForMatching,
101                     EMPTY_INDICES, new int[][] { { index,
102                             index + filter.length() - 1 } });
103         }
104         String JavaDoc camelCase = CamelUtil.getCamelCase(sortLabel);
105         index = camelCase.indexOf(filter);
106         if (index != -1) {
107             int[][] indices = CamelUtil.getCamelCaseIndices(sortLabel, index, filter
108                     .length());
109             return new QuickAccessEntry(this, providerForMatching, indices,
110                     EMPTY_INDICES);
111         }
112         String JavaDoc combinedCamelCase = CamelUtil.getCamelCase(combinedLabel);
113         index = combinedCamelCase.indexOf(filter);
114         if (index != -1) {
115             String JavaDoc providerCamelCase = CamelUtil.getCamelCase(providerForMatching
116                     .getName());
117             int lengthOfElementMatch = index + filter.length()
118                     - providerCamelCase.length();
119             if (lengthOfElementMatch > 0) {
120                 return new QuickAccessEntry(
121                         this,
122                         providerForMatching,
123                         CamelUtil.getCamelCaseIndices(sortLabel, 0, lengthOfElementMatch),
124                         CamelUtil.getCamelCaseIndices(providerForMatching.getName(),
125                                 index, filter.length() - lengthOfElementMatch));
126             }
127             return new QuickAccessEntry(this, providerForMatching,
128                     EMPTY_INDICES, CamelUtil.getCamelCaseIndices(providerForMatching
129                             .getName(), index, filter.length()));
130         }
131         return null;
132     }
133 }
134
Popular Tags