View Javadoc
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.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  /** Retrieve artifact information to be displayed in an artifact tree or table */
35  public class ArtifactLabelProvider extends ColumnLabelProvider implements
36  		DistConstants, SlcTypes {
37  	private static final long serialVersionUID = 8672622174076959016L;
38  
39  	// To be able to change column order easily
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  	// Utils
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  				// Silent
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  				// Silent
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 }