View Javadoc

1   /* ======================================
2    * Copyright (c) 2004-2005 Achim Nierbeck
3    * All rights reserved.
4    *
5    * You may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.gnu.org/licenses/lgpl.html
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * ======================================
16   */
17  
18  /*
19   * EntriesLabelProvider.java
20   * $Revision: 1.6 $
21   * Date 14.12.2005
22   * 
23   * $Author: nierbeck $ 
24   * $Date: 2006/02/04 13:12:31 $ 
25   * 
26   */
27  
28  package de.nierbeck.timeTrack.views;
29  
30  import java.text.DateFormat;
31  import java.util.Calendar;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import org.eclipse.jface.resource.ImageDescriptor;
36  import org.eclipse.jface.viewers.ITableLabelProvider;
37  import org.eclipse.jface.viewers.LabelProvider;
38  import org.eclipse.swt.graphics.Image;
39  import org.eclipse.ui.ISharedImages;
40  import org.eclipse.ui.PlatformUI;
41  
42  import de.nierbeck.timeTrack.TimeTrackPlugin;
43  import de.nierbeck.timeTrack.model.EntryType;
44  
45  /***
46   * @author Achim
47   * 
48   */
49  public class EntriesLabelProvider extends LabelProvider implements
50  		ITableLabelProvider {
51  
52  	/*
53  	 * Very simple caching mechanism for those darn Images
54  	 * keeps them from being loaded all the time :-)
55  	 */
56  	private static final Map IMAGE_CACHE = new HashMap();
57  	
58  	/***
59  	 * returns the Image to the according column
60  	 * 
61  	 *  @param columnIndex - the index number of the selected column
62  	 *  @param element - the element of the column
63  	 *  
64  	 *  @return an Image to the column
65  	 */
66  	public final Image getColumnImage(final Object element, final int columnIndex) {
67  		Image returnImage = null;
68  		if (element instanceof EntryType) {
69  			// EntryType entry = (EntryType) element;
70  			if (columnIndex == ColumnEnum.PROJECT_ORD) {
71  				if (IMAGE_CACHE.containsKey(ColumnEnum.PROJECT)) {
72  					returnImage = (Image) IMAGE_CACHE.get(ColumnEnum.PROJECT);
73  				} else {
74  					ImageDescriptor descriptor = PlatformUI.getWorkbench()
75  					.getSharedImages().getImageDescriptor(
76  							ISharedImages.IMG_OBJ_PROJECT_CLOSED);
77  					if (descriptor != null) {
78  						returnImage = descriptor.createImage();
79  						IMAGE_CACHE.put(ColumnEnum.PROJECT, returnImage);
80  					}
81  				}
82  			} else if (columnIndex == ColumnEnum.START_DATE_ORD) {
83  				if (IMAGE_CACHE.containsKey(ColumnEnum.START_DATE)) {
84  					returnImage = (Image) IMAGE_CACHE.get(ColumnEnum.START_DATE);
85  				} else {
86  					ImageDescriptor descriptor = TimeTrackPlugin.getImageDescriptor("icons/TimeTrack.gif");
87  					if (descriptor != null) {
88  						returnImage = descriptor.createImage();
89  						IMAGE_CACHE.put(ColumnEnum.START_DATE, returnImage);
90  					}
91  				}
92  			} else if (columnIndex == ColumnEnum.COMMENT_ORD) {
93  				if (IMAGE_CACHE.containsKey(ColumnEnum.COMMENT)) {
94  					returnImage = (Image) IMAGE_CACHE.get(ColumnEnum.COMMENT);
95  				} else {
96  					ImageDescriptor descriptor = PlatformUI.getWorkbench()
97  					.getSharedImages().getImageDescriptor(
98  							ISharedImages.IMG_OBJ_FILE);
99  					if (descriptor != null) {
100 						returnImage = descriptor.createImage();
101 						IMAGE_CACHE.put(ColumnEnum.COMMENT, returnImage);
102 					}
103 				}
104 			} else if (columnIndex == ColumnEnum.TASK_ORD) {
105 				if (IMAGE_CACHE.containsKey(ColumnEnum.TASK)) {
106 					returnImage = (Image) IMAGE_CACHE.get(ColumnEnum.TASK);
107 				} else {
108 					ImageDescriptor descriptor = PlatformUI
109 					.getWorkbench()
110 					.getSharedImages()
111 					.getImageDescriptor(
112 							org.eclipse.ui.ide.IDE.SharedImages.IMG_OBJS_TASK_TSK);
113 					if (descriptor != null) {
114 						returnImage = descriptor.createImage();
115 						IMAGE_CACHE.put(ColumnEnum.TASK, returnImage);
116 					}
117 				}
118 			}
119 		}
120 		// return getImage(element);
121 		return returnImage;
122 	}
123 
124 	/***
125 	 * @param element - the element of the column
126 	 * @param columnIndex - the index of the column
127 	 * 
128 	 * @return a string describing the column text
129 	 */
130 	public final String getColumnText(final Object element, final int columnIndex) {
131 		String returnString = "";
132 		if (element instanceof EntryType) {
133 			EntryType entry = (EntryType) element;
134 			switch (columnIndex) {
135 			case ColumnEnum.COMMENT_ORD:
136 				returnString = entry.getComment();
137 				break;
138 			case ColumnEnum.DURATION_ORD:
139 				returnString = Long.toString(entry.getDuration());
140 				break;
141 			case ColumnEnum.PROJECT_ORD:
142 				returnString = entry.getProject();
143 				break;
144 			case ColumnEnum.START_DATE_ORD:
145 				Calendar cal = entry.getStartTime();
146 				if (cal != null) {
147 					DateFormat dateFormat = DateFormat.getDateTimeInstance();
148 					String string = dateFormat.format(cal.getTime());
149 					returnString = string;
150 				}
151 				break;
152 			case ColumnEnum.TASK_ORD:
153 				returnString = entry.getTask();
154 				break;
155 			default: 
156 				returnString = "";
157 			}
158 		} else {
159 			returnString = getText(element);
160 		}
161 		return returnString;
162 	}
163 
164 	/***
165 	 * @param obj - does nothing
166 	 * 
167 	 * @return a image
168 	 */
169 	public final Image getImage(final Object obj) {
170 		return PlatformUI.getWorkbench().getSharedImages().getImage(
171 				ISharedImages.IMG_OBJ_ELEMENT);
172 	}
173 }