|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SizeSelectorPanel.java | 61.1% | 81.1% | 92.3% | 77.1% |
|
1 |
/*
|
|
2 |
* SimplyHTML, a word processor based on Java, HTML and CSS
|
|
3 |
* Copyright (C) 2002 Ulrich Hilger
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
import javax.swing.JPanel;
|
|
21 |
import javax.swing.text.AttributeSet;
|
|
22 |
import javax.swing.text.SimpleAttributeSet;
|
|
23 |
import javax.swing.JSpinner;
|
|
24 |
import javax.swing.JComboBox;
|
|
25 |
import javax.swing.JLabel;
|
|
26 |
import java.awt.FlowLayout;
|
|
27 |
import java.awt.Dimension;
|
|
28 |
import javax.swing.SpinnerNumberModel;
|
|
29 |
import java.awt.event.ActionListener;
|
|
30 |
import java.awt.event.ActionEvent;
|
|
31 |
import javax.swing.event.ChangeListener;
|
|
32 |
import java.awt.event.KeyListener;
|
|
33 |
import java.awt.event.FocusListener;
|
|
34 |
import javax.swing.text.html.CSS;
|
|
35 |
import javax.swing.text.html.HTML;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Panel to show and manipulate a CSS size value
|
|
39 |
*
|
|
40 |
* <p>Added support for negative integers in stage 8.</p>
|
|
41 |
*
|
|
42 |
* @author Ulrich Hilger
|
|
43 |
* @author Light Development
|
|
44 |
* @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
|
|
45 |
* @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
|
|
46 |
* @author published under the terms and conditions of the
|
|
47 |
* GNU General Public License,
|
|
48 |
* for details see file gpl.txt in the distribution
|
|
49 |
* package of this software
|
|
50 |
*
|
|
51 |
* @version stage 11, April 27, 2003
|
|
52 |
*/
|
|
53 |
|
|
54 |
public class SizeSelectorPanel extends JPanel implements AttributeComponent, |
|
55 |
ActionListener |
|
56 |
{ |
|
57 |
|
|
58 |
private Object attributeKey;
|
|
59 |
private Object htmlAttrKey;
|
|
60 |
private JSpinner valueSelector;
|
|
61 |
private JComboBox unitSelector;
|
|
62 |
private JLabel unitName;
|
|
63 |
//private LengthValue lv;
|
|
64 |
|
|
65 |
private int setValueCalls = 0; |
|
66 |
private int originalValue = 0; |
|
67 |
private String originalUnit;
|
|
68 |
private boolean allowNegative = false; |
|
69 |
|
|
70 |
public static final String UNIT_PT = "pt"; |
|
71 |
public static final String UNIT_PERCENT = "%"; |
|
72 |
public static final String[] UNIT_VALUES = {UNIT_PT, UNIT_PERCENT}; |
|
73 |
|
|
74 |
public static final int UNIT_TYPE_PT = 0; |
|
75 |
public static final int UNIT_TYPE_PERCENT = 1; |
|
76 |
|
|
77 |
public static final int TYPE_NONE = 0; |
|
78 |
public static final int TYPE_LABEL = 1; |
|
79 |
public static final int TYPE_COMBO = 2; |
|
80 |
|
|
81 |
/**
|
|
82 |
* construct a basic SizeSelectorPanel with a
|
|
83 |
* JSpinner to select a value
|
|
84 |
*
|
|
85 |
* @param key the attribute key this instance of SizeSelectionPanel
|
|
86 |
* represents
|
|
87 |
* @param allowNegative true, if negative values are to be allowed in the
|
|
88 |
* panel, false if not
|
|
89 |
*/
|
|
90 | 37 |
public SizeSelectorPanel(Object key, Object htmlKey, boolean allowNegative) { |
91 | 37 |
super(new FlowLayout()); |
92 | 37 |
attributeKey = key; |
93 | 37 |
htmlAttrKey = htmlKey; |
94 | 37 |
valueSelector = new JSpinner(new SpinnerNumberModel()); |
95 | 37 |
Dimension dim = new Dimension(50, 24);
|
96 | 37 |
valueSelector.setMinimumSize(dim); |
97 | 37 |
valueSelector.setPreferredSize(dim); |
98 | 37 |
add(valueSelector); |
99 | 37 |
originalUnit = getUnit(); |
100 | 37 |
this.allowNegative = allowNegative;
|
101 |
} |
|
102 |
|
|
103 |
/**
|
|
104 |
* construct a SizeSelectorPanel with a
|
|
105 |
* JSpinner to select a value and either a JComboBox to select a given
|
|
106 |
* unit for the selection value or a JLabel showing a fixed unit.
|
|
107 |
*
|
|
108 |
* @param key the attribute key this instance of SizeSelectionPanel
|
|
109 |
* represents
|
|
110 |
* @param allowNegative true, if negative values are to be allowed in the
|
|
111 |
* panel, false if not
|
|
112 |
* @param type the type of unit indicator, one of TYPE_LABEL and
|
|
113 |
* TYPE_COMBO
|
|
114 |
*/
|
|
115 | 37 |
public SizeSelectorPanel(Object key, Object htmlKey, boolean allowNegative, int type) |
116 |
{ |
|
117 | 37 |
this(key, htmlKey, allowNegative);
|
118 | 37 |
switch(type) {
|
119 | 33 |
case TYPE_LABEL:
|
120 |
//System.out.println("SizeSelectorPanel constructor setting Label");
|
|
121 | 33 |
unitName = new JLabel(UNIT_PT);
|
122 | 33 |
add(unitName); |
123 | 33 |
break;
|
124 | 4 |
case TYPE_COMBO:
|
125 |
//System.out.println("SizeSelectorPanel constructor setting COMBO");
|
|
126 | 4 |
unitSelector = new JComboBox(UNIT_VALUES);
|
127 | 4 |
unitSelector.addActionListener(this);
|
128 | 4 |
add(unitSelector); |
129 | 4 |
break;
|
130 |
} |
|
131 | 37 |
originalUnit = getUnit(); |
132 |
} |
|
133 |
|
|
134 | 2 |
public void actionPerformed(ActionEvent ae) { |
135 | 2 |
if(ae.getSource().equals(unitSelector)) {
|
136 |
//System.out.println("actionPerformed is unitSelector new value = " + unitSelector.getSelectedItem().toString());
|
|
137 | 2 |
adjustMinMax(unitSelector.getSelectedItem().toString()); |
138 |
} |
|
139 |
} |
|
140 |
|
|
141 | 34 |
public void setValue(String val) { |
142 |
//System.out.println("SizeSelectorPanel setValue STRING, val=" + val);
|
|
143 | 34 |
String unit = null;
|
144 | 34 |
int newVal = 0;
|
145 |
//if(attributeKey instanceof CSS.Attribute) {
|
|
146 |
//lv = new LengthValue(val);
|
|
147 | 34 |
float aVal = Util.getAbsoluteAttrVal(val);
|
148 |
//System.out.println("SizeSelectorPanel aVal=" + aVal);
|
|
149 | 34 |
unit = Util.getLastAttrUnit(); //lv.getUnit();
|
150 |
//System.out.println("SizeSelectorPanel unit=" + unit);
|
|
151 | 34 |
adjustMinMax(unit); |
152 | 34 |
if(unitSelector != null) { |
153 |
//System.out.println("SizeSelectorPanel setValue setting combo");
|
|
154 | 2 |
unitSelector.setSelectedItem(unit); |
155 |
} |
|
156 | 32 |
else if(unitName != null) { |
157 |
//System.out.println("SizeSelectorPanel setValue setting label");
|
|
158 | 32 |
unitName.setText(unit); |
159 |
} |
|
160 | 34 |
newVal = (int) aVal; // new Float(lv.getValue(100)).intValue(); |
161 |
//System.out.println("SizeSelectorPanel setValue newVal=" + newVal);
|
|
162 | 34 |
valueSelector.setValue(new Integer(newVal));
|
163 |
//}
|
|
164 |
/*
|
|
165 |
else {
|
|
166 |
newVal = Integer.parseInt(val);
|
|
167 |
valueSelector.setValue(new Integer(val));
|
|
168 |
unit = UNIT_PT;
|
|
169 |
}
|
|
170 |
*/
|
|
171 | 34 |
if(++setValueCalls < 2) {
|
172 | 32 |
originalValue = newVal; |
173 | 32 |
originalUnit = unit; |
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
/**
|
|
178 |
* set the value of this <code>AttributeComponent</code>
|
|
179 |
*
|
|
180 |
* @param a the set of attributes possibly having an
|
|
181 |
* attribute this component can display
|
|
182 |
*
|
|
183 |
* @return true, if the set of attributes had a matching attribute,
|
|
184 |
* false if not
|
|
185 |
*/
|
|
186 | 8 |
public boolean setValue(AttributeSet a) { |
187 | 8 |
boolean success = false; |
188 |
//System.out.println("SizeSelectorPanel setValue SET attributeKey=" + attributeKey + ", htmlAttrKey=" + htmlAttrKey);
|
|
189 | 8 |
Object valObj = a.getAttribute(attributeKey); |
190 | 8 |
if(valObj != null) { |
191 |
//System.out.println("SizeSelectorPanel CSS valObj=" + valObj);
|
|
192 | 6 |
setValue(valObj.toString()); |
193 | 6 |
success = true;
|
194 |
} |
|
195 |
else {
|
|
196 | 2 |
if(htmlAttrKey != null) { |
197 | 0 |
valObj = a.getAttribute(htmlAttrKey); |
198 | 0 |
if(valObj != null) { |
199 |
//System.out.println("SizeSelectorPanel HTML valObj=" + valObj);
|
|
200 | 0 |
setValue(valObj.toString()); |
201 | 0 |
success = true;
|
202 |
} |
|
203 |
} |
|
204 |
} |
|
205 | 8 |
return success;
|
206 |
} |
|
207 |
|
|
208 |
/**
|
|
209 |
* adjust the minimum and maximum values of the component
|
|
210 |
* according to the unit
|
|
211 |
*/
|
|
212 | 36 |
private void adjustMinMax(String unit) { |
213 |
//if(lv != null) {
|
|
214 | 36 |
SpinnerNumberModel model = |
215 |
(SpinnerNumberModel) valueSelector.getModel(); |
|
216 | 36 |
int minVal = 0;
|
217 | 36 |
if(allowNegative) {
|
218 | 0 |
minVal = Integer.MIN_VALUE; |
219 |
} |
|
220 | 36 |
if(unit.equalsIgnoreCase(UNIT_PERCENT)) {
|
221 |
//System.out.println("adjustMinMax percent");
|
|
222 | 4 |
model.setMinimum(new Integer(minVal));
|
223 | 4 |
model.setMaximum(new Integer(100));
|
224 |
} |
|
225 |
else {
|
|
226 |
//System.out.println("adjustMinMax pt");
|
|
227 | 32 |
model.setMinimum(new Integer(minVal));
|
228 | 32 |
model.setMaximum(new Integer(Integer.MAX_VALUE));
|
229 |
} |
|
230 |
//}
|
|
231 |
|
|
232 |
} |
|
233 |
|
|
234 |
/**
|
|
235 |
* get the unit string of this SizeSelectorPanel
|
|
236 |
*
|
|
237 |
* @return the unit string (one of UNIT_PT and UNIT_PERCENT)
|
|
238 |
*/
|
|
239 | 110 |
public String getUnit() {
|
240 | 110 |
String unit = "";
|
241 | 110 |
if(unitSelector != null) { |
242 | 10 |
unit = unitSelector.getSelectedItem().toString(); |
243 |
} |
|
244 | 100 |
else if(unitName != null) { |
245 | 63 |
unit = unitName.getText(); |
246 |
} |
|
247 |
else {
|
|
248 | 37 |
unit = UNIT_PT; |
249 |
} |
|
250 | 110 |
if(unit.equalsIgnoreCase(UNIT_PT)) {
|
251 | 86 |
unit = "";
|
252 |
} |
|
253 | 110 |
return unit;
|
254 |
} |
|
255 |
|
|
256 | 24 |
public boolean valueChanged() { |
257 | 24 |
Integer value = (Integer) valueSelector.getValue(); |
258 | 24 |
return ((value.intValue() != originalValue) || (getUnit() != originalUnit));
|
259 |
} |
|
260 |
|
|
261 | 4 |
public String getAttr() {
|
262 | 4 |
Integer value = (Integer) valueSelector.getValue(); |
263 | 4 |
String unit = getUnit(); |
264 | 4 |
return value.toString() + unit;
|
265 |
} |
|
266 |
|
|
267 | 8 |
public Integer getIntValue() {
|
268 | 8 |
return (Integer) valueSelector.getValue();
|
269 |
} |
|
270 |
|
|
271 |
/**
|
|
272 |
* get the value of this <code>AttributeComponent</code>
|
|
273 |
*
|
|
274 |
* @return the value selected from this component
|
|
275 |
*/
|
|
276 | 8 |
public AttributeSet getValue() {
|
277 | 8 |
SimpleAttributeSet a = new SimpleAttributeSet();
|
278 | 8 |
Integer value = getIntValue(); |
279 | 8 |
String unit = getUnit(); |
280 | 8 |
if(valueChanged()) {
|
281 | 2 |
if(attributeKey instanceof CSS.Attribute) { |
282 |
//a.addAttribute(attributeKey, value.toString() + unit);
|
|
283 | 0 |
Util.styleSheet().addCSSAttribute(a, |
284 |
(CSS.Attribute) attributeKey, value.toString() + unit); |
|
285 |
} |
|
286 |
else {
|
|
287 | 2 |
a.addAttribute(attributeKey, value.toString()); |
288 | 2 |
if(htmlAttrKey != null) { |
289 | 0 |
a.addAttribute(htmlAttrKey, value.toString()); |
290 |
} |
|
291 |
} |
|
292 |
} |
|
293 |
//System.out.println("SizeSelectorPanel getValue()='" + a + "'");
|
|
294 | 8 |
return a;
|
295 |
} |
|
296 |
|
|
297 | 0 |
public AttributeSet getValue(boolean includeUnchanged) { |
298 | 0 |
if(includeUnchanged) {
|
299 | 0 |
SimpleAttributeSet a = new SimpleAttributeSet();
|
300 | 0 |
Integer value = getIntValue(); |
301 | 0 |
String unit = getUnit(); |
302 | 0 |
if(attributeKey instanceof CSS.Attribute) { |
303 |
//a.addAttribute(attributeKey, value.toString() + unit);
|
|
304 | 0 |
Util.styleSheet().addCSSAttribute(a, |
305 |
(CSS.Attribute) attributeKey, value.toString() + unit); |
|
306 |
} |
|
307 |
else {
|
|
308 | 0 |
a.addAttribute(attributeKey, value.toString()); |
309 | 0 |
if(htmlAttrKey != null) { |
310 | 0 |
a.addAttribute(htmlAttrKey, value.toString()); |
311 |
} |
|
312 |
} |
|
313 |
//System.out.println("SizeSelectorPanel getValue()='" + a + "'");
|
|
314 | 0 |
return a;
|
315 |
} |
|
316 |
else {
|
|
317 | 0 |
return getValue();
|
318 |
} |
|
319 |
} |
|
320 |
|
|
321 | 2 |
public JSpinner getValueSelector() {
|
322 | 2 |
return valueSelector;
|
323 |
} |
|
324 |
|
|
325 |
} |
|