Source code: jplot/GridPanel.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.event.*;
36 import javax.swing.border.*;
37
38 /**
39 * Creates a panel with options to set or use grid lines in the graph.
40 * Calling the <code>show</code> method pops up a frame (dialog) which
41 * includes the panel.
42 */
43 public class GridPanel extends GriddedPanel {
44
45 private JDialog dialog;
46 private JCheckBox cb_hg;
47 private JCheckBox cb_vg;
48 private JButton b_color;
49 private Color actualColor;
50 private ColorPanel colorPanel;
51 private LineStyle lineStyle;
52 private JPanel stylePanel;
53 private GraphPars gp;
54 private JPlot jplot;
55
56 /**
57 * Principal constructor. Builds a panel with grid options.
58 * @param jp jplot instance
59 * @param gp class with the global graphical parameters
60 */
61 public GridPanel (JPlot jp, GraphPars _gp) {
62 gp = _gp;
63 dialog = null;
64 colorPanel = new ColorPanel(jplot.frame);
65 actualColor = gp.getGridColor();
66 jplot = jp;
67
68 EmptyBorder border = new EmptyBorder(new Insets(0,0,0,10));
69 Dimension cbField = new Dimension(110,24);
70
71 // build the widgets:
72 cb_hg = new JCheckBox("Horizontal",gp.drawGrid(gp.Y_AXIS));
73 cb_vg = new JCheckBox("Vertical",gp.drawGrid(gp.X_AXIS));
74 lineStyle = new LineStyle(new LinePars());
75 lineStyle.setColor(actualColor);
76 b_color = new JButton(new ImageIcon("color.jpg"));
77 b_color.addActionListener(new ActionListener() {
78 public void actionPerformed(ActionEvent e) {
79 Color color = colorPanel.show(jplot.frame,actualColor,100,100);
80 if (color != null) {
81 actualColor = color;
82 lineStyle.setColor(actualColor);
83 lineStyle.repaint();
84 }
85 }
86 });
87 b_color.setMargin(new Insets(0,0,0,0));
88 b_color.setToolTipText("Gridline color");
89 stylePanel = new JPanel(new FlowLayout());
90 stylePanel.add(lineStyle);
91 stylePanel.add(b_color);
92
93 // grid layout:
94 JLabel label = new JLabel("Gridlines:");
95 label.setBorder(border);
96 addComponent(label,1,1);
97 addComponent(cb_hg,1,2);
98 addComponent(cb_vg,1,3);
99
100 label = new JLabel("Gridcolor:");
101 label.setBorder(border);
102 addComponent(label,2,1);
103 addComponent(stylePanel,2,2);
104 }
105
106 /**
107 * Updates the panel with graph parameters.
108 */
109 public void refresh(GraphPars gp) {
110 cb_hg.setSelected(gp.drawGrid(gp.Y_AXIS));
111 cb_vg.setSelected(gp.drawGrid(gp.X_AXIS));
112 }
113
114 /**
115 * Return the current values
116 */
117 public void setValues(GraphPars gp) {
118 gp.setGridColor(actualColor);
119 gp.setDrawGrid(gp.X_AXIS,cb_vg.isSelected());
120 gp.setDrawGrid(gp.Y_AXIS,cb_hg.isSelected());
121 }
122
123 /**
124 * Return a modal JDialog.
125 */
126 public void show(Frame parent, int x, int y) {
127 if (dialog == null) {
128 dialog = new JDialog(parent,"Labels",true);
129 dialog.addWindowListener(new WindowAdapter() {
130 public void windowClosing(WindowEvent e) {
131 dialog.dispose();
132 }
133 });
134 dialog.getContentPane().add(this);
135 dialog.setLocation(x,y);
136 dialog.pack();
137 }
138 dialog.show(); // blocks until user brings dialog down.
139 }
140 }
141