Skip to content
Snippets Groups Projects
Commit 66412ffa authored by Simon Barner's avatar Simon Barner
Browse files

Support line breaks in string contents.

refs 2450
parent 570b1c0a
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@ package org.fortiss.tooling.base.ui.annotation.view.generic;
import static org.fortiss.tooling.base.ui.annotation.editingsupport.EditingSupportFactory.createEditingSupport;
import static org.fortiss.tooling.base.ui.annotation.labelprovider.LabelProviderFactory.createLabelProvider;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
......@@ -36,12 +37,16 @@ import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.window.ToolTip;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.fortiss.tooling.base.annotation.AnnotationEntry;
import org.fortiss.tooling.base.annotation.IAnnotationValueService;
import org.fortiss.tooling.base.annotation.valueprovider.IAnnotationValueProvider;
......@@ -69,7 +74,7 @@ import org.fortiss.tooling.base.ui.annotation.view.generic.filter.AnnotationFilt
* @author eder, diewald, barner
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: BF52B9E58D2D44E769E05ADCC62499CD
* @ConQAT.Rating YELLOW Hash: BBED92F55537563D7EF594EF27F36870
*/
public class GenericAnnotationView extends AnnotationViewPartBase {
/** Root composite of {@link GenericAnnotationView}. */
......@@ -101,6 +106,9 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
/** Default width of columns. */
static final int COLUMN_DEFAULT_WIDTH = 125;
/** Default height of rows. */
private int defaultRowHeight;
/**
* Sets a fixed {@link AnnotationFilter}.
* <ul>
......@@ -233,6 +241,11 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
}
tableViewer.getTable().setRedraw(true);
// After contents change, restore row height. It will be adjusted to the new content
// using the TableViewer's SWT.{MeasureItem, PaintItem, EraseItem} listeners installed
// in addRowHeightListener()
setTableItemHeight(tableViewer.getTable(), defaultRowHeight);
}
// Update the view
......@@ -258,6 +271,64 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
annotationFilterWidget.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
}
/**
* Sets the item height of the given {@link Table} to the specified value.
*
* <b>The underlying method {@code Table#setItemHeight} is not exposed. This wrapper method
* should only called during a full {@link #update(Collection)} of the table's contents.</b>>
*/
private void setTableItemHeight(Table table, int height) {
try {
Method method = table.getClass().getDeclaredMethod("setItemHeight", int.class);
if(method != null) {
boolean accessible = method.isAccessible();
method.setAccessible(true);
method.invoke(table, height);
method.setAccessible(accessible);
}
} catch(Exception e) {
// ignore
}
}
/**
* Installs a listener to the given {@link Table} that adjust the row height to to the current
* contents.
*/
private void addRowHeightListener(Table table) {
Listener rowHeightListener = new Listener() {
@Override
public void handleEvent(Event event) {
switch(event.type) {
case SWT.MeasureItem: {
TableItem item = (TableItem)event.item;
String text = item.getText(event.index);
Point size = event.gc.textExtent(text);
event.width = size.x;
event.height = Math.max(event.height, size.y);
break;
}
case SWT.PaintItem: {
TableItem item = (TableItem)event.item;
String text = item.getText(event.index);
Point size = event.gc.textExtent(text);
int offset =
event.index == 0 ? Math.max(0, (event.height - size.y) / 2) : 0;
event.gc.drawText(text, event.x, event.y + offset, true);
break;
}
case SWT.EraseItem: {
event.detail &= ~SWT.FOREGROUND;
break;
}
}
}
};
table.addListener(SWT.MeasureItem, rowHeightListener);
table.addListener(SWT.PaintItem, rowHeightListener);
table.addListener(SWT.EraseItem, rowHeightListener);
}
/** {@inheritDoc} */
@Override
public void createPartControl(Composite parent) {
......@@ -271,6 +342,13 @@ public class GenericAnnotationView extends AnnotationViewPartBase {
table.setLinesVisible(true);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
// Listener to adjust row height to contents
addRowHeightListener(table);
// Preserve default row-height (used to restore it during contents updates in
// update(Collection<AnnotationEntry>)).
defaultRowHeight = table.getItemHeight();
ColumnViewerToolTipSupport.enableFor(tableViewer, ToolTip.NO_RECREATE);
// Install layout for Table in order to ensure that it claims all available vertical space
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment