From 02b602398a4145a02ce2ff6b41d3cd13eae789d3 Mon Sep 17 00:00:00 2001 From: Bruno Sinou Date: Fri, 25 Oct 2013 12:29:40 +0000 Subject: [PATCH] Factorises utility classes to ease Table and Tree display of data from a JCR repository. https://www.argeo.org/bugzilla/show_bug.cgi?id=182 git-svn-id: https://svn.argeo.org/commons/branches/1.x@6572 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../ui/jcr/lists/ColumnDefinition.java | 88 +++++++++++++++++ .../eclipse/ui/jcr/lists/IListProvider.java | 20 ++++ .../NodeViewerComparator.java | 12 ++- .../{utils => lists}/RowViewerComparator.java | 2 +- .../jcr/lists/SimpleJcrNodeLabelProvider.java | 96 +++++++++++++++++++ .../jcr/lists/SimpleJcrRowLabelProvider.java | 45 +++++++++ 6 files changed, 260 insertions(+), 3 deletions(-) create mode 100644 base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/ColumnDefinition.java create mode 100644 base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/IListProvider.java rename base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/{utils => lists}/NodeViewerComparator.java (91%) rename base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/{utils => lists}/RowViewerComparator.java (97%) create mode 100644 base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/SimpleJcrNodeLabelProvider.java create mode 100644 base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/SimpleJcrRowLabelProvider.java diff --git a/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/ColumnDefinition.java b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/ColumnDefinition.java new file mode 100644 index 000000000..9e338276a --- /dev/null +++ b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/ColumnDefinition.java @@ -0,0 +1,88 @@ +package org.argeo.eclipse.ui.jcr.lists; + +/** + * Utility object to manage column in various tables and extracts displaying + * data from JCR + */ +public class ColumnDefinition { + private final static int DEFAULT_COLUMN_SIZE = 120; + + private String selectorName; + private String propertyName; + private String headerLabel; + private int propertyType; + private int columnSize = DEFAULT_COLUMN_SIZE; + + /** + * new column using default width + * + * @param selectorName + * @param propertyName + * @param propertyType + * @param headerLabel + */ + public ColumnDefinition(String selectorName, String propertyName, + int propertyType, String headerLabel) { + this.selectorName = selectorName; + this.propertyName = propertyName; + this.propertyType = propertyType; + this.headerLabel = headerLabel; + } + + /** + * + * @param selectorName + * @param propertyName + * @param propertyType + * @param headerLabel + * @param columnSize + */ + public ColumnDefinition(String selectorName, String propertyName, + int propertyType, String headerLabel, int columnSize) { + this.selectorName = selectorName; + this.propertyName = propertyName; + this.propertyType = propertyType; + this.headerLabel = headerLabel; + this.columnSize = columnSize; + } + + public String getSelectorName() { + return selectorName; + } + + public void setSelectorName(String selectorName) { + this.selectorName = selectorName; + } + + public String getPropertyName() { + return propertyName; + } + + public void setPropertyName(String propertyName) { + this.propertyName = propertyName; + } + + public String getHeaderLabel() { + return headerLabel; + } + + public void setHeaderLabel(String headerLabel) { + this.headerLabel = headerLabel; + } + + public int getPropertyType() { + return propertyType; + } + + public void setPropertyType(int propertyType) { + this.propertyType = propertyType; + } + + public int getColumnSize() { + return columnSize; + } + + public void setColumnSize(int columnSize) { + this.columnSize = columnSize; + } +} diff --git a/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/IListProvider.java b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/IListProvider.java new file mode 100644 index 000000000..622e2e259 --- /dev/null +++ b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/IListProvider.java @@ -0,0 +1,20 @@ +package org.argeo.eclipse.ui.jcr.lists; + +import java.util.List; + +/** + * Views and editors can implement this interface so that one of the row list + * that is displayed in the part (For instance in a Table or a Tree Viewer) can + * be rebuilt externally. typically to generate csv or calc extract. + */ +public interface IListProvider { + /** + * Returns an array of current and relevant elements + */ + public Object[] getElements(String extractId); + + /** + * Returns the column definition for passed ID + */ + public List getColumnDefinition(String extractId); +} diff --git a/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/NodeViewerComparator.java b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/NodeViewerComparator.java similarity index 91% rename from base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/NodeViewerComparator.java rename to base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/NodeViewerComparator.java index 83b49c628..11f12e6f5 100644 --- a/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/NodeViewerComparator.java +++ b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/NodeViewerComparator.java @@ -1,4 +1,4 @@ -package org.argeo.eclipse.ui.jcr.utils; +package org.argeo.eclipse.ui.jcr.lists; import java.math.BigDecimal; import java.util.Calendar; @@ -15,7 +15,15 @@ import org.eclipse.jface.viewers.ViewerComparator; /** * Base comparator to enable ordering on Table or Tree viewer that display Jcr - * Nodes + * Nodes. + * + * Note that the following snippet must be added before setting the comparator + * to the corresponding control: + * // IMPORTANT: initialize comparator before setting it + * ColumnDefinition firstCol = colDefs.get(0); + * comparator.setColumn(firstCol.getPropertyType(), + * firstCol.getPropertyName()); + * viewer.setComparator(comparator); */ public class NodeViewerComparator extends ViewerComparator { diff --git a/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/RowViewerComparator.java b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/RowViewerComparator.java similarity index 97% rename from base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/RowViewerComparator.java rename to base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/RowViewerComparator.java index 8ec09ad79..509f72324 100644 --- a/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/utils/RowViewerComparator.java +++ b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/RowViewerComparator.java @@ -1,4 +1,4 @@ -package org.argeo.eclipse.ui.jcr.utils; +package org.argeo.eclipse.ui.jcr.lists; import javax.jcr.Node; import javax.jcr.RepositoryException; diff --git a/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/SimpleJcrNodeLabelProvider.java b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/SimpleJcrNodeLabelProvider.java new file mode 100644 index 000000000..83b081b14 --- /dev/null +++ b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/SimpleJcrNodeLabelProvider.java @@ -0,0 +1,96 @@ +package org.argeo.eclipse.ui.jcr.lists; + +import java.text.DateFormat; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.text.SimpleDateFormat; + +import javax.jcr.Node; +import javax.jcr.PropertyType; +import javax.jcr.RepositoryException; +import javax.jcr.Value; + +import org.argeo.ArgeoException; +import org.eclipse.jface.viewers.ColumnLabelProvider; + +/** + * Base implementation of a label provider for widgets that display JCR Rows. + */ +public class SimpleJcrNodeLabelProvider extends ColumnLabelProvider { + + private final static String DEFAULT_DATE_FORMAT = "EEE, dd MMM yyyy"; + private final static String DEFAULT_NUMBER_FORMAT = "#,##0.0"; + + private DateFormat dateFormat; + private NumberFormat numberFormat; + + final private String propertyName; + + /** + * Default Label provider for a given property of a node. Using default + * pattern for date and number formating + */ + public SimpleJcrNodeLabelProvider(String propertyName) { + this.propertyName = propertyName; + dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); + numberFormat = DecimalFormat.getInstance(); + ((DecimalFormat) numberFormat).applyPattern(DEFAULT_NUMBER_FORMAT); + } + + /** + * Label provider for a given property of a node optionally precising date + * and/or number format patterns + */ + public SimpleJcrNodeLabelProvider(String propertyName, + String dateFormatPattern, String numberFormatPattern) { + this.propertyName = propertyName; + dateFormat = new SimpleDateFormat( + dateFormatPattern == null ? DEFAULT_DATE_FORMAT + : dateFormatPattern); + numberFormat = DecimalFormat.getInstance(); + ((DecimalFormat) numberFormat) + .applyPattern(numberFormatPattern == null ? DEFAULT_NUMBER_FORMAT + : numberFormatPattern); + } + + @Override + public String getText(Object element) { + try { + Node currNode = (Node) element; + + Value value = null; + if (currNode.hasProperty(propertyName)) + value = currNode.getProperty(propertyName).getValue(); + else + return ""; + + switch (value.getType()) { + case PropertyType.STRING: + return value.getString(); + case PropertyType.BOOLEAN: + return "" + value.getBoolean(); + case PropertyType.DATE: + return dateFormat.format(value.getDate().getTime()); + case PropertyType.LONG: + return "" + value.getLong(); + case PropertyType.DECIMAL: + return numberFormat.format(value.getDecimal()); + case PropertyType.DOUBLE: + return numberFormat.format(value.getDouble()); + default: + throw new ArgeoException("Unimplemented label provider " + + "for property type " + value.getType()); + } + } catch (RepositoryException re) { + throw new ArgeoException("Unable to get text from row", re); + } + } + + public void setDateFormat(String dateFormatPattern) { + dateFormat = new SimpleDateFormat(dateFormatPattern); + } + + public void setNumberFormat(String numberFormatPattern) { + ((DecimalFormat) numberFormat).applyPattern(numberFormatPattern); + } +} \ No newline at end of file diff --git a/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/SimpleJcrRowLabelProvider.java b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/SimpleJcrRowLabelProvider.java new file mode 100644 index 000000000..c44aa45cf --- /dev/null +++ b/base/runtime/org.argeo.eclipse.ui.jcr/src/main/java/org/argeo/eclipse/ui/jcr/lists/SimpleJcrRowLabelProvider.java @@ -0,0 +1,45 @@ +package org.argeo.eclipse.ui.jcr.lists; + +import javax.jcr.Node; +import javax.jcr.RepositoryException; +import javax.jcr.query.Row; + +import org.argeo.ArgeoException; + +/** + * Base implementation of a label provider for widgets that display JCR Rows. + */ +public class SimpleJcrRowLabelProvider extends SimpleJcrNodeLabelProvider { + + final private String selectorName; + + /** + * Default Label provider for a given property of a row. Using default + * pattern for date and number formating + */ + public SimpleJcrRowLabelProvider(String selectorName, String propertyName) { + super(propertyName); + this.selectorName = selectorName; + } + + /** + * Label provider for a given property of a node optionally precising date + * and/or number format patterns + */ + public SimpleJcrRowLabelProvider(String selectorName, String propertyName, + String dateFormatPattern, String numberFormatPattern) { + super(propertyName, dateFormatPattern, numberFormatPattern); + this.selectorName = selectorName; + } + + @Override + public String getText(Object element) { + try { + Row currRow = (Row) element; + Node currNode = currRow.getNode(selectorName); + return super.getText(currNode); + } catch (RepositoryException re) { + throw new ArgeoException("Unable to get text from row", re); + } + } +} \ No newline at end of file -- 2.30.2