View Javadoc
1   package org.argeo.eclipse.ui.fs;
2   
3   import java.nio.file.Path;
4   import java.util.List;
5   
6   import org.argeo.eclipse.ui.ColumnDefinition;
7   import org.eclipse.jface.viewers.CellLabelProvider;
8   import org.eclipse.jface.viewers.ILazyContentProvider;
9   import org.eclipse.jface.viewers.TableViewer;
10  import org.eclipse.jface.viewers.TableViewerColumn;
11  import org.eclipse.jface.viewers.Viewer;
12  import org.eclipse.swt.SWT;
13  import org.eclipse.swt.layout.GridData;
14  import org.eclipse.swt.widgets.Composite;
15  import org.eclipse.swt.widgets.Table;
16  import org.eclipse.swt.widgets.TableColumn;
17  
18  /**
19   * Canonical implementation of a JFace table viewer to display the content of a
20   * file folder
21   */
22  public class FsTableViewer extends TableViewer {
23  	private static final long serialVersionUID = -5632407542678477234L;
24  
25  	private boolean showHiddenItems = false;
26  	private boolean folderFirst = true;
27  	private boolean reverseOrder = false;
28  	private String orderProperty = FsUiConstants.PROPERTY_NAME;
29  
30  	private Path initialPath = null;
31  
32  	public FsTableViewer(Composite parent, int style) {
33  		super(parent, style | SWT.VIRTUAL);
34  	}
35  
36  	public Table configureDefaultSingleColumnTable(int tableWidthHint) {
37  
38  		return configureDefaultSingleColumnTable(tableWidthHint, new FileIconNameLabelProvider());
39  	}
40  
41  	public Table configureDefaultSingleColumnTable(int tableWidthHint, CellLabelProvider labelProvider) {
42  		Table table = this.getTable();
43  		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
44  		table.setLinesVisible(false);
45  		table.setHeaderVisible(false);
46  		// CmsUtils.markup(table);
47  		// CmsUtils.style(table, MaintenanceStyles.BROWSER_COLUMN);
48  
49  		TableViewerColumn column = new TableViewerColumn(this, SWT.NONE);
50  		TableColumn tcol = column.getColumn();
51  		tcol.setWidth(tableWidthHint);
52  		column.setLabelProvider(labelProvider);
53  		this.setContentProvider(new MyLazyCP());
54  		return table;
55  	}
56  
57  	public Table configureDefaultTable(List<ColumnDefinition> columns) {
58  		this.setContentProvider(new MyLazyCP());
59  		Table table = this.getTable();
60  		table.setLinesVisible(true);
61  		table.setHeaderVisible(true);
62  		// CmsUtils.markup(table);
63  		// CmsUtils.style(table, MaintenanceStyles.BROWSER_COLUMN);
64  		for (ColumnDefinition colDef : columns) {
65  			TableViewerColumn column = new TableViewerColumn(this, SWT.NONE);
66  			column.setLabelProvider(colDef.getLabelProvider());
67  			TableColumn tcol = column.getColumn();
68  			tcol.setResizable(true);
69  			tcol.setText(colDef.getLabel());
70  			tcol.setWidth(colDef.getMinWidth());
71  		}
72  		return table;
73  	}
74  
75  	public void setInput(Path dir, String filter) {
76  		Path[] rows = FsUiUtils.getChildren(dir, filter, showHiddenItems, folderFirst, orderProperty, reverseOrder);
77  		if (rows == null) {
78  			this.setInput(null);
79  			this.setItemCount(0);
80  			return;
81  		}
82  		boolean isRoot;
83  		try {
84  			isRoot = dir.getRoot().equals(dir);
85  		} catch (Exception e) {
86  			// FIXME Workaround for JCR root node access
87  			isRoot = dir.toString().equals("/");
88  		}
89  		final Object[] res;
90  		if (isRoot)
91  			res = rows;
92  		else if (initialPath != null && initialPath.equals(dir))
93  			res = rows;
94  		else {
95  			res = new Object[rows.length + 1];
96  			res[0] = new ParentDir(dir.getParent());
97  			for (int i = 1; i < res.length; i++) {
98  				res[i] = rows[i - 1];
99  			}
100 		}
101 		this.setInput(res);
102 		int length = res.length;
103 		this.setItemCount(length);
104 		this.refresh();
105 	}
106 
107 	/** Directly displays bookmarks **/
108 	public void setPathsInput(Path... paths) {
109 		this.setInput((Object[]) paths);
110 		this.setItemCount(paths.length);
111 		this.refresh();
112 	}
113 
114 	/**
115 	 * A path which is to be considered as root (and thus provide no link to a
116 	 * parent directory)
117 	 */
118 	public void setInitialPath(Path initialPath) {
119 		this.initialPath = initialPath;
120 	}
121 
122 	private class MyLazyCP implements ILazyContentProvider {
123 		private static final long serialVersionUID = 9096550041395433128L;
124 		private Object[] elements;
125 
126 		public void dispose() {
127 		}
128 
129 		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
130 			// IMPORTANT: don't forget this: an exception will be thrown if
131 			// a selected object is not part of the results anymore.
132 			viewer.setSelection(null);
133 			this.elements = (Object[]) newInput;
134 		}
135 
136 		public void updateElement(int index) {
137 			if (index < elements.length)
138 				FsTableViewer.this.replace(elements[index], index);
139 		}
140 	}
141 }