Example usage for java.text NumberFormat getInstance

List of usage examples for java.text NumberFormat getInstance

Introduction

In this page you can find the example usage for java.text NumberFormat getInstance.

Prototype

public static NumberFormat getInstance(Locale inLocale) 

Source Link

Document

Returns a general-purpose number format for the specified locale.

Usage

From source file:dk.netarkivet.common.webinterface.HTMLUtils.java

/**
 * Create a localized string representation of the given long.
 *
 * @param i A long integer/*from  w  w w  . java2 s  .c  o m*/
 * @param context The given JSP context
 * @return a localized string representation of the given long TODO Should this method throw ArgumentNotValid if the
 * context is null
 */
public static String localiseLong(long i, PageContext context) {
    NumberFormat nf = NumberFormat.getInstance(HTMLUtils.getLocaleObject(context));
    return nf.format(i);
}

From source file:dk.netarkivet.common.webinterface.HTMLUtils.java

/**
 * Create a localized string representation of the given long.
 *
 * @param i A long integer/*from  w  w  w  .j  a va  2s. c  o  m*/
 * @param locale The given locale.
 * @return a localized string representation of the given long TODO Should this method throw ArgumentNotValid if the
 * locale is null
 */
public static String localiseLong(long i, Locale locale) {
    NumberFormat nf = NumberFormat.getInstance(locale);
    return nf.format(i);
}

From source file:dk.netarkivet.common.webinterface.HTMLUtils.java

/**
 * Parse a given String for a long value.
 *
 * @param loc The given Locale.//from www . j a  v  a 2 s. com
 * @param paramValue The given parameter value
 * @param parameterName The given parameter name (used for debugging)
 * @param defaultValue The default value for the parameter, in case the string cannot be parsed
 * @return the long value found in the paramValue
 */
public static Long parseLong(Locale loc, String paramValue, String parameterName, Long defaultValue) {
    ArgumentNotValid.checkNotNull(loc, "Locale loc");
    ArgumentNotValid.checkNotNullOrEmpty(parameterName, "String parameterName");

    if (paramValue != null && paramValue.trim().length() > 0) {
        paramValue = paramValue.trim();
        try {
            return NumberFormat.getInstance(loc).parse(paramValue).longValue();
        } catch (ParseException e) {
            throw new ForwardedToErrorPage(
                    "Invalid value " + paramValue + " for integer parameter '" + parameterName + "'", e);
        }
    } else {
        return defaultValue;
    }
}

From source file:io.hummer.util.test.GenericTestResult.java

public String getPlottableAverages3D(List<Integer> levels, String[] keyTemplates, ResultType type,
        String... additionalCommands) {
    StringBuilder b = new StringBuilder();
    NumberFormat f = NumberFormat.getInstance(Locale.US);
    f.setMinimumFractionDigits(3);/*from   w ww  .  jav  a  2 s .c om*/
    f.setMaximumFractionDigits(3);
    f.setGroupingUsed(false);
    for (int l : levels) {
        for (String t : keyTemplates) {
            String key = t.replaceAll("<level>", "" + l);
            Double val = 0.0;
            if (type == ResultType.THROUGHPUT)
                val = getThroughput(key);
            else if (type == ResultType.MEAN)
                val = getMean(key);
            if (val.isNaN())
                val = 0.0;
            b.append(f.format(val));
            b.append("\n");
        }
        b.append("\n");
    }
    return b.toString();
}

From source file:com.github.dougkelly88.FLIMPlateReaderGUI.SequencingClasses.GUIComponents.XYSequencing.java

private void genZStackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_genZStackButtonActionPerformed
    //TODO: generate new FOV in current position if FOV table is empty. 
    //TODO: /* ww w .  j  a v  a  2s  .  c  om*/

    double startUm = -1.0;
    double endUm = -1.0;
    double stepUm = 1.0;

    String um = "(" + "\u00B5" + "m)";
    JLabel messageLabel = new JLabel("<html>Please enter start, end and delta values for Z stack: </html>");
    JLabel startLabel = new JLabel("Start Z position " + um + ":");
    JLabel endLabel = new JLabel("End Z position " + um + ":");
    JLabel stepLabel = new JLabel("Step size " + um + ":");

    // Uses a custom NumberFormat.
    NumberFormat customFormat = NumberFormat.getInstance(new Locale("en_US"));
    JFormattedTextField customFormatField = new JFormattedTextField(new NumberFormatter(customFormat));

    JFormattedTextField startField = new JFormattedTextField(customFormat);
    startField.setValue(-3.0);
    JFormattedTextField endField = new JFormattedTextField(customFormat);
    endField.setValue(3.0);
    JFormattedTextField stepField = new JFormattedTextField(customFormat);
    stepField.setValue(1.0);

    JPanel zStackDialog = new JPanel();
    zStackDialog.setLayout(new BorderLayout(50, 100));

    JPanel controlsPanel = new JPanel();
    controlsPanel.setLayout(new GridLayout(3, 2, 50, 10));

    controlsPanel.add(startLabel);
    controlsPanel.add(startField);
    controlsPanel.add(endLabel);
    controlsPanel.add(endField);
    controlsPanel.add(stepLabel);
    controlsPanel.add(stepField);

    zStackDialog.add(messageLabel, BorderLayout.PAGE_START);
    zStackDialog.add(controlsPanel, BorderLayout.CENTER);

    int result = JOptionPane.showConfirmDialog(this, zStackDialog, "Z stack setup",
            JOptionPane.OK_CANCEL_OPTION);
    if (result == JOptionPane.OK_OPTION) {

        // must be a better way to achieve this...?
        if (startField.getValue().getClass() == Double.class) {
            startUm = (Double) (startField.getValue());
        } else {
            startUm = ((Long) startField.getValue()).doubleValue();
        }

        if (endField.getValue().getClass() == Double.class) {
            endUm = (Double) (endField.getValue());
        } else {
            endUm = ((Long) endField.getValue()).doubleValue();
        }

        if (stepField.getValue().getClass() == Double.class) {
            stepUm = (Double) (stepField.getValue());
        } else {
            stepUm = ((Long) stepField.getValue()).doubleValue();
        }

        setZStackParams(startUm, endUm, stepUm);

        doZStackGeneration(getZStackParams());
    }

}

From source file:org.sakaiproject.sitestats.impl.ServerWideReportManagerImpl.java

private byte[] createToolAnalysisChart(int width, int height) {
    CategoryDataset dataset = getToolAnalysisDataSet();

    if (dataset == null) {
        return generateNoDataChart(width, height);
    }//from   w  ww  . ja  va  2s  .  c om

    JFreeChart chart = ChartFactory.createBarChart(null, // chart title
            null, // domain axis label
            null, // range axis label
            dataset, // data
            PlotOrientation.HORIZONTAL, // the plot orientation
            false, // legend
            false, // tooltips
            false // urls
    );

    // set background
    chart.setBackgroundPaint(parseColor(statsManager.getChartBackgroundColor()));

    // set chart border
    chart.setPadding(new RectangleInsets(10, 5, 5, 5));
    chart.setBorderVisible(true);
    chart.setBorderPaint(parseColor("#cccccc"));

    // set anti alias
    chart.setAntiAlias(true);

    CategoryPlot plot = (CategoryPlot) chart.getPlot();

    // set transparency
    plot.setForegroundAlpha(0.7f);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.white);

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setVisible(false);
    domainAxis.setUpperMargin(0);
    domainAxis.setLowerMargin(0);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setUpperMargin(0.20);
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator("{1}",
            NumberFormat.getInstance(new ResourceLoader().getLocale()));
    renderer.setBaseItemLabelGenerator(generator);
    renderer.setBaseItemLabelFont(new Font("SansSerif", Font.PLAIN, 9));
    renderer.setBaseItemLabelsVisible(true);
    renderer.setItemMargin(0);
    renderer.setSeriesPaint(0, Color.BLUE);

    BufferedImage img = chart.createBufferedImage(width, height);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ImageIO.write(img, "png", out);
    } catch (IOException e) {
        log.warn("Error occurred while generating SiteStats chart image data", e);
    }
    return out.toByteArray();
}

From source file:controllers.AnyplaceMapping.java

private static double calculateWeightOfConnection(String pois_a, String pois_b) throws DatasourceException {

    double lat_a = 0, lon_a = 0, lat_b = 0, lon_b = 0;

    NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);

    JsonNode pa = ProxyDataSource.getIDatasource().getFromKeyAsJson(pois_a);
    if (pa == null) {
        lat_a = 0.0;//from   www. ja  v  a 2 s .  com
        lon_a = 0.0;
    } else {
        // everything is ok
        try {
            lat_a = nf.parse(pa.path("coordinates_lat").textValue()).doubleValue();
            lon_a = nf.parse(pa.path("coordinates_lon").textValue()).doubleValue();
        } catch (ParseException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }
    }

    JsonNode pb = ProxyDataSource.getIDatasource().getFromKeyAsJson(pois_b);
    if (pb == null) {
        lat_b = 0.0;
        lon_b = 0.0;
    } else {
        // everything is ok
        try {
            lat_b = nf.parse(pb.path("coordinates_lat").textValue()).doubleValue();
            lon_b = nf.parse(pb.path("coordinates_lon").textValue()).doubleValue();
        } catch (ParseException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }
    }

    return GeoPoint.getDistanceBetweenPoints(lat_a, lon_a, lat_b, lon_b, "K");
}

From source file:net.sf.jasperreports.chartthemes.spring.GenericChartTheme.java

/**
 * Build and configure a meter chart./*from ww  w  . j  av a  2  s  . c  o  m*/
 */
protected JFreeChart createMeterChart() throws JRException {
    // Start by creating the plot that will hold the meter
    MeterPlot chartPlot = new MeterPlot((ValueDataset) getDataset());
    JRMeterPlot jrPlot = (JRMeterPlot) getPlot();

    // Set the shape
    MeterShapeEnum shape = jrPlot.getShapeValue() == null ? MeterShapeEnum.PIE : jrPlot.getShapeValue();

    switch (shape) {
    case CHORD:
        chartPlot.setDialShape(DialShape.CHORD);
        break;
    case CIRCLE:
        chartPlot.setDialShape(DialShape.CIRCLE);
        break;
    case DIAL:
        return createDialChart();
    case PIE:
    default:
        chartPlot.setDialShape(DialShape.PIE);
    }

    // Set the meter's range
    chartPlot.setRange(convertRange(jrPlot.getDataRange()));

    // Set the size of the meter
    int meterAngle = jrPlot.getMeterAngleInteger() == null ? 180 : jrPlot.getMeterAngleInteger();
    chartPlot.setMeterAngle(meterAngle);

    // Set the units - this is just a string that will be shown next to the
    // value
    String units = jrPlot.getUnits();
    if (units != null && units.length() > 0)
        chartPlot.setUnits(units);

    // Set the spacing between ticks.  I hate the name "tickSize" since to me it
    // implies I am changing the size of the tick, not the spacing between them.
    double tickInterval = jrPlot.getTickIntervalDouble() == null ? 10.0 : jrPlot.getTickIntervalDouble();
    chartPlot.setTickSize(tickInterval);

    // Set all the colors we support
    Color color = jrPlot.getMeterBackgroundColor();
    if (color != null)
        chartPlot.setDialBackgroundPaint(color);

    color = jrPlot.getNeedleColor();
    if (color != null)
        chartPlot.setNeedlePaint(color);

    JRFont tickLabelFont = jrPlot.getTickLabelFont();
    Integer defaultBaseFontSize = (Integer) getDefaultValue(defaultChartPropertiesMap,
            ChartThemesConstants.BASEFONT_SIZE);
    Font themeTickLabelFont = getFont(
            (JRFont) getDefaultValue(defaultPlotPropertiesMap, ChartThemesConstants.PLOT_TICK_LABEL_FONT),
            tickLabelFont, defaultBaseFontSize);
    chartPlot.setTickLabelFont(themeTickLabelFont);

    // localizing the default format, can be overridden by display.getMask()
    chartPlot.setTickLabelFormat(NumberFormat.getInstance(getLocale()));

    JRValueDisplay display = jrPlot.getValueDisplay();
    JRFont displayFont = display.getFont();
    Font themeDisplayFont = getFont(
            (JRFont) getDefaultValue(defaultPlotPropertiesMap, ChartThemesConstants.PLOT_DISPLAY_FONT),
            displayFont, defaultBaseFontSize);
    // Set how the value is displayed.
    if (display != null) {
        if (display.getColor() != null) {
            chartPlot.setValuePaint(display.getColor());
        }

        if (display.getMask() != null) {
            chartPlot.setTickLabelFormat(
                    new DecimalFormat(display.getMask(), DecimalFormatSymbols.getInstance(getLocale())));
        }
        if (display.getFont() != null) {
            chartPlot.setValueFont(themeDisplayFont);
        }

    }

    color = jrPlot.getTickColor();
    if (color != null)
        chartPlot.setTickPaint(color);

    // Now define all of the intervals, setting their range and color
    List<JRMeterInterval> intervals = jrPlot.getIntervals();
    if (intervals != null) {
        Iterator<JRMeterInterval> iter = intervals.iterator();
        while (iter.hasNext()) {
            JRMeterInterval interval = iter.next();
            if (interval != null)
                chartPlot.addInterval(convertInterval(interval));
        }
    }

    // Actually create the chart around the plot
    JFreeChart jfreeChart = new JFreeChart(evaluateTextExpression(getChart().getTitleExpression()), null,
            chartPlot, isShowLegend());

    // Set all the generic options
    configureChart(jfreeChart, getPlot());

    return jfreeChart;
}

From source file:net.sf.jasperreports.chartthemes.simple.SimpleChartTheme.java

/**
 * Build and configure a meter chart./*from  w w  w . jav a2 s  .  c  om*/
 */
protected JFreeChart createMeterChart() throws JRException {
    JRMeterPlot jrPlot = (JRMeterPlot) getPlot();

    // Start by creating the plot that will hold the meter
    MeterPlot chartPlot = new MeterPlot((ValueDataset) getDataset());

    // Set the shape
    MeterShapeEnum shape = jrPlot.getShapeValue() == null ? MeterShapeEnum.PIE : jrPlot.getShapeValue();
    if (shape == MeterShapeEnum.CHORD)
        chartPlot.setDialShape(DialShape.CHORD);
    else if (shape == MeterShapeEnum.CIRCLE)
        chartPlot.setDialShape(DialShape.CIRCLE);
    else if (shape == MeterShapeEnum.DIAL)
        return createDialChart();
    else
        chartPlot.setDialShape(DialShape.PIE);

    // Set the meter's range
    chartPlot.setRange(convertRange(jrPlot.getDataRange()));

    // Set the size of the meter
    int meterAngle = jrPlot.getMeterAngleInteger() == null ? 180 : jrPlot.getMeterAngleInteger();
    chartPlot.setMeterAngle(meterAngle);

    // Set the units - this is just a string that will be shown next to the
    // value
    String units = jrPlot.getUnits();
    if (units != null && units.length() > 0)
        chartPlot.setUnits(units);

    // Set the spacing between ticks.  I hate the name "tickSize" since to me it
    // implies I am changing the size of the tick, not the spacing between them.
    double tickInterval = jrPlot.getTickIntervalDouble() == null ? 10.0 : jrPlot.getTickIntervalDouble();
    chartPlot.setTickSize(tickInterval);

    // Set all the colors we support
    Color color = jrPlot.getMeterBackgroundColor();
    if (color != null)
        chartPlot.setDialBackgroundPaint(color);

    color = jrPlot.getNeedleColor();
    if (color != null)
        chartPlot.setNeedlePaint(color);

    JRBaseFont font = new JRBaseFont();
    FontUtil.copyNonNullOwnProperties(getPlotSettings().getTickLabelFont(), font);
    FontUtil.copyNonNullOwnProperties(jrPlot.getTickLabelFont(), font);
    font = new JRBaseFont(getChart(), font);
    chartPlot.setTickLabelFont(getFontUtil().getAwtFont(font, getLocale()));

    // localizing the default format, can be overridden by display.getMask()
    chartPlot.setTickLabelFormat(NumberFormat.getInstance(getLocale()));

    // Set how the value is displayed.
    JRValueDisplay display = jrPlot.getValueDisplay();
    if (display != null) {
        if (display.getColor() != null) {
            chartPlot.setValuePaint(display.getColor());
        }

        if (display.getMask() != null) {
            chartPlot.setTickLabelFormat(
                    new DecimalFormat(display.getMask(), DecimalFormatSymbols.getInstance(getLocale())));
        }

        font = new JRBaseFont();
        FontUtil.copyNonNullOwnProperties(getPlotSettings().getDisplayFont(), font);
        FontUtil.copyNonNullOwnProperties(jrPlot.getValueDisplay().getFont(), font);
        font = new JRBaseFont(getChart(), font);
        chartPlot.setValueFont(getFontUtil().getAwtFont(font, getLocale()));

    }

    color = jrPlot.getTickColor();
    if (color != null)
        chartPlot.setTickPaint(color);

    // Now define all of the intervals, setting their range and color
    List<JRMeterInterval> intervals = jrPlot.getIntervals();
    if (intervals != null) {
        Iterator<JRMeterInterval> iter = intervals.iterator();
        while (iter.hasNext()) {
            JRMeterInterval interval = iter.next();
            if (interval != null)
                chartPlot.addInterval(convertInterval(interval));
        }
    }

    // Actually create the chart around the plot
    JFreeChart jfreeChart = new JFreeChart(evaluateTextExpression(getChart().getTitleExpression()), null,
            chartPlot, isShowLegend());

    // Set all the generic options
    configureChart(jfreeChart, getPlot());

    return jfreeChart;
}

From source file:net.sf.jasperreports.engine.fill.DefaultChartTheme.java

/**
 *
 *///  ww  w  . j  ava2s .co  m
protected JFreeChart createMeterChart() throws JRException {
    JRMeterPlot jrPlot = (JRMeterPlot) getPlot();

    // Start by creating the plot that will hold the meter
    MeterPlot chartPlot = new MeterPlot((ValueDataset) getDataset());

    // Set the shape
    MeterShapeEnum shape = jrPlot.getShapeValue() == null ? MeterShapeEnum.PIE : jrPlot.getShapeValue();
    switch (shape) {
    case CHORD:
        chartPlot.setDialShape(DialShape.CHORD);
        break;
    case CIRCLE:
        chartPlot.setDialShape(DialShape.CIRCLE);
        break;
    case PIE:
    default:
        chartPlot.setDialShape(DialShape.PIE);
    }

    // Set the meter's range
    chartPlot.setRange(convertRange(jrPlot.getDataRange()));

    // Set the size of the meter
    int meterAngle = jrPlot.getMeterAngleInteger() == null ? 180 : jrPlot.getMeterAngleInteger();
    chartPlot.setMeterAngle(meterAngle);

    // Set the units - this is just a string that will be shown next to the
    // value
    String units = jrPlot.getUnits();
    if (units != null && units.length() > 0) {
        chartPlot.setUnits(units);
    }

    // Set the font used for tick labels
    if (jrPlot.getTickLabelFont() != null) {
        chartPlot.setTickLabelFont(fontUtil.getAwtFont(jrPlot.getTickLabelFont(), getLocale()));
    }

    // Set the spacing between ticks.  I hate the name "tickSize" since to me it
    // implies I am changing the size of the tick, not the spacing between them.
    double tickInterval = jrPlot.getTickIntervalDouble() == null ? 10.0 : jrPlot.getTickIntervalDouble();
    chartPlot.setTickSize(tickInterval);

    // Set all the colors we support
    Color color = jrPlot.getMeterBackgroundColor();
    if (color != null) {
        chartPlot.setDialBackgroundPaint(color);
    }

    color = jrPlot.getNeedleColor();
    if (color != null) {
        chartPlot.setNeedlePaint(color);
    }

    // localizing the default format, can be overridden by display.getMask()
    chartPlot.setTickLabelFormat(NumberFormat.getInstance(getLocale()));

    // Set how the value is displayed.
    JRValueDisplay display = jrPlot.getValueDisplay();
    if (display != null) {
        if (display.getColor() != null) {
            chartPlot.setValuePaint(display.getColor());
        }

        if (display.getMask() != null) {
            chartPlot.setTickLabelFormat(
                    new DecimalFormat(display.getMask(), DecimalFormatSymbols.getInstance(getLocale())));
        }

        if (display.getFont() != null) {
            chartPlot.setValueFont(fontUtil.getAwtFont(display.getFont(), getLocale()));
        }

    }

    color = jrPlot.getTickColor();
    if (color != null) {
        chartPlot.setTickPaint(color);
    }

    // Now define all of the intervals, setting their range and color
    List<JRMeterInterval> intervals = jrPlot.getIntervals();
    if (intervals != null) {
        Iterator<JRMeterInterval> iter = intervals.iterator();
        while (iter.hasNext()) {
            JRMeterInterval interval = iter.next();
            chartPlot.addInterval(convertInterval(interval));
        }
    }

    // Actually create the chart around the plot
    JFreeChart jfreeChart = new JFreeChart(evaluateTextExpression(getChart().getTitleExpression()), null,
            chartPlot, isShowLegend());

    // Set all the generic options
    configureChart(jfreeChart);

    return jfreeChart;
}