1 package fr.ove.openmath.jome.model;
2
3 import java.util.*;
4 import fr.ove.openmath.jome.model.*;
5 import fr.ove.openmath.jome.model.events.*;
6 import fr.ove.openmath.jome.ctrlview.events.*;
7 import fr.ove.openmath.jome.model.evaluation.*;
8
9 /***
10 * This is an fictive operator which replaces a real part (real in a syntactical
11 * mathematical meaning) of the formula by an icon.<BR>
12 *
13 * <CODE>Icon</CODE> represents a node in the formula tree structure.<BR>
14 * Its children are the iconified parts of the formula.
15 *
16 * @author © 1998 DIRAT Laurent
17 * @version 1.0 23/07/98
18 */
19 public class Icon extends FormulaTreeStructure {
20 /***
21 * The icon name.
22 */
23 private String iconName = null;
24
25 /***
26 * The list of iconified elements of the formula.
27 */
28 private Vector iconified = new Vector();
29
30 /***
31 * The rank in the FTS of the iconified elements.
32 */
33 private Vector rankOfIconified = new Vector();
34
35
36
37
38
39
40 /***
41 * Sets if the icon represents a substitution or not.<BR>
42 * The default is no, this means that the iconification process is an "automatic" one,
43 * i.e. the icon represents what is iconified.<BR>
44 * The substitution is when the user want to replace a selection by a friendly term.
45 * (Ex : Let a+b be EXP, the expression a+b+c becomes EXP+c)
46 */
47 private boolean isSubstitution = false;
48
49 /***
50 * The Constructor.<BR>
51 * @param toIconify the element (or part of the element, according to the case) to iconify
52 * in order to get the information for the icon name
53 *
54 */
55 public Icon(FormulaTreeStructure toIconify) {
56 this(toIconify.getIconName());
57 isSubstitution = false;
58 }
59
60 /***
61 * The Constructor.
62 * @param iconName the name of the icon
63 */
64 public Icon(String iconName) {
65 setResourceIdentifier("ICON");
66 setAsOperatorPriority(resourcesManager.getAsOperatorPriority("constantPriorities"));
67 setAsOperandPriority(resourcesManager.getAsOperandPriority("constantPriorities"));
68 isSubstitution = true;
69 this.iconName = iconName;
70 }
71
72 /***
73 * Inserts the icon (node) in the formula tree structure.<BR>
74 * @param current the position in the formula tree where the operator is to be insert.
75 * @return the new current position int hte formula tree.
76 */
77 public FormulaTreeStructure insert(FormulaTreeStructure current) {
78
79
80
81
82
83
84
85 FormulaTreeStructure father = (FormulaTreeStructure) current.getFather();
86
87 int rank = current.getRank();
88
89 father.addChild(this, rank);
90
91 for (int i = 0; i < iconified.size(); i++)
92 father.removeChild((FormulaTreeStructure) iconified.elementAt(i));
93
94 return null;
95 }
96
97 /***
98 * To check is the instance is an operator.
99 * @return <CODE>true</CODE> if it is an operator. <CODE>false</CODE> otherwise.
100 */
101 public boolean isOperator() {
102 return false;
103 }
104
105 /***
106 * The Creation of the corresponding linear expression of the formula.
107 */
108 public String createLinear(String linear) {
109
110
111
112
113 int nbIconified = iconified.size();
114 if (nbIconified == 1)
115 linear = getIconified(0).createLinear(linear);
116 else {
117 FormulaTreeStructure father = (FormulaTreeStructure) getIconified(0).getFather();
118
119 father = (FormulaTreeStructure) father.duplicate();
120
121 for (int i = 0; i < nbIconified; i++)
122 father.addChild(getIconified(i));
123
124 linear = father.createLinear(linear);
125 }
126
127 return linear;
128 }
129
130 /***
131 * Sets if the instance represents a substitution or not.
132 * @param isSubstitution <CODE>true</CODE> if the instance is a substitution.
133 * <CODE>false</CODE> otherwise.
134 */
135 public void setIsSubstitution(boolean isSubstitution) {
136 this.isSubstitution = isSubstitution;
137 }
138
139 /***
140 * Returns <CODE>true</CODE> if the instance is a substitution.
141 * <CODE>false</CODE> otherwise.
142 */
143 public boolean isSubstitution() {
144 return isSubstitution;
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158 public void addIconified(FormulaTreeStructure fts) {
159 iconified.addElement(fts);
160
161 rankOfIconified.addElement(new Integer(fts.getRank()));
162 }
163
164 /***
165 * Returns the number of iconified operators.
166 * @return the number of iconified operators.
167 */
168 public int getNbIconified() {
169 return iconified.size();
170 }
171
172 /***
173 * Returns the formula tree structure at the specified rank in the list of the fts to iconify.
174 * @param rank a rank into the list of fts to iconify.
175 * @return the fts at the specified rank.
176 */
177 public FormulaTreeStructure getIconified(int rank) {
178 return (FormulaTreeStructure) iconified.elementAt(rank);
179 }
180
181 /***
182 * Returns the rank in the formula tree structure of the iconified element at the specified
183 * rank in the list of iconified elments, before iconification.
184 * @param rank a rank into the list of fts to iconify.
185 * @return the rank in the FTS of the fts at the specified rank.
186 */
187 public int getRankOfIconified(int rank) {
188 return ((Integer) rankOfIconified.elementAt(rank)).intValue();
189 }
190
191 /***
192 * Evaluates the instance.
193 */
194 public String evaluate() {
195
196 return "";
197 }
198
199
200
201
202
203 /***
204 * @return <CODE>true</CODE> if the instance is an icon. <CODE>false</CODE> otherwise.
205 */
206 public boolean isIcon() {
207 return true;
208 }
209
210 /***
211 * Associates an icon name to the instance.
212 * @param iconName the name of the icon
213 */
214 public void setIconName(String iconName) {
215 this.iconName = iconName;
216 }
217
218 /***
219 * Returns the name of the icon associated to the instance.
220 *
221 * @returns The name of the icon, or <CODE>null</CODE> if there is no name
222 * associated.
223 */
224 public String getIconName() {
225 if (!isSubstitution) {
226
227
228
229
230 if (iconified.size() == 1)
231 iconName = getIconified(0).getIconName();
232 else
233 iconName = ((FormulaTreeStructure) getFather()).getIconName();
234 }
235
236 return iconName;
237 }
238
239 /***
240 * Returns <CODE>true</CODE> if the instance is iconifiable.
241 * <CODE>false</CODE> otherwise.
242 */
243 public boolean isIconifiable() {
244 return false;
245 }
246
247 /***
248 * Iconifies the instance.
249 */
250 public void iconify() {
251
252 }
253
254 /***
255 * Uniconifies the instance.
256 */
257 public void uniconify() {
258 FormulaTreeStructure father = (FormulaTreeStructure) getFather();
259
260 father.removeChild(this);
261
262
263
264 int fatherNbChildren = father.getNbChildren();
265 int countIconified = iconified.size();
266 FormulaTreeStructure iconifiedFts = null;
267 int rankOfIconifiedFts;
268 for (int i = 0; i < countIconified; i++) {
269 iconifiedFts = (FormulaTreeStructure) iconified.elementAt(i);
270 rankOfIconifiedFts = getRankOfIconified(i);
271 if (rankOfIconifiedFts > fatherNbChildren)
272 father.addChild(iconifiedFts);
273 else
274 father.addChild(iconifiedFts, rankOfIconifiedFts);
275
276
277
278 fatherNbChildren++;
279 }
280 }
281
282
283
284 }