提交 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; ...@@ -46,7 +46,7 @@ import java.util.List;
/** /**
* Tree/table viewer column controller * Tree/table viewer column controller
*/ */
public class ViewerColumnController { public class ViewerColumnController<ELEMENT> {
private static final Log log = Log.getLog(ViewerColumnController.class); private static final Log log = Log.getLog(ViewerColumnController.class);
...@@ -73,19 +73,16 @@ public class ViewerColumnController { ...@@ -73,19 +73,16 @@ public class ViewerColumnController {
control.setData(DATA_KEY, this); control.setData(DATA_KEY, this);
if (control instanceof Tree || control instanceof Table) { if (control instanceof Tree || control instanceof Table) {
menuListener = new Listener() { menuListener = event -> {
@Override Point pt = control.getDisplay().map(null, control, new Point(event.x, event.y));
public void handleEvent(Event event) { Rectangle clientArea = ((Composite) control).getClientArea();
Point pt = control.getDisplay().map(null, control, new Point(event.x, event.y)); if (RuntimeUtils.isPlatformMacOS()) {
Rectangle clientArea = ((Composite) control).getClientArea(); clickOnHeader = pt.y < 0;
if (RuntimeUtils.isPlatformMacOS()) { } else {
clickOnHeader = pt.y < 0; if (control instanceof Tree) {
clickOnHeader = clientArea.y <= pt.y && pt.y < (clientArea.y + ((Tree) control).getHeaderHeight());
} else { } else {
if (control instanceof Tree) { clickOnHeader = clientArea.y <= pt.y && pt.y < (clientArea.y + ((Table) control).getHeaderHeight());
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());
}
} }
} }
}; };
...@@ -120,6 +117,16 @@ public class ViewerColumnController { ...@@ -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) 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); addColumn(name, description, style, defaultVisible, required, false, null, labelProvider, null);
...@@ -317,30 +324,28 @@ public class ViewerColumnController { ...@@ -317,30 +324,28 @@ public class ViewerColumnController {
if (columnInfo.labelProvider instanceof ILazyLabelProvider) { if (columnInfo.labelProvider instanceof ILazyLabelProvider) {
hasLazyColumns = true; hasLazyColumns = true;
} else if (columnInfo.labelProvider instanceof ILabelProvider) { } else if (columnInfo.labelProvider instanceof ILabelProvider) {
columnInfo.sortListener = new SortListener(columnInfo); columnInfo.sortListener = new SortListener(viewer, columnInfo);
columnInfo.column.addListener(SWT.Selection, columnInfo.sortListener); columnInfo.column.addListener(SWT.Selection, columnInfo.sortListener);
} }
} }
if (hasLazyColumns) { if (hasLazyColumns) {
viewer.getControl().addListener(SWT.PaintItem, new Listener() { viewer.getControl().addListener(SWT.PaintItem, event -> {
public void handleEvent(Event event) { if (viewer instanceof TreeViewer) {
if (viewer instanceof TreeViewer) { TreeColumn column = ((TreeViewer) viewer).getTree().getColumn(event.index);
TreeColumn column = ((TreeViewer) viewer).getTree().getColumn(event.index); if (((ColumnInfo) column.getData()).labelProvider instanceof ILazyLabelProvider &&
if (((ColumnInfo) column.getData()).labelProvider instanceof ILazyLabelProvider && CommonUtils.isEmpty(((TreeItem) event.item).getText(event.index))) {
CommonUtils.isEmpty(((TreeItem) event.item).getText(event.index))) { final String lazyText = ((ILazyLabelProvider) ((ColumnInfo) column.getData()).labelProvider).getLazyText(event.item.getData());
final String lazyText = ((ILazyLabelProvider) ((ColumnInfo) column.getData()).labelProvider).getLazyText(event.item.getData()); if (!CommonUtils.isEmpty(lazyText)) {
if (!CommonUtils.isEmpty(lazyText)) { ((TreeItem) event.item).setText(event.index, lazyText);
((TreeItem) event.item).setText(event.index, lazyText);
}
} }
} else { }
TableColumn column = ((TableViewer) viewer).getTable().getColumn(event.index); } else {
if (((ColumnInfo) column.getData()).labelProvider instanceof ILazyLabelProvider && TableColumn column = ((TableViewer) viewer).getTable().getColumn(event.index);
CommonUtils.isEmpty(((TableItem) event.item).getText(event.index))) { if (((ColumnInfo) column.getData()).labelProvider instanceof ILazyLabelProvider &&
final String lazyText = ((ILazyLabelProvider) ((ColumnInfo) column.getData()).labelProvider).getLazyText(event.item.getData()); CommonUtils.isEmpty(((TableItem) event.item).getText(event.index))) {
if (!CommonUtils.isEmpty(lazyText)) { final String lazyText = ((ILazyLabelProvider) ((ColumnInfo) column.getData()).labelProvider).getLazyText(event.item.getData());
((TableItem) event.item).setText(event.index, lazyText); if (!CommonUtils.isEmpty(lazyText)) {
} ((TableItem) event.item).setText(event.index, lazyText);
} }
} }
} }
...@@ -357,7 +362,7 @@ public class ViewerColumnController { ...@@ -357,7 +362,7 @@ public class ViewerColumnController {
visibleList.add(column); visibleList.add(column);
} }
} }
Collections.sort(visibleList, new ColumnInfoComparator()); visibleList.sort(new ColumnInfoComparator());
return visibleList; return visibleList;
} }
...@@ -504,17 +509,15 @@ public class ViewerColumnController { ...@@ -504,17 +509,15 @@ public class ViewerColumnController {
UIUtils.createControlLabel(composite, "Select columns you want to display"); UIUtils.createControlLabel(composite, "Select columns you want to display");
List<ColumnInfo> orderedList = new ArrayList<>(columns); 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 = new Table(composite, SWT.BORDER | SWT.CHECK | SWT.H_SCROLL | SWT.V_SCROLL);
colTable.setLayoutData(new GridData(GridData.FILL_BOTH)); colTable.setLayoutData(new GridData(GridData.FILL_BOTH));
colTable.setLinesVisible(true); colTable.setLinesVisible(true);
colTable.addListener(SWT.Selection,new Listener() { colTable.addListener(SWT.Selection, event -> {
public void handleEvent(Event event) { if( event.detail == SWT.CHECK ) {
if( event.detail == SWT.CHECK ) { if (((TableItem)event.item).getGrayed()) {
if (((TableItem)event.item).getGrayed()) { ((TableItem)event.item).setChecked(true);
((TableItem)event.item).setChecked(true); event.doit = false;
event.doit = false;
}
} }
} }
}); });
...@@ -594,13 +597,15 @@ public class ViewerColumnController { ...@@ -594,13 +597,15 @@ public class ViewerColumnController {
} }
} }
private class SortListener implements Listener private static class SortListener implements Listener
{ {
ColumnViewer viewer;
ColumnInfo columnInfo; ColumnInfo columnInfo;
int sortDirection = SWT.DOWN; int sortDirection = SWT.DOWN;
Item prevColumn = null; Item prevColumn = null;
public SortListener(ColumnInfo columnInfo) { public SortListener(ColumnViewer viewer, ColumnInfo columnInfo) {
this.viewer = viewer;
this.columnInfo = columnInfo; this.columnInfo = columnInfo;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册