|
| 1 | +package edu.rice.cs.hpctraceviewer.ui.base; |
| 2 | + |
| 3 | +import org.eclipse.jface.preference.PreferenceStore; |
| 4 | +import org.eclipse.jface.util.IPropertyChangeListener; |
| 5 | +import org.eclipse.jface.util.PropertyChangeEvent; |
| 6 | +import org.eclipse.jface.viewers.ColumnViewerToolTipSupport; |
| 7 | +import org.eclipse.jface.viewers.TableViewer; |
| 8 | +import org.eclipse.swt.SWT; |
| 9 | +import org.eclipse.swt.events.DisposeEvent; |
| 10 | +import org.eclipse.swt.events.DisposeListener; |
| 11 | +import org.eclipse.swt.graphics.GC; |
| 12 | +import org.eclipse.swt.graphics.Point; |
| 13 | +import org.eclipse.swt.widgets.Composite; |
| 14 | +import org.eclipse.swt.widgets.Event; |
| 15 | +import org.eclipse.swt.widgets.Listener; |
| 16 | +import org.eclipse.swt.widgets.Table; |
| 17 | + |
| 18 | +import edu.rice.cs.hpcsetting.fonts.FontManager; |
| 19 | +import edu.rice.cs.hpcsetting.preferences.PreferenceConstants; |
| 20 | +import edu.rice.cs.hpcsetting.preferences.ViewerPreferenceManager; |
| 21 | + |
| 22 | +public abstract class AbstractBaseTableViewer |
| 23 | + extends TableViewer |
| 24 | + implements Listener, DisposeListener, IPropertyChangeListener |
| 25 | +{ |
| 26 | + |
| 27 | + private int tableRowHeight; |
| 28 | + |
| 29 | + public AbstractBaseTableViewer(Composite parent, int style) { |
| 30 | + super(parent, SWT.SINGLE | SWT.READ_ONLY | SWT.FULL_SELECTION | style); |
| 31 | + computeCellBounds(); |
| 32 | + |
| 33 | + PreferenceStore pref = ViewerPreferenceManager.INSTANCE.getPreferenceStore(); |
| 34 | + pref.addPropertyChangeListener(this); |
| 35 | + |
| 36 | + final Table table = getTable(); |
| 37 | + table.setLinesVisible(true); |
| 38 | + |
| 39 | + table.addDisposeListener(this); |
| 40 | + table.addListener(SWT.MeasureItem, this); |
| 41 | + |
| 42 | + ColumnViewerToolTipSupport.enableFor(this); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void widgetDisposed(DisposeEvent e) { |
| 47 | + getTable().removeDisposeListener(this); |
| 48 | + getTable().removeListener(SWT.MeasureItem, this); |
| 49 | + |
| 50 | + PreferenceStore pref = ViewerPreferenceManager.INSTANCE.getPreferenceStore(); |
| 51 | + pref.removePropertyChangeListener(this); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void handleEvent (Event event) { |
| 56 | + switch(event.type) { |
| 57 | + case SWT.MeasureItem: |
| 58 | + event.height = tableRowHeight; |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void propertyChange(PropertyChangeEvent event) { |
| 65 | + |
| 66 | + final String property = event.getProperty(); |
| 67 | + |
| 68 | + boolean need_to_refresh = (property.equals(PreferenceConstants.ID_FONT_GENERIC) || |
| 69 | + property.equals(PreferenceConstants.ID_FONT_METRIC)); |
| 70 | + |
| 71 | + if (need_to_refresh) { |
| 72 | + computeCellBounds(); |
| 73 | + refresh(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + final String text = "|{[(/`,q"; |
| 78 | + |
| 79 | + private void computeCellBounds() { |
| 80 | + |
| 81 | + GC gc = new GC(getControl()); |
| 82 | + |
| 83 | + gc.setFont(FontManager.getFontGeneric()); |
| 84 | + Point extent1 = gc.stringExtent(text); |
| 85 | + |
| 86 | + // check the height if we use generic font (tree column) |
| 87 | + // if this font is higher, we should use this height as the standard. |
| 88 | + |
| 89 | + gc.setFont(FontManager.getMetricFont()); |
| 90 | + Point extent2 = gc.stringExtent(text); |
| 91 | + |
| 92 | + Point extent = new Point(Math.max(extent1.x, extent2.x), |
| 93 | + Math.max(extent1.y, extent2.y)); |
| 94 | + |
| 95 | + extent = computeCellBounds(gc, extent); |
| 96 | + |
| 97 | + tableRowHeight = extent.y; |
| 98 | + |
| 99 | + gc.dispose(); |
| 100 | + } |
| 101 | + |
| 102 | + /**** |
| 103 | + * Called when it's necessary to recompute the cell bound |
| 104 | + * @param gc the graphic context. Implementer shouldn't dispose the gc. |
| 105 | + * @param Point the suggested bound |
| 106 | + * |
| 107 | + * @return Point final bound |
| 108 | + */ |
| 109 | + abstract protected Point computeCellBounds(GC gc, Point extent); |
| 110 | +} |
0 commit comments