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