001/*
002 * Copyright (c) 2010-2012 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-05-09 11:05:40 -0700 (Wed, 09 May 2012) $' 
007 * '$Revision: 29823 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.plotting;
031
032import java.awt.Dimension;
033import java.awt.GridBagConstraints;
034import java.awt.GridBagLayout;
035import java.awt.Insets;
036import java.awt.event.FocusEvent;
037import java.awt.event.FocusListener;
038import java.util.HashMap;
039import java.util.Map;
040
041import javax.swing.JComponent;
042import javax.swing.JLabel;
043import javax.swing.JPanel;
044import javax.swing.JTextField;
045
046import org.kepler.gui.PlotsEditorPanel;
047
048/**
049 * Created by IntelliJ IDEA.
050 * User: sean
051 * Date: Jul 7, 2010
052 * Time: 12:06:47 PM
053 */
054
055public class GraphLabelsPanel extends JPanel {
056        public GraphLabelsPanel(PlotEditor plotEditor) {
057                this.plotEditor = plotEditor;
058                this.setLayout(new GridBagLayout());
059                
060                addRow(GraphProperty.TITLE, 0);
061                addRow(GraphProperty.X_LABEL, 1);
062                addRow(GraphProperty.Y_LABEL, 2);
063        }
064
065        private void addRow(final GraphProperty property, int row) {
066                GridBagConstraints labelConstraints = new GridBagConstraints();
067                labelConstraints.gridx = 0;
068                labelConstraints.gridy = row;
069                labelConstraints.ipady = 0;
070                labelConstraints.insets = new Insets(0,10,0,10);
071                JLabel label = new JLabel(property.toString());
072                this.add(label, labelConstraints);
073                
074                GridBagConstraints textFieldConstraints = new GridBagConstraints();
075                textFieldConstraints.gridx = 1;
076                textFieldConstraints.gridy = row;
077                final JTextField textField = new JTextField();
078                textField.setText("test");
079                Dimension oldSize = textField.getPreferredSize();
080                textField.setText("");
081                Dimension dimension = new Dimension();
082                dimension.setSize(oldSize.getWidth() * 10, oldSize.getHeight());
083                
084                textField.setMinimumSize(dimension);
085                textField.setPreferredSize(dimension);
086                
087                if (GraphProperty.TITLE == property) {
088                        if (getPlotEditor().getPlot() != null) {
089                                String graphName = generateGraphName();
090                                getPlotEditor().getPlot().setGraphName(graphName);
091                                textField.setText(graphName);
092                        }
093                }
094                
095                textField.addFocusListener(new FocusListener() {
096                        public void focusGained(FocusEvent e) {}
097                        public void focusLost(FocusEvent e) {
098                                getPlotEditor().getPlot().setProperty(property, ((JTextField) e.getSource()).getText());
099//                              getPlotEditor().getPlot().setGraphName(((JTextField) e.getSource()).getText());
100                        }
101                });
102                this.add(textField, textFieldConstraints);
103                textFields.put(property, textField);
104                labels.put(property, label);
105        }
106
107        private String generateGraphName() {
108                PlotsEditorPanel editorPanel = plotEditor.getEditorPanel();
109                if (editorPanel == null) {
110                        return "ERROR";
111                }
112                int nextGraphId = editorPanel.getNextUnusedGraphId();
113                return "Graph " + nextGraphId;
114        }
115
116        public void setActive(boolean active) {
117                for (GraphProperty property : labels.keySet()) {                        
118                        JComponent component = textFields.get(property);
119                        JLabel label = labels.get(property);
120                        component.setEnabled(active);
121                        label.setEnabled(active);
122                }
123        }
124        
125        public PlotEditor getPlotEditor() {
126                return plotEditor;
127        }
128        
129        private Map<GraphProperty, JLabel> labels = new HashMap<GraphProperty, JLabel>();
130        private Map<GraphProperty, JComponent> textFields = new HashMap<GraphProperty, JComponent>();
131        final private PlotEditor plotEditor;
132}