/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 * Other names may be trademarks of their respective owners.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * http://www.netbeans.org/cddl-gplv2.html
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 * specific language governing permissions and limitations under the
 * License.  When distributing the software, include this License Header
 * Notice in each file and include the License file at
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the GPL Version 2 section of the License file that
 * accompanied this code. If applicable, add the following below the
 * License Header, with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Contributor(s):
 *
 * The Original Software is NetBeans. The Initial Developer of the Original
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 * Microsystems, Inc. All Rights Reserved.
 *
 * If you wish your version of this file to be governed by only the CDDL
 * or only the GPL Version 2, indicate your decision by adding
 * "[Contributor] elects to include this software in this distribution
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 * single choice of license, a recipient has the option to distribute
 * your version of this file under either the CDDL, the GPL Version 2 or
 * to extend the choice of license to its licensees as provided above.
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 * Version 2 license, then the option applies only if the new code is
 * made subject to such option by the copyright holder.
 */
package org.openide.text;

import org.openide.options.ContextSystemOption;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import java.awt.Font;
import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

/** Settings for output window.
*
* @author Ales Novak
*/
public final class PrintSettings extends ContextSystemOption {
    // final because overrides Externalizable methods

    /** serialVersionUID */
    static final long serialVersionUID = -9102470021814206818L;

    /** Constant for center position of page header. */
    public static final int CENTER = 0x1;

    /** Constant for right position of page header. */
    public static final int RIGHT = 0x2;

    /** Constant for left position of page header. */
    public static final int LEFT = 0x0;

    /** Property name of the wrap property */
    public static final String PROP_PAGE_FORMAT = "pageFormat"; // NOI18N

    /** Property name of the wrap property */
    public static final String PROP_WRAP = "wrap"; // NOI18N

    /** Property name of the header format  property */
    public static final String PROP_HEADER_FORMAT = "headerFormat"; // NOI18N

    /** Property name of the footer format property */
    public static final String PROP_FOOTER_FORMAT = "footerFormat"; // NOI18N

    /** Property name of the header font property */
    public static final String PROP_HEADER_FONT = "headerFont"; // NOI18N

    /** Property name of the footer font property */
    public static final String PROP_FOOTER_FONT = "footerFont"; // NOI18N

    /** Property name of the header alignment property */
    public static final String PROP_HEADER_ALIGNMENT = "headerAlignment"; // NOI18N

    /** Property name of the footer alignment property */
    public static final String PROP_FOOTER_ALIGNMENT = "footerAlignment"; // NOI18N

    /** Property name of the line ascent correction property */
    public static final String PROP_LINE_ASCENT_CORRECTION = "lineAscentCorrection"; // NOI18N
    private static final String HELP_ID = "editing.printing"; // !!! NOI18N


    public String displayName() {
        return NbBundle.getMessage(PrintSettings.class, "CTL_Print_settings");
    }

    public HelpCtx getHelpCtx() {
        return new HelpCtx(HELP_ID);
    }

    /** @return an instance of PageFormat
    * The returned page format is either previously set by
    * PageSetupAction or is acquired as a default PageFormat
    * from supported PrinterJob
    */
    public static PageFormat getPageFormat(PrinterJob pj) {
        return PrintPreferences.getPageFormat(pj);
    }

    /** @deprecated Use {@link #getPageFormat(PrinterJob)} instead. */
    public PageFormat getPageFormat() {
        return getPageFormat(PrinterJob.getPrinterJob());
    }

    /** sets page format */
    public void setPageFormat(PageFormat pf) {
        PrintPreferences.setPageFormat(pf);
        firePropertyChange(PROP_PAGE_FORMAT, null, pf);
    }

    public boolean getWrap() {
        return PrintPreferences.getWrap();
    }

    public void setWrap(boolean b) {
        PrintPreferences.setWrap(b);
        firePropertyChange(PROP_WRAP, (b ? Boolean.FALSE : Boolean.TRUE), (b ? Boolean.TRUE : Boolean.FALSE));
    }

    public String getHeaderFormat() {
        return PrintPreferences.getHeaderFormat();
    }

    public void setHeaderFormat(String s) {
        PrintPreferences.setHeaderFormat(s);
        firePropertyChange(PROP_HEADER_FORMAT, null, s);
    }

    public String getFooterFormat() {
        return PrintPreferences.getFooterFormat();
    }

    public void setFooterFormat(String s) {
        PrintPreferences.setFooterFormat(s);
        firePropertyChange(PROP_FOOTER_FORMAT, null, s);
    }

    public Font getHeaderFont() {
        return PrintPreferences.getHeaderFont();
    }

    public void setHeaderFont(Font f) {
        PrintPreferences.setHeaderFont(f);
        firePropertyChange(PROP_HEADER_FONT, null, f);
    }

    public Font getFooterFont() {
        return PrintPreferences.getFooterFont();
    }

    public void setFooterFont(Font f) {
        PrintPreferences.setFooterFont(f);
        firePropertyChange(PROP_FOOTER_FONT, null, f);
    }

    public int getHeaderAlignment() {
        return fromAlignment(PrintPreferences.getHeaderAlignment());
    }

    public void setHeaderAlignment(int alignment) {
        PrintPreferences.setHeaderAlignment(toAlignment(alignment));
        firePropertyChange(PROP_HEADER_ALIGNMENT, null, new Integer(alignment));
    }
    
    private PrintPreferences.Alignment toAlignment(int alignment) {
        PrintPreferences.Alignment retval = PrintPreferences.Alignment.CENTER;
        switch(alignment) {
            case CENTER:
                retval = PrintPreferences.Alignment.CENTER;
                break;
            case LEFT:
                retval = PrintPreferences.Alignment.LEFT;
                break;
            case RIGHT:
                retval = PrintPreferences.Alignment.RIGHT;
                break;                            
        }
        return retval;
    }
    
    private int fromAlignment(PrintPreferences.Alignment alignment) {
        int retval = CENTER;
        if (PrintPreferences.Alignment.CENTER.equals(alignment)) {
            retval = CENTER;
        } else if (PrintPreferences.Alignment.LEFT.equals(alignment)) {
            retval = LEFT;
        } else if (PrintPreferences.Alignment.RIGHT.equals(alignment)) {
            retval = RIGHT;
        }
        return retval;
    }
    

    public int getFooterAlignment() {
        return fromAlignment(PrintPreferences.getFooterAlignment());
    }

    public void setFooterAlignment(int alignment) {
        PrintPreferences.setFooterAlignment(toAlignment(alignment));
        firePropertyChange(PROP_FOOTER_ALIGNMENT, null, new Integer(alignment));
    }

    /** Getter for lineAscentCorrection property. */
    public float getLineAscentCorrection() {
        return PrintPreferences.getLineAscentCorrection();
    }

    /** Setter for lineAscentCorrection property.
    * @param correction the correction
    * @exception IllegalArgumentException if <tt>correction</tt> is less than 0.
    */
    public void setLineAscentCorrection(float correction) {
        PrintPreferences.setLineAscentCorrection(correction);
        firePropertyChange(PROP_LINE_ASCENT_CORRECTION, null, new Float(correction));
    }

    public void writeExternal(ObjectOutput obtos) throws IOException {
        // no-op, nobody should be externalizing this option, but if they do
        // just do not store anything
    }
    
    public void readExternal(ObjectInput obtis) throws IOException, ClassNotFoundException {
        // no-op, to ignore previously serialized options
    }
    
    /** Property editor for alignment properties */
    public static class AlignmentEditor extends java.beans.PropertyEditorSupport {
        private String sCENTER;
        private String sRIGHT;
        private String sLEFT;
        private String[] tags = new String[] {
                sLEFT = NbBundle.getMessage(PrintSettings.class, "CTL_LEFT"),
                sCENTER = NbBundle.getMessage(PrintSettings.class, "CTL_CENTER"),
                sRIGHT = NbBundle.getMessage(PrintSettings.class, "CTL_RIGHT")
            };

        public String[] getTags() {
            return tags;
        }

        public String getAsText() {
            return tags[((Integer) getValue()).intValue()];
        }

        public void setAsText(String s) {
            if (s.equals(sLEFT)) {
                setValue(new Integer(0));
            } else if (s.equals(sCENTER)) {
                setValue(new Integer(1));
            } else {
                setValue(new Integer(2));
            }
        }
    }

    /** Property editor for PageFormat instances */
    public static class PageFormatEditor extends java.beans.PropertyEditorSupport {
        /** No text */
        public String getAsText() {
            return null;
        }

        /* @return <tt>true</tt> */
        public boolean supportsCustomEditor() {
            return true;
        }

        /**
        * @return <tt>null</tt> Shows pageDialog, however.
        */
        public java.awt.Component getCustomEditor() {
            PageFormat pf = (PageFormat) getValue();
            PrinterJob pj = PrinterJob.getPrinterJob();
            PageFormat npf = pj.pageDialog(pf);

            //setValue(npf);
            ((PrintSettings)PrintSettings.findObject(PrintSettings.class)).setPageFormat((PageFormat) npf.clone());
            pj.cancel();

            return null;
        }
    }
}
