]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/controllers/ArtifactLabelProvider.java
Adapt run as OSGi to felix Gogo console
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / controllers / ArtifactLabelProvider.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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.ArgeoException;
26 import org.argeo.jcr.JcrUtils;
27 import org.argeo.slc.client.ui.dist.DistConstants;
28 import org.argeo.slc.client.ui.dist.DistImages;
29 import org.argeo.slc.jcr.SlcTypes;
30 import org.eclipse.jface.viewers.ColumnLabelProvider;
31 import org.eclipse.jface.viewers.ViewerCell;
32 import org.eclipse.swt.graphics.Image;
33
34 public class ArtifactLabelProvider extends ColumnLabelProvider implements
35 DistConstants, SlcTypes {
36
37 // To be able to change column order easily
38 public static final int COLUMN_TREE = 0;
39 public static final int COLUMN_DATE = 1;
40 public static final int COLUMN_SIZE = 2;
41
42 // Utils
43 protected static DateFormat timeFormatter = new SimpleDateFormat(
44 DATE_TIME_FORMAT);
45
46 public void update(ViewerCell cell) {
47 int colIndex = cell.getColumnIndex();
48 Object element = cell.getElement();
49 cell.setText(getColumnText(element, colIndex));
50 if (element instanceof Node && colIndex == 0) {
51 Node node = (Node) element;
52 try {
53 if (node.isNodeType(SLC_ARTIFACT_BASE))
54 cell.setImage(DistImages.IMG_ARTIFACT_BASE);
55 else if (node.isNodeType(SLC_ARTIFACT_VERSION_BASE))
56 cell.setImage(DistImages.IMG_ARTIFACT_VERSION_BASE);
57 } catch (RepositoryException e) {
58 // Silent
59 }
60 }
61 }
62
63 @Override
64 public Image getImage(Object element) {
65
66 if (element instanceof Node) {
67 Node node = (Node) element;
68 try {
69 if (node.isNodeType(SLC_ARTIFACT_BASE)) {
70 return DistImages.IMG_ARTIFACT_BASE;
71 } else if (node.isNodeType(SLC_ARTIFACT_VERSION_BASE)) {
72 return DistImages.IMG_ARTIFACT_VERSION_BASE;
73 }
74 } catch (RepositoryException e) {
75 // Silent
76 }
77 }
78 return null;
79 }
80
81 public String getColumnText(Object element, int columnIndex) {
82 try {
83 if (element instanceof Node) {
84 Node node = (Node) element;
85 switch (columnIndex) {
86 case COLUMN_TREE:
87 return node.getName();
88 case COLUMN_SIZE:
89 long size = JcrUtils.getNodeApproxSize(node) / 1024;
90 if (size > 1024)
91 return size / 1024 + " MB";
92 else
93 return size + " KB";
94 case COLUMN_DATE:
95 if (node.hasProperty(Property.JCR_LAST_MODIFIED))
96 return timeFormatter.format(node
97 .getProperty(Property.JCR_LAST_MODIFIED)
98 .getDate().getTime());
99 else
100 return null;
101 }
102 }
103 } catch (RepositoryException re) {
104 throw new ArgeoException(
105 "Unexepected error while getting property values", re);
106 }
107 return null;
108 }
109 }