use of net.sourceforge.nattable.layer.ILayer in project translationstudio8 by heartsome.
the class GridSearchStrategy method executeSearch.
public PositionCoordinate executeSearch(Object valueToMatch) {
ILayer contextLayer = getContextLayer();
if (!(contextLayer instanceof SelectionLayer)) {
throw new RuntimeException("For the GridSearchStrategy to work it needs the selectionLayer to be passed as the contextLayer.");
}
SelectionLayer selectionLayer = (SelectionLayer) contextLayer;
PositionCoordinate selectionAnchor = selectionLayer.getSelectionAnchor();
boolean hadSelectionAnchor = true;
if (selectionAnchor.columnPosition < 0 || selectionAnchor.rowPosition < 0) {
selectionAnchor.columnPosition = 0;
selectionAnchor.rowPosition = 0;
hadSelectionAnchor = false;
}
int anchorColumnPosition = selectionAnchor.columnPosition;
int startingRowPosition;
int[] columnsToSearch = null;
final int columnCount = selectionLayer.getColumnCount();
if (searchDirection.equals(ISearchDirection.SEARCH_FORWARD)) {
int rowPosition = hadSelectionAnchor ? selectionAnchor.rowPosition + 1 : selectionAnchor.rowPosition;
if (rowPosition > (contextLayer.getRowCount() - 1)) {
rowPosition = wrapSearch ? 0 : contextLayer.getRowCount() - 1;
}
int rowCount = selectionLayer.getRowCount();
startingRowPosition = rowPosition < rowCount ? rowPosition : 0;
if (selectionAnchor.rowPosition + 1 >= rowCount && anchorColumnPosition + 1 >= columnCount && hadSelectionAnchor) {
if (wrapSearch) {
anchorColumnPosition = 0;
} else {
return null;
}
} else if (selectionAnchor.rowPosition == rowCount - 1 && anchorColumnPosition < columnCount - 1) {
anchorColumnPosition++;
}
columnsToSearch = getColumnsToSearchArray(columnCount, anchorColumnPosition);
} else {
int rowPosition = selectionAnchor.rowPosition - 1;
if (rowPosition < 0) {
rowPosition = wrapSearch ? contextLayer.getRowCount() - 1 : 0;
}
startingRowPosition = rowPosition > 0 ? rowPosition : 0;
if (selectionAnchor.rowPosition - 1 < 0 && anchorColumnPosition - 1 < 0 && hadSelectionAnchor) {
if (wrapSearch) {
anchorColumnPosition = columnCount - 1;
} else {
return null;
}
} else if (selectionAnchor.rowPosition == 0 && anchorColumnPosition > 0) {
anchorColumnPosition--;
}
columnsToSearch = getDescendingColumnsToSearchArray(anchorColumnPosition);
}
PositionCoordinate executeSearch = searchGrid(valueToMatch, contextLayer, selectionLayer, anchorColumnPosition, startingRowPosition, columnsToSearch);
return executeSearch;
}
use of net.sourceforge.nattable.layer.ILayer in project translationstudio8 by heartsome.
the class XLIFFEditorImplWithNatTable method changeLayout.
public void changeLayout(boolean isHorizontalLayout) {
// 同步布局状态
this.isHorizontalLayout = isHorizontalLayout;
// 如果当前的 table 已经存在,销毁后再重新创建。
if (table != null && !table.isDisposed()) {
table.dispose();
for (Control control : parent.getChildren()) {
control.dispose();
}
}
// 构建 NatTable 的数据提供者
bodyDataProvider = setupBodyDataProvider(isHorizontalLayout);
// 构建 NatTable 列头的数据提供者
DefaultColumnHeaderDataProvider colHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabels);
// 构建 NatTable 的 body layer stack
bodyLayer = new BodyLayerStack(bodyDataProvider, isHorizontalLayout);
// 构建 NatTable 的 column header layout stack
ColumnHeaderLayerStack columnHeaderLayer = new ColumnHeaderLayerStack(colHeaderDataProvider);
// 构建 NatTable 之下的 composite layer,不使用默认的 configuration(默认的 configuration 是在点击可编辑单元格时,直接进入编辑状态)。
CompositeLayer compositeLayer = new CompositeLayer(1, 2);
compositeLayer.setChildLayer(GridRegion.COLUMN_HEADER, columnHeaderLayer, 0, 0);
compositeLayer.setChildLayer(GridRegion.BODY, bodyLayer, 0, 1);
// 标识 BodyLayer 在 CompositeLayer 上的位置
LayerUtil.setBodyLayerPosition(0, 1);
/* 给 composite layer 添加编辑相关的命令和 handler */
// 添加行背景色(奇数行和偶数行不同)
addRowBackgroundColor(compositeLayer);
// 构建 NatTable
table = new NatTable(parent, compositeLayer, false);
Language srcLang = LocaleService.getLanguageConfiger().getLanguageByCode(srcColumnName);
Language tgtLang = LocaleService.getLanguageConfiger().getLanguageByCode(tgtColumnName);
if (srcLang.isBidi() || tgtLang.isBidi()) {
table.setOrientation(SWT.RIGHT_TO_LEFT);
}
// 去除默认绘画器
table.removePaintListener(table);
// 使用自定义绘画器,此绘画器,具有自动计算行高功能。
table.addPaintListener(paintListenerWithAutoRowSize);
Listener[] ls = table.getListeners(SWT.Resize);
for (Listener l : ls) {
table.removeListener(SWT.Resize, l);
}
table.addListener(SWT.Resize, resizeListenerWithColumnResize);
table.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
// ActiveCellEditor.commit(); // 关闭编辑时,先提交未提交的单元格,避免造成内容丢失。Bug #2685
HsMultiActiveCellEditor.commit(true);
table.removeListener(SWT.Resize, resizeListenerWithColumnResize);
table.removePaintListener(paintListenerWithAutoRowSize);
}
});
table.setLayoutData(new GridData(GridData.FILL_BOTH));
// 给 NatTable 添加相应的配置
DefaultNatTableStyleConfiguration configuration = new DefaultNatTableStyleConfiguration();
configuration.hAlign = HorizontalAlignmentEnum.LEFT;
table.addConfiguration(configuration);
// To be changed 给NatTable添加选择列的功能 Add By Leakey
/*
* ColumnGroupModel columnGroupModel = new ColumnGroupModel(); DisplayColumnChooserCommandHandler
* columnChooserCommandHandler = new DisplayColumnChooserCommandHandler( bodyLayer.getSelectionLayer(),
* bodyLayer.getColumnHideShowLayer(), columnHeaderLayer.getColumnHeaderLayer(),
* columnHeaderLayer.getColumnHeaderDataLayer(), columnHeaderLayer.getColumnGroupHeaderLayer(), columnGroupModel
* ); bodyLayer.registerCommandHandler(columnChooserCommandHandler);
*/
/*
* 不使用默认的表头菜单,使用自定义的菜单,因此自定义菜单在 corner 中添加了相应的菜单项,所以需要指定这些添加的 command 在哪一层进一处理
*/
// 表头中添加自定义菜单会引发一些不可预料的问题,故先去掉
// table.addConfiguration(new HeaderMenuConfiguration(table));
/*
* 增加表格的自定义右键菜单
*/
table.addConfiguration(new BodyMenuConfiguration(this));
// 注册列头点击监听(处理排序)
table.addConfiguration(new SingleClickSortConfiguration());
/*
* 初始化“撤销/重做”历史
*/
initializeOperationHistory();
table.setData(IUndoContext.class.getName(), undoContext);
/* Weachy - 注册修改后保存内容并自适应大小的处理 handler(覆盖默认的handler:UpdateDataCommandHandler) */
bodyLayer.getBodyDataLayer().registerCommandHandler(new UpdateDataAndAutoResizeCommandHandler(table, bodyLayer.getBodyDataLayer()));
/* Weachy - 注册当前显示行的行高自适应处理 handler */
compositeLayer.registerCommandHandler(new AutoResizeCurrentRowsCommandHandler(compositeLayer));
/* Weachy - 移除系统默认的查找 handler,添加自定义的查找替换 handler */
bodyLayer.getSelectionLayer().unregisterCommandHandler(SearchCommand.class);
bodyLayer.getSelectionLayer().registerCommandHandler(new FindReplaceCommandHandler(bodyLayer.getSelectionLayer()));
/*
* 下面给 NatTable 添加可编辑单元格的配置
*/
table.addConfiguration(editableGridConfiguration());
// 添加标记的自定义显示样式
IConfigRegistry configRegistry = new ConfigRegistry();
/*
* 如果是水平布局,则使用 ColumnOverrideLabelAccumulator 实现指定列都使用相同的显示样式;否则使用 CellOverrideLabelAccumulator 实现根据显示的内容来显示样式。
*/
if (isHorizontalLayout) {
// 第一步:创建一个标签累加器,给需要绘制会不同效果的 cells 添加自定义的标签。在这里是第三列的标签列。
ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(bodyLayer);
columnLabelAccumulator.registerColumnOverrides(0, "LINENUMBER_CELL_LABEL");
columnLabelAccumulator.registerColumnOverrides(1, SOURCE_EDIT_CELL_LABEL);
columnLabelAccumulator.registerColumnOverrides(2, FLAG_CELL_LABEL);
columnLabelAccumulator.registerColumnOverrides(3, TARGET_EDIT_CELL_LABEL);
// 第二步:注册这个标签累加器。
bodyLayer.setConfigLabelAccumulator(columnLabelAccumulator);
// 第三步:把自定义的 cell painter,cell style 与自定义的标签进行关联。
addFlagLableToColumn(configRegistry);
addLineNumberToColumn(configRegistry);
} else {
ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(bodyLayer);
columnLabelAccumulator.registerColumnOverrides(0, "LINENUMBER_CELL_LABEL");
columnLabelAccumulator.registerColumnOverrides(1, FLAG_CELL_LABEL);
columnLabelAccumulator.registerColumnOverrides(VerticalNatTableConfig.TARGET_COL_INDEX, SOURCE_EDIT_CELL_LABEL);
columnLabelAccumulator.registerColumnOverrides(VerticalNatTableConfig.TARGET_COL_INDEX, TARGET_EDIT_CELL_LABEL);
bodyLayer.setConfigLabelAccumulator(columnLabelAccumulator);
// CellOverrideLabelAccumulator<TransUnitDummy> cellLabelAccumulator = new
// CellOverrideLabelAccumulator<TransUnitDummy>(
// (IRowDataProvider) bodyDataProvider);
// CellOverrideLabelAccumulator<TransUnitBean> cellLabelAccumulator = new
// CellOverrideLabelAccumulator<TransUnitBean>(
// (IRowDataProvider) bodyDataProvider);
// cellLabelAccumulator.registerOverride("flag", VerticalNatTableConfig.SOURCE_COL_INDEX,
// FOCUS_CELL_LABEL);
//
// bodyLayer.getBodyDataLayer().setConfigLabelAccumulator(cellLabelAccumulator);
addFlagLableToColumn(configRegistry);
addLineNumberToColumn(configRegistry);
}
table.setConfigRegistry(configRegistry);
// configure manually
table.configure();
/* Weachy - 垂直布局下,注册使键盘方向键以 2 行为一个单位移动选中行的处理 handler(覆盖默认的handler:MoveRowSelectionCommandHandler) */
if (!isHorizontalLayout) {
// SelectionLayer selectionLayer = bodyLayer.getSelectionLayer();
// selectionLayer.registerCommandHandler(new VerticalMoveRowSelectionCommandHandler(selectionLayer));
}
if (bodyLayer.selectionLayer.getRowCount() > 0) {
// 默认选中第一行
HsMultiActiveCellEditor.commit(true);
bodyLayer.selectionLayer.doCommand(new SelectCellCommand(bodyLayer.getSelectionLayer(), getTgtColumnIndex(), isHorizontalLayout ? 0 : 1, false, false));
HsMultiCellEditorControl.activeSourceAndTargetCell(this);
}
IWorkbenchPage page = getSite().getPage();
IViewReference[] viewReferences = page.getViewReferences();
IViewPart view;
for (int i = 0; i < viewReferences.length; i++) {
view = viewReferences[i].getView(false);
if (view == null) {
continue;
}
// 切换到其他视图,再切换回来,解决NatTable改变布局后其他视图无法监听到的问题。
view.setFocus();
// break;
}
// 改变布局方式后,把焦点给 NatTable
table.setFocus();
RowHeightCalculator rowHeightCalculator = new RowHeightCalculator(bodyLayer, table, 32);
ILayer lay = bodyLayer.getViewportLayer();
if (lay instanceof HorizontalViewportLayer) {
((HorizontalViewportLayer) bodyLayer.getViewportLayer()).setRowHeightCalculator(rowHeightCalculator);
} else if (lay instanceof VerticalViewportLayer) {
((VerticalViewportLayer) bodyLayer.getViewportLayer()).setRowHeightCalculator(rowHeightCalculator);
}
parent.layout();
NoteToolTip toolTip = new NoteToolTip(table);
toolTip.setPopupDelay(10);
toolTip.activate();
toolTip.setShift(new Point(10, 10));
StateToolTip stateTip = new StateToolTip(table);
stateTip.setPopupDelay(10);
stateTip.activate();
stateTip.setShift(new Point(10, 10));
NotSendToTmToolTip notSendToTMToolTip = new NotSendToTmToolTip(table);
notSendToTMToolTip.setPopupDelay(10);
notSendToTMToolTip.activate();
notSendToTMToolTip.setShift(new Point(10, 10));
HasQustionToolTip hasqustionTooltip = new HasQustionToolTip(table);
hasqustionTooltip.setPopupDelay(10);
hasqustionTooltip.activate();
hasqustionTooltip.setShift(new Point(10, 10));
// 在状态栏上显示当前文本段的信息。
updateStatusLine();
}
use of net.sourceforge.nattable.layer.ILayer in project translationstudio8 by heartsome.
the class HorizontalViewportLayer method moveRowPositionIntoViewport.
public void moveRowPositionIntoViewport(int scrollableRowPosition, boolean forceEntireCellIntoViewport) {
ILayer underlyingLayer = getUnderlyingLayer();
if (underlyingLayer.getRowIndexByPosition(scrollableRowPosition) >= 0) {
if (scrollableRowPosition >= getMinimumOriginRowPosition()) {
// 滚动条滚动过的行数
int originRowPosition = getOriginRowPosition();
if (scrollableRowPosition < originRowPosition) {
// Move up
setOriginRowPosition(scrollableRowPosition);
} else {
// 当前选中行的起始位置(包括滚动过的区域)
int scrollableRowStartY = underlyingLayer.getStartYOfRowPosition(scrollableRowPosition);
if (rowHeightCalculator != null)
rowHeightCalculator.recaculateRowHeight(scrollableRowPosition);
int currentRowHeight = underlyingLayer.getRowHeightByPosition(scrollableRowPosition);
// 当前选中行的结束位置,包括不可见的部分(包括滚动过的区域)
int scrollableRowEndY = scrollableRowStartY + currentRowHeight;
// 编辑区的高(不包括滚动过的区域)
int clientAreaHeight = getClientAreaHeight();
// 编辑区的高(包括滚动过的区域)
int viewportEndY = underlyingLayer.getStartYOfRowPosition(originRowPosition) + clientAreaHeight;
if (viewportEndY < scrollableRowEndY) {
// 选中行下半部分没有显示完全时
if (currentRowHeight >= clientAreaHeight) {
// 当前行高大于等于编辑区的高
// Move up:设置起始行为当前行
setOriginRowPosition(scrollableRowPosition);
} else {
// int targetOriginRowPosition = underlyingLayer.getRowPositionByY(scrollableRowEndY
// - clientAreaHeight) + 1;
// // Move up:将当前选中行显示完全
// setOriginRowPosition(targetOriginRowPosition);
int ch = clientAreaHeight;
int r = scrollableRowPosition;
for (; r > 0; r--) {
int h = rowHeightCalculator.recaculateRowHeight(r);
ch -= h;
if (ch < 0) {
break;
}
}
setOriginRowPosition(r + 1);
}
}
}
}
}
}
use of net.sourceforge.nattable.layer.ILayer in project translationstudio8 by heartsome.
the class InlineCellEditController method editCellInline.
public static boolean editCellInline(LayerCell cell, Character initialEditValue, Composite parent, IConfigRegistry configRegistry) {
try {
ActiveCellEditor.commit();
final List<String> configLabels = cell.getConfigLabels().getLabels();
Rectangle cellBounds = cell.getBounds();
ILayer layer = cell.getLayer();
int columnPosition = layer.getColumnPositionByX(cellBounds.x);
int columnIndex = layer.getColumnIndexByPosition(columnPosition);
int rowPosition = layer.getRowPositionByY(cellBounds.y);
int rowIndex = layer.getRowIndexByPosition(rowPosition);
boolean editable = configRegistry.getConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, DisplayMode.EDIT, configLabels).isEditable(columnIndex, rowIndex);
if (!editable) {
return false;
}
ICellEditor cellEditor = configRegistry.getConfigAttribute(EditConfigAttributes.CELL_EDITOR, DisplayMode.EDIT, configLabels);
IDisplayConverter displayConverter = configRegistry.getConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, DisplayMode.EDIT, configLabels);
IStyle cellStyle = new CellStyleProxy(configRegistry, DisplayMode.EDIT, configLabels);
IDataValidator dataValidator = configRegistry.getConfigAttribute(EditConfigAttributes.DATA_VALIDATOR, DisplayMode.EDIT, configLabels);
ICellEditHandler editHandler = new SingleEditHandler(cellEditor, layer, columnPosition, rowPosition);
final Rectangle editorBounds = layer.getLayerPainter().adjustCellBounds(new Rectangle(cellBounds.x, cellBounds.y, cellBounds.width, cellBounds.height));
Object originalCanonicalValue = cell.getDataValue();
ActiveCellEditor.activate(cellEditor, parent, originalCanonicalValue, initialEditValue, displayConverter, cellStyle, dataValidator, editHandler, columnPosition, rowPosition, columnIndex, rowIndex);
Control editorControl = ActiveCellEditor.getControl();
if (editorControl != null) {
editorControl.setBounds(editorBounds);
ILayerListener layerListener = layerListenerMap.get(layer);
if (layerListener == null) {
layerListener = new InlineCellEditLayerListener(layer);
layerListenerMap.put(layer, layerListener);
layer.addLayerListener(layerListener);
}
}
} catch (Exception e) {
if (cell == null) {
System.err.println("Cell being edited is no longer available. " + "Character: " + initialEditValue);
} else {
System.err.println("Error while editing cell (inline): " + "Cell: " + cell + "; Character: " + initialEditValue);
e.printStackTrace(System.err);
}
}
return true;
}
use of net.sourceforge.nattable.layer.ILayer in project translationstudio8 by heartsome.
the class LayerCommandUtil method convertColumnPositionToTargetContext.
public static ColumnPositionCoordinate convertColumnPositionToTargetContext(ColumnPositionCoordinate columnPositionCoordinate, ILayer targetLayer) {
if (columnPositionCoordinate != null) {
ILayer layer = columnPositionCoordinate.getLayer();
if (layer == targetLayer) {
return columnPositionCoordinate;
}
int columnPosition = columnPositionCoordinate.getColumnPosition();
int underlyingColumnPosition = layer.localToUnderlyingColumnPosition(columnPosition);
if (underlyingColumnPosition < 0) {
return null;
}
Collection<ILayer> underlyingLayers = layer.getUnderlyingLayersByColumnPosition(columnPosition);
if (underlyingLayers != null) {
for (ILayer underlyingLayer : underlyingLayers) {
if (underlyingLayer != null) {
ColumnPositionCoordinate convertedColumnPositionCoordinate = convertColumnPositionToTargetContext(new ColumnPositionCoordinate(underlyingLayer, underlyingColumnPosition), targetLayer);
if (convertedColumnPositionCoordinate != null) {
return convertedColumnPositionCoordinate;
}
}
}
}
}
return null;
}
Aggregations