提交 143686e4 编写于 作者: S Serge Rider

#6473 Object container selector control redesign

上级 0411ba00
......@@ -19,14 +19,11 @@ package org.jkiss.dbeaver.tools.transfer.database;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.operation.IRunnableContext;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.model.navigator.DBNDataSource;
import org.jkiss.dbeaver.model.navigator.DBNDatabaseNode;
import org.jkiss.dbeaver.model.navigator.DBNNode;
......@@ -34,6 +31,7 @@ import org.jkiss.dbeaver.model.runtime.DefaultProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSDataContainer;
import org.jkiss.dbeaver.model.struct.DBSDataManipulator;
import org.jkiss.dbeaver.model.struct.DBSObjectContainer;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.tools.transfer.DTUtils;
import org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer;
import org.jkiss.dbeaver.tools.transfer.IDataTransferSettings;
......@@ -277,24 +275,20 @@ public class DatabaseConsumerSettings implements IDataTransferSettings {
}
}
@NotNull
public String getContainerFullName() {
DBSObjectContainer container = getContainer();
return container == null ? "" :
container instanceof DBPDataSource ?
DBUtils.getObjectFullName(container, DBPEvaluationContext.UI) :
DBUtils.getObjectFullName(container, DBPEvaluationContext.UI) + " [" + container.getDataSource().getContainer().getName() + "]";
}
public void loadNode(IRunnableContext runnableContext) {
if (containerNode == null && !CommonUtils.isEmpty(containerNodePath)) {
if (!CommonUtils.isEmpty(containerNodePath)) {
public void loadNode(IRunnableContext runnableContext, @Nullable DBSObjectContainer producerContainer) {
if (containerNode == null && (producerContainer != null || !CommonUtils.isEmpty(containerNodePath))) {
if (producerContainer != null || !CommonUtils.isEmpty(containerNodePath)) {
try {
runnableContext.run(true, true, monitor -> {
try {
DBNNode node = DBWorkbench.getPlatform().getNavigatorModel().getNodeByPath(
new DefaultProgressMonitor(monitor),
containerNodePath);
DBNNode node;
if (producerContainer != null) {
node = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(producerContainer);
} else {
node = DBWorkbench.getPlatform().getNavigatorModel().getNodeByPath(
new DefaultProgressMonitor(monitor),
containerNodePath);
}
if (node instanceof DBNDatabaseNode) {
containerNode = (DBNDatabaseNode) node;
}
......
......@@ -104,7 +104,12 @@ public class DatabaseConsumerPageMapping extends ActiveWizardPage<DataTransferWi
{
// Target container
// Use first source object as cur selection (it's better than nothing)
containerPanel = new ObjectContainerSelectorPanel(composite, DTMessages.data_transfer_db_consumer_target_container, DTMessages.data_transfer_db_consumer_choose_container) {
containerPanel = new ObjectContainerSelectorPanel(composite,
DBWorkbench.getPlatform().getWorkspace().getActiveProject(),
"container.data-transfer.database-consumer",
DTMessages.data_transfer_db_consumer_target_container,
DTMessages.data_transfer_db_consumer_choose_container)
{
@Nullable
@Override
protected DBNNode getSelectedNode() {
......@@ -127,10 +132,7 @@ public class DatabaseConsumerPageMapping extends ActiveWizardPage<DataTransferWi
@Override
protected void setSelectedNode(DBNDatabaseNode node) {
settings.setContainerNode(node);
DBNDataSource dataSourceNode = DBNDataSource.getDataSourceNode(node);
setContainerInfo(
dataSourceNode == null ? node.getNodeIconDefault() : dataSourceNode.getNodeIconDefault(),
settings.getContainerFullName());
setContainerInfo(node);
// Reset mappings
for (DatabaseMappingContainer mappingContainer : settings.getDataMappings().values()) {
if (mappingContainer.getMappingType() != DatabaseMappingType.unspecified) {
......@@ -717,13 +719,25 @@ public class DatabaseConsumerPageMapping extends ActiveWizardPage<DataTransferWi
public void activatePage()
{
final DatabaseConsumerSettings settings = getDatabaseConsumerSettings();
settings.loadNode(getContainer());
DBSObjectContainer producerContainer = null;
for (DataTransferPipe pipe : getWizard().getSettings().getDataPipes()) {
if (pipe.getProducer() != null) {
DBSObject producerObject = pipe.getProducer().getDatabaseObject();
if (producerObject instanceof DBSDataContainer) {
DBSObject container = producerObject.getParentObject();
if (container instanceof DBSObjectContainer) {
producerContainer = (DBSObjectContainer) container;
}
}
}
}
settings.loadNode(getContainer(), producerContainer);
DBNDatabaseNode containerNode = settings.getContainerNode();
if (containerNode != null) {
DBNDataSource dataSourceNode = DBNDataSource.getDataSourceNode(containerNode);
containerPanel.setContainerInfo(
dataSourceNode == null ? containerNode.getNodeIconDefault() : dataSourceNode.getNodeIcon(),
settings.getContainerFullName());
//DBNDataSource dataSourceNode = DBNDataSource.getDataSourceNode(containerNode);
containerPanel.setContainerInfo(containerNode);
}
if (mappingViewer.getInput() == null) {
......
......@@ -64,6 +64,7 @@ import java.util.List;
*/
public class EditForeignKeyPage extends BaseObjectEditPage {
public static final String CONTAINER_LOGICAL_FK = "container.logical-fk";
private static final Log log = Log.getLog(EditForeignKeyPage.class);
public static final FKType FK_TYPE_PHYSICAL = new FKType("Physical", true);
......@@ -73,7 +74,7 @@ public class EditForeignKeyPage extends BaseObjectEditPage {
private DBSEntityAssociation foreignKey;
private DBSEntity curRefTable;
private List<DBSEntityConstraint> curConstraints;
private DBNDatabaseNode ownerTableNode;
private DBNDatabaseNode ownerTableNode, ownerContainerNode;
private Table tableList;
private Combo uniqueKeyCombo;
private Text fkNameText;
......@@ -485,10 +486,18 @@ public class EditForeignKeyPage extends BaseObjectEditPage {
}
private void createContainerSelector(Composite tableGroup) throws DBException {
ObjectContainerSelectorPanel containerPanel = new ObjectContainerSelectorPanel(tableGroup, "Reference table container", "Select reference table catalog/schema") {
ObjectContainerSelectorPanel containerPanel = new ObjectContainerSelectorPanel(
tableGroup,
foreignKey.getDataSource().getContainer().getRegistry().getProject(),
CONTAINER_LOGICAL_FK,
"Reference table container",
"Select reference table catalog/schema") {
@Nullable
@Override
protected DBNNode getSelectedNode() {
if (ownerContainerNode != null) {
return ownerContainerNode;
}
DBSObject containerObject;
if (ownerTableNode != null) {
DBNNode containerNode = ownerTableNode.getParentNode();
......@@ -513,17 +522,17 @@ public class EditForeignKeyPage extends BaseObjectEditPage {
log.error("Error getting real object container", e);
}
}
return DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(containerObject);
return ownerContainerNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(containerObject);
}
@Override
protected void setSelectedNode(DBNDatabaseNode node) {
ownerTableNode = node;
if (node == null) {
setContainerInfo(DBIcon.TYPE_UNKNOWN, "");
ownerContainerNode = node;
if (ownerContainerNode == null) {
setContainerInfo(null);
} else {
setContainerInfo(node.getNodeIconDefault(), node.getNodeFullName());
loadTableList(ownerTableNode);
setContainerInfo(node);
loadTableList(ownerContainerNode);
}
}
};
......@@ -536,7 +545,9 @@ public class EditForeignKeyPage extends BaseObjectEditPage {
while (containerNode instanceof DBNDatabaseFolder) {
containerNode = containerNode.getParentNode();
}
containerPanel.setContainerInfo(containerNode.getNodeIconDefault(), containerNode.getNodeFullName());
if (containerNode instanceof DBNDatabaseNode) {
containerPanel.setContainerInfo((DBNDatabaseNode) containerNode);
}
}
}
......
......@@ -17,17 +17,22 @@
*/
package org.jkiss.dbeaver.ui.controls;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBIcon;
import org.jkiss.dbeaver.model.DBPImage;
import org.jkiss.dbeaver.model.app.DBPProject;
import org.jkiss.dbeaver.model.navigator.DBNDatabaseNode;
import org.jkiss.dbeaver.model.navigator.DBNModel;
......@@ -38,19 +43,55 @@ import org.jkiss.dbeaver.model.struct.rdb.DBSSchema;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.utils.CommonUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
/**
* ObjectContainerSelectorPanel
*/
public abstract class ObjectContainerSelectorPanel extends Composite
{
private static final Log log = Log.getLog(ObjectContainerSelectorPanel.class);
public static final int MAX_HISTORY_LENGTH = 20;
private final DBPProject project;
private final String selectorId;
private final Label containerIcon;
private final Text containerName;
private final Combo containerNameCombo;
private final List<HistoryItem> historyItems = new ArrayList<>();
private static class HistoryItem {
private String containerName;
private String containerPath;
private String dataSourceName;
private DBNDatabaseNode containerNode;
HistoryItem(String containerName, String containerPath, String dataSourceName, DBNDatabaseNode node) {
this.containerName = containerName;
this.containerPath = containerPath;
this.dataSourceName = dataSourceName;
this.containerNode = node;
}
public String getFullName() {
String dsName = containerNode != null ? containerNode.getDataSourceContainer().getName() : dataSourceName;
if (CommonUtils.equalObjects(dsName, containerName)) {
return containerName;
}
return containerName + " [" + dsName + "]";
}
}
protected ObjectContainerSelectorPanel(Composite parent, String containerTitle, String containerHint) {
protected ObjectContainerSelectorPanel(Composite parent, DBPProject project, String selectorId, String containerTitle, String containerHint) {
super(parent, SWT.NONE);
this.project = project;
this.selectorId = selectorId;
GridLayout layout = new GridLayout(4, false);
layout.marginHeight = 0;
layout.marginWidth = 0;
......@@ -61,21 +102,26 @@ public abstract class ObjectContainerSelectorPanel extends Composite
containerIcon = new Label(this, SWT.NONE);
containerIcon.setImage(DBeaverIcons.getImage(DBIcon.TYPE_UNKNOWN));
containerName = new Text(this, SWT.BORDER | SWT.READ_ONLY);
containerName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
containerName.setText("");
containerNameCombo = new Combo(this, SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
containerNameCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
containerNameCombo.setText("");
if (containerHint != null) {
UIUtils.addEmptyTextHint(containerName, text -> containerHint);
UIUtils.addEmptyTextHint(containerNameCombo, text -> containerHint);
}
containerNameCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
handleContainerChange();
}
});
Button browseButton = new Button(this, SWT.PUSH);
browseButton.setImage(DBeaverIcons.getImage(DBIcon.TREE_FOLDER));
browseButton.setText("...");
Runnable containerSelector = () -> {
DBPProject activeProject = DBWorkbench.getPlatform().getWorkspace().getActiveProject();
if (activeProject != null) {
if (project != null) {
final DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
final DBNProject rootNode = navigatorModel.getRoot().getProjectNode(activeProject);
final DBNProject rootNode = navigatorModel.getRoot().getProjectNode(project);
DBNNode selectedNode = getSelectedNode();
DBNNode node = DBWorkbench.getPlatformUI().selectObject(
getShell(),
......@@ -87,6 +133,8 @@ public abstract class ObjectContainerSelectorPanel extends Composite
new Class[]{DBSSchema.class});
if (node instanceof DBNDatabaseNode) {
setSelectedNode((DBNDatabaseNode) node);
addNodeToHistory((DBNDatabaseNode) node);
saveHistory();
}
}
};
......@@ -96,21 +144,141 @@ public abstract class ObjectContainerSelectorPanel extends Composite
containerSelector.run();
}
});
containerName.addMouseListener(new MouseAdapter() {
containerNameCombo.addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent e) {
containerSelector.run();
}
});
loadHistory();
}
public void setContainerInfo(DBPImage image, String name) {
containerIcon.setImage(DBeaverIcons.getImage(image));
containerName.setText(name);
private HistoryItem addNodeToHistory(DBNDatabaseNode node) {
for (int i = 0; i < historyItems.size(); i++) {
HistoryItem item = historyItems.get(i);
if (item.containerPath.equals(node.getNodeItemPath())) {
item.containerNode = node;
moveHistoryItemToBeginning(item);
return item;
}
}
HistoryItem newItem = new HistoryItem(
node.getNodeFullName(),
node.getNodeItemPath(),
node.getDataSourceContainer().getName(),
node
);
historyItems.add(0, newItem);
return newItem;
}
private void moveHistoryItemToBeginning(HistoryItem item) {
historyItems.remove(item);
historyItems.add(0, item);
removeItemFromCombo(item);
containerNameCombo.add(item.getFullName(), 0);
containerNameCombo.select(0);
}
private void handleContainerChange() {
int historyIndex = containerNameCombo.getSelectionIndex();
if (historyIndex >= 0 && historyIndex < historyItems.size()) {
HistoryItem historyItem = historyItems.get(historyIndex);
if (historyItem.containerNode == null) {
// Load node
try {
UIUtils.runInProgressDialog(monitor -> {
try {
DBNNode node = DBWorkbench.getPlatform().getNavigatorModel().getNodeByPath(monitor, project, historyItem.containerPath);
if (node instanceof DBNDatabaseNode) {
historyItem.containerNode = (DBNDatabaseNode) node;
}
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (InvocationTargetException e) {
DBWorkbench.getPlatformUI().showError("Bad container path", "Can't find database node by path " + historyItem.containerPath, e.getTargetException());
}
}
if (historyItem.containerNode != null) {
setSelectedNode(historyItem.containerNode);
moveHistoryItemToBeginning(historyItem);
} else {
historyItems.remove(historyIndex);
containerNameCombo.remove(historyIndex);
}
}
//setSelectedNode(node);
}
private void loadHistory() {
IDialogSettings historySection = UIUtils.getDialogSettings("ObjectContainerSelector");
IDialogSettings projectSection = historySection.getSection(project.getName());
if (projectSection != null) {
IDialogSettings selectorSection = projectSection.getSection(selectorId);
if (selectorSection != null) {
for (int i = 1; i < MAX_HISTORY_LENGTH; i++) {
IDialogSettings itemSection = selectorSection.getSection("item" + i);
if (itemSection == null) {
break;
}
historyItems.add(new HistoryItem(
itemSection.get("name"),
itemSection.get("path"),
itemSection.get("data-source"),
null));
}
}
}
for (HistoryItem item : historyItems) {
containerNameCombo.add(item.getFullName());
}
}
private void saveHistory() {
IDialogSettings selectorHistorySection = UIUtils.getDialogSettings("ObjectContainerSelector");
IDialogSettings projectSection = UIUtils.getSettingsSection(selectorHistorySection, project.getName());
IDialogSettings selectorSection = projectSection.addNewSection(selectorId);
for (int i = 0; i < historyItems.size(); i++) {
HistoryItem item = historyItems.get(i);
IDialogSettings itemSection = selectorSection.addNewSection("item" + (i + 1));
itemSection.put("name", item.containerName);
itemSection.put("path", item.containerPath);
itemSection.put("data-source", item.dataSourceName);
}
}
public void setContainerInfo(DBNDatabaseNode node) {
if (node == null) {
containerIcon.setImage(DBeaverIcons.getImage(DBIcon.TYPE_UNKNOWN));
containerNameCombo.select(-1);
return;
}
HistoryItem item = addNodeToHistory(node);
containerIcon.setImage(DBeaverIcons.getImage(node.getNodeIconDefault()));
removeItemFromCombo(item);
containerNameCombo.add(item.getFullName(), 0);
containerNameCombo.select(0);
}
private void removeItemFromCombo(HistoryItem item) {
int itemCount = containerNameCombo.getItemCount();
for (int i = 0; i < itemCount; i++) {
if (containerNameCombo.getItem(i).equals(item.getFullName())) {
containerNameCombo.remove(i);
break;
}
}
}
protected abstract void setSelectedNode(DBNDatabaseNode node);
@Nullable
protected abstract DBNNode getSelectedNode();
}
\ No newline at end of file
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册