KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > text > FileLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.search.internal.ui.text;
12
13 import com.ibm.icu.text.MessageFormat;
14
15 import org.eclipse.core.runtime.IPath;
16
17 import org.eclipse.core.resources.IResource;
18
19 import org.eclipse.swt.graphics.Image;
20
21 import org.eclipse.jface.viewers.ILabelProviderListener;
22 import org.eclipse.jface.viewers.LabelProvider;
23
24 import org.eclipse.ui.model.WorkbenchLabelProvider;
25
26 import org.eclipse.search.internal.ui.SearchMessages;
27 import org.eclipse.search.ui.text.AbstractTextSearchResult;
28 import org.eclipse.search.ui.text.AbstractTextSearchViewPage;
29
30
31 public class FileLabelProvider extends LabelProvider {
32         
33     public static final int SHOW_LABEL= 1;
34     public static final int SHOW_LABEL_PATH= 2;
35     public static final int SHOW_PATH_LABEL= 3;
36     public static final int SHOW_PATH= 4;
37     
38     private static final String JavaDoc fgSeparatorFormat= "{0} - {1}"; //$NON-NLS-1$
39

40     private WorkbenchLabelProvider fLabelProvider;
41     private AbstractTextSearchViewPage fPage;
42         
43     private int fOrder;
44     private String JavaDoc[] fArgs= new String JavaDoc[2];
45
46     public FileLabelProvider(AbstractTextSearchViewPage page, int orderFlag) {
47         fLabelProvider= new WorkbenchLabelProvider();
48         fOrder= orderFlag;
49         fPage= page;
50     }
51
52     public void setOrder(int orderFlag) {
53         fOrder= orderFlag;
54     }
55     
56     public int getOrder() {
57         return fOrder;
58     }
59
60     public String JavaDoc getText(Object JavaDoc element) {
61         if (!(element instanceof IResource))
62             return null;
63
64         IResource resource= (IResource)element;
65         String JavaDoc text= null;
66
67         if (!resource.exists())
68             text= SearchMessages.FileLabelProvider_removed_resource_label;
69         
70         else {
71             IPath path= resource.getFullPath().removeLastSegments(1);
72             if (path.getDevice() == null)
73                 path= path.makeRelative();
74             if (fOrder == SHOW_LABEL || fOrder == SHOW_LABEL_PATH) {
75                 text= fLabelProvider.getText(resource);
76                 if (path != null && fOrder == SHOW_LABEL_PATH) {
77                     fArgs[0]= text;
78                     fArgs[1]= path.toString();
79                     text= MessageFormat.format(fgSeparatorFormat, fArgs);
80                 }
81             } else {
82                 if (path != null)
83                     text= path.toString();
84                 else
85                     text= ""; //$NON-NLS-1$
86
if (fOrder == SHOW_PATH_LABEL) {
87                     fArgs[0]= text;
88                     fArgs[1]= fLabelProvider.getText(resource);
89                     text= MessageFormat.format(fgSeparatorFormat, fArgs);
90                 }
91             }
92         }
93         
94         int matchCount= 0;
95         AbstractTextSearchResult result= fPage.getInput();
96         if (result != null)
97             matchCount= result.getMatchCount(element);
98         if (matchCount <= 1)
99             return text;
100         String JavaDoc format= SearchMessages.FileLabelProvider_count_format;
101         return MessageFormat.format(format, new Object JavaDoc[] { text, new Integer JavaDoc(matchCount) });
102     }
103
104     public Image getImage(Object JavaDoc element) {
105         if (!(element instanceof IResource))
106             return null;
107
108         IResource resource= (IResource)element;
109         Image image= fLabelProvider.getImage(resource);
110         return image;
111     }
112
113     public void dispose() {
114         super.dispose();
115         fLabelProvider.dispose();
116     }
117
118     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
119         return fLabelProvider.isLabelProperty(element, property);
120     }
121
122     public void removeListener(ILabelProviderListener listener) {
123         super.removeListener(listener);
124         fLabelProvider.removeListener(listener);
125     }
126
127     public void addListener(ILabelProviderListener listener) {
128         super.addListener(listener);
129         fLabelProvider.addListener(listener);
130     }
131 }
132
Popular Tags