Source code: jplot/LabelProperties.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 * This class pops up a panel with settings used by the labels.
40 * Large part of the panel concerns the font and text color, other
41 * parts define the exact location (x,y in pixels) and rotation of
42 * the label.
43 */
44 public class LabelProperties extends GriddedPanel {
45 private JTextField labelText;
46 private FontPanel fontPanel;
47 private JCheckBox autoPos, manuPos;
48 private JRadioButton hide;
49
50 private JTextField xPosField, yPosField, rotField;
51
52 JDialog dialog;
53 boolean result;
54 private JPlot jplot;
55 int type; // type of the label, i.e., TITLE, XLABEL, YLABEL...
56
57 //private Point point;
58 //private double rotation;
59
60 /**
61 * Principal constructor. Builds a panel with widgets which
62 * set the settings for one specific label.
63 */
64 public LabelProperties(JPlot jp) {
65
66 jplot = jp;
67 //point = new Point(0,0);
68 //rotation = 0.0;
69
70 // make the widgets:
71 fontPanel = new FontPanel(jplot,Utils.getDefaultFont(),Color.black);
72
73 // layout parameters:
74 Dimension longField = new Dimension(180,22);
75 Dimension shortField = new Dimension(80,22);
76 Dimension cbField = new Dimension(110,20);
77 EmptyBorder border = new EmptyBorder(new Insets(5,5,5,5));
78
79
80 GriddedPanel labelPanel = new GriddedPanel();
81 labelPanel.setBorder(new EtchedBorder());
82
83 // lay out the label text:
84 JLabel label = new JLabel("Label:");
85 labelText = new JTextField();
86 labelText.setPreferredSize(longField);
87 label.setBorder(border);
88 labelPanel.addComponent(label,1,1);
89 labelPanel.addFilledComponent(labelText,1,2,2,1,GridBagConstraints.HORIZONTAL);
90 //addFilledComponent(labelText,1,2);
91
92 // rotation:
93 label = new JLabel("Rotation:");
94 label.setBorder(border);
95 labelPanel.addComponent(label,2,1);
96 rotField = new JTextField("0.0");
97 rotField.setEnabled(true);
98 rotField.setPreferredSize(shortField);
99 labelPanel.addComponent(rotField,2,2);
100
101 // put location stuff:
102 label = new JLabel("Position:");
103 label.setBorder(border);
104 labelPanel.addComponent(label,3,1);
105
106 ButtonGroup bg = new ButtonGroup();
107 autoPos = new JCheckBox("automatic",true);
108 autoPos.setPreferredSize(cbField);
109 autoPos.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e) {
111 xPosField.setEnabled(false);
112 yPosField.setEnabled(false);
113 }
114 });
115 bg.add(autoPos);
116 labelPanel.addComponent(autoPos,3,2);
117
118 manuPos = new JCheckBox("manual",false);
119 manuPos.setPreferredSize(cbField);
120 manuPos.addActionListener(new ActionListener() {
121 public void actionPerformed(ActionEvent e) {
122 xPosField.setEnabled(true);
123 yPosField.setEnabled(true);
124 }
125 });
126 bg.add(manuPos);
127 labelPanel.addComponent(manuPos,3,3);
128
129 JPanel posPanel = new JPanel(new BorderLayout());
130 posPanel.add(new JLabel("x: "),BorderLayout.WEST);
131 xPosField = new JTextField("0.0");
132 xPosField.setEnabled(true);
133 posPanel.add(xPosField,BorderLayout.CENTER);
134 posPanel.setPreferredSize(shortField);
135 labelPanel.addComponent(posPanel,4,2);
136
137 posPanel = new JPanel(new BorderLayout());
138 posPanel.add(new JLabel("y: "),BorderLayout.WEST);
139 yPosField = new JTextField("0.0");
140 yPosField.setEnabled(true);
141 posPanel.add(yPosField,BorderLayout.CENTER);
142 posPanel.setPreferredSize(shortField);
143 labelPanel.addComponent(posPanel,4,3);
144
145 hide = new JRadioButton("hide",false);
146 hide.setPreferredSize(cbField);
147 labelPanel.addComponent(hide,5,2);
148
149 addComponent(labelPanel,1,1);
150
151 // and add the font panel:
152 addFilledComponent(fontPanel,2,1,3,1,GridBagConstraints.VERTICAL);
153 }
154
155 /**
156 * @return the current properties of this label in terms of a new
157 * graph label object.
158 */
159 public GraphLabel getGraphLabel() {
160 GraphLabel gl = new GraphLabel(type,labelText.getText(),
161 fontPanel.getSelectedFont(),
162 fontPanel.getSelectedColor());
163 gl.setRotation(Double.parseDouble(rotField.getText())*Math.PI/180);
164 if (manuPos.isSelected()) {
165 gl.setLocation(Double.parseDouble(xPosField.getText()),
166 Double.parseDouble(yPosField.getText()));
167 }
168 gl.setUsePosition(!autoPos.isSelected());
169 gl.hide(hide.isSelected());
170 return gl;
171 }
172
173 /**
174 * Updates the panel with a graph label object.
175 * @param gl graph label object used to refresh this class.
176 */
177 void refresh(GraphLabel gl) {
178 type = gl.getID();
179 labelText.setText(gl.getText());
180 xPosField.setText(Integer.toString((int)gl.getX()));
181 yPosField.setText(Integer.toString((int)gl.getY()));
182 rotField.setText(Graph.formatNumber(gl.getRotation()/Math.PI*180,4));
183 autoPos.setSelected(!gl.usePosition());
184 manuPos.setSelected(gl.usePosition());
185 hide.setSelected(gl.hide());
186 fontPanel.refresh(gl.getFont(),gl.getColor());
187 }
188
189 /**
190 * Pops up a modal frame including the font panel. Returns the
191 * actual font or null if the user pressed the cancel button.
192 * @param parent parent frame
193 * @param x x-position of the dialog
194 * @param y y-position of the dialog
195 * @return true if the user validated the current stuff, false otherwise
196 */
197 boolean show(Frame parent, int x, int y) {
198 if (dialog == null) {
199 JPanel panel = new JPanel(new BorderLayout());
200 dialog = new JDialog(parent,"Textlabel settings",true);
201 dialog.addWindowListener(new WindowAdapter() {
202 public void windowClosing(WindowEvent e) {
203 result = false;
204 dialog.dispose();
205 }
206 });
207 JPanel p = new JPanel(new FlowLayout());
208 p.setBorder(BorderFactory.createEtchedBorder());
209 JButton b = new JButton("Apply");
210 b.addActionListener(new ActionListener() {
211 public void actionPerformed(ActionEvent e) {
212 result = true;
213 dialog.dispose();
214 }
215 });
216 p.add(b);
217 b = new JButton("Cancel");
218 b.addActionListener(new ActionListener() {
219 public void actionPerformed(ActionEvent e) {
220 result = false;
221 dialog.dispose();
222 }
223 });
224 p.add(b);
225 panel.add(this,BorderLayout.CENTER);
226 panel.add(p,BorderLayout.SOUTH);
227 dialog.getContentPane().add(panel);
228 dialog.setLocation(x,y);
229 dialog.pack();
230 }
231 dialog.show(); // blocks until user brings dialog down.
232 return result;
233 }
234 }