提交 73e73f13 编写于 作者: J jurgen

Value handlers/editors model

上级 4924870e
......@@ -29,6 +29,11 @@ import org.jkiss.dbeaver.DBException;
*/
public interface DBDValueEditor
{
/**
* Create editor control(s)
*/
void createControl();
/**
* Gets control which actually performs edit
* @return control reference
......
/*
* Copyright (C) 2010-2014 Serge Rieder
* serge@jkiss.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.jkiss.dbeaver.model.impl.data;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ToolBar;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.data.DBDValueController;
import org.jkiss.dbeaver.model.data.DBDValueEditor;
import org.jkiss.dbeaver.ui.DBIcon;
import org.jkiss.dbeaver.ui.UIUtils;
/**
* BaseValueEditor
*/
public abstract class BaseValueEditor<T extends Control> implements DBDValueEditor {
protected final DBDValueController valueController;
protected T control;
private boolean activated;
protected BaseValueEditor(final DBDValueController valueController)
{
this.valueController = valueController;
}
public void createControl() {
this.control = createControl(valueController.getEditPlaceholder());
if (this.control != null) {
initInlineControl(this.control);
}
ToolBar editToolBar = valueController.getEditToolBar();
if (editToolBar != null) {
if (!valueController.isReadOnly()) {
UIUtils.createToolItem(editToolBar, "Save changes", DBIcon.SAVE.getImage(), new Action("Save") {
@Override
public void run() {
saveValue();
}
});
}
}
}
@Override
public Control getControl()
{
return control;
}
protected abstract T createControl(Composite editPlaceholder);
protected void initInlineControl(final Control inlineControl)
{
boolean isInline = (valueController.getEditType() == DBDValueController.EditType.INLINE);
// Panel controls
inlineControl.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e)
{
if (!activated) {
UIUtils.enableHostEditorKeyBindings(valueController.getValueSite(), false);
activated = true;
}
}
@Override
public void focusLost(FocusEvent e)
{
if (activated) {
UIUtils.enableHostEditorKeyBindings(valueController.getValueSite(), true);
activated = false;
}
}
});
inlineControl.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e)
{
if (activated) {
UIUtils.enableHostEditorKeyBindings(valueController.getValueSite(), true);
activated = false;
}
}
});
// if (!isInline) {
// inlineControl.setBackground(valueController.getEditPlaceholder().getBackground());
// }
if (isInline) {
inlineControl.setFont(valueController.getEditPlaceholder().getFont());
// There is a bug in windows. First time date control gain focus it renders cell editor incorrectly.
// Let's focus on it in async mode
inlineControl.getDisplay().asyncExec(new Runnable() {
@Override
public void run()
{
if (!inlineControl.isDisposed()) {
inlineControl.setFocus();
}
}
});
inlineControl.addTraverseListener(new TraverseListener() {
@Override
public void keyTraversed(TraverseEvent e)
{
if (e.detail == SWT.TRAVERSE_RETURN) {
saveValue();
e.doit = false;
e.detail = SWT.TRAVERSE_NONE;
} else if (e.detail == SWT.TRAVERSE_ESCAPE) {
valueController.closeInlineEditor();
e.doit = false;
e.detail = SWT.TRAVERSE_NONE;
} else if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
saveValue();
valueController.nextInlineEditor(e.detail == SWT.TRAVERSE_TAB_NEXT);
e.doit = false;
e.detail = SWT.TRAVERSE_NONE;
}
}
});
inlineControl.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e)
{
// Check new focus control in async mode
// (because right now focus is still on edit control)
inlineControl.getDisplay().asyncExec(new Runnable() {
@Override
public void run()
{
if (inlineControl.isDisposed()) {
return;
}
Control newFocus = inlineControl.getDisplay().getFocusControl();
if (newFocus != null) {
for (Control fc = newFocus.getParent(); fc != null; fc = fc.getParent()) {
if (fc == valueController.getEditPlaceholder()) {
// New focus is still a child of inline placeholder - do not close it
return;
}
}
}
saveValue();
}
});
}
});
}
}
private void saveValue()
{
try {
Object newValue = extractEditorValue();
valueController.closeInlineEditor();
valueController.updateValue(newValue);
} catch (DBException e) {
UIUtils.showErrorDialog(getControl().getShell(), "Value save", "Can't save edited value", e);
}
}
}
......@@ -18,22 +18,14 @@
*/
package org.jkiss.dbeaver.model.impl.data;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IContributionManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ToolBar;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.data.*;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCLogicalOperator;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.DBIcon;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.properties.PropertySourceAbstract;
/**
......@@ -73,155 +65,7 @@ public abstract class BaseValueHandler implements DBDValueHandler {
return DBUtils.getDefaultOperators(attribute);
}
protected abstract class ValueEditor<T extends Control> implements DBDValueEditor {
protected final DBDValueController valueController;
protected final T control;
private boolean activated;
protected ValueEditor(final DBDValueController valueController)
{
this.valueController = valueController;
this.control = createControl(valueController.getEditPlaceholder());
if (this.control != null) {
initInlineControl(this.control);
}
ToolBar editToolBar = valueController.getEditToolBar();
if (editToolBar != null) {
if (!valueController.isReadOnly()) {
UIUtils.createToolItem(editToolBar, "Save changes", DBIcon.SAVE.getImage(), new Action("Save") {
@Override
public void run()
{
saveValue();
}
});
}
}
}
@Override
public Control getControl()
{
return control;
}
protected abstract T createControl(Composite editPlaceholder);
protected void initInlineControl(final Control inlineControl)
{
boolean isInline = (valueController.getEditType() == DBDValueController.EditType.INLINE);
// Panel controls
inlineControl.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e)
{
if (!activated) {
UIUtils.enableHostEditorKeyBindings(valueController.getValueSite(), false);
activated = true;
}
}
@Override
public void focusLost(FocusEvent e)
{
if (activated) {
UIUtils.enableHostEditorKeyBindings(valueController.getValueSite(), true);
activated = false;
}
}
});
inlineControl.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e)
{
if (activated) {
UIUtils.enableHostEditorKeyBindings(valueController.getValueSite(), true);
activated = false;
}
}
});
// if (!isInline) {
// inlineControl.setBackground(valueController.getEditPlaceholder().getBackground());
// }
if (isInline) {
inlineControl.setFont(valueController.getEditPlaceholder().getFont());
// There is a bug in windows. First time date control gain focus it renders cell editor incorrectly.
// Let's focus on it in async mode
inlineControl.getDisplay().asyncExec(new Runnable() {
@Override
public void run()
{
if (!inlineControl.isDisposed()) {
inlineControl.setFocus();
}
}
});
inlineControl.addTraverseListener(new TraverseListener() {
@Override
public void keyTraversed(TraverseEvent e)
{
if (e.detail == SWT.TRAVERSE_RETURN) {
saveValue();
e.doit = false;
e.detail = SWT.TRAVERSE_NONE;
} else if (e.detail == SWT.TRAVERSE_ESCAPE) {
valueController.closeInlineEditor();
e.doit = false;
e.detail = SWT.TRAVERSE_NONE;
} else if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
saveValue();
valueController.nextInlineEditor(e.detail == SWT.TRAVERSE_TAB_NEXT);
e.doit = false;
e.detail = SWT.TRAVERSE_NONE;
}
}
});
inlineControl.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e)
{
// Check new focus control in async mode
// (because right now focus is still on edit control)
inlineControl.getDisplay().asyncExec(new Runnable() {
@Override
public void run()
{
if (inlineControl.isDisposed()) {
return;
}
Control newFocus = inlineControl.getDisplay().getFocusControl();
if (newFocus != null) {
for (Control fc = newFocus.getParent(); fc != null; fc = fc.getParent()) {
if (fc == valueController.getEditPlaceholder()) {
// New focus is still a child of inline placeholder - do not close it
return;
}
}
}
saveValue();
}
});
}
});
}
}
private void saveValue()
{
try {
Object newValue = extractEditorValue();
valueController.closeInlineEditor();
valueController.updateValue(newValue);
} catch (DBException e) {
UIUtils.showErrorDialog(getControl().getShell(), "Value save", "Can't save edited value", e);
}
}
}
protected abstract class ValueEditorEx<T extends Control> extends ValueEditor<T> implements DBDValueEditorStandalone {
protected abstract static class ValueEditorEx<T extends Control> extends BaseValueEditor<T> implements DBDValueEditorStandalone {
protected ValueEditorEx(final DBDValueController valueController)
{
......
/*
* Copyright (C) 2010-2014 Serge Rieder
* serge@jkiss.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.jkiss.dbeaver.model.impl.data;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.data.DBDValueController;
/**
* BooleanInlineEditor
*/
public class BooleanInlineEditor extends BaseValueEditor<Combo> {
public BooleanInlineEditor(DBDValueController controller) {
super(controller);
}
@Override
protected Combo createControl(Composite editPlaceholder)
{
final Combo editor = new Combo(editPlaceholder, SWT.READ_ONLY);
editor.add("FALSE");
editor.add("TRUE");
editor.setEnabled(!valueController.isReadOnly());
return editor;
}
@Override
public Object extractEditorValue()
{
switch (control.getSelectionIndex()) {
case 0:
return Boolean.FALSE;
case 1:
return Boolean.TRUE;
default:
return null;
}
}
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
control.setText(value == null ? "FALSE" : value.toString().toUpperCase());
}
}
/*
* Copyright (C) 2010-2014 Serge Rieder
* serge@jkiss.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.jkiss.dbeaver.model.impl.data;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.data.DBDValueController;
/**
* BooleanPanelEditor
*/
public class BooleanPanelEditor extends BaseValueEditor<List> {
public BooleanPanelEditor(DBDValueController controller) {
super(controller);
}
@Override
public Object extractEditorValue()
{
return control.getSelectionIndex() == 1;
}
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
control.setSelection(Boolean.TRUE.equals(value) ? 1 : 0);
}
@Override
protected List createControl(Composite editPlaceholder)
{
final List editor = new List(valueController.getEditPlaceholder(), SWT.SINGLE | SWT.READ_ONLY);
editor.add("FALSE");
editor.add("TRUE");
return editor;
}
}
/*
* Copyright (C) 2010-2014 Serge Rieder
* serge@jkiss.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.jkiss.dbeaver.model.impl.data;
import org.jkiss.dbeaver.model.data.DBDValueController;
/**
* DateTimeEditorHelper
*/
public interface DateTimeEditorHelper {
boolean isTimestamp(DBDValueController valueController);
boolean isTime(DBDValueController valueController);
boolean isDate(DBDValueController valueController);
Object getValueFromMillis(DBDValueController valueController, long ms);
}
/*
* Copyright (C) 2010-2014 Serge Rieder
* serge@jkiss.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.jkiss.dbeaver.model.impl.data;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.data.DBDValueController;
import org.jkiss.dbeaver.ui.UIUtils;
import java.util.Calendar;
import java.util.Date;
/**
* DateTimeInlineEditor
*/
public class DateTimeInlineEditor extends BaseValueEditor<DateTime> {
private final DateTimeEditorHelper helper;
private DateTime dateEditor;
private DateTime timeEditor;
public DateTimeInlineEditor(DBDValueController controller, DateTimeEditorHelper helper) {
super(controller);
this.helper = helper;
}
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
if (value instanceof Date) {
Calendar cl = Calendar.getInstance();
cl.setTime((Date) value);
if (dateEditor != null) {
dateEditor.setDate(cl.get(Calendar.YEAR), cl.get(Calendar.MONTH), cl.get(Calendar.DAY_OF_MONTH));
}
if (timeEditor != null) {
timeEditor.setTime(cl.get(Calendar.HOUR_OF_DAY), cl.get(Calendar.MINUTE), cl.get(Calendar.SECOND));
}
}
}
@Override
protected DateTime createControl(Composite editPlaceholder)
{
boolean inline = valueController.getEditType() == DBDValueController.EditType.INLINE;
final Composite dateTimeGroup = inline ?
valueController.getEditPlaceholder() :
new Composite(valueController.getEditPlaceholder(), SWT.BORDER);
if (!inline) {
dateTimeGroup.setLayout(new GridLayout(2, false));
}
boolean isDate = helper.isDate(valueController);
boolean isTime = helper.isTime(valueController);
boolean isTimeStamp = helper.isTimestamp(valueController) || (!isDate && !isTime);
if (!inline && (isDate || isTimeStamp)) {
UIUtils.createControlLabel(dateTimeGroup, "Date");
}
if (isDate || isTimeStamp) {
dateEditor = new DateTime(dateTimeGroup,
(inline ? SWT.DATE | SWT.DROP_DOWN | SWT.MEDIUM | SWT.BORDER : SWT.DATE | SWT.DROP_DOWN | SWT.LONG));
dateEditor.setEnabled(!valueController.isReadOnly());
}
if (!inline && (isTime || isTimeStamp)) {
UIUtils.createControlLabel(dateTimeGroup, "Time");
}
if (isTime || isTimeStamp) {
timeEditor = new DateTime(dateTimeGroup,
(inline ? SWT.BORDER : SWT.NONE) | SWT.TIME | SWT.LONG);
timeEditor.setEnabled(!valueController.isReadOnly());
}
if (dateEditor != null) {
if (timeEditor != null) {
initInlineControl(timeEditor);
}
return dateEditor;
}
return timeEditor;
}
@Override
public Object extractEditorValue()
{
Calendar cl = getCalendarFromControls(dateEditor, timeEditor);
return helper.getValueFromMillis(valueController, cl.getTimeInMillis());
}
public static Calendar getCalendarFromControls(DateTime dateEditor, DateTime timeEditor) {
Calendar cl = Calendar.getInstance();
cl.clear();
if (dateEditor != null) {
cl.set(Calendar.YEAR, dateEditor.getYear());
cl.set(Calendar.MONTH, dateEditor.getMonth());
cl.set(Calendar.DAY_OF_MONTH, dateEditor.getDay());
}
if (timeEditor != null) {
cl.set(Calendar.HOUR_OF_DAY, timeEditor.getHours());
cl.set(Calendar.MINUTE, timeEditor.getMinutes());
cl.set(Calendar.SECOND, timeEditor.getSeconds());
cl.set(Calendar.MILLISECOND, 0);
}
return cl;
}
}
......@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.jkiss.dbeaver.ui.dialogs.data;
package org.jkiss.dbeaver.model.impl.data;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
......@@ -29,9 +29,8 @@ import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.model.data.DBDValueController;
import org.jkiss.dbeaver.model.impl.jdbc.data.JDBCDateTimeValueHandler;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.data.ValueViewDialog;
import java.util.Calendar;
import java.util.Date;
......@@ -39,32 +38,33 @@ import java.util.Date;
/**
* DateTimeViewDialog
*/
public class DateTimeViewDialog extends ValueViewDialog {
public class DateTimeStandaloneEditor extends ValueViewDialog {
private final DateTimeEditorHelper helper;
private DateTime dateEditor;
private DateTime timeEditor;
public DateTimeViewDialog(DBDValueController valueController) {
public DateTimeStandaloneEditor(DBDValueController valueController, DateTimeEditorHelper helper) {
super(valueController);
this.helper = helper;
}
@Override
protected Control createDialogArea(Composite parent)
{
Object value = getValueController().getValue();
DBDValueController valueController = getValueController();
Object value = valueController.getValue();
DBSTypedObject valueType = getValueController().getValueType();
boolean isDate = valueType.getTypeID() == java.sql.Types.DATE;
boolean isTime = valueType.getTypeID() == java.sql.Types.TIME;
boolean isTimeStamp = valueType.getTypeID() == java.sql.Types.TIMESTAMP;
boolean isDate = helper.isDate(valueController);
boolean isTime = helper.isTime(valueController);
boolean isTimeStamp = helper.isTimestamp(valueController);
Composite dialogGroup = (Composite)super.createDialogArea(parent);
Composite panel = UIUtils.createPlaceholder(dialogGroup, isTimeStamp ? 2 : 3);
panel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
int style = SWT.BORDER;
if (getValueController().isReadOnly()) {
if (valueController.isReadOnly()) {
style |= SWT.READ_ONLY;
}
......@@ -95,7 +95,7 @@ public class DateTimeViewDialog extends ValueViewDialog {
gd.horizontalSpan = 2;
button.setLayoutData(gd);
}
button.setEnabled(!getValueController().isReadOnly());
button.setEnabled(!valueController.isReadOnly());
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e)
......@@ -125,7 +125,8 @@ public class DateTimeViewDialog extends ValueViewDialog {
@Override
public Object extractEditorValue()
{
return JDBCDateTimeValueHandler.getDate(dateEditor, timeEditor);
long ms = DateTimeInlineEditor.getCalendarFromControls(dateEditor, timeEditor).getTimeInMillis();
return helper.getValueFromMillis(getValueController(), ms);
}
@Override
......
......@@ -82,7 +82,13 @@ public class DefaultValueHandler extends BaseValueHandler {
case INLINE:
case PANEL:
return new DBDValueEditor() {
private Text control = new Text(controller.getEditPlaceholder(), SWT.BORDER);
private Text control;
@Override
public void createControl() {
control = new Text(controller.getEditPlaceholder(), SWT.BORDER);
}
@Override
public Control getControl() {
return control;
......
......@@ -20,12 +20,7 @@ package org.jkiss.dbeaver.model.impl.jdbc.data;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.data.DBDValueController;
import org.jkiss.dbeaver.model.data.DBDValueEditor;
......@@ -34,6 +29,8 @@ import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.data.BooleanInlineEditor;
import org.jkiss.dbeaver.model.impl.data.BooleanPanelEditor;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.dialogs.data.DefaultValueViewDialog;
......@@ -102,60 +99,9 @@ public class JDBCBooleanValueHandler extends JDBCAbstractValueHandler {
{
switch (controller.getEditType()) {
case INLINE:
{
return new ValueEditor<Combo>(controller) {
@Override
protected Combo createControl(Composite editPlaceholder)
{
final Combo editor = new Combo(editPlaceholder, SWT.READ_ONLY);
editor.add("FALSE");
editor.add("TRUE");
editor.setEnabled(!valueController.isReadOnly());
return editor;
}
@Override
public Object extractEditorValue()
{
switch (control.getSelectionIndex()) {
case 0:
return Boolean.FALSE;
case 1:
return Boolean.TRUE;
default:
return null;
}
}
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
control.setText(value == null ? "FALSE" : value.toString().toUpperCase());
}
};
}
return new BooleanInlineEditor(controller);
case PANEL:
{
return new ValueEditor<List>(controller) {
@Override
public Object extractEditorValue()
{
return control.getSelectionIndex() == 1;
}
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
control.setSelection(Boolean.TRUE.equals(value) ? 1 : 0);
}
@Override
protected List createControl(Composite editPlaceholder)
{
final List editor = new List(valueController.getEditPlaceholder(), SWT.SINGLE | SWT.READ_ONLY);
editor.add("FALSE");
editor.add("TRUE");
return editor;
}
};
}
return new BooleanPanelEditor(controller);
case EDITOR:
return new DefaultValueViewDialog(controller);
default:
......
......@@ -26,7 +26,6 @@ import org.eclipse.swt.widgets.Tree;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.model.data.DBDComplexValue;
import org.jkiss.dbeaver.model.data.DBDValueController;
import org.jkiss.dbeaver.model.data.DBDValueEditor;
......@@ -35,10 +34,10 @@ import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.data.BaseValueEditor;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.dialogs.data.ComplexObjectEditor;
import org.jkiss.dbeaver.ui.dialogs.data.DefaultValueViewDialog;
import org.jkiss.dbeaver.ui.properties.PropertySourceAbstract;
import java.sql.SQLException;
......@@ -110,7 +109,7 @@ public abstract class JDBCComplexValueHandler extends JDBCAbstractValueHandler {
{
switch (controller.getEditType()) {
case PANEL:
return new ValueEditor<Tree>(controller) {
return new BaseValueEditor<Tree>(controller) {
ComplexObjectEditor editor;
@Override
......
......@@ -44,6 +44,7 @@ import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.BytesContentStorage;
import org.jkiss.dbeaver.model.impl.ExternalContentStorage;
import org.jkiss.dbeaver.model.impl.StringContentStorage;
import org.jkiss.dbeaver.model.impl.data.BaseValueEditor;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
......@@ -282,7 +283,7 @@ public class JDBCContentValueHandler extends JDBCAbstractValueHandler {
if (controller.getValue() instanceof DBDContentCached) {
final boolean isText = ContentUtils.isTextContent(((DBDContent) controller.getValue()));
// String editor
return new ValueEditor<Text>(controller) {
return new BaseValueEditor<Text>(controller) {
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
......
......@@ -20,10 +20,6 @@ package org.jkiss.dbeaver.model.impl.jdbc.data;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IContributionManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
......@@ -34,10 +30,11 @@ import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.data.DateTimeEditorHelper;
import org.jkiss.dbeaver.model.impl.data.DateTimeInlineEditor;
import org.jkiss.dbeaver.model.impl.data.DateTimeStandaloneEditor;
import org.jkiss.dbeaver.model.impl.data.DefaultDataFormatter;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.data.DateTimeViewDialog;
import org.jkiss.dbeaver.ui.properties.PropertySourceAbstract;
import java.sql.SQLException;
......@@ -50,7 +47,7 @@ import java.util.Date;
/**
* JDBC string value handler
*/
public class JDBCDateTimeValueHandler extends JDBCAbstractValueHandler {
public class JDBCDateTimeValueHandler extends JDBCAbstractValueHandler implements DateTimeEditorHelper {
public static final String TYPE_NAME_DATE = "date"; //$NON-NLS-1$
public static final String TYPE_NAME_TIME = "time"; //$NON-NLS-1$
......@@ -124,72 +121,9 @@ public class JDBCDateTimeValueHandler extends JDBCAbstractValueHandler {
switch (controller.getEditType()) {
case INLINE:
case PANEL:
return new ValueEditor<DateTime>(controller) {
DateTime dateEditor;
DateTime timeEditor;
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
if (value instanceof Date) {
Calendar cl = Calendar.getInstance();
cl.setTime((Date) value);
if (dateEditor != null) {
dateEditor.setDate(cl.get(Calendar.YEAR), cl.get(Calendar.MONTH), cl.get(Calendar.DAY_OF_MONTH));
}
if (timeEditor != null) {
timeEditor.setTime(cl.get(Calendar.HOUR_OF_DAY), cl.get(Calendar.MINUTE), cl.get(Calendar.SECOND));
}
}
}
@Override
protected DateTime createControl(Composite editPlaceholder)
{
boolean inline = valueController.getEditType() == DBDValueController.EditType.INLINE;
final Composite dateTimeGroup = inline ?
valueController.getEditPlaceholder() :
new Composite(valueController.getEditPlaceholder(), SWT.BORDER);
if (!inline) {
dateTimeGroup.setLayout(new GridLayout(2, false));
}
boolean isDate = valueController.getValueType().getTypeID() == java.sql.Types.DATE;
boolean isTime = valueController.getValueType().getTypeID() == java.sql.Types.TIME;
boolean isTimeStamp = valueController.getValueType().getTypeID() == java.sql.Types.TIMESTAMP || (!isDate && !isTime);
if (!inline && (isDate || isTimeStamp)) {
UIUtils.createControlLabel(dateTimeGroup, "Date");
}
if (isDate || isTimeStamp) {
dateEditor = new DateTime(dateTimeGroup,
(inline ? SWT.DATE | SWT.DROP_DOWN | SWT.MEDIUM | SWT.BORDER : SWT.DATE | SWT.DROP_DOWN | SWT.LONG));
dateEditor.setEnabled(!valueController.isReadOnly());
}
if (!inline && (isTime || isTimeStamp)) {
UIUtils.createControlLabel(dateTimeGroup, "Time");
}
if (isTime || isTimeStamp) {
timeEditor = new DateTime(dateTimeGroup,
(inline ? SWT.BORDER : SWT.NONE) | SWT.TIME | SWT.LONG);
timeEditor.setEnabled(!valueController.isReadOnly());
}
if (dateEditor != null) {
if (timeEditor != null) {
initInlineControl(timeEditor);
}
return dateEditor;
}
return timeEditor;
}
@Override
public Object extractEditorValue()
{
return getDate(dateEditor, timeEditor);
}
};
return new DateTimeInlineEditor(controller, this);
case EDITOR:
return new DateTimeViewDialog(controller);
return new DateTimeStandaloneEditor(controller, this);
default:
return null;
}
......@@ -200,7 +134,7 @@ public class JDBCDateTimeValueHandler extends JDBCAbstractValueHandler {
public String getValueDisplayString(@NotNull DBSTypedObject column, Object value, @NotNull DBDDisplayFormat format)
{
if (value == null) {
return super.getValueDisplayString(column, value, format);
return super.getValueDisplayString(column, null, format);
}
if (value instanceof Date && format == DBDDisplayFormat.NATIVE) {
Calendar cal = Calendar.getInstance();
......@@ -279,8 +213,7 @@ public class JDBCDateTimeValueHandler extends JDBCAbstractValueHandler {
{
manager.add(new Action(CoreMessages.model_jdbc_set_to_current_time) {
@Override
public void run()
{
public void run() {
controller.updateValue(new Date());
}
});
......@@ -297,30 +230,6 @@ public class JDBCDateTimeValueHandler extends JDBCAbstractValueHandler {
getFormatter(controller.getValueType()).getPattern());
}
public static Date getDate(DateTime dateEditor, DateTime timeEditor)
{
Calendar cl = Calendar.getInstance();
cl.clear();
if (dateEditor != null) {
cl.set(Calendar.YEAR, dateEditor.getYear());
cl.set(Calendar.MONTH, dateEditor.getMonth());
cl.set(Calendar.DAY_OF_MONTH, dateEditor.getDay());
}
if (timeEditor != null) {
cl.set(Calendar.HOUR_OF_DAY, timeEditor.getHours());
cl.set(Calendar.MINUTE, timeEditor.getMinutes());
cl.set(Calendar.SECOND, timeEditor.getSeconds());
cl.set(Calendar.MILLISECOND, 0);
}
if (timeEditor == null) {
return new java.sql.Date(cl.getTimeInMillis());
} else if (dateEditor == null) {
return new java.sql.Time(cl.getTimeInMillis());
} else {
return new Timestamp(cl.getTimeInMillis());
}
}
@Nullable
private static java.sql.Time getTimeValue(Object value)
{
......@@ -385,4 +294,29 @@ public class JDBCDateTimeValueHandler extends JDBCAbstractValueHandler {
}
}
@Override
public boolean isTimestamp(DBDValueController valueController) {
return valueController.getValueType().getTypeID() == java.sql.Types.TIMESTAMP;
}
@Override
public boolean isTime(DBDValueController valueController) {
return valueController.getValueType().getTypeID() == java.sql.Types.TIME;
}
@Override
public boolean isDate(DBDValueController valueController) {
return valueController.getValueType().getTypeID() == java.sql.Types.DATE;
}
@Override
public Object getValueFromMillis(DBDValueController valueController, long ms) {
if (isTimestamp(valueController)) {
return new Timestamp(ms);
} else if (isTime(valueController)) {
return new java.sql.Time(ms);
} else {
return new java.sql.Date(ms);
}
}
}
\ No newline at end of file
......@@ -33,6 +33,7 @@ import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.data.BaseValueEditor;
import org.jkiss.dbeaver.model.impl.data.DefaultDataFormatter;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.UIUtils;
......@@ -220,7 +221,7 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
case INLINE:
case PANEL:
if (controller.getValueType().getDataKind() == DBPDataKind.BOOLEAN) {
return new ValueEditor<Combo>(controller) {
return new BaseValueEditor<Combo>(controller) {
@Override
protected Combo createControl(Composite editPlaceholder)
{
......@@ -249,7 +250,7 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
}
};
} else {
return new ValueEditor<Text>(controller) {
return new BaseValueEditor<Text>(controller) {
@Override
protected Text createControl(Composite editPlaceholder)
{
......
......@@ -33,6 +33,7 @@ import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.data.BaseValueEditor;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.dialogs.data.CursorViewDialog;
import org.jkiss.utils.CommonUtils;
......@@ -131,7 +132,7 @@ public class JDBCObjectValueHandler extends JDBCAbstractValueHandler {
{
switch (controller.getEditType()) {
case PANEL:
return new ValueEditor<Text>(controller) {
return new BaseValueEditor<Text>(controller) {
@Override
protected Text createControl(Composite editPlaceholder)
{
......
......@@ -31,6 +31,7 @@ import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.data.BaseValueEditor;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.dialogs.data.TextViewDialog;
import org.jkiss.utils.CommonUtils;
......@@ -76,7 +77,7 @@ public class JDBCStringValueHandler extends JDBCAbstractValueHandler {
switch (controller.getEditType()) {
case INLINE:
case PANEL:
return new ValueEditor<Text>(controller) {
return new BaseValueEditor<Text>(controller) {
@Override
protected Text createControl(Composite editPlaceholder)
{
......
......@@ -191,6 +191,7 @@ public class SQLQueryParameterBindDialog extends StatusDialog {
try {
DBDValueEditor editor = valueHandler.createEditor(valueController);
if (editor != null) {
editor.createControl();
tableEditor.minimumHeight = placeholder.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
tableEditor.setEditor(placeholder, item, 3);
} else {
......
......@@ -80,7 +80,10 @@ class FilterValueEditDialog extends BaseDialog {
label.setText(valueController.getBinding().getName() + " " + operator.getStringValue() + " :");
try {
editor = valueController.getValueHandler().createEditor(valueController);
editor.primeEditorValue(valueController.getValue());
if (editor != null) {
editor.createControl();
editor.primeEditorValue(valueController.getValue());
}
} catch (DBException e) {
log.error("Can't create inline value editor", e);
}
......
......@@ -1368,6 +1368,9 @@ public class ResultSetViewer extends Viewer
UIUtils.showErrorDialog(site.getShell(), "Cannot edit value", null, e);
return null;
}
if (editor != null) {
editor.createControl();
}
if (editor instanceof DBDValueEditorStandalone) {
valueController.registerEditor((DBDValueEditorStandalone)editor);
// show dialog in separate job to avoid block
......
......@@ -110,7 +110,9 @@ abstract class ViewValuePanel extends Composite {
return;
}
fillStandardToolBar();
if (valueViewer == null) {
if (valueViewer != null) {
valueViewer.createControl();
} else {
final Composite placeholder = UIUtils.createPlaceholder(viewPlaceholder, 1);
placeholder.setBackground(placeholder.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
placeholder.addPaintListener(new PaintListener() {
......
......@@ -201,6 +201,7 @@ public class ComplexObjectEditor extends TreeViewer {
try {
curCellEditor = valueHandler.createEditor(valueController);
if (curCellEditor != null) {
curCellEditor.createControl();
if (curCellEditor instanceof DBDValueEditorStandalone) {
((DBDValueEditorStandalone) curCellEditor).showValueEditor();
} else if (curCellEditor.getControl() != null) {
......
......@@ -101,6 +101,11 @@ public abstract class ValueViewDialog extends Dialog implements DBDValueEditorSt
dialogCount++;
}
@Override
public void createControl() {
}
protected IDialogSettings getDialogSettings()
{
return dialogSettings;
......@@ -115,7 +120,7 @@ public abstract class ValueViewDialog extends Dialog implements DBDValueEditorSt
protected DBDValueEditor createPanelEditor(final Composite placeholder)
throws DBException
{
return valueController.getValueHandler().createEditor(new DBDValueController() {
DBDValueEditor editor = valueController.getValueHandler().createEditor(new DBDValueController() {
@Override
public DBPDataSource getDataSource()
{
......@@ -202,6 +207,10 @@ public abstract class ValueViewDialog extends Dialog implements DBDValueEditorSt
{
}
});
if (editor != null) {
editor.createControl();
}
return editor;
}
public DBDValueController getValueController() {
......
......@@ -454,6 +454,11 @@ public class ContentEditor extends MultiPageAbstractEditor implements IDataSourc
return input == null ? null : input.getValueController();
}
@Override
public void createControl() {
}
@Override
public Control getControl()
{
......
......@@ -36,6 +36,7 @@ import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.data.BaseValueEditor;
import org.jkiss.dbeaver.model.impl.jdbc.data.JDBCAbstractValueHandler;
import org.jkiss.dbeaver.model.struct.DBSEntityAttribute;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
......@@ -130,7 +131,7 @@ public class MySQLEnumValueHandler extends JDBCAbstractValueHandler {
switch (controller.getEditType()) {
case INLINE:
{
return new ValueEditor<Combo>(controller) {
return new BaseValueEditor<Combo>(controller) {
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
......@@ -171,7 +172,7 @@ public class MySQLEnumValueHandler extends JDBCAbstractValueHandler {
}
case PANEL:
{
return new ValueEditor<List>(controller) {
return new BaseValueEditor<List>(controller) {
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
......
......@@ -26,6 +26,7 @@ import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.mysql.model.MySQLTableColumn;
import org.jkiss.dbeaver.model.data.DBDValueController;
import org.jkiss.dbeaver.model.data.DBDValueEditor;
import org.jkiss.dbeaver.model.impl.data.BaseValueEditor;
import org.jkiss.dbeaver.ui.dialogs.data.DefaultValueViewDialog;
import org.jkiss.utils.CommonUtils;
......@@ -49,7 +50,7 @@ public class MySQLSetValueHandler extends MySQLEnumValueHandler {
case PANEL:
final MySQLTableColumn column = ((MySQLTypeEnum) controller.getValue()).getColumn();
return new ValueEditor<org.eclipse.swt.widgets.List>(controller) {
return new BaseValueEditor<org.eclipse.swt.widgets.List>(controller) {
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册