KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > bookmarkexplorer > BookmarkLabelProvider


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
12 package org.eclipse.ui.views.bookmarkexplorer;
13
14 import org.eclipse.core.resources.IMarker;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.jface.viewers.ITableLabelProvider;
19 import org.eclipse.jface.viewers.LabelProvider;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
23 import org.eclipse.ui.internal.views.bookmarkexplorer.BookmarkMessages;
24
25 /**
26  * Provides labels for the bookmark navigator table
27  */

28 class BookmarkLabelProvider extends LabelProvider implements
29         ITableLabelProvider {
30
31     private Image image;
32     private ImageDescriptor desc;
33
34     final static int COLUMN_ICON = 0;
35
36     final static int COLUMN_DESCRIPTION = 1;
37
38     final static int COLUMN_RESOURCE = 2;
39
40     final static int COLUMN_FOLDER = 3;
41
42     final static int COLUMN_LOCATION = 4;
43
44     public BookmarkLabelProvider(BookmarkNavigator view) {
45         desc = IDEWorkbenchPlugin.getIDEImageDescriptor("obj16/bkmrk_tsk.gif"); //$NON-NLS-1$
46
image = JFaceResources.getResources().createImageWithDefault(desc);
47     }
48
49     /* (non-Javadoc)
50      * Method declared on LabelProvider.
51      */

52     public void dispose() {
53         if (image != null) {
54             JFaceResources.getResources().destroyImage(desc);
55             image = null;
56         }
57     }
58
59     /* (non-Javadoc)
60      * Method declared on LabelProvider.
61      */

62     public Image getImage(Object JavaDoc element) {
63         return image;
64     }
65
66     public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
67         if (!(element instanceof IMarker)) {
68             return ""; //$NON-NLS-1$
69
}
70         IMarker marker = (IMarker) element;
71
72         switch (columnIndex) {
73         case COLUMN_DESCRIPTION:
74             return marker.getAttribute(IMarker.MESSAGE, ""); //$NON-NLS-1$
75
case COLUMN_RESOURCE:
76             return marker.getResource().getName();
77         case COLUMN_FOLDER:
78             return getContainerName(marker);
79         case COLUMN_LOCATION: {
80             int line = marker.getAttribute(IMarker.LINE_NUMBER, -1);
81             if (line == -1) {
82                 return ""; //$NON-NLS-1$
83
}
84             return NLS.bind(BookmarkMessages.LineIndicator_text, String.valueOf(line));
85         }
86         }
87         return ""; //$NON-NLS-1$
88
}
89
90     public Image getColumnImage(Object JavaDoc element, int index) {
91         if (index == COLUMN_ICON) {
92             return image;
93         }
94         return null;
95     }
96
97     /**
98      * Returns the container name if it is defined, or empty string if not.
99      */

100     public static String JavaDoc getContainerName(IMarker marker) {
101         IPath path = marker.getResource().getFullPath();
102         int n = path.segmentCount() - 1;
103         // n is the number of segments in container, not path
104
if (n <= 0) {
105             return ""; //$NON-NLS-1$
106
}
107         int len = 0;
108         for (int i = 0; i < n; ++i) {
109             len += path.segment(i).length();
110         }
111         // account for /'s
112
if (n > 1) {
113             len += n - 1;
114         }
115         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(len);
116         for (int i = 0; i < n; ++i) {
117             if (i != 0) {
118                 sb.append('/');
119             }
120             sb.append(path.segment(i));
121         }
122         return sb.toString();
123     }
124 }
125
Popular Tags