X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Forg.argeo.cms.swt%2Fsrc%2Forg%2Fargeo%2Feclipse%2Fui%2Ffs%2FFsTableViewer.java;fp=eclipse%2Forg.argeo.cms.swt%2Fsrc%2Forg%2Fargeo%2Feclipse%2Fui%2Ffs%2FFsTableViewer.java;h=3b126e90bcdcc33dcc1b380e22f228f76a31ceb4;hb=8282011b0e20e80704b209ad55fa9fb132e16280;hp=0000000000000000000000000000000000000000;hpb=633a8acd189cc22f06944d278879601189be1bc8;p=lgpl%2Fargeo-commons.git diff --git a/eclipse/org.argeo.cms.swt/src/org/argeo/eclipse/ui/fs/FsTableViewer.java b/eclipse/org.argeo.cms.swt/src/org/argeo/eclipse/ui/fs/FsTableViewer.java new file mode 100644 index 000000000..3b126e90b --- /dev/null +++ b/eclipse/org.argeo.cms.swt/src/org/argeo/eclipse/ui/fs/FsTableViewer.java @@ -0,0 +1,141 @@ +package org.argeo.eclipse.ui.fs; + +import java.nio.file.Path; +import java.util.List; + +import org.argeo.eclipse.ui.ColumnDefinition; +import org.eclipse.jface.viewers.CellLabelProvider; +import org.eclipse.jface.viewers.ILazyContentProvider; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.TableViewerColumn; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableColumn; + +/** + * Canonical implementation of a JFace table viewer to display the content of a + * file folder + */ +public class FsTableViewer extends TableViewer { + private static final long serialVersionUID = -5632407542678477234L; + + private boolean showHiddenItems = false; + private boolean folderFirst = true; + private boolean reverseOrder = false; + private String orderProperty = FsUiConstants.PROPERTY_NAME; + + private Path initialPath = null; + + public FsTableViewer(Composite parent, int style) { + super(parent, style | SWT.VIRTUAL); + } + + public Table configureDefaultSingleColumnTable(int tableWidthHint) { + + return configureDefaultSingleColumnTable(tableWidthHint, new FileIconNameLabelProvider()); + } + + public Table configureDefaultSingleColumnTable(int tableWidthHint, CellLabelProvider labelProvider) { + Table table = this.getTable(); + table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); + table.setLinesVisible(false); + table.setHeaderVisible(false); + // CmsUtils.markup(table); + // CmsUtils.style(table, MaintenanceStyles.BROWSER_COLUMN); + + TableViewerColumn column = new TableViewerColumn(this, SWT.NONE); + TableColumn tcol = column.getColumn(); + tcol.setWidth(tableWidthHint); + column.setLabelProvider(labelProvider); + this.setContentProvider(new MyLazyCP()); + return table; + } + + public Table configureDefaultTable(List columns) { + this.setContentProvider(new MyLazyCP()); + Table table = this.getTable(); + table.setLinesVisible(true); + table.setHeaderVisible(true); + // CmsUtils.markup(table); + // CmsUtils.style(table, MaintenanceStyles.BROWSER_COLUMN); + for (ColumnDefinition colDef : columns) { + TableViewerColumn column = new TableViewerColumn(this, SWT.NONE); + column.setLabelProvider(colDef.getLabelProvider()); + TableColumn tcol = column.getColumn(); + tcol.setResizable(true); + tcol.setText(colDef.getLabel()); + tcol.setWidth(colDef.getMinWidth()); + } + return table; + } + + public void setInput(Path dir, String filter) { + Path[] rows = FsUiUtils.getChildren(dir, filter, showHiddenItems, folderFirst, orderProperty, reverseOrder); + if (rows == null) { + this.setInput(null); + this.setItemCount(0); + return; + } + boolean isRoot; + try { + isRoot = dir.getRoot().equals(dir); + } catch (Exception e) { + // FIXME Workaround for JCR root node access + isRoot = dir.toString().equals("/"); + } + final Object[] res; + if (isRoot) + res = rows; + else if (initialPath != null && initialPath.equals(dir)) + res = rows; + else { + res = new Object[rows.length + 1]; + res[0] = new ParentDir(dir.getParent()); + for (int i = 1; i < res.length; i++) { + res[i] = rows[i - 1]; + } + } + this.setInput(res); + int length = res.length; + this.setItemCount(length); + this.refresh(); + } + + /** Directly displays bookmarks **/ + public void setPathsInput(Path... paths) { + this.setInput((Object[]) paths); + this.setItemCount(paths.length); + this.refresh(); + } + + /** + * A path which is to be considered as root (and thus provide no link to a + * parent directory) + */ + public void setInitialPath(Path initialPath) { + this.initialPath = initialPath; + } + + private class MyLazyCP implements ILazyContentProvider { + private static final long serialVersionUID = 9096550041395433128L; + private Object[] elements; + + public void dispose() { + } + + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + // IMPORTANT: don't forget this: an exception will be thrown if + // a selected object is not part of the results anymore. + viewer.setSelection(null); + this.elements = (Object[]) newInput; + } + + public void updateElement(int index) { + if (index < elements.length) + FsTableViewer.this.replace(elements[index], index); + } + } +}