]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/internal/PropertyLabelProvider.java
Adapt file upload/download to latest RWT version.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / ui / workbench / jcr / internal / PropertyLabelProvider.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.eclipse.ui.workbench.jcr.internal;
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.ArgeoException;
27 import org.argeo.eclipse.ui.workbench.WorkbenchConstants;
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 implements
34 WorkbenchConstants {
35 private static final long serialVersionUID = -5405794508731390147L;
36
37 // To be able to change column order easily
38 public static final int COLUMN_PROPERTY = 0;
39 public static final int COLUMN_VALUE = 1;
40 public static final int COLUMN_ATTRIBUTES = 2;
41
42 // Utils
43 protected DateFormat timeFormatter = new SimpleDateFormat(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_ATTRIBUTES:
62 return JcrUtils.getPropertyDefinitionAsString(prop);
63 }
64 } else {
65 switch (columnIndex) {
66 case COLUMN_PROPERTY:
67 return prop.getName();
68 case COLUMN_VALUE:
69 return formatValueAsString(prop.getValue());
70 case COLUMN_ATTRIBUTES:
71 return JcrUtils.getPropertyDefinitionAsString(prop);
72 }
73 }
74 } else if (element instanceof Value) {
75 Value val = (Value) element;
76
77 switch (columnIndex) {
78 case COLUMN_PROPERTY:
79 // Nothing to show
80 return "";
81 case COLUMN_VALUE:
82 return formatValueAsString(val);
83 case COLUMN_ATTRIBUTES:
84 // Corresponding attributes are listed on the parent
85 return "";
86 }
87 }
88
89 } catch (RepositoryException re) {
90 throw new ArgeoException(
91 "Unexepected error while getting property values", re);
92 }
93 return null;
94 }
95
96 private String formatValueAsString(Value value) {
97 // TODO enhance this method
98 try {
99 String strValue;
100
101 if (value.getType() == PropertyType.BINARY)
102 strValue = "<binary>";
103 else if (value.getType() == PropertyType.DATE)
104 strValue = timeFormatter.format(value.getDate().getTime());
105 else
106 strValue = value.getString();
107 return strValue;
108 } catch (RepositoryException e) {
109 throw new ArgeoException("unexpected error while formatting value",
110 e);
111 }
112 }
113 }