1 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 ; 17 import java.util.Date ; 18 import java.util.Locale ; 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 56 public class RefactoringHistoryLabelProvider extends LabelProvider { 57 58 59 private final Image fCollectionImage; 60 61 62 private final Image fContainerImage; 63 64 65 private final RefactoringHistoryControlConfiguration fControlConfiguration; 66 67 68 private final Locale fCurrentLocale; 69 70 71 private DateFormat fDateFormat= null; 72 73 74 private Image fDecoratedElementImage= null; 75 76 77 private Image fDecoratedItemImage= null; 78 79 80 private final Image fElementImage; 81 82 83 private final Image fItemImage; 84 85 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 (RefactoringUIMessages.RefactoringHistoryLabelProvider_label_language, RefactoringUIMessages.RefactoringHistoryLabelProvider_label_country, RefactoringUIMessages.RefactoringHistoryLabelProvider_label_variant); 99 } 100 101 110 private Image decorateImage(final Image image, final Object 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 project= extended.getProject(); 119 if (project == null || "".equals(project)) { 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 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 160 private DateFormat getDateFormat() { 161 if (fDateFormat == null) 162 fDateFormat= DateFormat.getTimeInstance(DateFormat.SHORT); 163 return fDateFormat; 164 } 165 166 173 private String 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 [] { getDateFormat().format(new Date (stamp)), descriptor.getDescription()}); 178 } 179 return descriptor.getDescription(); 180 } 181 182 185 public Image getImage(final Object 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 198 public String getText(Object 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 buffer= new StringBuffer (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 stamp= new Date (date.getTimeStamp()); 218 Format format= null; 219 String pattern= ""; 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 (RefactoringUIMessages.RefactoringHistoryLabelProvider_this_month_format, fCurrentLocale); 240 break; 241 case RefactoringHistoryNode.LAST_MONTH: 242 pattern= fControlConfiguration.getLastMonthPattern(); 243 format= new java.text.SimpleDateFormat (RefactoringUIMessages.RefactoringHistoryLabelProvider_last_month_format, fCurrentLocale); 244 break; 245 case RefactoringHistoryNode.MONTH: 246 pattern= fControlConfiguration.getMonthPattern(); 247 format= new java.text.SimpleDateFormat (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 [] { 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 |