/*

 * Title: japlot.jaxodraw
 * Description: Graphical user interface for drawing Feynman diagrams
 * @author Daniele Binosi 
 * @author Lukas Theussl  

 /Copyright (C) 2003-2006,  Daniele Binosi and Lukas Theussl

 See the file LICENSE in the source distribution home directory
 for a full copy of the GPL (GNU General Public License).

    This file is part of japlot.jaxodraw.

    japlot.jaxodraw is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    japlot.jaxodraw is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 */
package japlot;


import japlot.jaxodraw.JaxoColor;
import japlot.jaxodraw.JaxoColorChooser;
import japlot.jaxodraw.JaxoPrefs;

import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.MemoryImageSource;

import java.util.ResourceBundle;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.JTabbedPane;

import jhplot.jadraw.JaAxes;
import jhplot.utils.GHFontChooser;


import jplot3d.JColor;
import jplot3d.Utils;

/** A panel to change the parameters of a box object. */
public class JaAxesOptionsPanel extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ResourceBundle language = ResourceBundle.getBundle(JaxoPrefs
			.getPref(JaxoPrefs.PREF_LANGUAGE));

	private final int X;
	private final int Y;
	private final int NAXES;

	private Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	private JaAxes theBox;
	private String sBox;
	private Icon theIcon;
	private ImageIcon colorPreviewIcon;
	private ImageIcon colorFillPreviewIcon;
	private JButton colorbt;
	private JButton colorFillbt;
	private JPanel colorPanel;
	private JPanel colorFillPanel;
	private TitledBorder colorPanelTitle;
	private TitledBorder colorFillPanelTitle;
	private Color newColor;
	private Color newFillColor;
	private Image colorPreview;
	private Image colorFillPreview;
	private int newXfin;
	private int newYfin;
	private int newWidth;
	private int newHeight;
	private int newRelWidth;
	private int newRelHeight;
	private int newXin;
	private int newYin;
	private float newStroke;
	private boolean changed = false;
	private JTabbedPane jtpMain;
	private JPanel jglobal;
	private JColor gridColor;
	private JCheckBox axesRange[];
	private JSpinner spnAxesMin[];
	private JSpinner spnAxesMax[];
        private JColor axesColor;

	final private Font fdefo = new Font("SansSerif", Font.BOLD, 14);
	private JCheckBox[] chkDisplay;
	private JCheckBox[] chkDisplayMirror;
	private JCheckBox[] chkDisplayGrid;
	private JCheckBox[] chkAuto;
	private JButton btnFontsAxes;
	private JSpinner[] spnLabelOffset;
	private JSpinner[] spnTickSize;
	private JSpinner[] spnTicks;
	private JSpinner[] spnSubTicks;
	private JSpinner[] spnSubTickSize;
	private SpinnerNumberModel modelXin;
	private SpinnerNumberModel modelXfin;

	private SpinnerNumberModel modelYfin;
	private SpinnerNumberModel modelYin;
	private SpinnerNumberModel modelstin;

       

	/**
	 * Constructor: pops up a panel that allows to change all the parameters of
	 * the box object.
	 * 
	 * @param box
	 *            The arc object that is edited.
	 */
	public JaAxesOptionsPanel(JaAxes box) {
		this.theBox = box;

		X = 0;
		Y = 1;
		NAXES = 2;
                // System.out.println("\nStart new JaAxesOptionsPanel"); 

		sBox = language.getString("Axes_parameters");
		theIcon = new ImageIcon(JaAxesOptionsPanel.class.getClassLoader()
				.getResource("japlot/jaxodraw/icons/box.png"));

		newColor = theBox.getColor();
		newFillColor = theBox.getFillColor();

		colorPreview = getChooserImage(newColor);
		colorFillPreview = getChooserImage(newFillColor);

		colorPreviewIcon = new ImageIcon(colorPreview);
		colorFillPreviewIcon = new ImageIcon(colorFillPreview);

		newWidth = theBox.getSize().width;
		newHeight = theBox.getSize().height;
		newRelWidth = theBox.getRelSize().width;
		newRelHeight = theBox.getRelSize().height;

		getXYCoord(theBox.getX(), theBox.getY(), newRelWidth, newRelHeight);

		newStroke = theBox.getStroke();

		// Dimension options
		JPanel pxin = new JPanel();
		pxin.setLayout(new GridLayout(0, 2));

		jtpMain = new JTabbedPane();
		jglobal = new JPanel();

		jtpMain.add("Global", jglobal);
		jtpMain.add("Axes", createAxisPane());
		jtpMain.add("Ranges", createAxisRange());

		jtpMain.setSelectedIndex(0);

		modelXin = new SpinnerNumberModel(newXin, 0, d.width, 1);
		JLabel xin = new JLabel("Xmin: ", JLabel.RIGHT);
		pxin.add(xin);

		JSpinner spxin = new JSpinner(modelXin);
		pxin.add(spxin);

		JPanel pyin = new JPanel();
		pyin.setLayout(new GridLayout(0, 2));

		modelYin = new SpinnerNumberModel(newYin, 0, d.height, 1);
		JLabel yin = new JLabel("Ymin: ", JLabel.RIGHT);
		pyin.add(yin);

		JSpinner spyin = new JSpinner(modelYin);
		pyin.add(spyin);

		JPanel pxfin = new JPanel();
		pxfin.setLayout(new GridLayout(0, 2));

		modelXfin = new SpinnerNumberModel(newXfin, 0, d.width, 1);
		JLabel xfin = new JLabel("Xmax: ", JLabel.RIGHT);
		pxfin.add(xfin);

		JSpinner spxfin = new JSpinner(modelXfin);
		pxfin.add(spxfin);

		JPanel pyfin = new JPanel();
		pyfin.setLayout(new GridLayout(0, 2));

		modelYfin = new SpinnerNumberModel(newYfin, 0, d.height, 1);
		JLabel yfin = new JLabel("Ymax: ", JLabel.RIGHT);
		pyfin.add(yfin);

		JSpinner spyfin = new JSpinner(modelYfin);
		pyfin.add(spyfin);

		JPanel dimSPanel = new JPanel();
		GridBagLayout gb = new GridBagLayout();
		GridBagConstraints sc = new GridBagConstraints();
		dimSPanel.setLayout(gb);

		sc.gridx = 0;
		sc.gridy = 0;
		sc.insets = new Insets(0, 0, 0, 5);
		pxin.setPreferredSize(new Dimension(120, 20));
		gb.setConstraints(pxin, sc);
		dimSPanel.add(pxin, sc);

		sc.gridx = 0;
		sc.gridy = 1;
		sc.insets = new Insets(5, 0, 5, 5);
		pyin.setPreferredSize(new Dimension(120, 20));
		gb.setConstraints(pyin, sc);
		dimSPanel.add(pyin, sc);

		sc.gridx = 1;
		sc.gridy = 0;
		sc.insets = new Insets(0, 5, 0, 5);
		pxfin.setPreferredSize(new Dimension(120, 20));
		gb.setConstraints(pxfin, sc);
		dimSPanel.add(pxfin, sc);

		sc.gridx = 1;
		sc.gridy = 1;
		sc.insets = new Insets(5, 5, 5, 5);
		pyfin.setPreferredSize(new Dimension(120, 20));
		gb.setConstraints(pyfin, sc);
		dimSPanel.add(pyfin, sc);

		JPanel finalDimSPanel = new JPanel();
		finalDimSPanel
				.setLayout(new BoxLayout(finalDimSPanel, BoxLayout.X_AXIS));

		TitledBorder dimSTitle = BorderFactory.createTitledBorder(language
				.getString("Position/Size"));
		finalDimSPanel.setBorder(dimSTitle);
		finalDimSPanel.add(dimSPanel);

		// Stroke options
		JPanel pstin = new JPanel();
		GridBagLayout subgb = new GridBagLayout();
		GridBagConstraints subc = new GridBagConstraints();
		pstin.setLayout(subgb);

		modelstin = new SpinnerNumberModel(newStroke, 0.0, 100.0, 0.2);
		JLabel stin = new JLabel(language.getString("Line_Width"), JLabel.RIGHT);
		JSpinner spstin = new JSpinner(modelstin);

		subc.gridx = 0;
		subc.gridy = 0;
		subc.insets = new Insets(0, 5, 5, 5);
		subgb.setConstraints(stin, subc);
		pstin.add(stin, subc);

		subc.gridx = 1;
		subc.gridy = 0;
		spstin.setPreferredSize(new Dimension(50, 20));
		subgb.setConstraints(spstin, subc);
		pstin.add(spstin, subc);

		JPanel strSPanel = new JPanel();
		GridBagLayout gb1 = new GridBagLayout();
		GridBagConstraints sc1 = new GridBagConstraints();
		strSPanel.setLayout(gb1);

		sc1.gridx = 0;
		sc1.gridy = 0;
		gb1.setConstraints(pstin, sc1);
		strSPanel.add(pstin, sc1);

		JPanel finalStrSPanel = new JPanel();
		finalStrSPanel
				.setLayout(new BoxLayout(finalStrSPanel, BoxLayout.X_AXIS));

		TitledBorder strSTitle = BorderFactory.createTitledBorder(language
				.getString("Width"));
		finalStrSPanel.setBorder(strSTitle);
		finalStrSPanel.add(strSPanel);

		// Color options
		colorbt = new JButton(colorPreviewIcon);
		colorbt.addActionListener(this);
		colorPanel = new JPanel();
		colorPanelTitle = BorderFactory.createTitledBorder(language
				.getString("Line_color"));
		colorPanel.add(colorbt);

		if (JaxoColor.isGrayScale(newFillColor)) {
			colorPanelTitle.setTitleColor(JaxoColor.GRAYSCALE165);
			colorbt.setEnabled(false);
		}
		colorPanel.setBorder(colorPanelTitle);

		colorFillbt = new JButton(colorFillPreviewIcon);
		colorFillbt.addActionListener(this);
		colorFillPanel = new JPanel();
		colorFillPanelTitle = BorderFactory.createTitledBorder(language
				.getString("Fill_color"));
		colorFillPanel.setBorder(colorFillPanelTitle);
		colorFillPanel.add(colorFillbt);

		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();

		jglobal.setLayout(gridbag);
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(0, 0, 5, 5);
		gridbag.setConstraints(finalDimSPanel, c);
		jglobal.add(finalDimSPanel, c);

		c.gridx = 0;
		c.gridy = 1;
		c.insets = new Insets(5, 0, 0, 5);
		gridbag.setConstraints(finalStrSPanel, c);
		jglobal.add(finalStrSPanel, c);

		c.gridx = 1;
		c.gridy = 0;
		c.insets = new Insets(0, 5, 5, 0);
		colorPanel.setPreferredSize(new Dimension(100, 70));
		gridbag.setConstraints(colorPanel, c);
		jglobal.add(colorPanel, c);

		c.gridx = 1;
		c.gridy = 1;
		c.insets = new Insets(5, 5, 0, 0);
		colorFillPanel.setPreferredSize(new Dimension(100, 70));
		gridbag.setConstraints(colorFillPanel, c);
		jglobal.add(colorFillPanel, c);

		add(jtpMain);

		String[] optionButtonNames = { language.getString("Accept"),
				language.getString("Cancel") };
		int n = JOptionPane.showOptionDialog(this, this, sBox,
				JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE,
				theIcon, optionButtonNames, optionButtonNames[0]);

		if (n == JOptionPane.OK_OPTION) {
			setAxesParameters();
		}
	}

	/**
	 * Indicates whether the user actually changed any of the parameters.
	 * 
	 * @return True if any of the properties have been changed.
	 */
	public final boolean hasChanged() {
		return changed;
	}

	/**
	 * Checks for ActionEvents that have ocurred on the panel, presently only
	 * whether one of the color buttons has been pressed in which case a new
	 * color chooser panel is popped up.
	 * 
	 * @param evt
	 *            The ActionEvent that has ocurred.
	 */
	public final void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == colorbt) {
			JaxoColorChooser colorChooser = new JaxoColorChooser();
			newColor = colorChooser.color(newColor, false);

			if (newColor != null) {
				colorPreviewIcon = new ImageIcon(getChooserImage(newColor));
				colorbt.setIcon(colorPreviewIcon);
			}
		}

		if (evt.getSource() == colorFillbt) {
			JaxoColorChooser colorChooser = new JaxoColorChooser();
			newFillColor = colorChooser.color(newFillColor, true);

			if (newFillColor != null) {
				colorFillPreviewIcon = new ImageIcon(
						getChooserImage(newFillColor));
				colorFillbt.setIcon(colorFillPreviewIcon);
				colorPanelTitle = BorderFactory.createTitledBorder(language
						.getString("Line_color"));
				colorbt.setEnabled(true);
				if (JaxoColor.isGrayScale(newFillColor)) {
					colorPanelTitle.setTitleColor(JaxoColor.GRAYSCALE165);
					colorbt.setEnabled(false);
				}
				colorPanel.setBorder(colorPanelTitle);
			}
		}
	}

	// ///////////////////////////////////////////////////////////////////////
	//
	// private auxiliary methods //
	//
	// ///////////////////////////////////////////////////////////////////////
	private void getXYCoord(int x, int y, int relw, int relh) {
		if (((relw > 0) && (relh >= 0)) || ((relw < 0) && (relh <= 0))) {
			newXin = x;
			newYin = y;
			newXfin = x + Math.abs(relw);
			newYfin = y + Math.abs(relh);
		}

		if (((relw >= 0) && (relh < 0)) || ((relw <= 0) && (relh > 0))) {
			newXin = x;
			newYin = y + Math.abs(relh);
			newXfin = x + Math.abs(relw);
			newYfin = y;
		}
	}

	private Image getChooserImage(Color iconColor) {
		MemoryImageSource source;
		int[] pixels;
		int width = 40;
		int height = 15;
		int size = width * height;
		pixels = new int[size];

		for (int i = 0; i < size; i++) {
			pixels[i] = iconColor.getRGB();
		}

		source = new MemoryImageSource(width, height, pixels, 0, width);

		Image jaxColorBox = createImage(source);

		return jaxColorBox;
	}

	private void setNewBox(int xin, int yin, int xfin, int yfin) {
		newWidth = Math.abs(xfin - xin);
		newHeight = Math.abs(yfin - yin);
		newRelWidth = xfin - xin;
		newRelHeight = yfin - yin;

		if ((newRelWidth > 0) && (newRelHeight >= 0)) {
			newXin = xin;
			newYin = yin;
		}

		if ((newRelWidth < 0) && (newRelHeight <= 0)) {
			newXin = xfin;
			newYin = yfin;
		}

		if ((newRelWidth >= 0) && (newRelHeight < 0)) {
			newXin = xin;
			newYin = yin + newRelHeight;
		}

		if ((newRelWidth <= 0) && (newRelHeight > 0)) {
			newXin = xin + newRelWidth;
			newYin = yin;
		}
	}

	private Container createAxisPane() {
		JPanel jpanel = new JPanel(new GridBagLayout());
		jpanel.add(createAxisControls());
		return jpanel;
	}

	private Container createAxisControls() {
		JPanel jpanel = new JPanel(new GridBagLayout());
		GridBagConstraints gridbagconstraints = new GridBagConstraints();

		gridbagconstraints.insets = new Insets(0, 5, 3, 5);
		gridbagconstraints.gridwidth = 1;
		gridbagconstraints.gridy = 0;
		gridbagconstraints.weightx = 1.0D;
		gridbagconstraints.fill = 2;
                jpanel.add(Utils.labeledComponent("Axis color:",
                                axesColor = new JColor(50, 20), true), gridbagconstraints);
                axesColor.setColor(theBox.getColor());

		jpanel.add(Utils.labeledComponent("Grid color:",
				gridColor = new JColor(50, 20), true), gridbagconstraints);
		gridColor.setColor(theBox.getGridColor());

		jpanel.add(btnFontsAxes = new JButton("Axis Font"), gridbagconstraints);
		btnFontsAxes.setFont(theBox.getLabelFont());

		chkDisplay = new JCheckBox[NAXES];
		chkDisplay[X] = new JCheckBox("Display X");
		chkDisplay[Y] = new JCheckBox("Display Y");

		chkDisplayMirror = new JCheckBox[NAXES];
		chkDisplayMirror[X] = new JCheckBox("X mirror");
		chkDisplayMirror[Y] = new JCheckBox("Y mirror");
		
		chkDisplayGrid = new JCheckBox[NAXES];
		chkDisplayGrid[X] = new JCheckBox("X grid");
		chkDisplayGrid[Y] = new JCheckBox("Y grid");
		
		
		
		spnTicks = new JSpinner[NAXES];
		spnSubTicks = new JSpinner[NAXES];
		spnSubTickSize = new JSpinner[NAXES];
		spnTickSize = new JSpinner[NAXES];
		spnLabelOffset = new JSpinner[NAXES];

		gridbagconstraints.gridy = 1;
		jpanel.add(chkDisplay[X], gridbagconstraints);
		jpanel.add(chkDisplayMirror[X], gridbagconstraints);

		gridbagconstraints.gridy = 2;
		jpanel.add(chkDisplay[Y], gridbagconstraints);
		jpanel.add(chkDisplayMirror[Y], gridbagconstraints);

		
		gridbagconstraints.gridy = 3;
		jpanel.add(chkDisplayGrid[X], gridbagconstraints);
		jpanel.add(chkDisplayGrid[Y], gridbagconstraints);

		
		
		
		gridbagconstraints.gridy = 2;
		gridbagconstraints.gridwidth = 0;

		/*
		 * gridbagconstraints.gridy = 3; gridbagconstraints.gridwidth = 0;
		 */

		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();

		p1.add(Utils
				.labeledComponent("X Ticks:", spnTicks[X] = new JSpinner(
						new SpinnerNumberModel(5, 3, 20, 1)), true));

		p1.add(Utils
				.labeledComponent("Y Ticks:", spnTicks[Y] = new JSpinner(
						new SpinnerNumberModel(5, 3, 20, 1)), true));

		p1.add(Utils.labeledComponent("X SubTicks:",
				spnSubTicks[X] = new JSpinner(new SpinnerNumberModel(5, 2, 20,
						1)), true));

		p1.add(Utils.labeledComponent("Y SubTicks",
				spnSubTicks[Y] = new JSpinner(new SpinnerNumberModel(5, 2, 20,
						1)), true));

		p2.add(Utils.labeledComponent("X TickSize:",
				spnTickSize[X] = new JSpinner(new SpinnerNumberModel(0.010D,
						0.0D, 0.999D, 0.005D)), true));

		p2.add(Utils.labeledComponent("Y TickSize:",
				spnTickSize[Y] = new JSpinner(new SpinnerNumberModel(0.010D,
						0.0D, 0.995D, 0.005D)), true));

		p2.add(Utils.labeledComponent("X SubTickSize:",
				spnSubTickSize[X] = new JSpinner(new SpinnerNumberModel(0.010D,
						0.0D, 0.995D, 0.005D)), true));

		p2.add(Utils.labeledComponent("Y SubTickSize:",
				spnSubTickSize[Y] = new JSpinner(new SpinnerNumberModel(0.01D,
						0.0D, 0.995D, 0.005D)), true));

		p3.add(Utils.labeledComponent("X Offset:",
				spnLabelOffset[X] = new JSpinner(new SpinnerNumberModel(0.0D,
						0.0D, 0.995D, 0.005D)), true));

		p3.add(Utils.labeledComponent("Y Offset:",
				spnLabelOffset[Y] = new JSpinner(new SpinnerNumberModel(0.01D,
						0.0D, 0.995D, 0.005D)), true));

		gridbagconstraints.gridy = 4;
		gridbagconstraints.gridwidth = 0;
		jpanel.add(p1, gridbagconstraints);

		gridbagconstraints.gridy = 5;
		gridbagconstraints.gridwidth = 0;
		jpanel.add(p2, gridbagconstraints);

		gridbagconstraints.gridy = 6;
		gridbagconstraints.gridwidth = 0;
		jpanel.add(p3, gridbagconstraints);

		for (int i = 0; i < NAXES; i++) {
			spnTicks[i].setFont(fdefo);
			spnSubTicks[i].setFont(fdefo);
			spnSubTickSize[i].setFont(fdefo);
			spnTickSize[i].setFont(fdefo);
			spnLabelOffset[i].setFont(fdefo);

			// settings
			spnTicks[i].setValue(new Integer(theBox.getTicksNumber(i)));
			spnSubTicks[i].setValue(new Integer(theBox.getSubTicksNumber(i)));
			
			spnSubTickSize[i].setValue(new Double(theBox.getSubTicksSize(i)));
			spnTickSize[i].setValue(new Double(theBox.getTicksSize(i)));
			
			spnLabelOffset[i].setValue(new Double(theBox.getLabelSpace(i)));
			chkDisplay[i].setSelected(theBox.isShow(i));
			chkDisplayMirror[i].setSelected(theBox.isShowMirror(i));
			chkDisplayGrid[i].setSelected(theBox.isShowGrid(i));

		}

		btnFontsAxes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				 selectFontsAxes();
			}

		});

		jpanel.setBorder(BorderFactory.createTitledBorder("Axis Settings"));
		return jpanel;
	}



 protected void selectFontsAxes() {

                GHFontChooser dlg = new GHFontChooser((Container) this, "Axes fonts");

                SimpleAttributeSet aset = new SimpleAttributeSet();

                Font f = theBox.getLabelFont();
                Color fgColor = theBox.getLabelColor();
                Color bkColor = theBox.getFillColor();

                StyleConstants.setFontFamily(aset, f.getName());
                StyleConstants.setFontSize(aset, f.getSize());
                int style = f.getStyle();
                StyleConstants.setItalic(aset, false);
                StyleConstants.setBold(aset, false);
                if (style == Font.BOLD)
                        StyleConstants.setBold(aset, true);
                if (style == Font.ITALIC)
                        StyleConstants.setItalic(aset, true);

                StyleConstants.setBackground(aset, bkColor);
                StyleConstants.setForeground(aset, fgColor);

                dlg.setAttributes(aset);
                dlg.setVisible(true);


                aset = (SimpleAttributeSet) dlg.getAttributes();

                     // get font attributes
                String newName = StyleConstants.getFontFamily(aset);
                int newSize = StyleConstants.getFontSize(aset);
                style = Font.PLAIN;
                if (StyleConstants.isBold(aset))
                            style = Font.BOLD;
                if (StyleConstants.isItalic(aset))
                        style = Font.ITALIC;
                Color newc = StyleConstants.getForeground(aset);
                Color newcb = StyleConstants.getBackground(aset);
                Font ff = new Font(newName, style, newSize);
                theBox.setLabelColor(newc);
                theBox.setFillColor(newcb);
                theBox.setLabelFont(ff);


        }



















	private Container createAxisRange() {
		JPanel jpanel = new JPanel(new GridBagLayout());
		GridBagConstraints gridbagconstraints = new GridBagConstraints();

		gridbagconstraints.insets = new Insets(0, 5, 3, 5);
		gridbagconstraints.gridwidth = 1;
		gridbagconstraints.gridy = 0;
		gridbagconstraints.weightx = 1.0D;
		gridbagconstraints.fill = 2;

		
		chkAuto = new JCheckBox[NAXES];
		chkAuto[X] = new JCheckBox("autoRange X");
		chkAuto[Y] = new JCheckBox("autoRange Y");
		
		
		
		
		gridbagconstraints.gridy = 1;
		jpanel.add(chkAuto[X] = new JCheckBox("Auto Range X"), gridbagconstraints);
		jpanel.add(chkAuto[Y] = new JCheckBox("Auto Range Y"), gridbagconstraints);
		
		chkAuto[X].setSelected(theBox.isAutoRange(X));
		chkAuto[Y].setSelected(theBox.isAutoRange(Y));

		gridbagconstraints.gridy = 2;
		gridbagconstraints.gridwidth = 0;

		jpanel.add(createAxisExtents(), gridbagconstraints);

		jpanel.setBorder(BorderFactory.createTitledBorder("Axis Ranges"));
		return jpanel;
	}

	private Container createAxisExtents() {
		Box box = new Box(1);
		String as[] = { "Log X", "Log Y" };

		spnAxesMin = new JSpinner[NAXES];
		spnAxesMax = new JSpinner[NAXES];
		axesRange = new JCheckBox[NAXES];

		for (int i = 0; i < NAXES; i++) {
			Box box1 = new Box(0);
			box1.add(new JLabel(as[i]));
			axesRange[i] = new JCheckBox();

			box1.add(axesRange[i]);

			box1.add(Box.createHorizontalStrut(5));
			box1.add(Utils.labeledComponent("Min:",
					spnAxesMin[i] = new JSpinner(new SpinnerNumberModel(0.0D,
							-5000D, 5000D, 0.10000000000000001D)), true));
			box1.add(Box.createHorizontalStrut(5));
			box1.add(Utils.labeledComponent("Max:",
					spnAxesMax[i] = new JSpinner(new SpinnerNumberModel(1.0D,
							-5000D, 5000D, 0.10000000000000001D)), true));

			spnAxesMin[i].setFont(fdefo);
			spnAxesMax[i].setFont(fdefo);

			spnAxesMin[i].setValue(new Double(theBox.getMin(i)));
			spnAxesMax[i].setValue(new Double(theBox.getMax(i)));
			axesRange[i].setSelected(theBox.isLogScale(i));

			// some initial settings

			box1.setAlignmentX(1.0F);
			box.add(box1);
		}

		box.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0));
		return box;
	}

	private void setAxesParameters() {

		if ((newColor != null) && (newFillColor != null)) {
			if (JaxoColor.isGrayScale(newFillColor)) {
				newColor = JaxoColor.BLACK;
			}
			theBox.setColor(newColor);
			theBox.setFillColor(newFillColor);

			newXin = (int) modelXin.getNumber().intValue();
			newYin = (int) modelYin.getNumber().intValue();
			newXfin = (int) modelXfin.getNumber().intValue();
			newYfin = (int) modelYfin.getNumber().intValue();

			newStroke = (float) Math.abs(modelstin.getNumber().floatValue());

			theBox.setStroke(newStroke);

			setNewBox(newXin, newYin, newXfin, newYfin);

			theBox.setX(newXin);
			theBox.setY(newYin);

			if (((newRelWidth >= 0) && (newRelHeight >= 0))
					|| ((newRelWidth < 0) && (newRelHeight < 0))) {
				theBox.setRelWAndH(newWidth, newHeight);
			} else if (((newRelWidth >= 0) && (newRelHeight < 0))
					|| ((newRelWidth < 0) && (newRelHeight >= 0))) {
				theBox.setRelWAndH(newWidth, -newHeight);
			}

			theBox.setGridColor(gridColor.getColor());
	                theBox.setColor(axesColor.getColor());
	

			for (int i = 0; i < NAXES; i++) {
				theBox.setShow(i, chkDisplay[i].isSelected());
				theBox.setShowMirror(i, chkDisplayMirror[i].isSelected());
				theBox.setShowGrid(i, chkDisplayGrid[i].isSelected());
				theBox.setAutoRange(i,chkAuto[i].isSelected());
				
				double d = ((Double) spnSubTickSize[i].getValue())
						.doubleValue();
				theBox.setSubTicksSize(i, d);

				d = ((Double) spnTickSize[i].getValue()).doubleValue();
				theBox.setTicksSize(i, d);

				// label space
				d = ((Double) spnLabelOffset[i].getValue()).doubleValue();
				theBox.setLabelSpace(i, d);

				// ranges
				d = ((Double) spnAxesMin[i].getValue()).doubleValue();
				theBox.setMin(i, d);

				d = ((Double) spnAxesMax[i].getValue()).doubleValue();
				theBox.setMax(i, d);

				theBox.setShowMirror(i, chkDisplayMirror[i].isSelected());
				
				
				int ii = ((Integer) spnTicks[i].getValue()).intValue();
			        theBox.setTicksNumber(i, ii);
				
				ii = ((Integer) spnSubTicks[i].getValue()).intValue();
				theBox.setSubTicksNumber(i, ii);

				// log scale and rebuild
				theBox.setLogScale(i, axesRange[i].isSelected());

			}

			changed = true;
		}

	}

}

 

Ads help maintain this website.