Source code: jplot/LegendPanel.java
1 /*
2 * JPLOT -- Java Plotting Interface for any programme or
3 * as an independent GUI
4 *
5 * Copyright (C) 1999 Jan van der Lee
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 *
22 * Send bugs, suggestions or queries to <jplot@cig.ensmp.fr>
23 * The latest releases are found at
24 * http://www.cig.ensmp.fr/~vanderlee/jplot.html
25 *
26 * Initally developed for use by the Centre d'Informatique Geologique
27 * Ecole des Mines de Paris, Fontainebleau, France.
28 */
29
30 package jplot;
31
32 import java.awt.*;
33 import java.util.*;
34 import javax.swing.*;
35 import java.awt.geom.*;
36 import java.awt.event.*;
37 import javax.swing.border.*;
38 import javax.swing.colorchooser.*;
39
40 /**
41 * LegendPanel creates a panel which allows a user to modify the
42 * parameters used to set the legend, such as position, color,
43 * font etc.
44 * Calling the <code>show</code> method pops up a frame (dialog) which
45 * includes the panel.
46 */
47 public class LegendPanel extends GriddedPanel {
48 private final int X = GraphPars.X_AXIS;
49 private final int Y = GraphPars.Y_AXIS;
50
51 private JDialog dialog;
52 //private JRadioButton rb_reset;
53 //private JCheckBox rb_show, rb_hide;
54 private FontPanel fontPanel;
55 private GraphPars gp;
56
57 private JRadioButton hide;
58 private JCheckBox autoPos, manuPos;
59 private JTextField xPosField, yPosField, rotField;
60 private JTextField sepField;
61 private JPlot jplot;
62
63 /**
64 * Principal constructor. Builds a panel with the options
65 * and apply/cancel buttons.
66 * @param gp instance of the object containing all graphical parameters
67 */
68 public LegendPanel (JPlot jp, GraphPars _gp) {
69 dialog = null;
70 gp = _gp;
71 jplot = jp;
72
73 /*
74 ButtonGroup bg = new ButtonGroup();
75 rb_show = new JCheckBox("show");
76 bg.add(rb_show);
77 rb_hide = new JCheckBox("hide");
78 bg.add(rb_hide);
79 rb_reset = new JRadioButton("reset");
80 */
81
82 Dimension mediumField = new Dimension(120,20);
83 Dimension cbField = new Dimension(110,20);
84 Dimension shortField = new Dimension(80,22);
85 EmptyBorder border = new EmptyBorder(new Insets(0,0,0,10));
86 setBorder(new EmptyBorder(new Insets(5,5,5,5)));
87
88 // this panel contains the non-font specific stuff:
89 GriddedPanel p = new GriddedPanel();
90 p.setBorder(new EtchedBorder());
91
92 // put location stuff:
93 JLabel label = new JLabel("Position:");
94 label.setBorder(border);
95 p.addComponent(label,1,1);
96
97 ButtonGroup bg = new ButtonGroup();
98 autoPos = new JCheckBox("automatic",true);
99 autoPos.setPreferredSize(cbField);
100 autoPos.addActionListener(new ActionListener() {
101 public void actionPerformed(ActionEvent e) {
102 xPosField.setEnabled(false);
103 yPosField.setEnabled(false);
104 }
105 });
106 bg.add(autoPos);
107 p.addComponent(autoPos,1,2);
108
109 manuPos = new JCheckBox("manual",false);
110 manuPos.setPreferredSize(cbField);
111 manuPos.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e) {
113 xPosField.setEnabled(true);
114 yPosField.setEnabled(true);
115 }
116 });
117 bg.add(manuPos);
118 p.addComponent(manuPos,1,3);
119
120 JPanel posPanel = new JPanel(new BorderLayout());
121 posPanel.add(new JLabel("x: "),BorderLayout.WEST);
122 xPosField = new JTextField();
123 xPosField.setEnabled(true);
124 posPanel.add(xPosField,BorderLayout.CENTER);
125 posPanel.setPreferredSize(shortField);
126 p.addComponent(posPanel,2,2);
127
128 posPanel = new JPanel(new BorderLayout());
129 posPanel.add(new JLabel("y: "),BorderLayout.WEST);
130 yPosField = new JTextField();
131 yPosField.setEnabled(true);
132 posPanel.add(yPosField,BorderLayout.CENTER);
133 posPanel.setPreferredSize(shortField);
134 p.addComponent(posPanel,2,3);
135
136 hide = new JRadioButton("hide",false);
137 hide.setPreferredSize(cbField);
138 hide.addActionListener(new ActionListener() {
139 public void actionPerformed(ActionEvent e) {
140 }
141 });
142 p.addComponent(hide,3,2);
143
144 label = new JLabel("Spacing:");
145 label.setBorder(border);
146 p.addComponent(label,4,1);
147 sepField = new JTextField();
148 sepField.setPreferredSize(shortField);
149 p.addComponent(sepField,4,2);
150 addComponent(p,1,1);
151
152 fontPanel = new FontPanel(jplot,gp.getLegendFont());
153 addFilledComponent(fontPanel,2,1,3,1,GridBagConstraints.VERTICAL);
154 }
155
156 /**
157 * Updates the panel with the current graphical parameters.
158 */
159 public void refresh() {
160 hide.setSelected(!gp.drawLegend());
161 fontPanel.refresh(gp.getLegendFont());
162 xPosField.setText(Float.toString((float)gp.getLegendPos(gp.X_AXIS)));
163 yPosField.setText(Float.toString((float)gp.getLegendPos(gp.Y_AXIS)));
164 sepField.setText(Float.toString((float)gp.getLegendSpacing()));
165 autoPos.setSelected(!gp.useLegendPos());
166 manuPos.setSelected(gp.useLegendPos());
167 }
168
169 public void setValues() {
170 gp.setDrawLegend(!hide.isSelected());
171 gp.setLegendFont(fontPanel.getSelectedFont());
172 if (manuPos.isSelected()) {
173 gp.setLegendPos(gp.X_AXIS,Double.parseDouble(xPosField.getText()));
174 gp.setLegendPos(gp.Y_AXIS,Double.parseDouble(yPosField.getText()));
175 }
176 gp.setUseLegendPos(!autoPos.isSelected());
177 gp.setLegendSpacing(Float.parseFloat(sepField.getText()));
178 }
179
180 /**
181 * Return a modal JDialog.
182 * @param parent frame which is the parent of this dialog.
183 * @param x x-position of the upper left corner
184 * @param y y-position of the upper left corner
185 */
186 public void show(Frame parent, int x, int y) {
187 if (dialog == null) {
188 dialog = new JDialog(parent,"Legend options",false);
189 dialog.addWindowListener(new WindowAdapter() {
190 public void windowClosing(WindowEvent e) {
191 dialog.dispose();
192 }
193 });
194
195 JPanel p = new JPanel(new FlowLayout());
196 p.setBorder(BorderFactory.createEtchedBorder());
197 JButton b = new JButton("Apply");
198 b.addActionListener(new ActionListener() {
199 public void actionPerformed(ActionEvent e) {
200 setValues();
201 gp.setDataChanged(true);
202 jplot.updateGraphIfShowing();
203 dialog.dispose();
204 }
205 });
206 p.add(b);
207 b = new JButton("Dismiss");
208 b.addActionListener(new ActionListener() {
209 public void actionPerformed(ActionEvent e) {
210 dialog.dispose();
211 }
212 });
213 p.add(b);
214 dialog.getContentPane().add(this,BorderLayout.CENTER);
215 dialog.getContentPane().add(p,BorderLayout.SOUTH);
216 dialog.pack();
217 dialog.setLocation(x,y);
218 }
219 refresh();
220 dialog.show(); // blocks until user brings dialog down.
221 }
222 }