提交 a1ba0624 编写于 作者: J jurgen

Use locale specific numbers formatting

Refactor dbc impl package

Former-commit-id: 09e1337a
上级 1faddb33
......@@ -257,13 +257,22 @@ public class CommonUtils {
public static boolean contains(short[] array, short value)
{
if (isEmpty(array)) return false;
for (short v : array) {
if (v == value) return true;
}
for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {
if (array[i] == value) return true;
}
return false;
}
public static boolean isEmpty(int[] array)
public static boolean contains(char[] array, char value)
{
if (array == null || array.length == 0) return false;
for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {
if (array[i] == value) return true;
}
return false;
}
public static boolean isEmpty(int[] array)
{
return array == null || array.length == 0;
}
......
......@@ -19,6 +19,8 @@
package org.jkiss.dbeaver.model;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.struct.DBSDataKind;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.editors.sql.format.SQLFormatter;
import org.jkiss.dbeaver.ui.editors.sql.format.SQLFormatterConfiguration;
import org.jkiss.dbeaver.ui.editors.sql.syntax.SQLSyntaxManager;
......@@ -127,6 +129,15 @@ public final class SQLUtils {
return pattern.matcher(string).matches();
}
public static void appendValue(StringBuilder buffer, DBSTypedObject type, Object value)
{
if (type.getDataKind() == DBSDataKind.NUMERIC || type.getDataKind() == DBSDataKind.BOOLEAN) {
buffer.append(value);
} else {
buffer.append('\'').append(value).append('\'');
}
}
public static String escapeString(String string)
{
return string.replaceAll("'", "\\'");
......
......@@ -19,6 +19,7 @@
package org.jkiss.dbeaver.model.data;
import org.eclipse.swt.widgets.Control;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
......@@ -28,6 +29,8 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
*/
public interface DBDValueEditor
{
Control getControl();
Object extractValue(DBRProgressMonitor monitor)
throws DBException;
......
package org.jkiss.dbeaver.model.exec;
import org.jkiss.dbeaver.model.data.DBDAttributeValue;
import org.jkiss.dbeaver.model.data.DBDDataFilter;
import org.jkiss.dbeaver.model.data.DBDDataReceiver;
import java.util.List;
/**
* Data request
*/
public interface DBCDataRequest {
void setDataReceiver(DBDDataReceiver dataReceiver);
void setDataFilter(DBDDataFilter dataFilter);
void setLimit(long firstRow, long maxRows);
void setKeys(List<DBDAttributeValue> attributes);
void setData(List<DBDAttributeValue> attributes);
long execute(DBCExecutionContext context);
long getResult();
String generateScript(DBCExecutionContext context);
}
......@@ -72,7 +72,9 @@ public class NumberDataFormatter implements DBDDataFormatter {
return null;
}
try {
return numberFormat.format(value);
synchronized (numberFormat) {
return numberFormat.format(value);
}
} catch (Exception e) {
return value.toString();
}
......@@ -81,7 +83,9 @@ public class NumberDataFormatter implements DBDDataFormatter {
@Override
public Object parseValue(String value) throws ParseException
{
return numberFormat.parse(value);
synchronized (numberFormat) {
return numberFormat.parse(value);
}
}
}
......@@ -27,7 +27,7 @@ import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCConnector;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCExecutionContext;
import org.jkiss.dbeaver.model.impl.jdbc.api.JDBCConnectionImpl;
import org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCConnectionImpl;
import org.jkiss.dbeaver.model.meta.Property;
import org.jkiss.dbeaver.model.qm.QMUtils;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
......
......@@ -161,6 +161,12 @@ public abstract class JDBCAbstractValueHandler implements DBDValueHandler {
}
}
@Override
public Control getControl()
{
return control;
}
public abstract Object extractValue(DBRProgressMonitor monitor) throws DBException;
protected abstract T createControl(Composite editPlaceholder);
......@@ -183,9 +189,9 @@ public abstract class JDBCAbstractValueHandler implements DBDValueHandler {
boolean isInline = (valueController.getEditType() == DBDValueController.EditType.INLINE);
if (!isInline) {
inlineControl.setBackground(valueController.getEditPlaceholder().getBackground());
}
// if (!isInline) {
// inlineControl.setBackground(valueController.getEditPlaceholder().getBackground());
// }
if (isInline) {
inlineControl.setFont(valueController.getEditPlaceholder().getFont());
......
......@@ -24,6 +24,7 @@ import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.model.DBConstants;
import org.jkiss.dbeaver.model.DBPDataTypeProvider;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.SQLUtils;
import org.jkiss.dbeaver.model.data.DBDArray;
import org.jkiss.dbeaver.model.data.DBDValue;
import org.jkiss.dbeaver.model.data.DBDValueCloneable;
......@@ -31,7 +32,7 @@ import org.jkiss.dbeaver.model.data.DBDValueHandler;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCResultSet;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCExecutionContext;
import org.jkiss.dbeaver.model.impl.jdbc.api.JDBCResultSetImpl;
import org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCResultSetImpl;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSDataType;
......@@ -186,12 +187,8 @@ public class JDBCArray implements DBDArray, DBDValueCloneable {
if (str.length() > 0) {
str.append(","); //$NON-NLS-1$
}
if (item instanceof Number) {
str.append(item);
} else {
String itemString = valueHandler.getValueDisplayString(type, item);
str.append(itemString);
}
String itemString = valueHandler.getValueDisplayString(type, item);
SQLUtils.appendValue(str, type, itemString);
}
return str.toString();
}
......
......@@ -24,7 +24,7 @@ import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.model.data.DBDCursor;
import org.jkiss.dbeaver.model.data.DBDValue;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCExecutionContext;
import org.jkiss.dbeaver.model.impl.jdbc.api.JDBCResultSetImpl;
import org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCResultSetImpl;
import java.sql.ResultSet;
......
......@@ -45,7 +45,9 @@ import org.jkiss.utils.CommonUtils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
import java.sql.Types;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;
/**
* JDBC number value handler
......@@ -57,11 +59,13 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
private static final String BAD_DOUBLE_VALUE = "2.2250738585072012e-308"; //$NON-NLS-1$
private Locale locale;
private DBDDataFormatter formatter;
public JDBCNumberValueHandler(DBDDataFormatterProfile formatterProfile)
{
try {
locale = formatterProfile.getLocale();
formatter = formatterProfile.createFormatter(TYPE_NAME_NUMBER);
} catch (Exception e) {
log.error("Could not create formatter for number value handler", e); //$NON-NLS-1$
......@@ -236,20 +240,21 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
case java.sql.Types.SMALLINT:
case java.sql.Types.TINYINT:
case java.sql.Types.BIT:
editor.addVerifyListener(UIUtils.INTEGER_VERIFY_LISTENER);
editor.addVerifyListener(UIUtils.getIntegerVerifyListener());
break;
default:
editor.addVerifyListener(UIUtils.NUMBER_VERIFY_LISTENER);
editor.addVerifyListener(UIUtils.getNumberVerifyListener(locale));
break;
}
editor.setEditable(!valueController.isReadOnly());
return editor;
}
@Override
public void refreshValue()
{
Object value = valueController.getValue();
control.setText(value == null ? "" : value.toString()); //$NON-NLS-1$
if (value != null) {
control.setText(getValueDisplayString(valueController.getAttributeMetaData(), value)); //$NON-NLS-1$
}
if (valueController.getEditType() == DBDValueController.EditType.INLINE) {
control.selectAll();
}
......@@ -261,7 +266,7 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
if (CommonUtils.isEmpty(text)) {
return null;
}
return convertStringToNumber(text, valueController.getValue(), valueController.getAttributeMetaData());
return convertStringToNumber(formatter, text, valueController.getValue(), valueController.getAttributeMetaData());
}
};
}
......@@ -292,32 +297,7 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
} else if (object instanceof Number) {
return object;
} else if (object instanceof String) {
String strValue = (String)object;
try {
switch (type.getTypeID()) {
case java.sql.Types.BIGINT:
return Long.valueOf(strValue);
case java.sql.Types.DOUBLE:
case java.sql.Types.REAL:
return Double.valueOf(strValue);
case java.sql.Types.FLOAT:
return Float.valueOf(strValue);
case java.sql.Types.INTEGER:
return Integer.valueOf(strValue);
case java.sql.Types.SMALLINT:
return Short.valueOf(strValue);
case java.sql.Types.TINYINT:
case java.sql.Types.BIT:
return Byte.valueOf(strValue);
case Types.NUMERIC:
return new BigDecimal(strValue);
default:
// Here may be any numeric value. BigDecimal or BigInteger for example
return new BigDecimal(strValue);
}
} catch (NumberFormatException e) {
return null;
}
return convertStringToNumber(formatter, (String)object, null, type);
} else {
log.warn("Unrecognized type '" + object.getClass().getName() + "' - can't convert to numeric");
return null;
......@@ -337,28 +317,35 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
controller.getAttributeMetaData().getScale());
}
public static Number convertStringToNumber(String text, Object originalValue, DBSTypedObject type)
public static Number convertStringToNumber(DBDDataFormatter formatter, String text, Object originalValue, DBSTypedObject type)
{
if (text == null || text.length() == 0) {
return null;
}
try {
if (originalValue instanceof Long) {
return Long.valueOf(text);
} else if (originalValue instanceof Integer) {
return Integer.valueOf(text);
} else if (originalValue instanceof Short) {
return Short.valueOf(text);
} else if (originalValue instanceof Byte) {
return Byte.valueOf(text);
} else if (originalValue instanceof Float) {
return Float.valueOf(text);
} else if (originalValue instanceof Double) {
return Double.valueOf(text);
} else if (originalValue instanceof BigInteger) {
return new BigInteger(text);
} else if (originalValue instanceof BigDecimal) {
return new BigDecimal(text);
return (Number)formatter.parseValue(text);
} catch (ParseException e) {
log.debug("Can't parse numeric value using formatter", e);
}
try {
if (originalValue instanceof Number) {
if (originalValue instanceof Long) {
return Long.valueOf(text);
} else if (originalValue instanceof Integer) {
return Integer.valueOf(text);
} else if (originalValue instanceof Short) {
return Short.valueOf(text);
} else if (originalValue instanceof Byte) {
return Byte.valueOf(text);
} else if (originalValue instanceof Float) {
return Float.valueOf(text);
} else if (originalValue instanceof Double) {
return Double.valueOf(text);
} else if (originalValue instanceof BigInteger) {
return new BigInteger(text);
} else {
return new BigDecimal(text);
}
} else {
switch (type.getTypeID()) {
case java.sql.Types.BIGINT:
......@@ -386,7 +373,7 @@ public class JDBCNumberValueHandler extends JDBCAbstractValueHandler {
}
}
} catch (NumberFormatException e) {
log.error("Bad numeric value '" + text + "' - " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
log.debug("Bad numeric value '" + text + "' - " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
}
......
......@@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.SQLUtils;
import org.jkiss.dbeaver.model.data.DBDStructure;
import org.jkiss.dbeaver.model.data.DBDValue;
import org.jkiss.dbeaver.model.data.DBDValueCloneable;
......@@ -154,14 +155,8 @@ public class JDBCStruct implements DBDStructure, DBDValueCloneable {
str.append("NULL");
} else {
DBDValueHandler valueHandler = DBUtils.findValueHandler(type.getDataSource(), entry.getKey());
if (item instanceof Number) {
str.append(item);
} else {
String strValue = valueHandler.getValueDisplayString(entry.getKey(), item);
str.append('\'');
str.append(strValue);
str.append('\'');
}
String strValue = valueHandler.getValueDisplayString(entry.getKey(), item);
SQLUtils.appendValue(str, entry.getKey(), strValue);
}
i++;
}
......
......@@ -16,7 +16,7 @@
* 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.jdbc.api;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCExecutionContext;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.dbc;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.api;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.jkiss.dbeaver.model.data.DBDAttributeValue;
import org.jkiss.dbeaver.model.data.DBDDataFilter;
import org.jkiss.dbeaver.model.data.DBDDataReceiver;
import org.jkiss.dbeaver.model.exec.DBCDataRequest;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import java.util.List;
/**
* JDBC data request
*/
public abstract class JDBCDataRequest implements DBCDataRequest {
protected DBDDataReceiver dataReceiver;
protected DBDDataFilter dataFilter;
protected long firstRow;
protected long maxRows;
protected List<DBDAttributeValue> keys;
protected List<DBDAttributeValue> data;
protected long result;
protected StringBuilder script = new StringBuilder();
@Override
public void setDataReceiver(DBDDataReceiver dataReceiver)
{
this.dataReceiver = dataReceiver;
}
@Override
public void setDataFilter(DBDDataFilter dataFilter)
{
this.dataFilter = dataFilter;
}
@Override
public void setLimit(long firstRow, long maxRows)
{
this.firstRow = firstRow;
this.maxRows = maxRows;
}
@Override
public void setKeys(List<DBDAttributeValue> attributes)
{
this.keys = attributes;
}
@Override
public void setData(List<DBDAttributeValue> attributes)
{
this.data = attributes;
}
@Override
public long getResult()
{
return this.result;
}
@Override
public String generateScript(DBCExecutionContext context)
{
return this.script.toString();
}
}
......@@ -16,7 +16,7 @@
* 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.jdbc.api;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.api;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCExecutionContext;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.api;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCExecutionContext;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.api;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -26,7 +26,6 @@ import org.jkiss.dbeaver.model.exec.DBCResultSetMetaData;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCExecutionContext;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.impl.jdbc.dbc.JDBCResultSetMetaData;
import org.jkiss.dbeaver.model.qm.QMUtils;
import java.io.InputStream;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.dbc;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.exec.DBCAttributeMetaData;
......@@ -24,7 +24,6 @@ import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCResultSet;
import org.jkiss.dbeaver.model.exec.DBCResultSetMetaData;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.impl.jdbc.api.JDBCResultSetImpl;
import org.jkiss.dbeaver.model.struct.DBSDataSourceContainer;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.rdb.DBSTable;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.api;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.api;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.dbc;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.exec.DBCAttributeMetaData;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.dbc;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBPDataSource;
......
......@@ -16,7 +16,7 @@
* 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.jdbc.api;
package org.jkiss.dbeaver.model.impl.jdbc.exec;
import java.io.InputStream;
import java.io.Reader;
......
......@@ -23,6 +23,7 @@ import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.data.DBDAttributeValue;
import org.jkiss.dbeaver.model.data.DBDDataFilter;
import org.jkiss.dbeaver.model.data.DBDDataReceiver;
import org.jkiss.dbeaver.model.exec.DBCDataRequest;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import java.util.List;
......@@ -43,6 +44,8 @@ public interface DBSDataContainer extends DBSObject {
int getSupportedFeatures();
//DBCDataRequest makeDataRequest(DBCExecutionContext context, int type);
long readData(
DBCExecutionContext context,
DBDDataReceiver dataReceiver,
......
......@@ -258,7 +258,7 @@ public class RuntimeUtils {
if (valueType == null || CharSequence.class.isAssignableFrom(valueType)) {
return value;
} else if (valueType == Long.class) {
return new Long(value);
return Long.valueOf(value);
} else if (valueType == Long.TYPE) {
return Long.parseLong(value);
} else if (valueType == Integer.class) {
......@@ -266,19 +266,19 @@ public class RuntimeUtils {
} else if (valueType == Integer.TYPE) {
return Integer.parseInt(value);
} else if (valueType == Short.class) {
return new Short(value);
return Short.valueOf(value);
} else if (valueType == Short.TYPE) {
return Short.parseShort(value);
} else if (valueType == Byte.class) {
return new Byte(value);
return Byte.valueOf(value);
} else if (valueType == Byte.TYPE) {
return Byte.parseByte(value);
} else if (valueType == Double.class) {
return new Double(value);
return Double.valueOf(value);
} else if (valueType == Double.TYPE) {
return Double.parseDouble(value);
} else if (valueType == Float.class) {
return new Float(value);
return Float.valueOf(value);
} else if (valueType == Float.TYPE) {
return Float.parseFloat(value);
} else if (valueType == BigInteger.class) {
......
......@@ -59,6 +59,7 @@ import org.jkiss.utils.CommonUtils;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.text.DecimalFormatSymbols;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.*;
......@@ -73,7 +74,7 @@ public class UIUtils {
public static final char PARAGRAPH_CHAR = (char)182;
public static final VerifyListener INTEGER_VERIFY_LISTENER = new VerifyListener() {
private static final VerifyListener INTEGER_VERIFY_LISTENER = new VerifyListener() {
@Override
public void verifyText(VerifyEvent e)
{
......@@ -88,20 +89,38 @@ public class UIUtils {
}
};
public static final VerifyListener NUMBER_VERIFY_LISTENER = new VerifyListener() {
@Override
public void verifyText(VerifyEvent e)
{
for (int i = 0; i < e.text.length(); i++) {
char ch = e.text.charAt(i);
if (!Character.isDigit(ch) && ch != '.' && ch != '-' && ch != 'e' && ch != 'E') {
e.doit = false;
return;
public static VerifyListener getIntegerVerifyListener()
{
return INTEGER_VERIFY_LISTENER;
}
public static VerifyListener getNumberVerifyListener(Locale locale)
{
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
final char[] allowedChars = new char[] {
symbols.getDecimalSeparator(),
symbols.getGroupingSeparator(),
symbols.getMinusSign(),
symbols.getZeroDigit(),
symbols.getMonetaryDecimalSeparator(),
'+'
};
final String exponentSeparator = symbols.getExponentSeparator();
return new VerifyListener() {
@Override
public void verifyText(VerifyEvent e)
{
for (int i = 0; i < e.text.length(); i++) {
char ch = e.text.charAt(i);
if (!Character.isDigit(ch) && !CommonUtils.contains(allowedChars, ch) && exponentSeparator.indexOf(ch) == -1) {
e.doit = false;
return;
}
}
e.doit = true;
}
e.doit = true;
}
};
};
}
public static Object makeStringForUI(Object object)
{
......
......@@ -27,6 +27,7 @@ import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.utils.CommonUtils;
import java.math.BigDecimal;
import java.util.Locale;
/**
* Number cell editor
......@@ -52,9 +53,9 @@ public class CustomNumberCellEditor extends TextCellEditor {
valueType == Double.TYPE ||
valueType == BigDecimal.class)
{
text.addVerifyListener(UIUtils.NUMBER_VERIFY_LISTENER);
text.addVerifyListener(UIUtils.getNumberVerifyListener(Locale.getDefault()));
} else {
text.addVerifyListener(UIUtils.INTEGER_VERIFY_LISTENER);
text.addVerifyListener(UIUtils.getIntegerVerifyListener());
}
return text;
}
......
......@@ -3167,9 +3167,6 @@ public class ResultSetViewer extends Viewer implements IDataSourceProvider, ISpr
Object getValue(ResultSetViewer viewer, DBDAttributeBinding column, boolean useDefault)
{
Object value = viewer.curRows.get(viewer.getCurrentRow())[viewer.getMetaColumnIndex(column.getAttribute())];
if (value instanceof Number) {
return value.toString();
}
return column.getValueHandler().getValueDisplayString(column.getAttribute(), value);
}
},
......
......@@ -44,7 +44,7 @@ abstract class ViewValuePanel extends Composite {
gl.marginHeight = 0;
gl.marginWidth = 0;
setLayout(gl);
this.setBackground(this.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
//this.setBackground(this.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
this.setLayoutData(new GridData(GridData.FILL_BOTH));
Color infoBackground = getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
......
......@@ -509,17 +509,10 @@ public class Spreadsheet extends LightGrid implements Listener {
}
prevCol = pos.col;
}
// Make some formatting
// We don't want to use locale-specific formatter because numbers and dates
// become not valid for SQL queries
Object element = contentProvider.getElement(pos);
String text;
if (element instanceof Number) {
text = element.toString();
} else {
text = contentLabelProvider.getText(pos);
String text = contentLabelProvider.getText(pos);
if (text != null) {
tdt.append(text);
}
tdt.append(text == null ? "" : text);
}
if (tdt.length() > 0) {
TextTransfer textTransfer = TextTransfer.getInstance();
......
......@@ -133,7 +133,7 @@ public class CursorViewDialog extends ValueViewDialog implements ResultSetProvid
public int getSupportedFeatures()
{
// Nothing but plain read
return 0;
return DATA_SELECT;
}
@Override
......
......@@ -20,24 +20,22 @@
package org.jkiss.dbeaver.ui.dialogs.data;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.model.data.DBDValueController;
import org.jkiss.dbeaver.model.impl.jdbc.data.JDBCNumberValueHandler;
import org.jkiss.dbeaver.model.data.DBDValueEditor;
import org.jkiss.dbeaver.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.ui.UIUtils;
import java.sql.Types;
/**
* TextViewDialog
*/
public class NumberViewDialog extends ValueViewDialog {
private Text textEdit;
private Combo bitEdit;
private boolean isBoolean = false;
private Object value;
private DBDValueEditor panelEditor;
public NumberViewDialog(DBDValueController valueController) {
super(valueController);
......@@ -46,95 +44,37 @@ public class NumberViewDialog extends ValueViewDialog {
@Override
protected Control createDialogArea(Composite parent)
{
DBDValueController valueController = getValueController();
value = valueController.getValue();
Composite dialogGroup = (Composite)super.createDialogArea(parent);
Label label = new Label(dialogGroup, SWT.NONE);
label.setText(CoreMessages.dialog_data_label_value);
int style = SWT.BORDER;
if (getValueController().isReadOnly()) {
style |= SWT.READ_ONLY;
}
int valueType = getValueController().getAttributeMetaData().getTypeID();
if (valueType == Types.BIT || valueType == Types.BOOLEAN) {
// Bit (boolean)
style |= SWT.READ_ONLY;
bitEdit = new Combo(dialogGroup, style);
if (valueType == Types.BOOLEAN || value instanceof Boolean) {
isBoolean = true;
bitEdit.add("FALSE");
bitEdit.add("TRUE");
Boolean boolValue = ((Boolean) value);
bitEdit.select(Boolean.TRUE.equals(boolValue) ? 1 : 0);
} else {
bitEdit.add("0 (FALSE)");
bitEdit.add("1 (TRUE)");
int intValue = ((Number) value).intValue();
bitEdit.select(intValue == 0 ? 0 : 1);
}
GridData gd = new GridData(GridData.FILL_BOTH);
gd.widthHint = 50;
gd.grabExcessVerticalSpace = true;
bitEdit.setLayoutData(gd);
bitEdit.setFocus();
bitEdit.setEnabled(!getValueController().isReadOnly());
} else {
// Numbers
textEdit = new Text(dialogGroup, style);
switch (getValueController().getAttributeMetaData().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:
textEdit.addVerifyListener(UIUtils.INTEGER_VERIFY_LISTENER);
break;
default:
textEdit.addVerifyListener(UIUtils.NUMBER_VERIFY_LISTENER);
break;
}
if (value != null) {
// Use simple toString() because we don't want formatted values.
textEdit.setText(value.toString());
}
int maxSize = getValueController().getAttributeMetaData().getPrecision();
if (maxSize > 0) {
textEdit.setTextLimit(maxSize + 2);
}
textEdit.setBackground(getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.widthHint = 300;
//gd.grabExcessVerticalSpace = true;
textEdit.setLayoutData(gd);
textEdit.setFocus();
textEdit.setEditable(!getValueController().isReadOnly());
Composite editorPlaceholder = UIUtils.createPlaceholder(dialogGroup, 1);
editorPlaceholder.setLayoutData(new GridData(GridData.FILL_BOTH));
editorPlaceholder.setLayout(new FillLayout());
if (super.isForeignKey()) {
super.createEditorSelector(dialogGroup, textEdit);
}
try {
panelEditor = createPanelEditor(editorPlaceholder);
panelEditor.refreshValue();
} catch (DBException e) {
log.error(e);
return dialogGroup;
}
if (super.isForeignKey() && panelEditor.getControl() instanceof Text) {
super.createEditorSelector(dialogGroup, (Text)panelEditor.getControl());
}
return dialogGroup;
}
@Override
protected Object getEditorValue()
{
if (textEdit != null) {
return JDBCNumberValueHandler.convertStringToNumber(
textEdit.getText(),
value,
getValueController().getAttributeMetaData());
} else if (bitEdit != null) {
if (isBoolean) {
return bitEdit.getSelectionIndex() == 0 ? Boolean.FALSE : Boolean.TRUE;
} else {
return (byte)bitEdit.getSelectionIndex();
}
} else {
try {
return panelEditor.extractValue(VoidProgressMonitor.INSTANCE);
} catch (DBException e) {
// NEver be here
log.error(e);
return null;
}
}
......@@ -142,7 +82,6 @@ public class NumberViewDialog extends ValueViewDialog {
@Override
public void refreshValue()
{
value = getValueController().getValue();
textEdit.setText(value.toString());
panelEditor.refreshValue();
}
}
\ No newline at end of file
......@@ -81,12 +81,14 @@ public class TextViewDialog extends ValueViewDialog {
boolean readOnly = getValueController().isReadOnly();
boolean useHex = !isForeignKey;
long maxSize = getValueController().getAttributeMetaData().getMaxLength();
editorContainer = new CTabFolder(dialogGroup, SWT.FLAT | SWT.TOP);
editorContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
if (useHex) {
editorContainer = new CTabFolder(dialogGroup, SWT.FLAT | SWT.TOP);
editorContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
lengthLabel = new Label(editorContainer, SWT.RIGHT);
lengthLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
editorContainer.setTopRight(lengthLabel, SWT.FILL);
lengthLabel = new Label(editorContainer, SWT.RIGHT);
lengthLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
editorContainer.setTopRight(lengthLabel, SWT.FILL);
}
int selectedType = 0;
if (getDialogSettings().get(VALUE_TYPE_SELECTOR) != null) {
......@@ -97,7 +99,7 @@ public class TextViewDialog extends ValueViewDialog {
if (readOnly) {
style |= SWT.READ_ONLY;
}
textEdit = new Text(editorContainer, style);
textEdit = new Text(useHex ? editorContainer : dialogGroup, style);
textEdit.setText(stringValue);
if (maxSize > 0) {
......@@ -121,10 +123,12 @@ public class TextViewDialog extends ValueViewDialog {
}
});
CTabItem item = new CTabItem(editorContainer, SWT.NO_FOCUS);
item.setText("Text");
item.setImage(DBIcon.TYPE_TEXT.getImage());
item.setControl(textEdit);
if (useHex) {
CTabItem item = new CTabItem(editorContainer, SWT.NO_FOCUS);
item.setText("Text");
item.setImage(DBIcon.TYPE_TEXT.getImage());
item.setControl(textEdit);
}
}
Point minSize = null;
if (useHex) {
......@@ -141,16 +145,16 @@ public class TextViewDialog extends ValueViewDialog {
item.setText("Hex");
item.setImage(DBIcon.TYPE_BINARY.getImage());
item.setControl(hexEditControl);
}
if (selectedType >= editorContainer.getItemCount()) {
selectedType = 0;
}
editorContainer.setSelection(selectedType);
editorContainer.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event)
{
getDialogSettings().put(VALUE_TYPE_SELECTOR, editorContainer.getSelectionIndex());
if (selectedType >= editorContainer.getItemCount()) {
selectedType = 0;
}
editorContainer.setSelection(selectedType);
editorContainer.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event)
{
getDialogSettings().put(VALUE_TYPE_SELECTOR, editorContainer.getSelectionIndex());
/*
switch (editorContainer.getSelectionIndex()) {
case 0: {
......@@ -162,9 +166,10 @@ public class TextViewDialog extends ValueViewDialog {
break;
}
*/
}
});
updateValueLength();
}
});
updateValueLength();
}
if (isForeignKey) {
super.createEditorSelector(dialogGroup, textEdit);
......@@ -221,22 +226,20 @@ public class TextViewDialog extends ValueViewDialog {
@Override
protected Object getEditorValue()
{
switch (editorContainer.getSelectionIndex()) {
case 0:
return textEdit.getText();
case 1:
System.out.println(getBinaryContent());
return getBinaryContent();
default:
return null;
if (editorContainer == null || editorContainer.getSelectionIndex() == 0) {
return textEdit.getText();
} else {
return getBinaryContent();
}
}
private void updateValueLength()
{
long maxSize = getValueController().getAttributeMetaData().getMaxLength();
long length = textEdit.getText().length();
lengthLabel.setText("Length: " + length + (maxSize > 0 ? " [" + maxSize + "]" : ""));
if (lengthLabel != null) {
long maxSize = getValueController().getAttributeMetaData().getMaxLength();
long length = textEdit.getText().length();
lengthLabel.setText("Length: " + length + (maxSize > 0 ? " [" + maxSize + "]" : ""));
}
}
@Override
......
......@@ -36,11 +36,13 @@ import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.data.*;
import org.jkiss.dbeaver.model.exec.DBCAttributeMetaData;
......@@ -110,6 +112,92 @@ public abstract class ValueViewDialog extends Dialog implements DBDValueEditorEx
return getEnumerableConstraint() != null;
}
protected DBDValueEditor createPanelEditor(final Composite placeholder)
throws DBException
{
return valueController.getValueHandler().createEditor(new DBDValueController() {
@Override
public DBPDataSource getDataSource()
{
return valueController.getDataSource();
}
@Override
public DBSAttributeBase getAttributeMetaData()
{
return valueController.getAttributeMetaData();
}
@Override
public Object getValue()
{
return valueController.getValue();
}
@Override
public void updateValue(Object value)
{
valueController.updateValue(value);
}
@Override
public DBDValueHandler getValueHandler()
{
return valueController.getValueHandler();
}
@Override
public EditType getEditType()
{
return EditType.PANEL;
}
@Override
public boolean isReadOnly()
{
return valueController.isReadOnly();
}
@Override
public IWorkbenchPartSite getValueSite()
{
return valueController.getValueSite();
}
@Override
public Composite getEditPlaceholder()
{
return placeholder;
}
@Override
public ToolBar getEditToolBar()
{
return null;
}
@Override
public void closeInlineEditor()
{
}
@Override
public void nextInlineEditor(boolean next)
{
}
@Override
public void unregisterEditor(DBDValueEditorEx editor)
{
}
@Override
public void showMessage(String message, boolean error)
{
}
});
}
public DBDValueController getValueController() {
return valueController;
}
......@@ -214,6 +302,7 @@ public abstract class ValueViewDialog extends Dialog implements DBDValueEditorEx
shell.setSize(new Point(
Integer.parseInt(sizeString.substring(0, divPos)),
Integer.parseInt(sizeString.substring(divPos + 1))));
shell.layout();
}
Monitor primary = shell.getMonitor();
......@@ -223,7 +312,7 @@ public abstract class ValueViewDialog extends Dialog implements DBDValueEditorEx
int y = bounds.y + (bounds.height - rect.height) / 3;
x += dialogCount * 20;
y += dialogCount * 20;
shell.setLocation (x, y);
shell.setLocation(x, y);
}
private String getInfoVisiblePrefId()
......@@ -303,6 +392,12 @@ public abstract class ValueViewDialog extends Dialog implements DBDValueEditorEx
protected abstract Object getEditorValue();
@Override
public Control getControl()
{
return editor;
}
@Override
public Object extractValue(DBRProgressMonitor monitor) throws DBException
{
......@@ -401,11 +496,7 @@ public abstract class ValueViewDialog extends Dialog implements DBDValueEditorEx
if (selection != null && selection.length > 0) {
handleEditorChange = false;
Object value = selection[0].getData();
if (value instanceof Number) {
editor.setText(value.toString());
} else {
editor.setText(selection[0].getText());
}
editor.setText(selection[0].getText());
handleEditorChange = true;
}
}
......
......@@ -30,6 +30,7 @@ import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.*;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.IContentEditorPart;
......@@ -442,6 +443,12 @@ public class ContentEditor extends MultiPageAbstractEditor implements IDataSourc
return input == null ? null : input.getValueController();
}
@Override
public Control getControl()
{
return getControl(getActivePage());
}
@Override
public Object extractValue(DBRProgressMonitor monitor) throws DBException
{
......
......@@ -804,7 +804,7 @@ public class SQLEditor extends SQLEditorBase
@Override
public int getSupportedFeatures()
{
return 0;
return DATA_SELECT;
}
@Override
......
......@@ -787,7 +787,7 @@ public class DataSourceManagementToolbar implements DBPRegistryListener, DBPEven
}
//resultSetSize.setDigits(7);
resultSetSize.setLayoutData(gd);
resultSetSize.addVerifyListener(UIUtils.INTEGER_VERIFY_LISTENER);
resultSetSize.addVerifyListener(UIUtils.getIntegerVerifyListener());
resultSetSize.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e)
......
......@@ -134,7 +134,7 @@ public class MySQLConnectionPage extends ConnectionPageAdvanced
gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
gd.widthHint = 40;
portText.setLayoutData(gd);
portText.addVerifyListener(UIUtils.INTEGER_VERIFY_LISTENER);
portText.addVerifyListener(UIUtils.getIntegerVerifyListener());
portText.addModifyListener(textListener);
Label dbLabel = UIUtils.createControlLabel(addrGroup, MySQLMessages.dialog_connection_database);
......
......@@ -196,7 +196,7 @@ public class OracleConnectionPage extends ConnectionPageAdvanced
gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
gd.widthHint = 40;
portText.setLayoutData(gd);
portText.addVerifyListener(UIUtils.INTEGER_VERIFY_LISTENER);
portText.addVerifyListener(UIUtils.getIntegerVerifyListener());
portText.addModifyListener(controlModifyListener);
UIUtils.createControlLabel(targetContainer, OracleMessages.dialog_connection_database);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册