|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AttributeComboBox.java | 41.2% | 59.3% | 66.7% | 53.2% |
|
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.JComboBox;
|
|
21 |
import javax.swing.text.AttributeSet;
|
|
22 |
import javax.swing.text.SimpleAttributeSet;
|
|
23 |
import javax.swing.text.html.CSS;
|
|
24 |
import javax.swing.text.html.HTML;
|
|
25 |
import javax.swing.text.StyleConstants;
|
|
26 |
|
|
27 |
/**
|
|
28 |
* ComboBox to show and manipulate an attribute out
|
|
29 |
* of a given set of attribute values.
|
|
30 |
*
|
|
31 |
* @author Ulrich Hilger
|
|
32 |
* @author Light Development
|
|
33 |
* @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
|
|
34 |
* @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
|
|
35 |
* @author published under the terms and conditions of the
|
|
36 |
* GNU General Public License,
|
|
37 |
* for details see file gpl.txt in the distribution
|
|
38 |
* package of this software
|
|
39 |
*
|
|
40 |
* @version stage 11, April 27, 2003
|
|
41 |
*/
|
|
42 |
|
|
43 |
public class AttributeComboBox extends JComboBox implements |
|
44 |
AttributeComponent |
|
45 |
{ |
|
46 |
|
|
47 |
/** CSS attribute key associated with this component */
|
|
48 |
private Object attributeKey;
|
|
49 |
|
|
50 |
/** HTML attribute key associated with this component */
|
|
51 |
private Object htmlAttributeKey;
|
|
52 |
|
|
53 |
/** attribute names associated with the items of this component */
|
|
54 |
private String[] names;
|
|
55 |
|
|
56 |
/** indicates wether or not a call to setValue is the initial one */
|
|
57 |
private int setValCount = 0; |
|
58 |
|
|
59 |
/** stores the initial value for tracking changes */
|
|
60 |
private int originalIndex = -2; |
|
61 |
|
|
62 |
/**
|
|
63 |
* construct an <code>AttributeComboBox</code>
|
|
64 |
*
|
|
65 |
* @param items the items to appear in the list of this component
|
|
66 |
* @param names the attributes to associate with items
|
|
67 |
* (in the same order)
|
|
68 |
* @param key the CSS attribute key this component represents
|
|
69 |
* @param htmlKey the HTL attribute key this component represents
|
|
70 |
*
|
|
71 |
* @see getValue
|
|
72 |
*/
|
|
73 | 8 |
public AttributeComboBox(String[] items, String[] names, Object key,
|
74 |
Object htmlKey) |
|
75 |
{ |
|
76 | 8 |
super(items);
|
77 | 8 |
this.names = names;
|
78 | 8 |
attributeKey = key; |
79 | 8 |
htmlAttributeKey = htmlKey; |
80 |
} |
|
81 |
|
|
82 |
/**
|
|
83 |
* set the value of this <code>AttributeComponent</code>
|
|
84 |
*
|
|
85 |
* @param a the set of attributes possibly having an
|
|
86 |
* attribute this component can display
|
|
87 |
*
|
|
88 |
* @return true, if the set of attributes had a matching attribute,
|
|
89 |
* false if not
|
|
90 |
*/
|
|
91 | 7 |
public boolean setValue(AttributeSet a) { |
92 |
//System.out.println("AttributeComboBox setValue");
|
|
93 |
//de.calcom.cclib.html.HTMLDiag hd = new de.calcom.cclib.html.HTMLDiag();
|
|
94 |
//hd.listAttributes(a, 2);
|
|
95 | 7 |
boolean success = false; |
96 | 7 |
Object valObj; |
97 | 7 |
if(attributeKey != null) |
98 |
{ |
|
99 | 7 |
valObj = a.getAttribute(attributeKey); |
100 | 7 |
if(valObj == null && htmlAttributeKey != null) |
101 |
{ |
|
102 | 6 |
valObj = a.getAttribute(htmlAttributeKey); |
103 | 6 |
if(valObj != null) |
104 |
{ |
|
105 | 1 |
setValue(valObj); |
106 | 1 |
success = true;
|
107 |
} |
|
108 |
} |
|
109 |
/*
|
|
110 |
correction start: missing list-style-type attribute from style sheet
|
|
111 |
*/
|
|
112 | 1 |
else if(valObj == null && |
113 |
attributeKey.equals(CSS.Attribute.LIST_STYLE_TYPE)) |
|
114 |
{ |
|
115 | 0 |
Object name = a.getAttribute(StyleConstants.NameAttribute); |
116 | 0 |
if(name != null && |
117 |
name.toString().equalsIgnoreCase(HTML.Tag.UL.toString())) |
|
118 |
{ |
|
119 | 0 |
setValue("disc");
|
120 |
} |
|
121 | 0 |
else if(name != null && |
122 |
name.toString().equalsIgnoreCase(HTML.Tag.OL.toString())) |
|
123 |
{ |
|
124 | 0 |
setValue("decimal");
|
125 |
} |
|
126 |
} |
|
127 |
/*
|
|
128 |
correction end: missing list-style-type attribute from style sheet
|
|
129 |
*/
|
|
130 |
else {
|
|
131 |
//System.out.println("AttributeComboBox setValue value=" + valObj);
|
|
132 | 1 |
setValue(valObj); |
133 | 1 |
success = true;
|
134 |
} |
|
135 |
} |
|
136 |
else
|
|
137 |
{ |
|
138 | 0 |
if(htmlAttributeKey != null) |
139 |
{ |
|
140 | 0 |
valObj = a.getAttribute(htmlAttributeKey); |
141 | 0 |
if(valObj != null) { |
142 | 0 |
setValue(valObj); |
143 | 0 |
success = true;
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 | 7 |
return success;
|
148 |
} |
|
149 |
|
|
150 | 0 |
public void reset() { |
151 | 0 |
setValCount = 0; |
152 | 0 |
originalIndex = -2; |
153 |
} |
|
154 |
|
|
155 | 2 |
private void setValue(Object valObj) { |
156 | 2 |
if(valObj != null) { |
157 | 2 |
String valStr = valObj.toString(); |
158 | 2 |
int i = 0;
|
159 | 2 |
while(!valStr.equalsIgnoreCase(names[i])) {
|
160 | 0 |
i++; |
161 |
} |
|
162 | 2 |
setSelectedIndex(i); |
163 | 2 |
if(++setValCount < 2) {
|
164 | 2 |
originalIndex = i; |
165 |
} |
|
166 |
} |
|
167 |
} |
|
168 |
|
|
169 |
/**
|
|
170 |
* get the value of this <code>AttributeComponent</code>
|
|
171 |
*
|
|
172 |
* <p>If one an attribute key is not set, the value will not
|
|
173 |
* be returned for that attribute key. If both attribute keys are
|
|
174 |
* set, two attributes with identical value are returned. Up
|
|
175 |
* to J2SE 1.4, the Java language does not render
|
|
176 |
* CSS.Attribute.VERTICAL_ALIGN but it does render HTML.Attribute.VALIGN.
|
|
177 |
* Some browsers handle it vice versa, so it is useful to
|
|
178 |
* store both attributes.</p>
|
|
179 |
*
|
|
180 |
* @return the value selected from this component
|
|
181 |
*/
|
|
182 | 6 |
public AttributeSet getValue() {
|
183 | 6 |
SimpleAttributeSet a = new SimpleAttributeSet();
|
184 | 6 |
int value = getSelectedIndex();
|
185 |
//System.out.println("AttributeComboBox getValue originalIndex=" + originalIndex + " value=" + value);
|
|
186 | 6 |
if(originalIndex != value) {
|
187 |
//System.out.println("changed " + attributeKey + " originalIndex=" + originalIndex + " value=" + value);
|
|
188 | 5 |
if(attributeKey != null) { |
189 | 4 |
Util.styleSheet().addCSSAttribute(a, (CSS.Attribute) attributeKey, names[value]); |
190 |
//a.addAttribute(attributeKey, names[value]);
|
|
191 |
} |
|
192 | 5 |
if(htmlAttributeKey != null) { |
193 | 5 |
a.addAttribute(htmlAttributeKey, names[value]); |
194 |
} |
|
195 |
} |
|
196 | 6 |
return a;
|
197 |
} |
|
198 |
|
|
199 | 0 |
public AttributeSet getValue(boolean includeUnchanged) { |
200 | 0 |
if(includeUnchanged) {
|
201 | 0 |
SimpleAttributeSet a = new SimpleAttributeSet();
|
202 | 0 |
int value = getSelectedIndex();
|
203 | 0 |
if(attributeKey != null) { |
204 | 0 |
Util.styleSheet().addCSSAttribute(a, (CSS.Attribute) attributeKey, names[value]); |
205 |
} |
|
206 | 0 |
if(htmlAttributeKey != null) { |
207 | 0 |
a.addAttribute(htmlAttributeKey, names[value]); |
208 |
} |
|
209 | 0 |
return a;
|
210 |
} |
|
211 |
else {
|
|
212 | 0 |
return getValue();
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
|
|