]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/providers/PropertyLabelProvider.java
Imporve JCR keyring and remote repository exploring
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / providers / PropertyLabelProvider.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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.jcr.ui.explorer.providers;
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.jcr.JcrUtils;
28 import org.argeo.jcr.ui.explorer.JcrExplorerConstants;
29 import org.eclipse.jface.viewers.ColumnLabelProvider;
30 import org.eclipse.jface.viewers.ViewerCell;
31
32 public class PropertyLabelProvider extends ColumnLabelProvider implements
33 JcrExplorerConstants {
34
35 // To be able to change column order easily
36 public static final int COLUMN_PROPERTY = 0;
37 public static final int COLUMN_VALUE = 1;
38 public static final int COLUMN_ATTRIBUTES = 2;
39
40 // Utils
41 protected DateFormat timeFormatter = new SimpleDateFormat(DATE_TIME_FORMAT);
42
43 public void update(ViewerCell cell) {
44 Object element = cell.getElement();
45 cell.setText(getColumnText(element, cell.getColumnIndex()));
46 // Image image = getImage(element);
47 // cell.setImage(image);
48 // cell.setBackground(getBackground(element));
49 // cell.setForeground(getForeground(element));
50 // cell.setFont(getFont(element));
51 }
52
53 public String getColumnText(Object element, int columnIndex) {
54 try {
55 if (element instanceof Property) {
56 Property prop = (Property) element;
57 if (prop.isMultiple()) {
58 switch (columnIndex) {
59 case COLUMN_PROPERTY:
60 return prop.getName();
61 case COLUMN_VALUE:
62 // Corresponding values are listed on children
63 return "";
64 case COLUMN_ATTRIBUTES:
65 return JcrUtils.getPropertyDefinitionAsString(prop);
66 }
67 } else {
68 switch (columnIndex) {
69 case COLUMN_PROPERTY:
70 return prop.getName();
71 case COLUMN_VALUE:
72 return formatValueAsString(prop.getValue());
73 case COLUMN_ATTRIBUTES:
74 return JcrUtils.getPropertyDefinitionAsString(prop);
75 }
76 }
77 } else if (element instanceof Value) {
78 Value val = (Value) element;
79
80 switch (columnIndex) {
81 case COLUMN_PROPERTY:
82 // Nothing to show
83 return "";
84 case COLUMN_VALUE:
85 return formatValueAsString(val);
86 case COLUMN_ATTRIBUTES:
87 // Corresponding attributes are listed on the parent
88 return "";
89 }
90 }
91
92 } catch (RepositoryException re) {
93 throw new ArgeoException(
94 "Unexepected error while getting property values", re);
95 }
96 return null;
97 }
98
99 private String formatValueAsString(Value value) {
100 // TODO enhance this method
101 try {
102 String strValue;
103
104 if (value.getType() == PropertyType.BINARY)
105 strValue = "<binary>";
106 else if (value.getType() == PropertyType.DATE)
107 strValue = timeFormatter.format(value.getDate().getTime());
108 else
109 strValue = value.getString();
110 return strValue;
111 } catch (RepositoryException e) {
112 throw new ArgeoException("unexpected error while formatting value",
113 e);
114 }
115 }
116 }