]> git.argeo.org Git - gpl/argeo-slc.git/blob - controllers/ArtifactLabelProvider.java
Prepare next development cycle
[gpl/argeo-slc.git] / controllers / ArtifactLabelProvider.java
1 package org.argeo.slc.client.ui.dist.controllers;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5
6 import javax.jcr.Node;
7 import javax.jcr.Property;
8 import javax.jcr.RepositoryException;
9
10 import org.argeo.jcr.JcrUtils;
11 import org.argeo.slc.SlcException;
12 import org.argeo.slc.SlcTypes;
13 import org.argeo.slc.client.ui.dist.DistConstants;
14 import org.argeo.slc.client.ui.dist.DistImages;
15 import org.eclipse.jface.viewers.ColumnLabelProvider;
16 import org.eclipse.jface.viewers.ViewerCell;
17 import org.eclipse.swt.graphics.Image;
18
19 /** Retrieve artifact information to be displayed in an artifact tree or table */
20 public class ArtifactLabelProvider extends ColumnLabelProvider implements
21 DistConstants, SlcTypes {
22 private static final long serialVersionUID = 8672622174076959016L;
23
24 // To be able to change column order easily
25 public static final int COLUMN_TREE = 0;
26 public static final int COLUMN_DATE = 1;
27 public static final int COLUMN_SIZE = 2;
28
29 // Utils
30 protected static DateFormat timeFormatter = new SimpleDateFormat(
31 DATE_TIME_FORMAT);
32
33 public void update(ViewerCell cell) {
34 int colIndex = cell.getColumnIndex();
35 Object element = cell.getElement();
36 cell.setText(getColumnText(element, colIndex));
37 if (element instanceof Node && colIndex == 0) {
38 Node node = (Node) element;
39 try {
40 if (node.isNodeType(SLC_ARTIFACT_BASE))
41 cell.setImage(DistImages.IMG_ARTIFACT_BASE);
42 else if (node.isNodeType(SLC_ARTIFACT_VERSION_BASE))
43 cell.setImage(DistImages.IMG_ARTIFACT_VERSION_BASE);
44 } catch (RepositoryException e) {
45 // Silent
46 }
47 }
48 }
49
50 @Override
51 public Image getImage(Object element) {
52
53 if (element instanceof Node) {
54 Node node = (Node) element;
55 try {
56 if (node.isNodeType(SLC_ARTIFACT_BASE)) {
57 return DistImages.IMG_ARTIFACT_BASE;
58 } else if (node.isNodeType(SLC_ARTIFACT_VERSION_BASE)) {
59 return DistImages.IMG_ARTIFACT_VERSION_BASE;
60 }
61 } catch (RepositoryException e) {
62 // Silent
63 }
64 }
65 return null;
66 }
67
68 public String getColumnText(Object element, int columnIndex) {
69 try {
70 if (element instanceof Node) {
71 Node node = (Node) element;
72 switch (columnIndex) {
73 case COLUMN_TREE:
74 return node.getName();
75 case COLUMN_SIZE:
76 long size = JcrUtils.getNodeApproxSize(node) / 1024;
77 if (size > 1024)
78 return size / 1024 + " MB";
79 else
80 return size + " KB";
81 case COLUMN_DATE:
82 if (node.hasProperty(Property.JCR_LAST_MODIFIED))
83 return timeFormatter.format(node
84 .getProperty(Property.JCR_LAST_MODIFIED)
85 .getDate().getTime());
86 else
87 return null;
88 }
89 }
90 } catch (RepositoryException re) {
91 throw new SlcException(
92 "Unexepected error while getting property values", re);
93 }
94 return null;
95 }
96 }