PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/rssowl-2.0.5/org.rssowl.ui/src/org/rssowl/ui/internal/editors/feed/NewsTableLabelProvider.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 747 lines | 463 code | 129 blank | 155 comment | 172 complexity | 1e2251624952e251022daea7cf61be15 MD5 | raw file
  1. /* ********************************************************************** **
  2. ** Copyright notice **
  3. ** **
  4. ** (c) 2005-2009 RSSOwl Development Team **
  5. ** http://www.rssowl.org/ **
  6. ** **
  7. ** All rights reserved **
  8. ** **
  9. ** This program and the accompanying materials are made available under **
  10. ** the terms of the Eclipse Public License v1.0 which accompanies this **
  11. ** distribution, and is available at: **
  12. ** http://www.rssowl.org/legal/epl-v10.html **
  13. ** **
  14. ** A copy is found in the file epl-v10.html and important notices to the **
  15. ** license from the team is found in the textfile LICENSE.txt distributed **
  16. ** in this package. **
  17. ** **
  18. ** This copyright notice MUST APPEAR in all copies of the file! **
  19. ** **
  20. ** Contributors: **
  21. ** RSSOwl Development Team - initial API and implementation **
  22. ** **
  23. ** ********************************************************************** */
  24. package org.rssowl.ui.internal.editors.feed;
  25. import org.eclipse.jface.resource.ImageDescriptor;
  26. import org.eclipse.jface.resource.JFaceResources;
  27. import org.eclipse.jface.resource.LocalResourceManager;
  28. import org.eclipse.jface.viewers.ColumnViewer;
  29. import org.eclipse.jface.viewers.OwnerDrawLabelProvider;
  30. import org.eclipse.jface.viewers.ViewerCell;
  31. import org.eclipse.jface.viewers.ViewerColumn;
  32. import org.eclipse.osgi.util.NLS;
  33. import org.eclipse.swt.SWT;
  34. import org.eclipse.swt.graphics.Color;
  35. import org.eclipse.swt.graphics.Font;
  36. import org.eclipse.swt.graphics.GC;
  37. import org.eclipse.swt.graphics.Image;
  38. import org.eclipse.swt.graphics.Point;
  39. import org.eclipse.swt.graphics.RGB;
  40. import org.eclipse.swt.graphics.Rectangle;
  41. import org.eclipse.swt.widgets.Display;
  42. import org.eclipse.swt.widgets.Event;
  43. import org.eclipse.swt.widgets.Item;
  44. import org.eclipse.swt.widgets.Scrollable;
  45. import org.eclipse.swt.widgets.TableItem;
  46. import org.eclipse.swt.widgets.TreeItem;
  47. import org.rssowl.core.persist.IAttachment;
  48. import org.rssowl.core.persist.IBookMark;
  49. import org.rssowl.core.persist.ICategory;
  50. import org.rssowl.core.persist.ILabel;
  51. import org.rssowl.core.persist.INews;
  52. import org.rssowl.core.persist.INewsBin;
  53. import org.rssowl.core.persist.IPerson;
  54. import org.rssowl.core.persist.INews.State;
  55. import org.rssowl.core.persist.dao.DynamicDAO;
  56. import org.rssowl.core.persist.reference.NewsBinReference;
  57. import org.rssowl.core.util.CoreUtils;
  58. import org.rssowl.core.util.DateUtils;
  59. import org.rssowl.core.util.StringUtils;
  60. import org.rssowl.core.util.URIUtils;
  61. import org.rssowl.ui.internal.Application;
  62. import org.rssowl.ui.internal.EntityGroup;
  63. import org.rssowl.ui.internal.OwlUI;
  64. import java.text.DateFormat;
  65. import java.util.Date;
  66. import java.util.HashMap;
  67. import java.util.List;
  68. import java.util.Map;
  69. import java.util.Set;
  70. /**
  71. * @author Ismael Juma (ismael@juma.me.uk)
  72. * @author bpasero
  73. */
  74. public class NewsTableLabelProvider extends OwnerDrawLabelProvider {
  75. /** News Column Model to use */
  76. protected NewsColumnViewModel fColumnModel;
  77. /* Some Colors of a Label */
  78. private static final String LABEL_COLOR_BLACK = "0,0,0"; //$NON-NLS-1$
  79. private static final String LABEL_COLOR_WHITE = "255,255,255"; //$NON-NLS-1$
  80. /** Resource Manager to use */
  81. protected LocalResourceManager fResources;
  82. /* Date Formatter for News */
  83. private final DateFormat fDateFormat = OwlUI.getShortDateFormat();
  84. /* Pre-Cache some Colors being used */
  85. private Color fStickyBgColor;
  86. private Color fGradientFgColor;
  87. private Color fGradientBgColor;
  88. private Color fGradientEndColor;
  89. private Color fGroupFgColor;
  90. private Color fGroupBgColor;
  91. private Color fNewsBgGradientStartColor;
  92. private Color fNewsBgGradientEndColor;
  93. private RGB fListBackground;
  94. private RGB fListSelectionBackground;
  95. /* Pre-Cache some Images being used */
  96. private Image fNewsUnreadIcon;
  97. private Image fNewsNewIcon;
  98. private Image fNewsUpdatedIcon;
  99. private Image fNewsReadIcon;
  100. private Image fNewsStickyIcon;
  101. private Image fNewsNonStickyIcon;
  102. private Image fGroupIcon;
  103. /* Pre-Cache some Fonts being used */
  104. private Font fBoldFont;
  105. /* A cache for the Feed and Location Column */
  106. private Map<Long, String> fMapBinIdToLocation = new HashMap<Long, String>();
  107. private Map<String, String> fMapFeedLinkToLocation = new HashMap<String, String>();
  108. private Map<String, ImageDescriptor> fMapFeedLinkToFeedIcon = new HashMap<String, ImageDescriptor>();
  109. /**
  110. * Creates a new instance of this LabelProvider
  111. *
  112. * @param model the column model.
  113. */
  114. public NewsTableLabelProvider(NewsColumnViewModel model) {
  115. fColumnModel = model;
  116. fResources = new LocalResourceManager(JFaceResources.getResources());
  117. createResources();
  118. }
  119. /*
  120. * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#initialize(org.eclipse.jface.viewers.ColumnViewer, org.eclipse.jface.viewers.ViewerColumn)
  121. */
  122. @Override
  123. protected void initialize(ColumnViewer viewer, ViewerColumn column) {
  124. super.initialize(viewer, column, false); //Disable Custom Ownerdrawn
  125. }
  126. /**
  127. * @param model
  128. */
  129. public void init(NewsColumnViewModel model) {
  130. fColumnModel = model;
  131. }
  132. void updateResources() {
  133. /* Sticky Color */
  134. fStickyBgColor = OwlUI.getThemeColor(OwlUI.STICKY_BG_COLOR_ID, fResources, new RGB(255, 255, 180));
  135. /* News Background Color */
  136. createNewsListBackgroundResources();
  137. }
  138. private void createResources() {
  139. /* Colors */
  140. fStickyBgColor = OwlUI.getThemeColor(OwlUI.STICKY_BG_COLOR_ID, fResources, new RGB(255, 255, 180));
  141. fGradientFgColor = OwlUI.getColor(fResources, OwlUI.GROUP_GRADIENT_FG_COLOR);
  142. fGradientBgColor = OwlUI.getColor(fResources, OwlUI.GROUP_GRADIENT_BG_COLOR);
  143. fGradientEndColor = OwlUI.getColor(fResources, OwlUI.GROUP_GRADIENT_END_COLOR);
  144. fGroupFgColor = OwlUI.getColor(fResources, OwlUI.GROUP_FG_COLOR);
  145. fGroupBgColor = OwlUI.getColor(fResources, OwlUI.GROUP_BG_COLOR);
  146. fListBackground = fResources.getDevice().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB();
  147. fListSelectionBackground = fResources.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION).getRGB();
  148. createNewsListBackgroundResources();
  149. /* Icons */
  150. fNewsUnreadIcon = OwlUI.getImage(fResources, OwlUI.NEWS_STATE_UNREAD);
  151. fNewsNewIcon = OwlUI.getImage(fResources, OwlUI.NEWS_STATE_NEW);
  152. fNewsUpdatedIcon = OwlUI.getImage(fResources, OwlUI.NEWS_STATE_UPDATED);
  153. fNewsReadIcon = OwlUI.getImage(fResources, OwlUI.NEWS_STATE_READ);
  154. fNewsStickyIcon = OwlUI.getImage(fResources, OwlUI.NEWS_PINNED);
  155. fNewsNonStickyIcon = OwlUI.getImage(fResources, OwlUI.NEWS_PIN);
  156. fGroupIcon = OwlUI.getImage(fResources, OwlUI.GROUP);
  157. /* Fonts */
  158. fBoldFont = OwlUI.getThemeFont(OwlUI.HEADLINES_FONT_ID, SWT.BOLD);
  159. }
  160. private void createNewsListBackgroundResources() {
  161. fNewsBgGradientStartColor = null;
  162. fNewsBgGradientEndColor = null;
  163. RGB listBackgroundRGB = Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB();
  164. RGB newsBgColorRGB = OwlUI.getThemeRGB(OwlUI.NEWS_LIST_BG_COLOR_ID, listBackgroundRGB);
  165. if (newsBgColorRGB != null && !listBackgroundRGB.equals(newsBgColorRGB) && !isInvalidColor(newsBgColorRGB)) {
  166. fNewsBgGradientEndColor = OwlUI.getColor(fResources, newsBgColorRGB);
  167. RGB newsBgColorRGBLighter = new RGB(0, 0, 0);
  168. newsBgColorRGBLighter.red = Math.min(newsBgColorRGB.red + 5, 255);
  169. newsBgColorRGBLighter.green = Math.min(newsBgColorRGB.green + 5, 255);
  170. newsBgColorRGBLighter.blue = Math.min(newsBgColorRGB.blue + 5, 255);
  171. fNewsBgGradientStartColor = OwlUI.getColor(fResources, newsBgColorRGBLighter);
  172. }
  173. }
  174. /*
  175. * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
  176. */
  177. @Override
  178. public void update(ViewerCell cell) {
  179. NewsColumn column = fColumnModel.getColumn(cell.getColumnIndex());
  180. /* Text */
  181. cell.setText(getColumnText(cell.getElement(), column, cell.getColumnIndex()));
  182. /* Image */
  183. cell.setImage(getColumnImage(cell.getElement(), column, cell.getColumnIndex()));
  184. /* Font */
  185. cell.setFont(getFont(cell.getElement(), cell.getColumnIndex()));
  186. /* Foreground */
  187. Color foreground = getForeground(cell.getElement(), cell.getColumnIndex());
  188. /* This is required to invalidate + redraw the entire TableItem! */
  189. if (!OwlUI.isHighContrast()) {
  190. Item item = (Item) cell.getItem();
  191. if (item instanceof TreeItem)
  192. ((TreeItem) cell.getItem()).setForeground(foreground);
  193. else if (item instanceof TableItem)
  194. ((TableItem) cell.getItem()).setForeground(foreground);
  195. }
  196. /* Background */
  197. if (!OwlUI.isHighContrast())
  198. cell.setBackground(getBackground(cell.getElement(), cell.getColumnIndex()));
  199. }
  200. /*
  201. * @see org.eclipse.jface.viewers.CellLabelProvider#getToolTipText(java.lang.Object)
  202. */
  203. @Override
  204. public String getToolTipText(Object element) {
  205. /* News */
  206. if (element instanceof INews) {
  207. INews news = (INews) element;
  208. String feedRef = news.getFeedLinkAsText();
  209. IBookMark bookMark = CoreUtils.getBookMark(feedRef);
  210. String name = null;
  211. if (bookMark != null)
  212. name = bookMark.getName();
  213. else
  214. name = feedRef;
  215. if (news.getParentId() != 0) {
  216. INewsBin bin = DynamicDAO.load(INewsBin.class, news.getParentId());
  217. if (bin != null) {
  218. name = NLS.bind(Messages.NewsTableLabelProvider_BIN_NAME, bin.getName(), name);
  219. }
  220. }
  221. return StringUtils.replaceAll(name, "&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
  222. }
  223. /* Entity Group */
  224. else if (element instanceof EntityGroup) {
  225. return StringUtils.replaceAll(((EntityGroup) element).getName(), "&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
  226. }
  227. return super.getToolTipText(element);
  228. }
  229. /*
  230. * @see org.eclipse.jface.viewers.CellLabelProvider#getToolTipShift(java.lang.Object)
  231. */
  232. @Override
  233. public Point getToolTipShift(Object object) {
  234. if (Application.IS_WINDOWS)
  235. return new Point(0, 21);
  236. return super.getToolTipShift(object);
  237. }
  238. /**
  239. * @param element
  240. * @param column
  241. * @param colIndex
  242. * @return String
  243. */
  244. protected String getColumnText(Object element, NewsColumn column, int colIndex) {
  245. String text = null;
  246. /* Handle News */
  247. if (element instanceof INews) {
  248. INews news = (INews) element;
  249. switch (column) {
  250. case TITLE:
  251. text = CoreUtils.getHeadline(news, true);
  252. break;
  253. case DATE:
  254. Date date = DateUtils.getRecentDate(news);
  255. text = fDateFormat.format(date);
  256. break;
  257. case PUBLISHED:
  258. Date published = news.getPublishDate();
  259. if (published != null)
  260. text = fDateFormat.format(published);
  261. break;
  262. case MODIFIED:
  263. Date modified = news.getModifiedDate();
  264. if (modified != null)
  265. text = fDateFormat.format(modified);
  266. break;
  267. case RECEIVED:
  268. Date received = news.getReceiveDate();
  269. if (received != null)
  270. text = fDateFormat.format(received);
  271. break;
  272. case AUTHOR:
  273. IPerson author = news.getAuthor();
  274. if (author != null) {
  275. if (author.getName() != null)
  276. text = author.getName();
  277. else if (author.getEmail() != null)
  278. text = author.getEmail().toString();
  279. }
  280. break;
  281. case CATEGORY:
  282. List<ICategory> categories = news.getCategories();
  283. if (!categories.isEmpty()) {
  284. StringBuilder str = new StringBuilder();
  285. for (ICategory category : categories) {
  286. if (StringUtils.isSet(category.getName()))
  287. str.append(category.getName().trim()).append(", "); //$NON-NLS-1$
  288. else if (StringUtils.isSet(category.getDomain()))
  289. str.append(category.getDomain().trim()).append(", "); //$NON-NLS-1$
  290. }
  291. if (str.length() > 0)
  292. str = str.delete(str.length() - 2, str.length());
  293. text = str.toString();
  294. }
  295. break;
  296. case LABELS:
  297. Set<ILabel> labels = CoreUtils.getSortedLabels(news);
  298. if (!labels.isEmpty()) {
  299. StringBuilder str = new StringBuilder();
  300. for (ILabel label : labels) {
  301. str.append(label.getName()).append(", "); //$NON-NLS-1$
  302. }
  303. if (str.length() > 0)
  304. str = str.delete(str.length() - 2, str.length());
  305. text = str.toString();
  306. }
  307. break;
  308. case STATUS:
  309. State state = news.getState();
  310. if (state == State.NEW)
  311. text = Messages.NewsTableLabelProvider_NEW;
  312. else if (state == State.UNREAD)
  313. text = Messages.NewsTableLabelProvider_UNREAD;
  314. else if (state == State.UPDATED)
  315. text = Messages.NewsTableLabelProvider_UPDATED;
  316. else if (state == State.READ)
  317. text = Messages.NewsTableLabelProvider_READ;
  318. break;
  319. case LOCATION:
  320. /* Location: Bin */
  321. if (news.getParentId() > 0) {
  322. String location = fMapBinIdToLocation.get(news.getParentId());
  323. if (location == null) {
  324. NewsBinReference ref = new NewsBinReference(news.getParentId());
  325. INewsBin bin = ref.resolve();
  326. location = bin.getName();
  327. fMapBinIdToLocation.put(news.getParentId(), location);
  328. }
  329. text = location;
  330. }
  331. /* Location: Bookmark */
  332. else {
  333. String location = fMapFeedLinkToLocation.get(news.getFeedLinkAsText());
  334. if (location == null) {
  335. IBookMark bookmark = CoreUtils.getBookMark(news.getFeedLinkAsText());
  336. if (bookmark != null) {
  337. location = bookmark.getName();
  338. fMapFeedLinkToLocation.put(news.getFeedLinkAsText(), location);
  339. }
  340. }
  341. text = location;
  342. }
  343. break;
  344. case LINK:
  345. text = CoreUtils.getLink(news);
  346. if (StringUtils.isSet(text)) {
  347. text = StringUtils.replaceAll(text, URIUtils.HTTP, ""); //$NON-NLS-1$
  348. text = StringUtils.replaceAll(text, "www.", ""); //$NON-NLS-1$ //$NON-NLS-2$
  349. }
  350. }
  351. }
  352. /* Handle EntityGroup */
  353. else if (element instanceof EntityGroup && column == NewsColumn.TITLE)
  354. text = ((EntityGroup) element).getName();
  355. /* Make sure to normalize the Text for the Table */
  356. return text != null ? StringUtils.normalizeString(text) : null;
  357. }
  358. /**
  359. * @param element
  360. * @param newsColumn
  361. * @param colIndex
  362. * @return Image
  363. */
  364. protected Image getColumnImage(Object element, NewsColumn newsColumn, int colIndex) {
  365. /* News */
  366. if (element instanceof INews) {
  367. INews news = (INews) element;
  368. /* News Icon */
  369. if (newsColumn == NewsColumn.TITLE) {
  370. if (news.getState() == INews.State.UNREAD)
  371. return fNewsUnreadIcon;
  372. else if (news.getState() == INews.State.NEW)
  373. return fNewsNewIcon;
  374. else if (news.getState() == INews.State.UPDATED)
  375. return fNewsUpdatedIcon;
  376. else if (news.getState() == INews.State.READ)
  377. return fNewsReadIcon;
  378. }
  379. /* Feed Column */
  380. else if (newsColumn == NewsColumn.FEED) {
  381. String feedRef = news.getFeedLinkAsText();
  382. ImageDescriptor feedIcon = fMapFeedLinkToFeedIcon.get(feedRef);
  383. if (feedIcon == null) {
  384. IBookMark bookMark = CoreUtils.getBookMark(feedRef);
  385. if (bookMark != null)
  386. feedIcon = OwlUI.getFavicon(bookMark);
  387. if (feedIcon == null)
  388. feedIcon = OwlUI.BOOKMARK;
  389. fMapFeedLinkToFeedIcon.put(feedRef, feedIcon);
  390. }
  391. return OwlUI.getImage(fResources, feedIcon);
  392. }
  393. /* Sticky State */
  394. else if (newsColumn == NewsColumn.STICKY) {
  395. if (news.isFlagged())
  396. return fNewsStickyIcon;
  397. return fNewsNonStickyIcon;
  398. }
  399. /* Attachment */
  400. else if (newsColumn == NewsColumn.ATTACHMENTS) {
  401. List<IAttachment> attachments = news.getAttachments();
  402. if (!attachments.isEmpty())
  403. return OwlUI.getImage(fResources, OwlUI.ATTACHMENT);
  404. }
  405. }
  406. /* EntityGroup Image */
  407. else if (element instanceof EntityGroup && newsColumn == NewsColumn.TITLE) {
  408. EntityGroup group = (EntityGroup) element;
  409. if (group.getImage() != null)
  410. return OwlUI.getImage(fResources, group.getImage());
  411. return fGroupIcon;
  412. }
  413. return null;
  414. }
  415. /**
  416. * @param element
  417. * @param columnIndex
  418. * @return Font
  419. */
  420. protected Font getFont(Object element, int columnIndex) {
  421. /* Use a Bold Font for Unread News */
  422. if (element instanceof INews) {
  423. INews news = (INews) element;
  424. INews.State state = news.getState();
  425. if (state == null)
  426. return null;
  427. /* Bold for New, Updated and Unread News */
  428. if (state == INews.State.NEW || state == INews.State.UPDATED || state == INews.State.UNREAD)
  429. return fBoldFont;
  430. }
  431. /* Use Bold Font for EntityGroup */
  432. if (element instanceof EntityGroup)
  433. return fBoldFont;
  434. return null;
  435. }
  436. /**
  437. * @param element
  438. * @param columnIndex
  439. * @return Color
  440. */
  441. protected Color getBackground(Object element, int columnIndex) {
  442. /* Handle INews */
  443. if (element instanceof INews && ((INews) element).isFlagged())
  444. return fStickyBgColor;
  445. /* Handle EntityGroup */
  446. else if (element instanceof EntityGroup)
  447. return fGroupBgColor;
  448. return null;
  449. }
  450. /**
  451. * @param element
  452. * @param columnIndex
  453. * @return Color
  454. */
  455. protected Color getForeground(Object element, int columnIndex) {
  456. /* Handle EntityGroup */
  457. if (element instanceof EntityGroup) {
  458. EntityGroup group = (EntityGroup) element;
  459. if (group.getColorHint() != null) {
  460. if (!fListBackground.equals(group.getColorHint()) && !fListSelectionBackground.equals(group.getColorHint()))
  461. return OwlUI.getColor(fResources, group.getColorHint());
  462. }
  463. return fGroupFgColor;
  464. }
  465. /* Handle INews */
  466. else if (element instanceof INews) {
  467. Set<ILabel> labels = CoreUtils.getSortedLabels((INews) element);
  468. if (!labels.isEmpty()) {
  469. RGB labelRGB = OwlUI.getRGB(labels.iterator().next());
  470. if (!fListBackground.equals(labelRGB) && !fListSelectionBackground.equals(labelRGB))
  471. return OwlUI.getColor(fResources, labelRGB);
  472. }
  473. }
  474. return null;
  475. }
  476. /*
  477. * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
  478. */
  479. @Override
  480. public void dispose() {
  481. fResources.dispose();
  482. }
  483. /*
  484. * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object,
  485. * java.lang.String)
  486. */
  487. @Override
  488. public boolean isLabelProperty(Object element, String property) {
  489. return false;
  490. }
  491. /*
  492. * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#erase(org.eclipse.swt.widgets.Event,
  493. * java.lang.Object)
  494. */
  495. @Override
  496. public void erase(Event event, Object element) {
  497. /* Erase News */
  498. if (element instanceof INews)
  499. eraseNews(event, (INews) element);
  500. /* Erase Group */
  501. else if (element instanceof EntityGroup)
  502. eraseGroup(event, (EntityGroup) element);
  503. }
  504. private void eraseGroup(Event event, EntityGroup group) {
  505. Scrollable scrollable = (Scrollable) event.widget;
  506. GC gc = event.gc;
  507. /* Draw Color if Selected */
  508. if (group.getColorHint() != null && (event.detail & SWT.SELECTED) != 0) {
  509. /* Some conditions under which we don't override the selection color */
  510. if (!scrollable.isFocusControl() || isInvalidColor(group.getColorHint()))
  511. return;
  512. Rectangle clArea = scrollable.getClientArea();
  513. Rectangle itemRect = event.getBounds();
  514. /* Paint the selection beyond the end of last column */
  515. OwlUI.codExpandRegion(event, scrollable, gc, clArea);
  516. /* Draw Rectangle */
  517. Color oldBackground = gc.getBackground();
  518. gc.setBackground(OwlUI.getColor(fResources, group.getColorHint()));
  519. gc.fillRectangle(0, itemRect.y, clArea.width, itemRect.height);
  520. gc.setBackground(oldBackground);
  521. gc.setForeground(scrollable.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
  522. /* Mark as Selected being handled */
  523. event.detail &= ~SWT.SELECTED;
  524. }
  525. /* Draw Gradient */
  526. else
  527. OwlUI.codDrawGradient(event, fGradientFgColor, fGradientBgColor, fGradientEndColor);
  528. }
  529. private void eraseNews(Event event, INews news) {
  530. Scrollable scrollable = (Scrollable) event.widget;
  531. GC gc = event.gc;
  532. /* Handle selected News (Linux: Note Bug 444) */
  533. if ((event.detail & SWT.SELECTED) != 0 && (Application.IS_WINDOWS || !news.isFlagged())) {
  534. /* Do not override selection color if not focus control */
  535. if (!scrollable.isFocusControl())
  536. return;
  537. /* Load Labels */
  538. Set<ILabel> labels = CoreUtils.getSortedLabels(news);
  539. if (labels.isEmpty())
  540. return;
  541. ILabel label = labels.iterator().next();
  542. if (isInvalidColor(label))
  543. return;
  544. Rectangle clArea = scrollable.getClientArea();
  545. Rectangle itemRect = event.getBounds();
  546. /* Paint the selection beyond the end of last column */
  547. OwlUI.codExpandRegion(event, scrollable, gc, clArea);
  548. /* Draw Rectangle */
  549. Color oldBackground = gc.getBackground();
  550. gc.setBackground(OwlUI.getColor(fResources, label));
  551. gc.fillRectangle(0, itemRect.y, clArea.width, itemRect.height);
  552. gc.setBackground(oldBackground);
  553. gc.setForeground(scrollable.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
  554. /* Mark as Selected being handled */
  555. event.detail &= ~SWT.SELECTED;
  556. }
  557. /* Handle Non-Selected flagged News */
  558. else if (news.isFlagged()) {
  559. Rectangle clArea = scrollable.getClientArea();
  560. Rectangle itemRect = event.getBounds();
  561. /* Paint the selection beyond the end of last column */
  562. OwlUI.codExpandRegion(event, scrollable, gc, clArea);
  563. /* Draw Rectangle */
  564. Color oldBackground = gc.getBackground();
  565. gc.setBackground(fStickyBgColor);
  566. gc.fillRectangle(0, itemRect.y, clArea.width, itemRect.height);
  567. gc.setBackground(oldBackground);
  568. /* Mark as Background being handled */
  569. event.detail &= ~SWT.BACKGROUND;
  570. }
  571. /* Handle News List Background Color if set */
  572. else if (fNewsBgGradientStartColor != null && fNewsBgGradientEndColor != null) {
  573. int index = 0;
  574. /* Tree */
  575. if (event.item instanceof TreeItem) {
  576. TreeItem item = (TreeItem) event.item;
  577. TreeItem parentItem = item.getParentItem();
  578. if (parentItem != null)
  579. index = parentItem.indexOf(item);
  580. else
  581. index = item.getParent().indexOf(item);
  582. }
  583. /* Table */
  584. else if (event.item instanceof TableItem) {
  585. TableItem item = (TableItem) event.item;
  586. index = item.getParent().indexOf(item);
  587. }
  588. if (index % 2 != 0)
  589. OwlUI.codDrawGradient(event, fNewsBgGradientStartColor, fNewsBgGradientEndColor, fNewsBgGradientEndColor);
  590. }
  591. }
  592. private boolean isInvalidColor(ILabel label) {
  593. return label.getColor().equals(LABEL_COLOR_BLACK) || label.getColor().equals(LABEL_COLOR_WHITE);
  594. }
  595. private boolean isInvalidColor(RGB color) {
  596. if (color.blue == 0 && color.red == 0 && color.green == 0)
  597. return true;
  598. if (color.blue == 255 && color.red == 255 && color.green == 255)
  599. return true;
  600. return false;
  601. }
  602. /*
  603. * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#measure(org.eclipse.swt.widgets.Event,
  604. * java.lang.Object)
  605. */
  606. @Override
  607. protected void measure(Event event, Object element) {
  608. /* Ignore */
  609. }
  610. /*
  611. * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#paint(org.eclipse.swt.widgets.Event,
  612. * java.lang.Object)
  613. */
  614. @Override
  615. protected void paint(Event event, Object element) {
  616. /* Ignore */
  617. }
  618. }