1 package fr.ove.openmath.jome.ctrlview.bidim;
2
3 import java.awt.*;
4 import fr.ove.openmath.jome.ctrlview.bidim.*;
5 import fr.ove.openmath.jome.ctrlview.bidim.selection.events.SelectionEvent;
6 import fr.ove.openmath.jome.model.*;
7 import fr.ove.openmath.jome.behaviour.*;
8
9 /***
10 * A layout manager that lays components to be displayed between something.<BR>
11 * Obviously, what is called something will be parenthesis, braces, ....
12 *
13 * @author © 1999 DIRAT Laurent
14 * @version 2.0 13/12/1999
15 */
16 public abstract class EnclosingLayout extends HorizontalLayout {
17 /***
18 * The opening of the enclosure
19 */
20 private SymbolDisplay opening;
21
22 /***
23 * The closing of the enclosure
24 */
25 private SymbolDisplay closing;
26
27 /***
28 * Returns the opening
29 */
30 public SymbolDisplay getOpening() {
31 return opening;
32 }
33
34 /***
35 * Returns the closing
36 */
37 public SymbolDisplay getClosing() {
38 return closing;
39 }
40
41 /***
42 * According to the operator, the layout manager has to add some components (e.g. brackets, ...)
43 * or has to perform some "re-oganisation" before rendering.<BR>
44 * As soon as the layout manager is set to the display, this mehtod MUST be called with the display laid out
45 * as parameter. This method serves as well as a registering method. So all sub-classes of the instance MUST
46 * call super.initDisplay(displayToLay).
47 * @param displayToLay the display laid by the instance
48 */
49 public void initDisplay(Display displayToLay) {
50 super.initDisplay(displayToLay);
51
52 FormulaTreeStructure fts = (FormulaTreeStructure) displayToLay.getListener();
53 if (((Maskable) fts).isVisible()) {
54 opening = createOpening();
55
56
57
58
59
60
61
62
63
64 opening.addControlListener(fts);
65 this.displayToLay.add(opening);
66
67 closing = createClosing();
68 closing.addControlListener(fts);
69 this.displayToLay.add(closing);
70 }
71 }
72
73 /***
74 * Checks the validity of the selection.
75 */
76 public void validateSelection() {
77 SelectionEvent selEvt = new SelectionEvent(displayToLay);
78
79 if (((Maskable) displayToLay.getListener()).isVisible()) {
80
81
82
83 if (opening.isSelected() || closing.isSelected()) {
84
85 displayToLay.select();
86
87 selEvt.setAction(SelectionEvent.PURGE, null);
88 displayToLay.fireSelectionEvent(selEvt);
89
90 selEvt.setAction(SelectionEvent.ADD, displayToLay);
91 displayToLay.fireSelectionEvent(selEvt);
92 }
93 }
94
95
96
97 Display display = displayToLay;
98 if (displayToLay.getParent() instanceof Display) {
99 display = (Display) displayToLay.getParent();
100 FormulaTreeStructure fts = (FormulaTreeStructure) display.getListener();
101 if (fts.getFather() != null)
102 ((DisplayLayout) display.getLayout()).validateSelection();
103 }
104
105
106 display.repaint();
107 }
108
109 /***
110 * Checks the validity of the deselection.
111 * @param display the display to deselect.
112 */
113 public void validateDeselection(Display display) {
114 Display father = displayToLay;
115 Display tmp;
116 SelectionEvent selEvt = new SelectionEvent(displayToLay);
117
118
119 if (father.isSelected()) {
120 father.setNotSelected();
121
122 selEvt.setAction(SelectionEvent.REMOVE, father);
123 father.fireSelectionEvent(selEvt);
124
125 if (((Maskable) displayToLay.getListener()).isVisible()) {
126 if (display == opening)
127 closing.setNotSelected();
128 else
129 opening.setNotSelected();
130
131
132 selEvt.setAction(SelectionEvent.ADD, (Display) father.getComponent(2));
133 father.fireSelectionEvent(selEvt);
134 }
135
136
137 if (father.getParent() instanceof Display) {
138 father = (Display) father.getParent();
139 FormulaTreeStructure fts = (FormulaTreeStructure) father.getListener();
140 if (fts.getFather() != null)
141 ((DisplayLayout) father.getLayout()).validateDeselection(displayToLay);
142 }
143
144
145
146
147 validateSelection();
148
149
150 father.repaint();
151 }
152
153 }
154
155 /***
156 * Computes the size of the display according to its children size (if any),
157 * and its different attributes.
158 * @return the size of the display.
159 */
160 public Dimension computeAttributes() {
161 Display display = null;
162
163 if (((Maskable) displayToLay.getListener()).isVisible()) {
164 Displayable openingSymbol = opening.getSymbol();
165 Displayable closingSymbol = closing.getSymbol();
166
167
168
169
170 openingSymbol.setHeight(0);
171 opening.setHeight(0);
172 closingSymbol.setHeight(0);
173 closing.setHeight(0);
174
175
176 opening.setAttributes(0, 0, 0, 0);
177 closing.setAttributes(0, 0, 0, 0);
178 ((Display) displayToLay.getComponent(2)).setShiftX(0);
179
180 Dimension dim = super.computeAttributes();
181
182
183
184
185 openingSymbol.setAscent(displayToLay.getAscent());
186 openingSymbol.setDescent(displayToLay.getDescent());
187 openingSymbol.setHeight(displayToLay.getHeight());
188 opening.setComputeAttributes(true);
189 opening.invalidate();
190 opening.setSize(opening.getPreferredSize());
191
192 closingSymbol.setAscent(displayToLay.getAscent());
193 closingSymbol.setDescent(displayToLay.getDescent());
194 closingSymbol.setHeight(displayToLay.getHeight());
195 closing.setComputeAttributes(true);
196 closing.invalidate();
197 closing.setSize(closing.getPreferredSize());
198
199
200 closing.setShiftX(dim.width - 2*closing.getWidth());
201
202 ((Display) displayToLay.getComponent(2)).setShiftX(-closing.getShiftX() - closing.getWidth());
203
204 return dim;
205 }
206 else
207 return super.computeAttributes();
208 }
209
210 /***
211 * The display needs to be rebuilt. We do this.
212 */
213 public void rebuildDisplay() {
214
215
216 displayToLay.computeAncestorsAttributes();
217 }
218
219
220
221
222
223 /***
224 * Returns the opening
225 */
226 public abstract SymbolDisplay createOpening();
227
228 /***
229 * Returns the closing
230 */
231 public abstract SymbolDisplay createClosing();
232 }