提交 388a22d0 编写于 作者: J jurgen

Number value editor

上级 1cd7cba2
/*
* 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;
/**
* BitInlineEditor
*/
public class BitInlineEditor extends BaseValueEditor<Combo> {
public BitInlineEditor(DBDValueController controller) {
super(controller);
}
@Override
protected Combo createControl(Composite editPlaceholder)
{
final Combo editor = new Combo(valueController.getEditPlaceholder(), SWT.READ_ONLY);
editor.add("0"); //$NON-NLS-1$
editor.add("1"); //$NON-NLS-1$
editor.setEnabled(!valueController.isReadOnly());
return editor;
}
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
control.setText(value == null ? "0" : value.toString()); //$NON-NLS-1$
}
@Override
public Object extractEditorValue()
{
switch (control.getSelectionIndex()) {
case 0:
return (byte) 0;
case 1:
return (byte) 1;
default:
return null;
}
}
}
/*
* 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.DBDDataFormatter;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import java.util.Locale;
/**
* NumberEditorHelper
*/
public interface NumberEditorHelper {
Locale getLocale();
DBDDataFormatter getFormatter();
Class<? extends Number> getNumberType(DBSTypedObject type, Object originalValue);
}
/*
* 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.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.data.DBDDataFormatter;
import org.jkiss.dbeaver.model.data.DBDDisplayFormat;
import org.jkiss.dbeaver.model.data.DBDValueController;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.utils.CommonUtils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
/**
* NumberInlineEditor
*/
public class NumberInlineEditor extends BaseValueEditor<Text> {
static final Log log = LogFactory.getLog(NumberInlineEditor.class);
private static final int MAX_NUMBER_LENGTH = 100;
private static final String BAD_DOUBLE_VALUE = "2.2250738585072012e-308"; //$NON-NLS-1$
private final NumberEditorHelper helper;
public NumberInlineEditor(DBDValueController controller, NumberEditorHelper helper) {
super(controller);
this.helper = helper;
}
@Override
protected Text createControl(Composite editPlaceholder)
{
final Text editor = new Text(valueController.getEditPlaceholder(), SWT.BORDER);
editor.setEditable(!valueController.isReadOnly());
editor.setTextLimit(MAX_NUMBER_LENGTH);
Class<? extends Number> type = helper.getNumberType(valueController.getValueType(), valueController.getValue());
if (type == Float.class || type == Double.class || type == BigDecimal.class) {
editor.addVerifyListener(UIUtils.getNumberVerifyListener(helper.getLocale()));
} else {
editor.addVerifyListener(UIUtils.getIntegerVerifyListener(helper.getLocale()));
}
return editor;
}
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
if (value != null) {
control.setText(valueController.getValueHandler().getValueDisplayString(valueController.getValueType(), value, DBDDisplayFormat.UI));
} else {
control.setText("");
}
if (valueController.getEditType() == DBDValueController.EditType.INLINE) {
control.selectAll();
}
}
@Nullable
@Override
public Object extractEditorValue()
{
String text = control.getText();
if (CommonUtils.isEmpty(text)) {
return null;
}
Class<? extends Number> hintType = helper.getNumberType(valueController.getValueType(), valueController.getValue());
return convertStringToNumber(text, hintType, helper.getFormatter());
}
@Nullable
public static Number convertStringToNumber(String text, Class<? extends Number> hintType, DBDDataFormatter formatter)
{
if (text == null || text.length() == 0) {
return null;
}
try {
if (hintType == Long.class) {
try {
return Long.valueOf(text);
} catch (NumberFormatException e) {
return new BigInteger(text);
}
} else if (hintType == Integer.class) {
return Integer.valueOf(text);
} else if (hintType == Short.class) {
return Short.valueOf(text);
} else if (hintType == Byte.class) {
return Byte.valueOf(text);
} else if (hintType == Float.class) {
return Float.valueOf(text);
} else if (hintType == Double.class) {
return toDouble(text);
} else if (hintType == BigInteger.class) {
return new BigInteger(text);
} else {
return new BigDecimal(text);
}
} catch (NumberFormatException e) {
log.debug("Bad numeric value '" + text + "' - " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
try {
return (Number)formatter.parseValue(text, hintType);
} catch (ParseException e1) {
log.debug("Can't parse numeric value [" + text + "] using formatter", e);
return null;
}
}
}
private static Number toDouble(String text)
{
if (text.equals(BAD_DOUBLE_VALUE)) {
return Double.MIN_VALUE;
}
return Double.valueOf(text);
}
}
......@@ -18,10 +18,6 @@
*/
package org.jkiss.dbeaver.model.impl.jdbc.data;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
......@@ -33,28 +29,24 @@ 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.BitInlineEditor;
import org.jkiss.dbeaver.model.impl.data.DefaultDataFormatter;
import org.jkiss.dbeaver.model.impl.data.NumberEditorHelper;
import org.jkiss.dbeaver.model.impl.data.NumberInlineEditor;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.data.DefaultValueViewDialog;
import org.jkiss.utils.CommonUtils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Locale;
/**
* JDBC number value handler
*/
public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
public class JDBCNumberValueHandler extends JDBCAbstractValueHandler implements NumberEditorHelper {
private static final String TYPE_NAME_NUMBER = "number"; //$NON-NLS-1$
private static final int MAX_NUMBER_LENGTH = 100;
private static final String BAD_DOUBLE_VALUE = "2.2250738585072012e-308"; //$NON-NLS-1$
private Locale locale;
private DBDDataFormatter formatter;
......@@ -221,79 +213,9 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
case INLINE:
case PANEL:
if (controller.getValueType().getDataKind() == DBPDataKind.BOOLEAN) {
return new BaseValueEditor<Combo>(controller) {
@Override
protected Combo createControl(Composite editPlaceholder)
{
final Combo editor = new Combo(valueController.getEditPlaceholder(), SWT.READ_ONLY);
editor.add("0"); //$NON-NLS-1$
editor.add("1"); //$NON-NLS-1$
editor.setEnabled(!valueController.isReadOnly());
return editor;
}
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
control.setText(value == null ? "0" : value.toString()); //$NON-NLS-1$
}
@Override
public Object extractEditorValue()
{
switch (control.getSelectionIndex()) {
case 0:
return (byte) 0;
case 1:
return (byte) 1;
default:
return null;
}
}
};
return new BitInlineEditor(controller);
} else {
return new BaseValueEditor<Text>(controller) {
@Override
protected Text createControl(Composite editPlaceholder)
{
final Text editor = new Text(valueController.getEditPlaceholder(), SWT.BORDER);
editor.setEditable(!valueController.isReadOnly());
editor.setTextLimit(MAX_NUMBER_LENGTH);
switch (valueController.getValueType().getTypeID()) {
case java.sql.Types.BIGINT:
case java.sql.Types.INTEGER:
case java.sql.Types.SMALLINT:
case java.sql.Types.TINYINT:
case java.sql.Types.BIT:
editor.addVerifyListener(UIUtils.getIntegerVerifyListener(locale));
break;
default:
editor.addVerifyListener(UIUtils.getNumberVerifyListener(locale));
break;
}
return editor;
}
@Override
public void primeEditorValue(@Nullable Object value) throws DBException
{
if (value != null) {
control.setText(getValueDisplayString(valueController.getValueType(), value, DBDDisplayFormat.UI));
} else {
control.setText("");
}
if (valueController.getEditType() == DBDValueController.EditType.INLINE) {
control.selectAll();
}
}
@Nullable
@Override
public Object extractEditorValue()
{
String text = control.getText();
if (CommonUtils.isEmpty(text)) {
return null;
}
return convertStringToNumber(formatter, text, valueController.getValue(), valueController.getValueType());
}
};
return new NumberInlineEditor(controller, this);
}
case EDITOR:
return new DefaultValueViewDialog(controller);
......@@ -323,100 +245,53 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
} else if (object instanceof Number) {
return object;
} else if (object instanceof String) {
return convertStringToNumber(formatter, (String)object, null, type);
return NumberInlineEditor.convertStringToNumber((String)object, getNumberType(type, null), formatter);
} else {
log.warn("Unrecognized type '" + object.getClass().getName() + "' - can't convert to numeric");
return null;
}
}
@Nullable
public static Number convertStringToNumber(DBDDataFormatter formatter, String text, @Nullable Object originalValue, DBSTypedObject type)
{
if (text == null || text.length() == 0) {
return null;
}
Class<?> hintType = null;
try {
if (originalValue instanceof Number) {
if (originalValue instanceof Long) {
hintType = Long.class;
return Long.valueOf(text);
} else if (originalValue instanceof Integer) {
hintType = Integer.class;
return Integer.valueOf(text);
} else if (originalValue instanceof Short) {
hintType = Short.class;
return Short.valueOf(text);
} else if (originalValue instanceof Byte) {
hintType = Byte.class;
return Byte.valueOf(text);
} else if (originalValue instanceof Float) {
hintType = Float.class;
return Float.valueOf(text);
} else if (originalValue instanceof Double) {
hintType = Double.class;
return Double.valueOf(text);
} else if (originalValue instanceof BigInteger) {
hintType = BigInteger.class;
return new BigInteger(text);
} else {
hintType = BigDecimal.class;
return new BigDecimal(text);
}
} else {
switch (type.getTypeID()) {
case java.sql.Types.BIGINT:
hintType = Long.class;
try {
return Long.parseLong(text);
} catch (NumberFormatException e) {
return new BigInteger(text);
}
case java.sql.Types.DECIMAL:
case java.sql.Types.DOUBLE:
case java.sql.Types.REAL:
hintType = Double.class;
return toDouble(text);
case java.sql.Types.FLOAT:
hintType = Float.class;
return Float.valueOf(text);
case java.sql.Types.INTEGER:
hintType = Integer.class;
return Integer.valueOf(text);
case java.sql.Types.SMALLINT:
case java.sql.Types.TINYINT:
hintType = Short.class;
return Short.valueOf(text);
case java.sql.Types.NUMERIC:
hintType = BigDecimal.class;
return new BigDecimal(text);
default:
if (type.getScale() > 0) {
hintType = Double.class;
return toDouble(text);
} else {
hintType = Long.class;
return Long.valueOf(text);
}
}
}
} catch (NumberFormatException e) {
log.debug("Bad numeric value '" + text + "' - " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
try {
return (Number)formatter.parseValue(text, hintType);
} catch (ParseException e1) {
log.debug("Can't parse numeric value [" + text + "] using formatter", e);
return null;
}
}
@Override
public Locale getLocale() {
return locale;
}
private static Number toDouble(String text)
{
if (text.equals(BAD_DOUBLE_VALUE)) {
return Double.MIN_VALUE;
@Override
public DBDDataFormatter getFormatter() {
return formatter;
}
@Override
public Class<? extends Number> getNumberType(DBSTypedObject type, Object originalValue) {
if (originalValue instanceof Number) {
return ((Number)originalValue).getClass();
} else {
switch (type.getTypeID()) {
case java.sql.Types.BIGINT:
return Long.class;
case java.sql.Types.DECIMAL:
case java.sql.Types.DOUBLE:
case java.sql.Types.REAL:
return Double.class;
case java.sql.Types.FLOAT:
return Float.class;
case java.sql.Types.INTEGER:
return Integer.class;
case java.sql.Types.SMALLINT:
case java.sql.Types.TINYINT:
return Short.class;
case java.sql.Types.BIT:
return Byte.class;
case java.sql.Types.NUMERIC:
return BigDecimal.class;
default:
if (type.getScale() > 0) {
return Double.class;
} else {
return Long.class;
}
}
}
return Double.valueOf(text);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册