KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > ui > refactoring > history > RefactoringHistoryLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ltk.ui.refactoring.history;
12
13 import com.ibm.icu.text.DateFormat;
14 import com.ibm.icu.text.SimpleDateFormat;
15
16 import java.text.Format JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.Locale JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21
22 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
23 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
24
25 import org.eclipse.ltk.internal.ui.refactoring.Messages;
26 import org.eclipse.ltk.internal.ui.refactoring.RefactoringPluginImages;
27 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
28 import org.eclipse.ltk.internal.ui.refactoring.history.RefactoringDescriptorImageDescriptor;
29 import org.eclipse.ltk.internal.ui.refactoring.history.RefactoringHistoryDate;
30 import org.eclipse.ltk.internal.ui.refactoring.history.RefactoringHistoryEntry;
31 import org.eclipse.ltk.internal.ui.refactoring.history.RefactoringHistoryNode;
32 import org.eclipse.ltk.internal.ui.refactoring.history.RefactoringImageDescriptor;
33
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.swt.graphics.Point;
36 import org.eclipse.swt.graphics.Rectangle;
37
38 import org.eclipse.jface.viewers.LabelProvider;
39
40 /**
41  * Label provider to display a refactoring history. This label provider can be
42  * used in conjunction with {@link RefactoringHistoryContentProvider} or
43  * directly on {@link RefactoringDescriptorProxy} and {@link RefactoringHistory}
44  * elements.
45  * <p>
46  * Note: this class is not indented to be subclassed outside the refactoring
47  * framework.
48  * </p>
49  *
50  * @see IRefactoringHistoryControl
51  * @see RefactoringHistoryControlConfiguration
52  * @see RefactoringHistoryContentProvider
53  *
54  * @since 3.2
55  */

56 public class RefactoringHistoryLabelProvider extends LabelProvider {
57
58     /** The collection image */
59     private final Image fCollectionImage;
60
61     /** The container image */
62     private final Image fContainerImage;
63
64     /** The control configuration to use */
65     private final RefactoringHistoryControlConfiguration fControlConfiguration;
66
67     /** The current locale to use */
68     private final Locale JavaDoc fCurrentLocale;
69
70     /** The cached date format, or <code>null</code> */
71     private DateFormat fDateFormat= null;
72
73     /** The decorated element image */
74     private Image fDecoratedElementImage= null;
75
76     /** The decorated item image */
77     private Image fDecoratedItemImage= null;
78
79     /** The element image */
80     private final Image fElementImage;
81
82     /** The item image */
83     private final Image fItemImage;
84
85     /**
86      * Creates a new refactoring history label provider.
87      *
88      * @param configuration
89      * the refactoring history control configuration to use
90      */

91     public RefactoringHistoryLabelProvider(final RefactoringHistoryControlConfiguration configuration) {
92         Assert.isNotNull(configuration);
93         fControlConfiguration= configuration;
94         fItemImage= RefactoringPluginImages.DESC_OBJS_REFACTORING.createImage();
95         fContainerImage= RefactoringPluginImages.DESC_OBJS_REFACTORING_DATE.createImage();
96         fElementImage= RefactoringPluginImages.DESC_OBJS_REFACTORING_TIME.createImage();
97         fCollectionImage= RefactoringPluginImages.DESC_OBJS_REFACTORING_COLL.createImage();
98         fCurrentLocale= new Locale JavaDoc(RefactoringUIMessages.RefactoringHistoryLabelProvider_label_language, RefactoringUIMessages.RefactoringHistoryLabelProvider_label_country, RefactoringUIMessages.RefactoringHistoryLabelProvider_label_variant);
99     }
100
101     /**
102      * Decorates the image for the specified element.
103      *
104      * @param image
105      * the image to decorate
106      * @param element
107      * the associated element
108      * @return the decorated image
109      */

110     private Image decorateImage(final Image image, final Object JavaDoc element) {
111         Image result= image;
112         RefactoringDescriptorProxy extended= null;
113         if (element instanceof RefactoringHistoryEntry)
114             extended= ((RefactoringHistoryEntry) element).getDescriptor();
115         else if (element instanceof RefactoringDescriptorProxy)
116             extended= (RefactoringDescriptorProxy) element;
117         if (extended != null) {
118             final String JavaDoc project= extended.getProject();
119             if (project == null || "".equals(project)) { //$NON-NLS-1$
120
if (image == fElementImage && fDecoratedElementImage != null)
121                     result= fDecoratedElementImage;
122                 else if (image == fItemImage && fDecoratedItemImage != null)
123                     result= fDecoratedItemImage;
124                 else {
125                     final Rectangle bounds= image.getBounds();
126                     result= new RefactoringDescriptorImageDescriptor(new RefactoringImageDescriptor(image), RefactoringDescriptorImageDescriptor.WORKSPACE, new Point(bounds.width, bounds.height)).createImage();
127                     if (image == fElementImage)
128                         fDecoratedElementImage= result;
129                     else if (image == fItemImage)
130                         fDecoratedItemImage= result;
131                 }
132             }
133         }
134         return result;
135     }
136
137     /**
138      * {@inheritDoc}
139      */

140     public void dispose() {
141         if (fContainerImage != null)
142             fContainerImage.dispose();
143         if (fCollectionImage != null)
144             fCollectionImage.dispose();
145         if (fElementImage != null)
146             fElementImage.dispose();
147         if (fItemImage != null)
148             fItemImage.dispose();
149         if (fDecoratedElementImage != null)
150             fDecoratedElementImage.dispose();
151         if (fDecoratedItemImage != null)
152             fDecoratedItemImage.dispose();
153     }
154
155     /**
156      * Returns a cached date format for the current locale.
157      *
158      * @return a cached date format for the current locale
159      */

160     private DateFormat getDateFormat() {
161         if (fDateFormat == null)
162             fDateFormat= DateFormat.getTimeInstance(DateFormat.SHORT);
163         return fDateFormat;
164     }
165
166     /**
167      * Returns the label for the specified refactoring descriptor.
168      *
169      * @param descriptor
170      * the refactoring descriptor
171      * @return the label of the descriptor
172      */

173     private String JavaDoc getDescriptorLabel(final RefactoringDescriptorProxy descriptor) {
174         if (fControlConfiguration.isTimeDisplayed()) {
175             final long stamp= descriptor.getTimeStamp();
176             if (stamp >= 0)
177                 return Messages.format(fControlConfiguration.getRefactoringPattern(), new String JavaDoc[] { getDateFormat().format(new Date JavaDoc(stamp)), descriptor.getDescription()});
178         }
179         return descriptor.getDescription();
180     }
181
182     /**
183      * {@inheritDoc}
184      */

185     public Image getImage(final Object JavaDoc element) {
186         Image image= null;
187         final boolean time= fControlConfiguration.isTimeDisplayed();
188         if (element instanceof RefactoringHistoryEntry || element instanceof RefactoringDescriptorProxy)
189             image= time ? fElementImage : fItemImage;
190         else
191             image= time ? fContainerImage : fCollectionImage;
192         return decorateImage(image, element);
193     }
194
195     /**
196      * {@inheritDoc}
197      */

198     public String JavaDoc getText(Object JavaDoc element) {
199         if (element instanceof RefactoringHistoryEntry) {
200             final RefactoringHistoryEntry entry= (RefactoringHistoryEntry) element;
201             return getDescriptorLabel(entry.getDescriptor());
202         } else if (element instanceof RefactoringDescriptorProxy) {
203             return getDescriptorLabel((RefactoringDescriptorProxy) element);
204         } else if (element instanceof RefactoringHistory) {
205             return RefactoringUIMessages.RefactoringHistoryControlConfiguration_collection_label;
206         } else if (element instanceof RefactoringHistoryNode) {
207             final RefactoringHistoryNode node= (RefactoringHistoryNode) element;
208             final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(32);
209             final int kind= node.getKind();
210             switch (kind) {
211                 case RefactoringHistoryNode.COLLECTION:
212                     buffer.append(fControlConfiguration.getCollectionLabel());
213                     break;
214                 default: {
215                     if (node instanceof RefactoringHistoryDate) {
216                         final RefactoringHistoryDate date= (RefactoringHistoryDate) node;
217                         final Date JavaDoc stamp= new Date JavaDoc(date.getTimeStamp());
218                         Format format= null;
219                         String JavaDoc pattern= ""; //$NON-NLS-1$
220
switch (kind) {
221                             case RefactoringHistoryNode.THIS_WEEK:
222                                 pattern= fControlConfiguration.getThisWeekPattern();
223                                 format= new SimpleDateFormat(RefactoringUIMessages.RefactoringHistoryLabelProvider_this_week_format, fCurrentLocale);
224                                 break;
225                             case RefactoringHistoryNode.LAST_WEEK:
226                                 pattern= fControlConfiguration.getLastWeekPattern();
227                                 format= new SimpleDateFormat(RefactoringUIMessages.RefactoringHistoryLabelProvider_last_week_format, fCurrentLocale);
228                                 break;
229                             case RefactoringHistoryNode.WEEK:
230                                 pattern= fControlConfiguration.getWeekPattern();
231                                 format= new SimpleDateFormat(RefactoringUIMessages.RefactoringHistoryLabelProvider_week_format, fCurrentLocale);
232                                 break;
233                             case RefactoringHistoryNode.YEAR:
234                                 pattern= fControlConfiguration.getYearPattern();
235                                 format= new SimpleDateFormat(RefactoringUIMessages.RefactoringHistoryLabelProvider_year_format, fCurrentLocale);
236                                 break;
237                             case RefactoringHistoryNode.THIS_MONTH:
238                                 pattern= fControlConfiguration.getThisMonthPattern();
239                                 format= new java.text.SimpleDateFormat JavaDoc(RefactoringUIMessages.RefactoringHistoryLabelProvider_this_month_format, fCurrentLocale);
240                                 break;
241                             case RefactoringHistoryNode.LAST_MONTH:
242                                 pattern= fControlConfiguration.getLastMonthPattern();
243                                 format= new java.text.SimpleDateFormat JavaDoc(RefactoringUIMessages.RefactoringHistoryLabelProvider_last_month_format, fCurrentLocale);
244                                 break;
245                             case RefactoringHistoryNode.MONTH:
246                                 pattern= fControlConfiguration.getMonthPattern();
247                                 format= new java.text.SimpleDateFormat JavaDoc(RefactoringUIMessages.RefactoringHistoryLabelProvider_month_format, fCurrentLocale);
248                                 break;
249                             case RefactoringHistoryNode.DAY:
250                                 pattern= fControlConfiguration.getDayPattern();
251                                 final int type= node.getParent().getKind();
252                                 if (type == RefactoringHistoryNode.THIS_WEEK || type == RefactoringHistoryNode.LAST_WEEK) {
253                                     final SimpleDateFormat simple= new SimpleDateFormat(RefactoringUIMessages.RefactoringHistoryLabelProvider_day_format, fCurrentLocale);
254                                     buffer.append(Messages.format(RefactoringUIMessages.RefactoringHistoryControlConfiguration_day_detailed_pattern, new String JavaDoc[] { simple.format(stamp), DateFormat.getDateInstance().format(stamp)}));
255                                 } else
256                                     format= DateFormat.getDateInstance();
257                                 break;
258                             case RefactoringHistoryNode.YESTERDAY:
259                                 pattern= fControlConfiguration.getYesterdayPattern();
260                                 format= DateFormat.getDateInstance();
261                                 break;
262                             case RefactoringHistoryNode.TODAY:
263                                 pattern= fControlConfiguration.getTodayPattern();
264                                 format= DateFormat.getDateInstance();
265                                 break;
266                         }
267                         if (format != null)
268                             buffer.append(Messages.format(pattern,format.format(stamp)));
269                     }
270                 }
271             }
272             return buffer.toString();
273         }
274         return super.getText(element);
275     }
276 }
277
Popular Tags