All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jpedal.objects.javascript.functions.AFPercent Maven / Gradle / Ivy

There is a newer version: 7.15.25
Show newest version
/*
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.idrsolutions.com
 * Help section for developers at http://www.idrsolutions.com/support/
 *
 * (C) Copyright 1997-2017 IDRsolutions and Contributors.
 *
 * This file is part of JPedal/JPDF2HTML5
 *
 @LICENSE@
 *
 * ---------------
 * AFPercent.java
 * ---------------
 */
package org.jpedal.objects.javascript.functions;

import org.jpedal.objects.acroforms.AcroRenderer;
import org.jpedal.objects.acroforms.actions.ActionHandler;
import org.jpedal.objects.raw.FormObject;
import org.jpedal.sun.PrintfFormat;

public class AFPercent extends AFNumber {

    public AFPercent(final AcroRenderer acro, final FormObject formObject) {
        super(acro, formObject);
    }


    @Override
    public int execute(final String js, final String[] args, final int type, final int event, final char keyPressed) {

        int messageCode = ActionHandler.NOMESSAGE;

        if (args == null) {
            debug("Unknown implementation in " + js);

        } else if (args.length < 1) {
            debug("Values length is less than 1");
        } else {

            //settings
            int decCount = JSFunction.getStaticDecimalCount();
            if (decCount == -1) {
                decCount = Integer.parseInt(args[1]);
            }

            int gapFormat = JSFunction.getStaticGapFormat();
            if (gapFormat == -1) {
                gapFormat = Integer.parseInt(args[2]);
            }

            if (type == KEYSTROKE) {

                messageCode = validateNumber(type, event, decCount, gapFormat, 0, "", true);

            } else if (type == FORMAT) {

                //current form value
                String currentVal;

                currentVal = (String) formObject.getFormValue();

                float number = 0;
                String mask = "";

                if (currentVal != null && !currentVal.isEmpty()) {
                    final StringBuffer numValu = convertStringToNumber(currentVal, gapFormat);

                    //reset if no number or validate
                    if (numValu.length() > 0) {
                        number = Float.parseFloat(numValu.toString()) * 100;
                    }


                    mask = mask + '%' + gapFormat + '.' + decCount + 'f';

                    //apply mask and add % to end
                    currentVal = new PrintfFormat(mask).sprintf(number) + '%';

                } else {
                    currentVal = "";
                }

                //write back
                formObject.setLastValidValue(currentVal);
                formObject.updateValue(currentVal, false, true);


            } else {
                debug("Unknown type " + args[0] + " in " + js);
            }
        }

        return messageCode;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy