Source code: jplot/StyleChooser.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 * StyleChooser is a class which enables the user to set the drawing style.
42 * It must be called with a DataArray object, which contains the initial
43 * drawing style. This class also provides the data drawing style
44 * chooser, an advanced specific purpose chooser which allows to select
45 * a line- and/or point drawing style, a plotting style, colors etc.
46 */
47 public class StyleChooser {
48
49 private LinePars lp;
50 private LineStyle lineStyle;
51 private JDialog chooser;
52 private SmallToggleButton b1,b2,b3,b4;
53 private ImageIcon icon1,icon2,icon3;
54 private DashPanel dashPanel;
55 private PointPanel pointPanel;
56 private ColorPanel colorPanel, popupColorPanel;
57 private JPanel chooserPanel;
58
59 private SmallColorPreview startColorPreview;
60 private SmallColorPreview endColorPreview;
61 private SmallColorPreview fillColorPreview;
62 private JButton b_startColor, b_endColor;
63 private JButton b_fillColor;
64 private JRadioButton rb_slideColor;
65
66 private JCheckBox cb_show, cb_hide;
67 private JTextField legendName,divider,additioner;
68 private JPlot jplot;
69 private String title;
70
71 private boolean isGlobal;
72
73 /**
74 * Principal constructor.
75 * @param jp parent jplot instance to which this dialog is attached
76 * @param title current title of the dialog
77 * @param lp line parameter settings (pointtype, color, linestyle...)
78 * @param global true if built as a global chooser
79 */
80 public StyleChooser(JPlot jp, String title, LinePars lp,
81 boolean global) {
82 jplot = jp;
83 this.title = title;
84 isGlobal = global;
85 if (lp != null) this.lp = lp;
86 else this.lp = new LinePars();
87 chooser = null;
88 }
89
90 /**
91 * Constructor.
92 * @param jp parent jplot instance to which this dialog is attached
93 * @param title current title of the dialog
94 * @param lp line parameter settings (pointtype, color, linestyle...)
95 */
96 public StyleChooser(JPlot jp, String title, LinePars lp) {
97 this(jp,title,lp,false);
98 }
99
100 /**
101 * Principal constructor. The argument is a DataArray object, which
102 * contains the drawing styles. Always built as a local chooser.
103 * @param jp parent jplot instance to which this dialog is attached
104 * @param title current title of the dialog
105 * @param global true if built as a global chooser
106 */
107 public StyleChooser(JPlot jp, String title, boolean global) {
108 this(jp,title,null,global);
109 }
110
111 /*
112 * Returns a chooser panel for selecting a new drawing style. The
113 * panel contains five fields, with the <b>style</b> (e.g., error
114 * boxes, lines, points, histograms etc.), the <b>line-type</b>
115 * (solid, dashed, etc.), the <b>point-type</b> (none, dots,
116 * triangles etc.), the </b>color</b> and a window showing the
117 * actual choice.
118 *
119 * @param isGlobal true if the style chooser is used for all the
120 * datapoints
121 */
122 private void getChooserPanel() {
123 chooserPanel = new JPanel(new BorderLayout());
124
125 //fillColor = Color.blue;
126 Insets insets = new Insets(0,0,0,0);
127
128 JPanel mainPanel = new JPanel(new BorderLayout());
129
130 // the northern part contains the minigraphs
131 // which define the drawing styles:
132 //------------------------------------------
133 JPanel north = new JPanel(new BorderLayout());
134
135 JPanel p = new JPanel();
136 north.setBorder(new TitledBorder(new EtchedBorder(),"Styles"));
137 ImageIcon icon = jplot.getImageIcon("plot1.png");
138 b1 = new SmallToggleButton(false,icon,null,"Line connecting points");
139 b1.addActionListener(new ActionListener() {
140 public void actionPerformed(ActionEvent e) {
141 setGraphStyles(lp.LINES);
142 }
143 });
144 b1.setMargin(insets);
145 p.add(b1);
146
147 icon = jplot.getImageIcon("plot2.png");
148 b2 = new SmallToggleButton(false,icon,null,"Histogram");
149 b2.addActionListener(new ActionListener() {
150 public void actionPerformed(ActionEvent e) {
151 setGraphStyles(lp.HISTO);
152 }
153 });
154 b2.setMargin(insets);
155 p.add(b2);
156
157 icon = jplot.getImageIcon("plot3.png");
158 b3 = new SmallToggleButton(false,icon,null,"Bar graph");
159 b3.addActionListener(new ActionListener() {
160 public void actionPerformed(ActionEvent e) {
161 setGraphStyles(lp.STICKS);
162 }
163 });
164 b3.setMargin(insets);
165 p.add(b3);
166 north.add(p,BorderLayout.WEST);
167
168 icon = jplot.getImageIcon("plot4.png");
169 b4 = new SmallToggleButton(false,icon,null,"Fill color");
170 b4.addActionListener(new ActionListener() {
171 public void actionPerformed(ActionEvent e) {
172 b_fillColor.setEnabled(b4.isSelected());
173 }
174 });
175 b4.setMargin(insets);
176 p = new JPanel();
177 p.add(b4);
178
179 //fillColorPreview = new SmallColorPreview(lp.getFillColor(),28,28);
180 fillColorPreview = new SmallColorPreview(lp.getFillColor(),32,17);
181 p.add(fillColorPreview);
182
183 b_fillColor = new JButton(jplot.getImageIcon("color.jpg"));
184 b_fillColor.setMargin(insets);
185 b_fillColor.setToolTipText("Text color");
186 b_fillColor.setSelected(false);
187 b_fillColor.addActionListener(new ActionListener() {
188 public void actionPerformed(ActionEvent e) {
189 if (popupColorPanel == null) {
190 popupColorPanel = new ColorPanel(jplot.frame);
191 }
192 Color color = popupColorPanel.show(jplot.frame,
193 lp.getFillColor(),100,100);
194 if (color != null) fillColorPreview.setColor(color);
195 }
196 });
197 p.add(b_fillColor);
198 north.add(p,BorderLayout.EAST);
199
200 mainPanel.add(north,BorderLayout.NORTH);
201
202 // the western part is about linestyle (dash-types):
203 //--------------------------------------------------
204 dashPanel = new DashPanel(lp);
205 mainPanel.add(dashPanel,BorderLayout.WEST);
206
207 // center panel contains the plotting points:
208 //-------------------------------------------
209 pointPanel = new PointPanel(lp);
210 mainPanel.add(pointPanel,BorderLayout.CENTER);
211
212 // right panel with the colors:
213 //-----------------------------
214 colorPanel = new ColorPanel(jplot.frame,lp.getColor());
215 mainPanel.add(colorPanel,BorderLayout.EAST);
216
217 // a small panel with the legend label and a checkbox:
218 //----------------------------------------------------
219 GriddedPanel legend=null;
220 GriddedPanel global=null;
221 if (!isGlobal) {
222 legend = new GriddedPanel();
223 legend.setBorder(new TitledBorder(new EtchedBorder(),"Legend label"));
224 legendName = new JTextField(lp.getName());
225 legend.addFilledComponent(legendName,1,1,2,1,GridBagConstraints.HORIZONTAL);
226 ButtonGroup bg = new ButtonGroup();
227 cb_show = createCheckBox("show ",lp.drawLegend());
228 bg.add(cb_show);
229 legend.addComponent(cb_show,1,3);
230 cb_hide = createCheckBox("hide ",!lp.drawLegend());
231 bg.add(cb_hide);
232 legend.addComponent(cb_hide,1,4);
233 }
234
235 // button to reset the plot style to default:
236 else {
237 global = new GriddedPanel();
238 global.setBorder(new TitledBorder(new EtchedBorder(),"Global options"));
239
240 rb_slideColor = new JRadioButton("slide color");
241 rb_slideColor.setSelected(false);
242 rb_slideColor.addActionListener(new ActionListener() {
243 public void actionPerformed(ActionEvent e) {
244 b_startColor.setEnabled(rb_slideColor.isSelected());
245 b_endColor.setEnabled(rb_slideColor.isSelected());
246 }
247 });
248 global.addComponent(rb_slideColor,1,1);
249
250 b_startColor = new JButton(jplot.getImageIcon("color.jpg"));
251 b_startColor.addActionListener(new ActionListener() {
252 public void actionPerformed(ActionEvent e) {
253 if (popupColorPanel == null) {
254 popupColorPanel = new ColorPanel(jplot.frame);
255 }
256 Color color = popupColorPanel.show(jplot.frame,
257 lp.getStartColor(),100,100);
258 if (color != null) startColorPreview.setColor(color);
259 }
260 });
261 b_startColor.setMargin(new Insets(0,0,0,0));
262 b_startColor.setToolTipText("Change the start color");
263 startColorPreview = new SmallColorPreview(lp.getStartColor(),40,18);
264 p = new JPanel(new FlowLayout());
265 p.add(startColorPreview);
266 p.add(b_startColor);
267
268 JLabel label = new JLabel("Start:");
269 //label.setBorder(border);
270 global.addComponent(label,1,2);
271 global.addComponent(p,1,3);
272
273 b_endColor = new JButton(jplot.getImageIcon("color.jpg"));
274 b_endColor.addActionListener(new ActionListener() {
275 public void actionPerformed(ActionEvent e) {
276 if (popupColorPanel == null) {
277 popupColorPanel = new ColorPanel(jplot.frame);
278 }
279 Color color = popupColorPanel.show(jplot.frame,
280 lp.getEndColor(),100,100);
281 if (color != null) endColorPreview.setColor(color);
282 }
283 });
284 b_endColor.setMargin(new Insets(0,0,0,0));
285 b_endColor.setToolTipText("Change the end color");
286 endColorPreview = new SmallColorPreview(lp.getEndColor(),40,18);
287 p = new JPanel(new FlowLayout());
288 p.add(endColorPreview);
289 p.add(b_endColor);
290
291 label = new JLabel("End:");
292 //label.setBorder(border);
293 global.addComponent(label,1,4);
294 global.addComponent(p,1,5);
295 }
296
297 JPanel scaling = new JPanel(new FlowLayout());
298 scaling.setBorder(new TitledBorder(new EtchedBorder(),"Scaling"));
299 divider = new JTextField(Float.toString(1.0f/lp.getMultiplier()));
300 divider.setPreferredSize(new Dimension(65,24));
301 scaling.add(new JLabel("divide by:"));
302 scaling.add(divider);
303 additioner = new JTextField(Float.toString(lp.getAdditioner()));
304 additioner.setPreferredSize(new Dimension(50,24));
305 scaling.add(new JLabel("offset:"));
306 scaling.add(additioner);
307
308 p = new JPanel();
309 p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
310 if (!isGlobal) p.add(legend);
311 else p.add(global);
312 p.add(scaling);
313 mainPanel.add(p,BorderLayout.SOUTH);
314 chooserPanel.add(mainPanel,BorderLayout.CENTER);
315
316 // south contains buttons OK or Cancel
317 //------------------------------------
318 p = new JPanel();
319 p.setBorder(new EtchedBorder());
320 JPanel buttons = new JPanel(new GridLayout(1,2));
321 JButton b = new JButton("Apply");
322 b.addActionListener(new ActionListener() {
323 public void actionPerformed(ActionEvent e) {
324 lp.setColor(colorPanel.getSelectedColor());
325 if (b4.isSelected()) {
326 lp.fill(true);
327 lp.setFillColor(fillColorPreview.getColor());
328 }
329 else lp.fill(false);
330 if (!isGlobal) {
331 lp.setName(legendName.getText());
332 if (cb_show.isSelected()) lp.setDrawLegend(true);
333 else lp.setDrawLegend(false);
334 }
335 else if (rb_slideColor.isSelected()) {
336 lp.setSlideColor(startColorPreview.getColor(),
337 endColorPreview.getColor());
338 }
339 if (!divider.getText().equals("")) {
340 lp.setMultiplier(1.0f/Float.parseFloat(divider.getText()));
341 }
342 else lp.setMultiplier(1.0f);
343 if (!additioner.getText().equals("")) {
344 lp.setAdditioner(Float.parseFloat(additioner.getText()));
345 }
346 else lp.setAdditioner(0.0f);
347 lp.setDataChanged(true);
348 chooser.dispose();
349 }
350 });
351 buttons.add(b);
352 b = new JButton("Cancel");
353 b.addActionListener(new ActionListener() {
354 public void actionPerformed(ActionEvent e) {
355 lp = null;
356 chooser.dispose();
357 }
358 });
359 buttons.add(b);
360 p.add(buttons);
361 chooserPanel.add(p,BorderLayout.SOUTH);
362 }
363
364 /*
365 * Make a label specifically used by the tic-panel.
366 * @return a label with specific font and alignments.
367 */
368 private JLabel makeLabel(String txt)
369 {
370 JLabel label = new JLabel(txt + " ");
371 label.setVerticalAlignment(SwingConstants.BOTTOM);
372 label.setHorizontalAlignment(SwingConstants.CENTER);
373 label.setFont(new Font("SansSerif",Font.ITALIC,11));
374 label.setForeground(Color.black);
375 return label;
376 }
377
378 private JCheckBox createCheckBox(String s, boolean b) {
379 JCheckBox cb = new JCheckBox(" " + s + " ", b);
380 cb.setFont(new Font("serif", Font.PLAIN, 12));
381 cb.setHorizontalAlignment(JCheckBox.LEFT);
382 return cb;
383 }
384
385 /*
386 * updates the panel with the current data
387 */
388 private void setGraphStyles(int type) {
389 lp.setGraphStyle(type);
390 b1.setSelected(type == lp.LINES);
391 b2.setSelected(type == lp.HISTO);
392 b3.setSelected(type == lp.STICKS);
393 chooserPanel.repaint();
394 }
395
396 /*
397 * updates the panel with the current data
398 */
399 private void refresh() {
400 setGraphStyles(lp.getGraphStyle());
401 b4.setSelected(lp.fill());
402 b_fillColor.setEnabled(lp.fill());
403 dashPanel.refresh(lp);
404 pointPanel.refresh(lp);
405 colorPanel.refresh(lp.getColor());
406 fillColorPreview.setColor(lp.getFillColor());
407 divider.setText(Double.toString(1.0/lp.getMultiplier()));
408 additioner.setText(Double.toString(lp.getAdditioner()));
409 if (!isGlobal) {
410 cb_show.setSelected(lp.drawLegend());
411 cb_hide.setSelected(!lp.drawLegend());
412 legendName.setText(lp.getName());
413 }
414 else {
415 startColorPreview.setColor(lp.getStartColor());
416 endColorPreview.setColor(lp.getEndColor());
417 rb_slideColor.setSelected(lp.slideColor());
418 b_startColor.setEnabled(rb_slideColor.isSelected());
419 b_endColor.setEnabled(rb_slideColor.isSelected());
420 }
421 }
422
423 /**
424 * Return a modal JDialog with a customized linestyle chooser.
425 * The panel includes choices for dashes, pointstyles and colors.
426 * @param x coordinate of the pin used to fix this frame
427 * @param y coordinate of the pin used to fix this frame
428 * @param lp initial line parameters
429 * @return new line style parameters or null if canceled
430 */
431 public LinePars show(int x, int y, LinePars initLinePars) {
432 if (initLinePars == null) lp = new LinePars();
433 else lp = new LinePars(initLinePars);
434
435 if (chooser == null) {
436 chooser = new JDialog(jplot.frame,title,true);
437 chooser.addWindowListener(new WindowAdapter() {
438 public void windowClosing(WindowEvent e) {
439 lp = null;
440 chooser.dispose();
441 }
442 });
443 getChooserPanel();
444 chooser.setLocation(x,y);
445 chooser.getContentPane().add(chooserPanel);
446 chooser.pack();
447 }
448 refresh();
449 chooser.pack();
450 chooser.show(); // blocks until user brings dialog down.
451 return lp;
452 }
453 }
454