DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. A Custom Float PropertyEditor (Part 2)

A Custom Float PropertyEditor (Part 2)

Alied Pérez user avatar by
Alied Pérez
·
Mar. 03, 11 · Interview
Like (0)
Save
Tweet
Share
6.85K Views

Join the DZone community and get the full member experience.

Join For Free

In my last post I showed how to set a JSpinner as PropertyEditor for float properties and how to reuse it for different needs. Now I will further extend it to make the users' life a bit easier (yes, now and then, we should think about them ;-)). Let's keep our previous example of a 3D "bodies in space" application. So far, you can already set its dimensions and coordinates all around your universe. Now you also want to rotate it, say, on the XY plane (assuming Z-axis points upwards). How to do it is beyond this post; suffice to say that it (often) involves heavy trigonometrical calculations (and there are many libraries that handle it, if I may add). Anyway, these math libraries (e.g. java.lang.Math) commonly ask radians as parameters for their trigonometrical functions (and your application should do as well). However, again, think of the user who has to write "0.78539816339744830961566084581988" just to rotate 45º clockwise a bloody couch in a new living room plan. Not very likely I guess.

Let's now set a PropertyEditor so the user can rotate a couch in a more user-friendly way. Extend the class FloatPropertyEditor and override the functions getAsText() and setAsText(), as well as the class FloatInPlaceEditor with the functions getValue() and setValue(). These are where the degrees to radians and viceversa conversion takes place.

public class AngleFloatPropertyEditor extends FloatPropertyEditor {

    public AngleFloatPropertyEditor() {
        super(new SpinnerNumberModel(0.0f, 0.0f, 360.0f, 1.0f));
    }

    public AngleFloatPropertyEditor(SpinnerNumberModel model) {
        super(model);
    }

    @Override
    public InplaceEditor getInplaceEditor() {
        return new FloatInplaceEditor(model){

            @Override
            public Object getValue() {
                Float f = (Float)super.getValue();
                if(f == null)
                    f = Float.valueOf(0.0f);
                return new Float(Math.toRadians(f));
            }

            @Override
            public void setValue(Object object) {
                Float f = (Float)object;
                if(f == null)
                    f = Float.valueOf(0.0f);
                super.setValue(new Float(Math.toDegrees((Float)object)));
            }

        };
    }

    @Override
    public String getAsText() {
        Float f = (Float)getValue();
        if (f == null) {
            return "0.00";
        }
        return NumberFormat.getNumberInstance().format(Math.toDegrees(f.doubleValue()));
    }

    @Override
    public void setAsText(String s) {
        try {
            setValue(new Float(Math.toRadians(NumberFormat.getNumberInstance().parse(s).doubleValue())));
        } catch (ParseException ex) {
            setValue(Float.valueOf(0.0f));
        }
    }

}

You can now use radians internally, while your user can enter (and read) the angles in degrees. As always, you can extend the above code to fit your needs, like add a degree sign at the end. In a next post, I will show another way to extend FloatPropertyEditor to edit a different data type.

Thank  you very much if you've read this far.

Muchas gracias por leer hasta aquí.

Data Types

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • How to Secure Your CI/CD Pipeline
  • How To Use Terraform to Provision an AWS EC2 Instance
  • How Do the Docker Client and Docker Servers Work?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: