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.cms.ui.jcr;
17  
18  import java.text.DateFormat;
19  import java.text.SimpleDateFormat;
20  
21  import javax.jcr.Property;
22  import javax.jcr.PropertyType;
23  import javax.jcr.RepositoryException;
24  import javax.jcr.Value;
25  
26  import org.argeo.cms.ui.CmsConstants;
27  import org.argeo.eclipse.ui.EclipseUiException;
28  import org.argeo.jcr.JcrUtils;
29  import org.eclipse.jface.viewers.ColumnLabelProvider;
30  import org.eclipse.jface.viewers.ViewerCell;
31  
32  /** Default basic label provider for a given JCR Node's properties */
33  public class PropertyLabelProvider extends ColumnLabelProvider {
34  	private static final long serialVersionUID = -5405794508731390147L;
35  
36  	// To be able to change column order easily
37  	public static final int COLUMN_PROPERTY = 0;
38  	public static final int COLUMN_VALUE = 1;
39  	public static final int COLUMN_TYPE = 2;
40  	public static final int COLUMN_ATTRIBUTES = 3;
41  
42  	// Utils
43  	protected DateFormat timeFormatter = new SimpleDateFormat(CmsConstants.DATE_TIME_FORMAT);
44  
45  	public void update(ViewerCell cell) {
46  		Object element = cell.getElement();
47  		cell.setText(getColumnText(element, cell.getColumnIndex()));
48  	}
49  
50  	public String getColumnText(Object element, int columnIndex) {
51  		try {
52  			if (element instanceof Property) {
53  				Property prop = (Property) element;
54  				if (prop.isMultiple()) {
55  					switch (columnIndex) {
56  					case COLUMN_PROPERTY:
57  						return prop.getName();
58  					case COLUMN_VALUE:
59  						// Corresponding values are listed on children
60  						return "";
61  					case COLUMN_TYPE:
62  						return JcrBrowserUtils.getPropertyTypeAsString(prop);
63  					case COLUMN_ATTRIBUTES:
64  						return JcrUtils.getPropertyDefinitionAsString(prop);
65  					}
66  				} else {
67  					switch (columnIndex) {
68  					case COLUMN_PROPERTY:
69  						return prop.getName();
70  					case COLUMN_VALUE:
71  						return formatValueAsString(prop.getValue());
72  					case COLUMN_TYPE:
73  						return JcrBrowserUtils.getPropertyTypeAsString(prop);
74  					case COLUMN_ATTRIBUTES:
75  						return JcrUtils.getPropertyDefinitionAsString(prop);
76  					}
77  				}
78  			} else if (element instanceof Value) {
79  				Value val = (Value) element;
80  				switch (columnIndex) {
81  				case COLUMN_PROPERTY:
82  					// Nothing to show
83  					return "";
84  				case COLUMN_VALUE:
85  					return formatValueAsString(val);
86  				case COLUMN_TYPE:
87  					// listed on the parent
88  					return "";
89  				case COLUMN_ATTRIBUTES:
90  					// Corresponding attributes are listed on the parent
91  					return "";
92  				}
93  			}
94  		} catch (RepositoryException re) {
95  			throw new EclipseUiException("Cannot retrieve prop value on " + element, re);
96  		}
97  		return null;
98  	}
99  
100 	private String formatValueAsString(Value value) {
101 		// TODO enhance this method
102 		try {
103 			String strValue;
104 
105 			if (value.getType() == PropertyType.BINARY)
106 				strValue = "<binary>";
107 			else if (value.getType() == PropertyType.DATE)
108 				strValue = timeFormatter.format(value.getDate().getTime());
109 			else
110 				strValue = value.getString();
111 			return strValue;
112 		} catch (RepositoryException e) {
113 			throw new EclipseUiException("unexpected error while formatting value", e);
114 		}
115 	}
116 }