View Javadoc

1   package fr.ove.openmath.jome.ctrlview.bidim;
2   
3   import java.awt.*;
4   import java.io.Serializable;
5   import java.lang.IllegalArgumentException;
6   import fr.ove.openmath.jome.ctrlview.bidim.Display;
7   import fr.ove.openmath.jome.ctrlview.bidim.selection.events.SelectionEvent;
8   
9   /***
10  * A layout manager.
11  *
12  * @author © 1999 DIRAT Laurent
13  * @version 2.0 13/12/1999
14  */
15  public abstract class DisplayLayout implements LayoutManager2, Serializable , Cloneable {
16      /***
17      * The display laid out by the instance.
18      */
19      protected Display displayToLay = null;
20      
21      
22      /***
23      * According to the operator, the layout manager has to add some components (e.g. brackets, ...)
24      * or has to perform some "re-oganisation" before rendering.<BR>
25      * As soon as the layout manager is set to the display, this mehtod MUST be called with the display laid out
26      * as parameter. This method serves as well as a registering method. So all sub-classes of the instance MUST
27      * call super.initDisplay(displayToLay).
28      * @param displayToLay the display laid by the instance
29      */
30      public void initDisplay(Display displayToLay) {
31          this.displayToLay = displayToLay;
32      }
33      
34      
35      /***
36      * Returns <CODE>true</CODE> if during the selection, the display which is located
37      * to the left of the display that the instance lays, must be selected too.
38      * <CODE>false</CODE> otherwise.
39      */
40      public boolean selectLeftDisplay() {
41          return false;
42      }
43      
44      /***
45      * Returns <CODE>true</CODE> if during the selection, the display which is located
46      * to the right of the display that the instance lays, must be selected too.
47      * <CODE>false</CODE> otherwise.
48      */
49      public boolean selectRightDisplay() {
50          return false;
51      }
52      
53      /***
54      * Selects the display (and its children if any)
55      * @param the display to select.
56      */
57      public void selectDisplay() {
58          displayToLay.select();
59          
60          SelectionEvent selEvt = new SelectionEvent(displayToLay);
61          // On purge la liste des ÈlÈments sÈlectionnÈs.
62          selEvt.setAction(SelectionEvent.PURGE, null);
63          displayToLay.fireSelectionEvent(selEvt);
64          selEvt.setAction(SelectionEvent.ADD, displayToLay);
65          displayToLay.fireSelectionEvent(selEvt);
66          
67          Display display = displayToLay;
68          if (displayToLay.getParent() instanceof Display) {
69              display = (Display) displayToLay.getParent();
70              ((DisplayLayout) display.getLayout()).validateSelection();
71          }
72      }
73  
74  
75      /***
76      * Deselects the display.
77      * @param the display to deselect.
78      */
79      public void deselectDisplay() {
80          displayToLay.deselect();
81  
82          SelectionEvent selEvt = new SelectionEvent(displayToLay);
83          selEvt.setAction(SelectionEvent.REMOVE, displayToLay);
84          displayToLay.fireSelectionEvent(selEvt);
85          
86          if (displayToLay.getParent() instanceof Display) {
87              Display display = (Display) displayToLay.getParent();
88              ((DisplayLayout) display.getLayout()).validateDeselection(displayToLay);
89          }
90      }
91      
92      
93      /***
94      * Updates the level of the display laid out.
95      * @param display the display laid out
96      * @param level the level put to the display.
97      */
98      public void updateLevel(int level) {
99          Display tmp;
100         
101         if (displayToLay.getUpdateLevel()) {
102             // On met le niveau adÈquat
103             displayToLay.setLevel(level);
104             // On scale le display en fonction du niveau
105             displayToLay.scaleDisplay();
106             // On n'a plus besoin de mettre ? jour le niveau de l'instance
107             displayToLay.setUpdateLevel(false);
108             // Le comportement par dÈfaut est que les displays enfant de l'instance courante
109             // ont le mÍme niveau que leur pËre.
110             int count = displayToLay.getComponentCount();
111             for (int i = 0; i < count; i++) {
112                 tmp = (Display) displayToLay.getComponent(i);
113                 ((DisplayLayout) tmp.getLayout()).updateLevel(level);
114             }
115         }
116     }
117     
118     
119     // ############################################
120     // ### Les diffÈrentes mÈthodes abstraites  ###
121     // ############################################
122     
123     
124     /***
125     * Checks the validity of the selection.
126     */
127     public abstract void validateSelection();
128     
129     /***
130     * Checks the validity of the deselection.<BR>
131     * @param display the display which just has been deselected.
132     */
133     public abstract void validateDeselection(Display display);
134     
135     /***
136     * The display needs to be rebuilt. We do this.
137     */
138     public abstract void rebuildDisplay();
139     
140     /***
141     * Computes the size of the display according to its children size (if any),
142     * and its different attributes.
143     * @param display the display laid by the instance
144     * @return the size of the display.
145     */
146     public abstract Dimension computeAttributes();
147     
148     // #################################################
149     // ### ImplÈmentation des diffÈrentes interfaces ###
150     // #################################################
151     
152     // ***************************************************
153     // *** ImplÈmentation de l'interface LayoutManager ***
154     
155     /***
156      * Adds the specified component with the specified name to
157      * the layout.
158      * @param name the component name
159      * @param comp the component to be added
160      */
161     public void addLayoutComponent(String name, Component comp) {
162     }
163 
164     /***
165      * Removes the specified component from the layout.
166      * @param comp the component ot be removed
167      */
168     public void removeLayoutComponent(Component comp) {
169     }
170 
171     /*** 
172      * Calculates the minimum size dimensions for the specified 
173      * panel given the components in the specified parent container.
174      * @param parent the component to be laid out
175      * @see #preferredLayoutSize
176      */
177     public Dimension minimumLayoutSize(Container parent) {
178         return preferredLayoutSize(parent);
179     }
180     
181     /***
182      * Calculates the preferred size dimensions for the specified 
183      * panel given the components in the specified parent container.
184      * @param parent the component to be laid out
185      *  
186      * @see #minimumLayoutSize
187      */
188     public Dimension preferredLayoutSize(Container parent) {
189         if (((Display) parent).getComputeAttributes())
190             return computeAttributes();
191         else
192             return parent.getSize();
193     }
194     
195     // *** Fin de l'interface LayoutManager ***
196     // ****************************************
197     
198     
199     // ****************************************************
200     // *** ImplÈmentation de l'interface LayoutManager2 ***
201 
202     /***
203      * Adds the specified component to the layout, using the specified
204      * constraint object.
205      * @param comp the component to be added
206      * @param constraints  where/how the component is added to the layout.
207      */
208     public void addLayoutComponent(Component comp, Object constraints) {
209 	    if (!(constraints instanceof Display))
210     		throw new IllegalArgumentException("The component to add must be a Display instance !!!");
211     }
212 
213     /*** 
214      * Returns the maximum size of this component.
215      * @see java.awt.Component#getMinimumSize()
216      * @see java.awt.Component#getPreferredSize()
217      * @see LayoutManager
218      */
219     public Dimension maximumLayoutSize(Container target) {
220         return preferredLayoutSize(target);
221     }
222 
223     /***
224      * Returns the alignment along the x axis.  This specifies how
225      * the component would like to be aligned relative to other 
226      * components.  The value should be a number between 0 and 1
227      * where 0 represents alignment along the origin, 1 is aligned
228      * the furthest away from the origin, 0.5 is centered, etc.
229      */
230     public float getLayoutAlignmentX(Container target) {
231         return 0.0f;
232     }
233 
234     /***
235      * Returns the alignment along the y axis.  This specifies how
236      * the component would like to be aligned relative to other 
237      * components.  The value should be a number between 0 and 1
238      * where 0 represents alignment along the origin, 1 is aligned
239      * the furthest away from the origin, 0.5 is centered, etc.
240      */
241     public float getLayoutAlignmentY(Container target) {
242         return 0.0f;
243     }
244 
245     /***
246      * Invalidates the layout, indicating that if the layout manager
247      * has cached information it should be discarded.
248      */
249     public void invalidateLayout(Container target) {
250     }
251     
252     // *** Fin de l'interface LayoutManager ***
253     // ****************************************
254 }
255