View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components;
21  
22  import java.beans.Customizer;
23  
24  import javax.swing.BoxLayout;
25  import javax.swing.ButtonGroup;
26  import javax.swing.ButtonModel;
27  import javax.swing.JComponent;
28  import javax.swing.JPanel;
29  import javax.swing.JRadioButton;
30  
31  import com.cosylab.gui.components.range2.RescalingValuePolicy;
32  import com.cosylab.gui.components.range2.ShiftValuePolicy;
33  import com.cosylab.gui.components.range2.TrimValuePolicy;
34  
35  public class PiperCustomizer extends AbstractDisplayerPanelCustomizer {
36  	private static final long serialVersionUID = 1L;
37  	public static final String VALUE_DISPLAY = "Value Display";
38  	public static final String VALUE_POLICY = "Value Range Policy";
39  	private static final String UNITS = "units";
40  	private static final String UNITS_VISIBLE = "unitsVisible";
41  	private static final String FORMAT = "format";
42  	private static final String GRAPH_MIN = "minimum";
43  	private static final String GRAPH_MAX = "maximum";
44  	private static final String ENHANCED = "enhanced";
45  	private static final String STRETCH = "stretch";
46  	private static final String TILTING_ENABLED = "tiltingEnabled";
47  	private static final String RESIZABLE = "resizable";
48  	public static final String TITLE = "title";
49  	public static final String[] VISUAL_BASIC_PROPERTIES = {TITLE, RESIZABLE, ENHANCED,  TILTING_ENABLED, STRETCH };
50  	public static String[] VALUE_DISPLAY_PROPERTIES = {
51  		GRAPH_MIN, GRAPH_MAX, UNITS,UNITS_VISIBLE, FORMAT
52  	};
53  
54  	/** Supported aspects. */
55  	public static String[] ASPECTS = { VISUAL_BASIC, VALUE_DISPLAY, VALUE_POLICY };
56  	
57  	/*
58  	 * Auxilary Editor implementation for customizing DisplayerPanel layout.
59  	 */
60  	private class WPanel extends JPanel implements Customizer
61  	{
62  		private static final long serialVersionUID = 1L;
63  		private Piper displayer;
64  		private JRadioButton trimButton;
65  		private JRadioButton rescalingButton;
66  		private JRadioButton shiftButton;
67  		private ButtonGroup bg;
68  
69  		/**
70  		 * Creates a new WPanel object.
71  		 */
72  		public WPanel()
73  		{
74  			bg = new ButtonGroup();
75  
76  			trimButton = new JRadioButton("Trim Value Range");
77  			rescalingButton = new JRadioButton("Rescale Value Range");
78  			shiftButton = new JRadioButton("Shift Value Range");
79  
80  			bg.add(trimButton);
81  			bg.add(rescalingButton);
82  			bg.add(shiftButton);
83  
84  			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
85  			add(trimButton);
86  			add(rescalingButton);
87  			add(shiftButton);
88  		}
89  
90  		/**
91  		 * DOCUMENT ME!
92  		 *
93  		 * @param object DOCUMENT ME!
94  		 * @param aspect DOCUMENT ME!
95  		 *
96  		 * @return DOCUMENT ME!
97  		 *
98  		 * @throws IllegalArgumentException DOCUMENT ME!
99  		 */
100 		public JComponent getEditorComponent(Object object, String aspect)
101 			throws IllegalArgumentException
102 		{
103 			if (aspect.equals(VALUE_POLICY)) {
104 				displayer = (Piper)object;
105 				initialize();
106 
107 				return this;
108 			}
109 
110 			return null;
111 		}
112 
113 		private void initialize()
114 		{
115 			if (displayer == null) {
116 				return;
117 			}
118 
119 			if (displayer.getValuePolicy() instanceof RescalingValuePolicy) {
120 				rescalingButton.setSelected(true);
121 			}
122 			if (displayer.getValuePolicy() instanceof TrimValuePolicy) {
123 				trimButton.setSelected(true);
124 			}
125 			if (displayer.getValuePolicy() instanceof ShiftValuePolicy) {
126 				shiftButton.setSelected(true);
127 			}
128 		}
129 
130 		/**
131 		 * DOCUMENT ME!
132 		 *
133 		 * @param object DOCUMENT ME!
134 		 * @param aspect DOCUMENT ME!
135 		 *
136 		 * @return DOCUMENT ME!
137 		 */
138 		public boolean canEdit(Object object, String aspect)
139 		{
140 			return aspect.equals(VALUE_POLICY);
141 		}
142 
143 		/**
144 		 * DOCUMENT ME!
145 		 */
146 		public void applySettings()
147 		{
148 			ButtonModel bm = bg.getSelection();
149 
150 			if (bm == null) {
151 				return;
152 			}
153 
154 			if (bm == shiftButton.getModel()) {
155 				displayer.setValuePolicy(new ShiftValuePolicy());
156 			} else if (bm == rescalingButton.getModel()) {
157 				displayer.setValuePolicy(new RescalingValuePolicy());
158 			} else if (bm == trimButton.getModel()) {
159 				displayer.setValuePolicy(new TrimValuePolicy());
160 			} else {
161 				throw new AssertionError();
162 			}
163 
164 			initialize();
165 		}
166 
167 		/**
168 		 * DOCUMENT ME!
169 		 */
170 		public void revertSettings()
171 		{
172 		}
173 
174 		/**
175 		 * DOCUMENT ME!
176 		 */
177 		public void stopEditing()
178 		{
179 		}
180 
181 		public void setObject(Object bean) {
182 			displayer = (Piper) bean;
183 		}
184 	}
185 
186 	public PiperCustomizer() {
187 		addCustomizerTable(VISUAL_BASIC, VISUAL_BASIC_PROPERTIES);
188 		addCustomizerTable(VALUE_DISPLAY, VALUE_DISPLAY_PROPERTIES);
189 		addCustomizer(VALUE_POLICY, new WPanel());
190 		setSize(331, 185);
191 	}
192 }