View Javadoc

1   /*
2    * Copyright (c) 2006 Stiftung Deutsches Elektronen-Synchroton,
3    * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
4    *
5    * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS.
6    * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
7    * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND
8    * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
9    * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10   * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
11   * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE
12   * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR
13   * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
14   * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
15   * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
16   * OR MODIFICATIONS.
17   * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION,
18   * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS
19   * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY
20   * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
21   */
22  
23  package de.desy.acop.displayers.selector;
24  
25  import java.awt.Color;
26  import java.awt.Dimension;
27  import java.awt.GridBagConstraints;
28  import java.awt.GridBagLayout;
29  import java.awt.Insets;
30  import java.awt.event.ActionEvent;
31  import java.awt.event.ActionListener;
32  import java.awt.event.ItemEvent;
33  import java.awt.event.ItemListener;
34  
35  import javax.swing.JButton;
36  import javax.swing.JCheckBox;
37  import javax.swing.JColorChooser;
38  import javax.swing.JComboBox;
39  import javax.swing.JLabel;
40  import javax.swing.JPanel;
41  
42  import com.cosylab.gui.components.NumberField;
43  import com.cosylab.gui.components.util.RunnerHelper;
44  import com.cosylab.gui.components.util.ColorManager;
45  
46  import de.desy.acop.chart.AcopDisplayMode;
47  import de.desy.acop.chart.AcopDrawStyleEnum;
48  import de.desy.acop.chart.AcopFFTEnum;
49  import de.desy.acop.displayers.tools.AcopGraphParameters;
50  
51  /**
52   * <code>GraphCustomizerPanel</code> is a customization panel for 
53   * <code>AcopGraphParameters</code>. Panel has user interface which enable
54   * selecting various properties which are part of the graph parameters.
55   * 
56   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
57   * @version $Id: Templates.xml,v 1.10 2004/01/13 16:17:13 jbobnar Exp $
58   * @see AcopGraphParameters
59   *
60   */
61  public class GraphCustomizerPanel extends MultipleDisplayerAbstractSettingsPanel<AcopGraphParameters> {
62  
63  		private static final long serialVersionUID = -2986099881478526811L;
64  		private JLabel graphStyleLabel;
65  		private JLabel graphModeLabel;
66  		private JLabel graphFFTLabel;
67  		private JLabel graphColorLabel;
68  		private JLabel trendLengthLabel;
69  		private JComboBox drawStyleCombo;
70  		private JComboBox graphModeCombo;
71  		private JComboBox graphFFTCombo;
72  		private JLabel graphColorSelector;
73  		private Color selectedColor;
74  		private JPanel propertiesPanel;
75  		private JButton colorPicker;
76  		private JCheckBox trendCheckBox;
77  		private NumberField trendLength;
78  		private JLabel graphWidthLabel;
79  		private NumberField graphWidthField;
80  		private JCheckBox useDefaultColor;
81  		
82  		/**
83  		 * Constructs new GraphCustomizerPanel.
84  		 *
85  		 */
86  		public GraphCustomizerPanel() {
87  			setLayout(new GridBagLayout());
88  			add(getPropertiesPanel(), new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0,0,0,0),0,0));
89  		}
90  		
91  		private JPanel getPropertiesPanel() {
92  			if (propertiesPanel == null) {
93  		        propertiesPanel = new JPanel(new GridBagLayout());
94  		        
95  		        graphStyleLabel = new JLabel("Style");
96  		        graphStyleLabel.setPreferredSize(new Dimension(50,21));
97  		        graphStyleLabel.setMinimumSize(new Dimension(50,21));
98  		        graphStyleLabel.setMaximumSize(new Dimension(50,21));
99  		        drawStyleCombo = new JComboBox(AcopDrawStyleEnum.values());
100 		        drawStyleCombo.setPreferredSize(new Dimension(120,21));
101 		        drawStyleCombo.setMinimumSize(new Dimension(120,21));
102 		        drawStyleCombo.setMaximumSize(new Dimension(120,21));
103 		        propertiesPanel.add(graphStyleLabel, new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
104 		        propertiesPanel.add(drawStyleCombo, new GridBagConstraints(1,0,3,1,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
105 		        
106 		        graphModeLabel = new JLabel("Mode");
107 		        graphModeLabel.setPreferredSize(new Dimension(50,21));
108 		        graphModeLabel.setMinimumSize(new Dimension(50,21));
109 		        graphModeLabel.setMaximumSize(new Dimension(50,21));
110 		        graphModeCombo = new JComboBox(AcopDisplayMode.values());
111 		        graphModeCombo.setPreferredSize(new Dimension(120,21));
112 		        graphModeCombo.setMinimumSize(new Dimension(120,21));
113 		        graphModeCombo.setMaximumSize(new Dimension(120,21));
114 		        propertiesPanel.add(graphModeLabel, new GridBagConstraints(0,1,1,1,1,1,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
115 		        propertiesPanel.add(graphModeCombo, new GridBagConstraints(1,1,3,1,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
116 		        
117 		        graphFFTLabel = new JLabel("FFT");
118 		        graphFFTLabel.setPreferredSize(new Dimension(50,21));
119 		        graphFFTLabel.setMinimumSize(new Dimension(50,21));
120 		        graphFFTLabel.setMaximumSize(new Dimension(50,21));
121 		        graphFFTCombo = new JComboBox(AcopFFTEnum.values());
122 		        graphFFTCombo.setPreferredSize(new Dimension(120,21));
123 		        graphFFTCombo.setMinimumSize(new Dimension(120,21));
124 		        graphFFTCombo.setMaximumSize(new Dimension(120,21));
125 		        propertiesPanel.add(graphFFTLabel, new GridBagConstraints(0,2,1,1,1,1,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
126 		        propertiesPanel.add(graphFFTCombo, new GridBagConstraints(1,2,3,1,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
127 		        
128 		        graphColorLabel = new JLabel("Color");
129 		        graphColorLabel.setPreferredSize(new Dimension(50,21));
130 		        graphColorLabel.setMinimumSize(new Dimension(50,21));
131 		        graphColorLabel.setMaximumSize(new Dimension(50,21));
132 		        graphColorSelector = new JLabel();
133 		        graphColorSelector.setOpaque(true);
134 		        graphColorSelector.setPreferredSize(new Dimension(88,21));
135 		        graphColorSelector.setMinimumSize(new Dimension(88,21));
136 		        graphColorSelector.setMaximumSize(new Dimension(88,21));
137 
138 		        selectedColor = ColorManager.DEFAULT_COLORS[0];
139 		        graphColorSelector.setBackground(selectedColor);
140 		        propertiesPanel.add(graphColorLabel, new GridBagConstraints(0,3,1,1,1,1,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
141 		        propertiesPanel.add(graphColorSelector, new GridBagConstraints(1,3,2,1,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
142 		        
143 		        colorPicker = new JButton("...");
144 		        colorPicker.setPreferredSize(new Dimension(28,21));
145 		        colorPicker.setMinimumSize(new Dimension(28,21));
146 		        colorPicker.setMaximumSize(new Dimension(28,21));
147 		        colorPicker.addActionListener(new ActionListener() {
148 		        	public void actionPerformed(ActionEvent e) {
149 		        		selectColor();
150 		        		if (selectedColor != null)
151 		        			graphColorSelector.setBackground(selectedColor);	
152 		        	}
153 		        });
154 		        propertiesPanel.add(colorPicker, new GridBagConstraints(3,3,1,1,0.1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
155 		        
156 		        useDefaultColor = new JCheckBox("Automatic color");
157 		        useDefaultColor.addItemListener(new ItemListener() {
158 		        	public void itemStateChanged(ItemEvent e) {
159 		        		colorPicker.setEnabled(!useDefaultColor.isSelected());
160 		        		graphColorSelector.setEnabled(!useDefaultColor.isSelected());
161 		        		graphColorLabel.setEnabled(!useDefaultColor.isSelected());
162 		        	}
163 		        });
164 		        propertiesPanel.add(useDefaultColor, new GridBagConstraints(0,4,4,1,1,1,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
165 		        useDefaultColor.setSelected(true);
166 		        
167 		        
168 		        graphWidthLabel = new JLabel("Width");
169 		        graphWidthLabel.setPreferredSize(new Dimension(50,21));
170 		        graphWidthLabel.setMinimumSize(new Dimension(50,21));
171 		        graphWidthLabel.setMaximumSize(new Dimension(50,21));
172 		        graphWidthField = new NumberField();
173 		        graphWidthField.setPreferredSize(new Dimension(120,21));
174 		        graphWidthField.setMinimumSize(new Dimension(120,21));
175 		        graphWidthField.setMaximumSize(new Dimension(120,21));
176 		        graphWidthField.setNumberType(Integer.class);
177 		        graphWidthField.setValue(new Integer(1));
178 		        propertiesPanel.add(graphWidthLabel, new GridBagConstraints(0,5,1,1,1,1,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
179 		        propertiesPanel.add(graphWidthField, new GridBagConstraints(1,5,3,1,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
180 		        
181 		        trendCheckBox = new JCheckBox("Draw as trend");
182 		        trendCheckBox.addActionListener(new ActionListener() {
183 		        	public void actionPerformed(ActionEvent e) {
184 		        		if (trendCheckBox.isSelected()) {
185 		        			setGraphMode(AcopDisplayMode.PolyLine.ordinal());
186 		        		}
187 		        	}
188 		        });
189 		        propertiesPanel.add(trendCheckBox, new GridBagConstraints(0,6,4,1,0.1,1,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
190 		        
191 		        trendLengthLabel = new JLabel("Trend length");
192 		        trendLengthLabel.setPreferredSize(new Dimension(80,21));
193 		        trendLengthLabel.setMinimumSize(new Dimension(80,21));
194 		        trendLengthLabel.setMaximumSize(new Dimension(80,21));
195 		        
196 		        trendLength = new NumberField();
197 		        trendLength.setPreferredSize(new Dimension(90,21));
198 		        trendLength.setMinimumSize(new Dimension(90,21));
199 		        trendLength.setMaximumSize(new Dimension(90,21));
200 		        trendLength.setNumberType(Integer.class);
201 		        trendLength.setValue(new Integer(100));
202 		        propertiesPanel.add(trendLengthLabel, new GridBagConstraints(0,7,3,1,1,1,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
203 		        propertiesPanel.add(trendLength, new GridBagConstraints(2,7,2,1,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
204 		        
205 	        }
206 			
207 	        return propertiesPanel;
208 		}
209 		
210 		private void selectColor() {
211 			selectedColor = JColorChooser.showDialog(this, "Graph color", selectedColor);
212 		}
213 		
214 		/**
215 		 * Returns the selected color.
216 		 * 
217 		 * @return selected color
218 		 */
219 		public Color getColor() {
220 			return useDefaultColor.isSelected() ? null : selectedColor;
221 		}
222 		
223 		/**
224 		 * Returns the selected graph style.
225 		 * 
226 		 * @return the graph style
227 		 * @see AcopDrawStyleEnum
228 		 */
229 		public int getGraphStyle() {
230 			return ((AcopDrawStyleEnum)drawStyleCombo.getSelectedItem()).ordinal();
231 		}
232 		
233 		/**
234 		 * Returns the selected graph mode.
235 		 * 
236 		 * @return the graph mode
237 		 * @see AcopDisplayMode
238 		 */
239 		public int getGraphMode() {
240 			return ((AcopDisplayMode)graphModeCombo.getSelectedItem()).ordinal();
241 		}
242 		
243 		/**
244 		 * Returns the selected fft mode.
245 		 * 
246 		 * @return the fft
247 		 * @see AcopFFTEnum
248 		 */
249 		public int getGraphFFT() {
250 			return ((AcopFFTEnum)graphFFTCombo.getSelectedItem()).ordinal();
251 		}
252 		
253 		/**
254 		 * Sets the color to the UI.
255 		 * 
256 		 * @param color new color
257 		 */
258 		public void setColor(Color color) {
259 			if (color==null) {
260 				color=Color.BLACK;
261 			}
262 			selectedColor = color;
263 			graphColorSelector.setBackground(selectedColor);
264 		}
265 		
266 		/**
267 		 * Sets the draw style to the UI.
268 		 * @param drawStyle new draw style
269 		 * @see AcopDrawStyleEnum
270 		 */
271 		public void setDrawStyle(int drawStyle) {
272 			drawStyleCombo.setSelectedIndex(drawStyle);
273 		}
274 		
275 		/**
276 		 * Sets the graph mode to the UI.
277 		 * 
278 		 * @param graphMode new graph mode
279 		 * @see AcopDisplayMode 
280 		 */
281 		public void setGraphMode(int graphMode) {
282 			graphModeCombo.setSelectedIndex(graphMode);
283 		}
284 		
285 		/**
286 		 * Sets the graph fft to the UI.
287 		 * 
288 		 * @param graphFFT new graph FFT
289 		 * @see AcopFFTEnum
290 		 */
291 		public void setGraphFFT(int graphFFT) {
292 			graphFFTCombo.setSelectedIndex(graphFFT);
293 		}
294 		
295 		/**
296 		 * Sets the trend mode.
297 		 * 
298 		 * @param isTrend true if in trend mode
299 		 */
300 		public void setTrend(boolean isTrend) {
301 			trendCheckBox.setSelected(isTrend);
302 		}
303 		
304 		/**
305 		 * Returns true if trend mode is selected.
306 		 * 
307 		 * @return true if trend selected
308 		 */
309 		public boolean isTrend() {
310 			return trendCheckBox.isSelected();
311 		}
312 		
313 		/**
314 		 * Sets the length of the trend line.
315 		 * 
316 		 * @param length new trend length
317 		 * 
318 		 */
319 		public void setTrendLength(int length) {
320 			trendLength.setValue(length);
321 		}
322 		
323 		/**
324 		 * Returns the length of the trend
325 		 * 
326 		 * @return trend length
327 		 */
328 		public int getTrendLength() {
329 			return trendLength.getValue().intValue();
330 		}
331 		
332 		/**
333 		 * Sets the graph width to the UI.
334 		 * 
335 		 * @param width the width of the graph lines
336 		 */
337 		public void setGraphWidth(int width) {
338 			graphWidthField.setValue(new Integer(width));
339 		}
340 		
341 		/**
342 		 * Returns the graph width.
343 		 * 
344 		 * @return the graph width
345 		 */
346 		public int getGraphWidth() {
347 			return graphWidthField.getValue().intValue();
348 		}
349 		
350 		/*
351 		 * (non-Javadoc)
352 		 * @see de.desy.acop.displayers.selector.MultipleDisplayerAbstractSettingsPanel#setParameters(de.desy.acop.displayers.tools.AcopDisplayerParameters)
353 		 */
354 		public void setParameters(AcopGraphParameters param) {
355 			if (param == null) {
356 				setColor(null);
357 				setGraphFFT(0);
358 				setGraphMode(0);
359 				setGraphFFT(0);
360 				setTrend(false);
361 				setGraphWidth(1);
362 				setTrendLength(100);
363 			} else {
364 				setColor(param.getColor());
365 				setDrawStyle(param.getDrawStyle());
366 				setGraphMode(param.getMode());
367 				setGraphFFT(param.getFFT());
368 				setTrend(param.isTrend());
369 				setGraphWidth(param.getWidth());
370 				setTrendLength(param.getTrendLength());
371 			}
372 		}
373 		
374 		public static void main(String[] args) {
375 			RunnerHelper.runComponent(new GraphCustomizerPanel(), 600,600);
376 		}
377 		
378 		/**
379 		 * Enables/disables the trend controls. If true trend will be enabled.
380 		 * 
381 		 * @param enable
382 		 */
383 		public void toggleTrendControls(boolean enable) {
384 			trendCheckBox.setVisible(enable);
385 			trendLength.setVisible(enable);
386 			trendLengthLabel.setVisible(enable);
387 		}
388 		
389 	}
390