提交 2c5975c0 编写于 作者: S serge-rider

Column controller typing


Former-commit-id: d10b5488
上级 9ffe7ef5
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2018 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ui.controls;
/**
* IColumnTextProvider
*/
public interface IColumnTextProvider<ELEMENT>
{
String getText(ELEMENT element);
}
\ No newline at end of file
......@@ -46,7 +46,7 @@ import java.util.List;
/**
* Tree/table viewer column controller
*/
public class ViewerColumnController {
public class ViewerColumnController<ELEMENT> {
private static final Log log = Log.getLog(ViewerColumnController.class);
......@@ -73,19 +73,16 @@ public class ViewerColumnController {
control.setData(DATA_KEY, this);
if (control instanceof Tree || control instanceof Table) {
menuListener = new Listener() {
@Override
public void handleEvent(Event event) {
Point pt = control.getDisplay().map(null, control, new Point(event.x, event.y));
Rectangle clientArea = ((Composite) control).getClientArea();
if (RuntimeUtils.isPlatformMacOS()) {
clickOnHeader = pt.y < 0;
menuListener = event -> {
Point pt = control.getDisplay().map(null, control, new Point(event.x, event.y));
Rectangle clientArea = ((Composite) control).getClientArea();
if (RuntimeUtils.isPlatformMacOS()) {
clickOnHeader = pt.y < 0;
} else {
if (control instanceof Tree) {
clickOnHeader = clientArea.y <= pt.y && pt.y < (clientArea.y + ((Tree) control).getHeaderHeight());
} else {
if (control instanceof Tree) {
clickOnHeader = clientArea.y <= pt.y && pt.y < (clientArea.y + ((Tree) control).getHeaderHeight());
} else {
clickOnHeader = clientArea.y <= pt.y && pt.y < (clientArea.y + ((Table) control).getHeaderHeight());
}
clickOnHeader = clientArea.y <= pt.y && pt.y < (clientArea.y + ((Table) control).getHeaderHeight());
}
}
};
......@@ -120,6 +117,16 @@ public class ViewerColumnController {
});
}
public void addColumn(String name, String description, int style, boolean defaultVisible, boolean required, IColumnTextProvider<ELEMENT> labelProvider)
{
addColumn(name, description, style, defaultVisible, required, false, null, new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
cell.setText(labelProvider.getText((ELEMENT) cell.getElement()));
}
}, null);
}
public void addColumn(String name, String description, int style, boolean defaultVisible, boolean required, CellLabelProvider labelProvider)
{
addColumn(name, description, style, defaultVisible, required, false, null, labelProvider, null);
......@@ -317,30 +324,28 @@ public class ViewerColumnController {
if (columnInfo.labelProvider instanceof ILazyLabelProvider) {
hasLazyColumns = true;
} else if (columnInfo.labelProvider instanceof ILabelProvider) {
columnInfo.sortListener = new SortListener(columnInfo);
columnInfo.sortListener = new SortListener(viewer, columnInfo);
columnInfo.column.addListener(SWT.Selection, columnInfo.sortListener);
}
}
if (hasLazyColumns) {
viewer.getControl().addListener(SWT.PaintItem, new Listener() {
public void handleEvent(Event event) {
if (viewer instanceof TreeViewer) {
TreeColumn column = ((TreeViewer) viewer).getTree().getColumn(event.index);
if (((ColumnInfo) column.getData()).labelProvider instanceof ILazyLabelProvider &&
CommonUtils.isEmpty(((TreeItem) event.item).getText(event.index))) {
final String lazyText = ((ILazyLabelProvider) ((ColumnInfo) column.getData()).labelProvider).getLazyText(event.item.getData());
if (!CommonUtils.isEmpty(lazyText)) {
((TreeItem) event.item).setText(event.index, lazyText);
}
viewer.getControl().addListener(SWT.PaintItem, event -> {
if (viewer instanceof TreeViewer) {
TreeColumn column = ((TreeViewer) viewer).getTree().getColumn(event.index);
if (((ColumnInfo) column.getData()).labelProvider instanceof ILazyLabelProvider &&
CommonUtils.isEmpty(((TreeItem) event.item).getText(event.index))) {
final String lazyText = ((ILazyLabelProvider) ((ColumnInfo) column.getData()).labelProvider).getLazyText(event.item.getData());
if (!CommonUtils.isEmpty(lazyText)) {
((TreeItem) event.item).setText(event.index, lazyText);
}
} else {
TableColumn column = ((TableViewer) viewer).getTable().getColumn(event.index);
if (((ColumnInfo) column.getData()).labelProvider instanceof ILazyLabelProvider &&
CommonUtils.isEmpty(((TableItem) event.item).getText(event.index))) {
final String lazyText = ((ILazyLabelProvider) ((ColumnInfo) column.getData()).labelProvider).getLazyText(event.item.getData());
if (!CommonUtils.isEmpty(lazyText)) {
((TableItem) event.item).setText(event.index, lazyText);
}
}
} else {
TableColumn column = ((TableViewer) viewer).getTable().getColumn(event.index);
if (((ColumnInfo) column.getData()).labelProvider instanceof ILazyLabelProvider &&
CommonUtils.isEmpty(((TableItem) event.item).getText(event.index))) {
final String lazyText = ((ILazyLabelProvider) ((ColumnInfo) column.getData()).labelProvider).getLazyText(event.item.getData());
if (!CommonUtils.isEmpty(lazyText)) {
((TableItem) event.item).setText(event.index, lazyText);
}
}
}
......@@ -357,7 +362,7 @@ public class ViewerColumnController {
visibleList.add(column);
}
}
Collections.sort(visibleList, new ColumnInfoComparator());
visibleList.sort(new ColumnInfoComparator());
return visibleList;
}
......@@ -504,17 +509,15 @@ public class ViewerColumnController {
UIUtils.createControlLabel(composite, "Select columns you want to display");
List<ColumnInfo> orderedList = new ArrayList<>(columns);
Collections.sort(orderedList, new ColumnInfoComparator());
orderedList.sort(new ColumnInfoComparator());
colTable = new Table(composite, SWT.BORDER | SWT.CHECK | SWT.H_SCROLL | SWT.V_SCROLL);
colTable.setLayoutData(new GridData(GridData.FILL_BOTH));
colTable.setLinesVisible(true);
colTable.addListener(SWT.Selection,new Listener() {
public void handleEvent(Event event) {
if( event.detail == SWT.CHECK ) {
if (((TableItem)event.item).getGrayed()) {
((TableItem)event.item).setChecked(true);
event.doit = false;
}
colTable.addListener(SWT.Selection, event -> {
if( event.detail == SWT.CHECK ) {
if (((TableItem)event.item).getGrayed()) {
((TableItem)event.item).setChecked(true);
event.doit = false;
}
}
});
......@@ -594,13 +597,15 @@ public class ViewerColumnController {
}
}
private class SortListener implements Listener
private static class SortListener implements Listener
{
ColumnViewer viewer;
ColumnInfo columnInfo;
int sortDirection = SWT.DOWN;
Item prevColumn = null;
public SortListener(ColumnInfo columnInfo) {
public SortListener(ColumnViewer viewer, ColumnInfo columnInfo) {
this.viewer = viewer;
this.columnInfo = columnInfo;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册