View Javadoc
1   package org.argeo.tracker.e4.parts;
2   
3   import java.util.List;
4   
5   import org.argeo.cms.ui.eclipse.forms.FormToolkit;
6   import org.argeo.cms.ui.eclipse.forms.IManagedForm;
7   import org.argeo.eclipse.ui.ColumnDefinition;
8   import org.argeo.eclipse.ui.EclipseUiUtils;
9   import org.eclipse.jface.action.ToolBarManager;
10  import org.eclipse.jface.layout.TableColumnLayout;
11  import org.eclipse.jface.viewers.ColumnLabelProvider;
12  import org.eclipse.jface.viewers.ColumnWeightData;
13  import org.eclipse.jface.viewers.IStructuredContentProvider;
14  import org.eclipse.jface.viewers.TableViewer;
15  import org.eclipse.jface.viewers.TableViewerColumn;
16  import org.eclipse.jface.viewers.Viewer;
17  import org.eclipse.swt.SWT;
18  import org.eclipse.swt.events.DisposeEvent;
19  import org.eclipse.swt.events.DisposeListener;
20  import org.eclipse.swt.graphics.Cursor;
21  import org.eclipse.swt.layout.GridData;
22  import org.eclipse.swt.layout.GridLayout;
23  import org.eclipse.swt.widgets.Composite;
24  import org.eclipse.swt.widgets.Label;
25  import org.eclipse.swt.widgets.Table;
26  import org.eclipse.swt.widgets.TableColumn;
27  import org.eclipse.swt.widgets.Text;
28  import org.eclipse.swt.widgets.ToolBar;
29  
30  /** Centralise useful methods to ease implementation of the UI */
31  class TrackerUiUtils {
32  
33  	public static TableViewer createTableViewer(final Composite parent, int tableStyle,
34  			List<ColumnDefinition> columnDefs) {
35  
36  		int style = tableStyle | SWT.H_SCROLL | SWT.V_SCROLL;
37  		Table table = new Table(parent, style);
38  		TableColumnLayout layout = new TableColumnLayout();
39  
40  		// TODO the table layout does not works with the scrolled form
41  		parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
42  		table.setLayoutData(EclipseUiUtils.fillAll());
43  
44  		TableViewer viewer = new TableViewer(table);
45  		table.setLinesVisible(true);
46  		table.setHeaderVisible(true);
47  
48  		for (ColumnDefinition colDef : columnDefs)
49  			createTableColumn(viewer, layout, colDef);
50  
51  		viewer.setContentProvider(new IStructuredContentProvider() {
52  			private static final long serialVersionUID = -3133493667354601923L;
53  
54  			@Override
55  			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
56  				// This must be called after the inputChanged method has
57  				// returned
58  				// viewer.refresh();
59  			}
60  
61  			@Override
62  			public void dispose() {
63  			}
64  
65  			@Override
66  			public Object[] getElements(Object inputElement) {
67  				return (Object[]) inputElement;
68  			}
69  		});
70  		return viewer;
71  	}
72  
73  	/** Default creation of a column for a survey answer table */
74  	public static TableViewerColumn createTableColumn(TableViewer tableViewer, TableColumnLayout layout,
75  			ColumnDefinition columnDef) {
76  
77  		boolean resizable = true;
78  		TableViewerColumn tvc = new TableViewerColumn(tableViewer, SWT.NONE);
79  		TableColumn column = tvc.getColumn();
80  
81  		column.setText(columnDef.getLabel());
82  		column.setWidth(columnDef.getMinWidth());
83  		column.setResizable(resizable);
84  
85  		ColumnLabelProvider lp = columnDef.getLabelProvider();
86  		tvc.setLabelProvider(lp);
87  		layout.setColumnData(column, new ColumnWeightData(columnDef.getWeight(), columnDef.getMinWidth(), resizable));
88  		return tvc;
89  	}
90  
91  	@Deprecated
92  	public static Label createFormBoldLabel(FormToolkit toolkit, Composite parent, String value) {
93  		// We add a blank space before to workaround the cropping of the word
94  		// first letter in some OS/Browsers (typically MAC/Firefox 31 )
95  		Label label = new Label(parent, SWT.END);
96  		label.setText(" " + value);
97  		label.setFont(EclipseUiUtils.getBoldFont(parent));
98  		label.setLayoutData(new GridData(SWT.END, SWT.CENTER));
99  		return label;
100 	}
101 
102 	/** Appends a section with a title in a table wrap layout */
103 	public static Section addFormSection(FormToolkit tk, Composite parent, String title) {
104 		// Section section = new Section(parent, Section.TITLE_BAR);
105 		// CmsUtils.style(section, "custom");
106 		// Section section = tk.createSection(parent,SWT.NONE); // SWT.NONE
107 		Section section = new Section(parent, SWT.NONE);
108 		section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
109 		section.setText(title);
110 		Composite body = tk.createComposite(section, SWT.NO_FOCUS);
111 		body.setLayout(new GridLayout());
112 		section.setClient(body);
113 		return section;
114 	}
115 
116 	/** Appends an expendable section with a title */
117 	public static Section addFormSection(final IManagedForm form, FormToolkit tk, Composite parent, String title,
118 			boolean isExpended) {
119 		// int style = Section.TITLE_BAR | Section.TWISTIE;
120 		// if (isExpended)
121 		// style |= Section.EXPANDED;
122 		// Section section = tk.createSection(parent, style);
123 		// section.addExpansionListener(new ExpansionAdapter() {
124 		// public void expansionStateChanged(ExpansionEvent e) {
125 		// form.reflow(true);
126 		// }
127 		// });
128 		Section section = new Section(parent, SWT.NONE);
129 		section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
130 		section.setText(title);
131 		Composite body = tk.createComposite(section, SWT.NO_FOCUS);
132 		body.setLayout(new GridLayout());
133 		section.setClient(body);
134 		return section;
135 	}
136 
137 	public static ToolBarManager addMenu(Composite section) {
138 		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
139 		ToolBar toolbar = toolBarManager.createControl(section);
140 		//section.setTextClient(toolbar);
141 		final Cursor handCursor = new Cursor(section.getDisplay(), SWT.CURSOR_HAND);
142 		toolbar.setCursor(handCursor);
143 		toolbar.addDisposeListener(new DisposeListener() {
144 			private static final long serialVersionUID = 6093858538389954404L;
145 
146 			public void widgetDisposed(DisposeEvent e) {
147 				if ((handCursor != null) && (handCursor.isDisposed() == false)) {
148 					handCursor.dispose();
149 				}
150 			}
151 		});
152 		return toolBarManager;
153 	}
154 
155 	// EASILY CREATE WIDGETS
156 	/** Creates a simple label / value pair with no content for the value */
157 	public static Label createLL(FormToolkit toolkit, Composite body, String label) {
158 		return createLL(toolkit, body, label, "", 1);
159 	}
160 
161 	/** Creates a simple label / value pair. */
162 	public static Label createLL(FormToolkit toolkit, Composite body, String label, String value) {
163 		return createLL(toolkit, body, label, value, 1);
164 	}
165 
166 	/** Creates a simple label / value pair. */
167 	public static Label createLL(FormToolkit toolkit, Composite body, String label, String value, int colSpan) {
168 		Label lbl = toolkit.createLabel(body, label);
169 		lbl.setFont(EclipseUiUtils.getBoldFont(body));
170 		lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
171 
172 		Label valuelbl = toolkit.createLabel(body, value, SWT.WRAP);
173 		valuelbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, colSpan, 1));
174 		return valuelbl;
175 	}
176 
177 	/** Creates label and multiline text. */
178 	public static Text createLMT(FormToolkit toolkit, Composite body, String label, String value) {
179 		Label lbl = toolkit.createLabel(body, label);
180 		lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
181 		Text text = toolkit.createText(body, value, SWT.BORDER | SWT.MULTI);
182 		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
183 		return text;
184 	}
185 
186 	/** Creates label and text. */
187 	public static Text createLT(FormToolkit toolkit, Composite body, String label, String value) {
188 		Label lbl = toolkit.createLabel(body, label);
189 		lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
190 		Text text = toolkit.createText(body, value, SWT.BORDER);
191 		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
192 		return text;
193 	}
194 }