<commit_before>/*
 * Copyright 2015 <NAME> (<EMAIL>).
 *
 * 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.ui.swing;

import com.dua3.meja.model.Cell;
import com.dua3.meja.model.CellStyle;
import com.dua3.meja.model.CellType;
import com.dua3.meja.model.Font;
import com.dua3.meja.model.HAlign;
import com.dua3.meja.model.VAlign;
import com.dua3.meja.util.AttributedStringHelper;
import java.text.AttributedString;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextPane;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.BoxView;
import javax.swing.text.ComponentView;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.IconView;
import javax.swing.text.LabelView;
import javax.swing.text.ParagraphView;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

/**
 *
 * @author Axel Howind (<EMAIL>)
 */
public class CellEditorPane extends JTextPane {

    private static final long serialVersionUID = 1L;

    static java.awt.Font getAwtFont(Font font, float scale) {
        int style = (font.isBold() ? java.awt.Font.BOLD : 0) | (font.isItalic() ? java.awt.Font.ITALIC : 0);
        return new java.awt.Font(font.getFamily(), style, Math.round(scale * font.getSizeInPoints()));
    }

    /**
     * Translate {@code HALign.ALIGN_AUTOMATIC} to the actual value for the cell
     * type.
     *
     * @param hAlign the horizontal alignment
     * @param type the cell type
     * @return
     * <ul>
     * <li>{@code hAlign}, if {@code hAlign!=HAlign.ALIGN_AUTOMATIC}</li>
     * <li>otherwise the horizontal alignment to apply to cells of the given
     * type</li>
     * </ul>
     */
    public static HAlign getHAlign(HAlign hAlign, CellType type) {
        if (hAlign != HAlign.ALIGN_AUTOMATIC) {
            return hAlign;
        }
        
        switch (type) {
            case BLANK:
            case BOOLEAN:
            case ERROR:
            case NUMERIC:
            case DATE:
                return HAlign.ALIGN_RIGHT;
            case TEXT:
            case FORMULA:
                return HAlign.ALIGN_LEFT;
            default:
                throw new IllegalStateException();
        }
    }
    private VAlign vAlign = VAlign.ALIGN_TOP;

    public CellEditorPane() {
        setEditorKit(new CellEditorKit());
    }

    /**
     * Set the editor content to the content of given cell.
     *
     * @param cell the cell to display
     * @param scale the scale to apply
     * @param eval set to true to display formula results instead of the formula itself
     */
    public void setContent(Cell cell, float scale, boolean eval) {
        CellStyle cellStyle = cell.getCellStyle();
        final Font font = cellStyle.getFont();
        setFont(getAwtFont(font, scale));
        setBackground(cellStyle.getFillBgColor());
        setForeground(font.getColor());

        final StyledDocument doc;
        if (!eval && cell.getCellType() == CellType.FORMULA) {
            AttributeSet dfltAttr = getCellAttributes(cellStyle, cell);
            doc = new DefaultStyledDocument();
            try {
                doc.insertString(0, "="+cell.getFormula(), dfltAttr);
            } catch (BadLocationException ex) {
                Logger.getLogger(CellEditorPane.class.getName()).log(Level.SEVERE, "Exception", ex);
            }
        } else {
            // get attributed text first because this will update the
            // result type for formulas which is required for HAlign.ALIGN_AUTOMATIC
            final AttributedString text = cell.getAttributedString();
            AttributeSet dfltAttr = getCellAttributes(cellStyle, cell);
            doc = AttributedStringHelper.toStyledDocument(text, dfltAttr, scale);
        }
        setDocument(doc);

        this.vAlign = cellStyle.getVAlign();

        revalidate();
    }

    public SimpleAttributeSet getCellAttributes(final CellStyle cellStyle, Cell cell) throws IllegalStateException {
        SimpleAttributeSet dfltAttr = new SimpleAttributeSet();
        switch (getHAlign(cellStyle.getHAlign(), cell.getResultType())) {
            case ALIGN_LEFT:
                StyleConstants.setAlignment(dfltAttr, StyleConstants.ALIGN_LEFT);
                break;
            case ALIGN_CENTER:
                StyleConstants.setAlignment(dfltAttr, StyleConstants.ALIGN_CENTER);
                break;
            case ALIGN_RIGHT:
                StyleConstants.setAlignment(dfltAttr, StyleConstants.ALIGN_RIGHT);
                break;
            case ALIGN_JUSTIFY:
                StyleConstants.setAlignment(dfltAttr, StyleConstants.ALIGN_JUSTIFIED);
                break;
            case ALIGN_AUTOMATIC: // ALIGN_AUTOMATIC should already be resolved
            default:
                throw new IllegalStateException();
        }
<commit_msg>Remove duplicate code from CellEditorPane.setContent()<commit_after>/*
 * Copyright 2015 <NAME> (<EMAIL>).
 *
 * 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.ui.swing;

import com.dua3.meja.model.Cell;
import com.dua3.meja.model.CellStyle;
import com.dua3.meja.model.CellType;
import com.dua3.meja.model.Font;
import com.dua3.meja.model.HAlign;
import com.dua3.meja.model.VAlign;
import com.dua3.meja.util.AttributedStringHelper;
import java.text.AttributedString;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextPane;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.BoxView;
import javax.swing.text.ComponentView;
import javax.swing.text.Element;
import javax.swing.text.IconView;
import javax.swing.text.LabelView;
import javax.swing.text.ParagraphView;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

/**
 *
 * @author <NAME> (<EMAIL>)
 */
public class CellEditorPane extends JTextPane {

    private static final long serialVersionUID = 1L;

    static java.awt.Font getAwtFont(Font font, float scale) {
        int style = (font.isBold() ? java.awt.Font.BOLD : 0) | (font.isItalic() ? java.awt.Font.ITALIC : 0);
        return new java.awt.Font(font.getFamily(), style, Math.round(scale * font.getSizeInPoints()));
    }

    /**
     * Translate {@code HALign.ALIGN_AUTOMATIC} to the actual value for the cell
     * type.
     *
     * @param hAlign the horizontal alignment
     * @param type the cell type
     * @return
     * <ul>
     * <li>{@code hAlign}, if {@code hAlign!=HAlign.ALIGN_AUTOMATIC}</li>
     * <li>otherwise the horizontal alignment to apply to cells of the given
     * type</li>
     * </ul>
     */
    public static HAlign getHAlign(HAlign hAlign, CellType type) {
        if (hAlign != HAlign.ALIGN_AUTOMATIC) {
            return hAlign;
        }
        
        switch (type) {
            case BLANK:
            case BOOLEAN:
            case ERROR:
            case NUMERIC:
            case DATE:
                return HAlign.ALIGN_RIGHT;
            case TEXT:
            case FORMULA:
                return HAlign.ALIGN_LEFT;
            default:
                throw new IllegalStateException();
        }
    }
    private VAlign vAlign = VAlign.ALIGN_TOP;

    public CellEditorPane() {
        setEditorKit(new CellEditorKit());
    }

    /**
     * Set the editor content to the content of given cell.
     *
     * @param cell the cell to display
     * @param scale the scale to apply
     * @param eval set to true to display formula results instead of the formula itself
     */
    public void setContent(Cell cell, float scale, boolean eval) {        
        CellStyle cellStyle = cell.getCellStyle();
        final Font font = cellStyle.getFont();
        setFont(getAwtFont(font, scale));
        setBackground(cellStyle.getFillBgColor());
        setForeground(font.getColor());

        final AttributedString text;
        if (!eval && cell.getCellType() == CellType.FORMULA) {
            text = new AttributedString("="+cell.getFormula());
        } else {
            text = cell.getAttributedString();
        }
        
        AttributeSet dfltAttr = getCellAttributes(cellStyle, cell);
        setDocument(AttributedStringHelper.toStyledDocument(text, dfltAttr, scale));

        this.vAlign = cellStyle.getVAlign();

        revalidate();
        repaint();
    }

    public SimpleAttributeSet getCellAttributes(final CellStyle cellStyle, Cell cell) throws IllegalStateException {
        SimpleAttributeSet dfltAttr = new SimpleAttributeSet();
        switch (getHAlign(cellStyle.getHAlign(), cell.getResultType())) {
            case ALIGN_LEFT:
                StyleConstants.setAlignment(dfltAttr, StyleConstants.ALIGN_LEFT);
                break;
            case ALIGN_CENTER:
                StyleConstants.setAlignment(dfltAttr, StyleConstants.ALIGN_CENTER);
                break;
            case ALIGN_RIGHT:
                StyleConstants.setAlignment(dfltAttr, StyleConstants.ALIGN_RIGHT);
                break;
            case ALIGN_JUSTIFY:
                StyleConstants.setAlignment(dfltAttr, StyleConstants.ALIGN_JUSTIFIED);
                break;
            case ALIGN_AUTOMATIC: // ALIGN_AUTOMATIC should already be resolved
            default:
                throw new IllegalStateException();
        }

{'fraction_non_alphanumeric': 0.09388626032099452, 'fraction_numerical': 0.0036181463957695517, 'mean_word_length': 2.6320754716981134, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4147820', 'n_tokens_mistral': 3107, 'n_tokens_neox': 3033, 'n_words': 804}