Skip to main content

NumberMetaData questions

8 replies [Last post]
chicuslavic
Offline
Joined: 2005-02-26

1. what currency is doing here? I have kg
2. why not just do value.compareTo(maximum) ? in IntegerRangeValidator for example
3. why messages are hardcoded? where is localization?
4. what class should I use for bigDecimal?
5. why not use RangeValidator for all numeric type with value.compareTo? why inner class? why not a separate class for all Comparable ?

Slavic

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
chicuslavic
Offline
Joined: 2005-02-26

if you guide me I can write the code and test

I'm interested to see this working

chicuslavic
Offline
Joined: 2005-02-26

Composite MetaData
ex: 10.4 kg, 100.78 €
Usually we have 2 fields in the database: value, unit
we can have different unit for each row, for example invoice lines:
1. juice 2 l
2. orange 3 kg

so units are changing, same for currency (euro, usd, etc.) (price list for example)

so here any idea how to handle it?
probably it's a special swing widget

chicuslavic
Offline
Joined: 2005-02-26

MetaData - class between widgets and data
his job:
- field identifier
- informations necessary to build graphic representation of the field
- informations about state on the screen of the field
- informations and methods for transformation representation->object, object->representation
- validators of the field
informations:
- field identifier
__ name
- informations necessary to build graphic representation of the field
__ class type
__ label
__ field description, help (link with javaHelp?)
- informations and methods for transformation representation->object, object->representation
__ encode/decode methods (we don't do this in JFormatedText?)
- informations about state on the screen of the field
__ required/not required
__ editable/not editable
- validators of the field
__ list of validators (maybe a description for validator? like this we can see dans tooltip or status bar the constraints ex: "String: 53 chars, or Integer: 1

MetaData Types:
String, Boolean, Enumerare, Numeric (Integer, BigDecimal, Long, Short, Byte, Float, Double), Date (Date, Time, DateTime), Image, Link, ProprietaryType

Why we need different MetaData subclass? - have a default preconfigured MetaData?

String - dimension (x), multiline
Numeric - max/min value, (BigDecimal: fraction number, integer number)
Date - max/min value
Image - dimension (x, y)
Link - dimension
ProprietaryType - dimension

Validators:
isNull
range ( max, min, <=, >=)

I think for standard SQL applications it's good
I'm on the right way?

chicuslavic
Offline
Joined: 2005-02-26

1. maybe validators should have own package?

org.jdesktop.swing.data.validator
Validator
StringValidator
NumberValidator
BigDecimalValidator
when validator is called? on commiting edit? I'dont see that

2. about NumberMetaData it's not clear what it is for?
what information it should have?
for example number format representation?
field comment? (short description)

chicuslavic
Offline
Joined: 2005-02-26

Hi,

how I send files?

/*
* $Id: NumberMetaData.java,v 1.2 2004/09/08 23:36:49 aim Exp $
*
* Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
* Santa Clara, California 95054, U.S.A. All rights reserved.
*/

package org.jdesktop.swing.data;

import java.text.MessageFormat;
import java.util.Locale;

/**
*

* Class for representing meta-data for a numerical data field which is one of
* the following types:
*

    *
  • java.lang.Integer
  • *

  • java.lang.Long
  • *

  • java.lang.Short
  • *

  • java.lang.Float
  • *

  • java.lang.Double
  • *

* This meta-data class defines additional properties and edit constraints which
* are applicable to numerical values, such as minimum, maximum, whether or not
* the value is a currency, etc. Example usage:
*
*


 * NumberMetaData metaData = new NumberMetaData("interestrate", Float.class,
 * 		"Interest Rate");
 * metaData.setMinimum(new Float(4.5));
 * metaData.setMaximum(new Float(6.8));
 * 

*
* Setting a minimum and/or maximum constraint will implicitly cause a range
* validator to be added to the meta-data object.
*

*
* @author Amy Fowler
* @version 1.0
*/

public class NumberMetaData extends MetaData {

protected Number minimum = null;
protected Number maximum = null;
protected boolean currency = false;
private Validator rangeValidator = null;

/**
* Instantiates a meta-data object with a default name "numbervalue"
* and a default field class equal to java.lang.Integer. This
* provides the no-argument constructor required for JavaBeans. It is
* recommended that the program explicitly set a meaningful "name"
* property.
*/
public NumberMetaData() {
this("numbervalue");
}

/**
* Instantiates a meta-data object with the specified name and a default field
* class equal to java.lang.Integer.
*
* @param name
* String containing the name of the data field
*/
public NumberMetaData(String name) {
super(name);
this.klass = Integer.class;
}

/**
* Instantiates a meta-data object with the specified name and field class.
*
* @param name
* String containing the name of the data field
* @param klass
* Class indicating type of data field
*/
public NumberMetaData(String name, Class klass) {
this(name);
this.klass = klass;
}

/**
* Instantiates a meta-data object with the specified name, field class, and
* label.
*
* @param name
* String containing the name of the data field
* @param klass
* Class indicating type of data field
* @param label
* String containing the user-displayable label for the data field
*/
public NumberMetaData(String name, Class klass, String label) {
this(name, klass);
this.label = label;
}

/**
* Gets the meta-data "minimum" property which indicates the minimum
* value of the associated data field. The default is null,
* which indicates no minimum.
*
* @see #setMinimum
* @return Number containing the minimum value of the data field.
*/
public Number getMinimum() {
return minimum;
}

/**
* Sets the meta-data "minimum" property. Setting a minimum and/or
* maximum will cause an appropriate range validator object to be added to
* this meta-data object.
*
* @see #getMinimum
* @param minimum
* Number containing the minimum value of the data field.
* @throws IllegalArgumentException
* if the minimum object's class does not equal the meta-data's
* field class
*/
public void setMinimum(Number minimum) {
if (klass != minimum.getClass()) {
throw new IllegalArgumentException(new MessageFormat(
"{0}: minimum value is class {1} but should be {2}")
.format(new Object[] { getName(), minimum.getClass().getName(),
getClass().getName() }));
}
Number oldMinimum = this.minimum;
this.minimum = minimum;
setupRangeValidator();
firePropertyChange("minimum", oldMinimum, minimum);
}

/**
* Gets the meta-data "maximum" property which indicates the maximum
* value of the associated data field. The default is null,
* which indicates no maximum.
*
* @see #setMaximum
* @return Number containing the maximum value of the data field.
*/
public Number getMaximum() {
return maximum;
}

/**
* Sets the meta-data "maximum" property. Setting a minimum and/or
* maximum will cause an appropriate range validator object to be added to
* this meta-data object.
*
* @see #getMaximum
* @param maximum
* Number containing the maximum value of the data field.
* @throws IllegalArgumentException
* if the maximum object's class does not equal the meta-data's
* field class
*/
public void setMaximum(Number maximum) {
if (getElementClass() != maximum.getClass()) {
throw new IllegalArgumentException(new MessageFormat(
"{0}: maximum value is class {1} but should be {2}")
.format(new Object[] { getName(), maximum.getClass().getName(),
getClass().getName() }));
}
Number oldMaximum = this.maximum;
this.maximum = maximum;
setupRangeValidator();
firePropertyChange("maximum", oldMaximum, maximum);
}

/**
* Gets the meta-data "currency" property which indicates whether
* this data field represents a currency value. The default is
* false.
*
* @see #setCurrency
* @return boolean indicating whether the data field represents a currency
*/
public boolean isCurrency() {
return currency;
}

/**
* Sets the meta-data "currency" property.
*
* @see #isCurrency
* @param currency
* boolean indicating whether the data field represents a currency
*/
public void setCurrency(boolean currency) {
boolean oldCurrency = this.currency;
this.currency = currency;
firePropertyChange("currency", oldCurrency, currency);
}

private void setupRangeValidator() {
if (maximum != null || minimum != null) {
if (rangeValidator == null) {
rangeValidator = getRangeValidator();
addValidator(rangeValidator);
}
} else if (rangeValidator != null) {
removeValidator(rangeValidator);
rangeValidator = null;
}
}

private Validator getRangeValidator() {
if (klass.equals(Integer.class)) {
return new RangeValidator();
} else if (klass.equals(Long.class)) {
return new RangeValidator();
} else if (klass.equals(Float.class)) {
return new RangeValidator();
} else if (klass.equals(Short.class)) {
return new RangeValidator();
} else if (klass.equals(Double.class)) {
return new RangeValidator();
}
return null;
}

/**
* @todo aim: these could be exposed as public static Validator classes, but
* they would then have to become stateful.
*/
private class RangeValidator implements Validator {

public boolean validate(Object value, Locale locale, String[] error) {
if (value == null)
return true;
Comparable comparable = (Comparable) value;
if (comparable.compareTo(maximum) > 0) {
error[0] = new MessageFormat("{0}: value {1} exceeds maximum {2}")
.format(new Object[] { getName(), value, maximum });
return false;
}
if (comparable.compareTo(minimum) < 0) {
new MessageFormat("{0}: value {1} is less than the minimum {2}")
.format(new Object[] { getName(), value, minimum });
return false;
}
return true;
}
}
}

Kleopatra

jdnc-interest@javadesktop.org wrote:
>
> how I send files?

the place to put them is the jdnc-incubator: to get write access you
need to sign the JCA (bureacratics, sigh).

>
> /*
> * $Id: NumberMetaData.java,v 1.2 2004/09/08 23:36:49 aim Exp $
> *
> * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
> * Santa Clara, California 95054, U.S.A. All rights reserved.
> */
>
> package org.jdesktop.swing.data;

hmm, you seem to be working with a pre-reorg version (not that it makes
much of a difference in this case, there are not many changes concerning
the metaData) - the project is broken up into modules swingx,
databinding, jdnc (under the jdnc/src/* ). Sorry for the mess.

Thanks, I'll give it a closer look soon (after JavaOne ;-)

Jeanette

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

dhall
Offline
Joined: 2006-02-17

I've had a modification of NumberMetaData in the incubator for several months -- primarily to illustrate functor based validation.

In glancing at it, it seems to be a fairly short step from a more generic MetaData which would of course cover quite a lot more capability.

Dave

Amy Fowler

jdnc-interest@javadesktop.org wrote:

>1. what currency is doing here? I have kg
>2. why not just do value.compareTo(maximum) ? in IntegerRangeValidator for example
>3. why messages are hardcoded? where is localization?
>4. what class should I use for bigDecimal?
>5. why not use RangeValidator for all numeric type with value.compareTo? why inner class? why not a separate class for all Comparable ?
>
>
isCurrency() is there (dispite much internal team debate - everyone against
it except for me) because isCurrency() existed on
java.sql.ResultSetMetaData.
I agree that it would be preferable to provide a more generic way to
configure
the unit of measure for a numeric piece of data.

The other suggestions would all be nice improvements to what was the
first iteration of the class. Would you be interested in making the
changes
and contributing them back?

Aim

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net