/*
 * Copyright 2015 Axel Howind (axel@dua3.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dua3.meja.model.generic;

import com.dua3.meja.model.BorderStyle;
import com.dua3.meja.model.CellStyle;
import com.dua3.meja.model.Direction;
import com.dua3.meja.model.FillPattern;
import com.dua3.meja.model.Font;
import com.dua3.meja.model.HAlign;
import com.dua3.meja.model.VAlign;
import java.awt.Color;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Axel Howind (axel@dua3.com)
 */
public class GenericCellStyle implements CellStyle {

    private static final BorderStyle defaultBorderStyle = new BorderStyle(0.0f, Color.BLACK);

    private final GenericWorkbook workbook;

    private Font font;
    private Color fillBgColor;
    private Color fillFgColor;
    private FillPattern fillPattern;
    private HAlign hAlign;
    private VAlign vAlign;
    private final BorderStyle[] borderStyle = new BorderStyle[Direction.values().length];
    private boolean wrap;
    private String dataFormat;

    // formatting helper
    transient private DateFormat dateFormatter = null;
    transient private NumberFormat numberFormatter = null;

    /**
     * Construct a new {@code GenericCellStyle}.
     * @param workbook the workbook the style is defined in
     */
    public GenericCellStyle(GenericWorkbook workbook) {
        this.workbook = workbook;
        this.font = new GenericFont();
        this.fillPattern = FillPattern.NONE;
        this.fillBgColor = Color.WHITE;
        this.fillFgColor = Color.WHITE;
        this.hAlign = HAlign.ALIGN_AUTOMATIC;
        this.vAlign = VAlign.ALIGN_MIDDLE;
        this.wrap = false;
        this.dataFormat = "";

        for (Direction d : Direction.values()) {
            borderStyle[d.ordinal()] = defaultBorderStyle;
        }
    }

    @Override
    public String getName() {
        return workbook.getCellStyleName(this);
    }

    @Override
    public Font getFont() {
        return font;
    }

    @Override
    public Color getFillBgColor() {
        return fillBgColor;
    }

    @Override
    public Color getFillFgColor() {
        return fillFgColor;
    }

    @Override
    public FillPattern getFillPattern() {
        return fillPattern;
    }

    @Override
    public HAlign getHAlign() {
        return hAlign;
    }

    @Override
    public VAlign getVAlign() {
        return vAlign;
    }

    @Override
    public BorderStyle getBorderStyle(Direction d) {
        return borderStyle[d.ordinal()];
    }

    @Override
    public boolean isWrap() {
        return wrap;
    }

    @Override
    public void setFont(Font font) {
        this.font = font;
    }

    @Override
    public void setHAlign(HAlign hAlign) {
        this.hAlign = hAlign;
    }

    @Override
    public void setVAlign(VAlign vAlign) {
        this.vAlign = vAlign;
    }

    @Override
    public void setWrap(boolean wrap) {
        this.wrap = wrap;
    }

    @Override
    public void setFillBgColor(Color fillBgColor) {
        this.fillBgColor = fillBgColor;
    }

    @Override
    public void setFillFgColor(Color fillFgColor) {
        this.fillFgColor = fillFgColor;
    }

    @Override
    public void setFillPattern(FillPattern fillPattern) {
        this.fillPattern = fillPattern;
    }

    @Override
    public void setBorderStyle(Direction d, BorderStyle borderStyle) {
        this.borderStyle[d.ordinal()] = borderStyle;
    }

    @Override
    public String getDataFormat() {
        return dataFormat;
    }

    @Override
    public void setDataFormat(String format) {
        this.dataFormat = format;
        this.dateFormatter = null;
        this.numberFormatter = null;
    }

    @Override
    public void copyStyle(CellStyle other) {
        setHAlign(other.getHAlign());
        setVAlign(other.getVAlign());
        for (Direction d : Direction.values()) {
            setBorderStyle(d, other.getBorderStyle(d));
        }
        setDataFormat(other.getDataFormat());
        setFillBgColor(other.getFillBgColor());
        setFillFgColor(other.getFillFgColor());
        setFillPattern(other.getFillPattern());
        setFont(other.getFont());
        setWrap(other.isWrap());
        setFont(new GenericFont(other.getFont()));
    }

    @Override
    public GenericWorkbook getWorkbook() {
        return workbook;
    }

    /**
     * Format date for oputput.
     * @param date the date to format
     * @return text representation of {@code date}
     */
    public String format(Date date) {
        if (dateFormatter==null) {
            try {
                dateFormatter = new SimpleDateFormat(dataFormat, workbook.getLocale());
            } catch (Exception e) {
                Logger.getLogger(GenericCellStyle.class.getName()).log(Level.WARNING, "Not a date pattern: ''{0}''", dataFormat);
                dateFormatter = SimpleDateFormat.getDateInstance();
            }
        }
        
        return dateFormatter.format(date);
    }

    /**
     * Format number for oputput.
     * @param n the number to format
     * @return text representation of {@code n}
     */
    public String format(Number n) {
        if (numberFormatter==null) {
            try {
                String fmt = dataFormat==null||dataFormat.isEmpty() ? "0.##########": dataFormat;
                numberFormatter = new DecimalFormat(fmt);
            } catch (Exception e) {
                Logger.getLogger(GenericCellStyle.class.getName()).log(Level.WARNING, "Not a number pattern: ''{0}''", dataFormat);
                numberFormatter = DecimalFormat.getInstance(workbook.getLocale());
                numberFormatter.setGroupingUsed(false);
            }
        }
        
        return numberFormatter.format(n);
    }

}

@@ -1707,16 +1707,21 @@
 taFormat
+ = %22%22
 ;%0A%0A    /
@@ -5256,32 +5256,231 @@
           try %7B%0A
+                if (dataFormat==null %7C%7C dataFormat.isEmpty()) %7B%0A                    dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, workbook.getLocale());%0A                %7D else %7B%0A    
                 
@@ -5543,32 +5543,50 @@
 k.getLocale());%0A
+                %7D%0A
             %7D ca

fix freshly created GenericCellStyle not displaying dates at all because of empty format string (use locale dependent default short date format in this case)