1 package fr.ove.openmath.mathematica;
2
3 import javax.swing.*;
4 import javax.swing.event.*;
5 import java.io.*;
6 import java.awt.*;
7 import java.util.*;
8 import java.beans.PropertyVetoException;
9
10 /***
11 * An extension of a JDesktopPane which handles the position of the new inner frames
12 * created, names them automatically, etc.
13 *
14 *
15 */
16 public class Desktop extends JDesktopPane {
17 /***
18 * The total number of inner frames opened.
19 */
20 private int nbUntitled = 0;
21
22 /***
23 * The next position of the next inner frame opened.
24 */
25 private Point framePosition = new Point(0,0);
26
27 /***
28 * The list of inner frames currently opened.
29 */
30 private Hashtable listOpened = new Hashtable();
31
32 /***
33 * Keeps the right order of inner frames opened
34 */
35 private Vector orderOpened = new Vector();
36
37 /***
38 * The list of content pane of the palette we want to have in the desktop.
39 */
40 private Hashtable palettesContentPanes;
41
42 /***
43 * Indicates if the new inner frame created is opened with its maximal size.<BR>
44 * The default is not.
45 */
46 private boolean maximize = false;
47
48 /***
49 * The Constructor.
50 */
51 public Desktop() {
52 setOpaque(false);
53 }
54
55 /***
56 * Adds a new inner frame with the specified content pane.
57 * @param contentPane the specified content pane.
58 * @return the inner frame added.
59 */
60 public JInternalFrame addNewInternalFrame(Container contentPane) {
61 JInternalFrame jif = createJInternalFrame(contentPane);
62 listOpened.put(jif.getTitle(), jif);
63 orderOpened.addElement(jif);
64 setLayer(jif, JLayeredPane.DEFAULT_LAYER.intValue());
65 add(jif);
66 try {
67 Dimension size = jif.getContentPane().getPreferredSize();
68 jif.setBounds(framePosition.x, framePosition.y, size.width, size.height);
69 computeNextFramePosition();
70
71 if (maximize)
72 jif.setMaximum(true);
73
74 jif.setSelected(true);
75 jif.setVisible(true);
76
77 } catch (PropertyVetoException pve) {
78 pve.printStackTrace();
79 }
80
81 return jif;
82 }
83
84 /***
85 * Add a new inner frame into the desktop that serves as a palette. It remains above
86 * the other "normal" inner frames which are in the desktop.
87 * @param contentPane the content of the inner frame.
88 *
89 public void addNewPalette(Container contentPane) {
90 JInternalFrame jif = new MyJInternalFrame("Palette Editor", this);
91 jif.setResizable(false);
92 jif.setClosable(true);
93 jif.setIconifiable(true);
94 jif.setMaximizable(false);
95 jif.setContentPane(contentPane);
96 setLayer(jif, JLayeredPane.PALETTE_LAYER.intValue());
97 try {
98 jif.pack();
99 jif.setSelected(true);
100 jif.setVisible(true);
101
102 } catch (PropertyVetoException pve) {
103 pve.printStackTrace();
104 }
105
106 add(jif);
107 }*/
108
109 /***
110 * Adds a new palette to the desktop.
111 * @param paletteIdentifier the palette identfier.
112 * @param contentPan the palette (i.e. the content pane for the inner frame opened in the desktop)
113 */
114 public void addNewPaletteContentPane(String paletteIdentifier, Container contentPane) {
115 if (palettesContentPanes == null)
116 palettesContentPanes = new Hashtable();
117
118 palettesContentPanes.put(paletteIdentifier, contentPane);
119 }
120
121 /***
122 * Shows the palette with the specified name identfier.
123 * @param identfier the palette identifier.
124 */
125 public void showPalette(String paletteIdentifier) {
126 Container contentPane = (Container) palettesContentPanes.get(paletteIdentifier);
127 if (contentPane != null) {
128
129 JInternalFrame[] allJifs = getAllFramesInLayer(JLayeredPane.PALETTE_LAYER.intValue());
130 JInternalFrame jif = null;
131 for (int i = 0; i < allJifs.length; i++) {
132 if (allJifs[i].getTitle().equals(paletteIdentifier))
133 return;
134 }
135
136
137 jif = new MyJInternalFrame(paletteIdentifier, this);
138 jif.setResizable(false);
139 jif.setClosable(true);
140 jif.setIconifiable(true);
141 jif.setMaximizable(false);
142 jif.setContentPane(contentPane);
143 setLayer(jif, JLayeredPane.PALETTE_LAYER.intValue());
144 try {
145 jif.pack();
146 jif.setSelected(true);
147 jif.setVisible(true);
148
149 } catch (PropertyVetoException pve) {
150 pve.printStackTrace();
151 }
152
153 add(jif);
154 }
155 }
156
157 /***
158 * Removes (and close) the specified inner frame from the desktop.
159 * @param jif the inner frame to remove.
160 */
161 public JInternalFrame removeInternalFrame(JInternalFrame jif) {
162 if (listOpened.contains(jif)) {
163 String key;
164 JInternalFrame value;
165 for (Enumeration e = listOpened.keys(); e.hasMoreElements(); ) {
166 key = (String) e.nextElement();
167 value = (JInternalFrame) listOpened.get(key);
168 if (value == jif) {
169 listOpened.remove(key);
170 orderOpened.removeElement(jif);
171 return value;
172 }
173 }
174 }
175 return null;
176 }
177
178 /***
179 * Removes (and close) the inner frame with the specified name from the desktop.
180 * @param name the name of the inner frame to remove.
181 */
182 public JInternalFrame removeInternalFrame(String name) {
183 JInternalFrame jif = null;
184 for (Enumeration e = orderOpened.elements(); e.hasMoreElements(); ) {
185 jif = (JInternalFrame) e.nextElement();
186 if (name.equals(jif.getTitle())) {
187 orderOpened.removeElement(jif);
188 listOpened.remove(name);
189 break;
190 }
191 }
192 return jif;
193 }
194
195 /***
196 * Gets the inner frame with specified name.
197 * @param name the name of the inner frame.
198 * @return the inner frame with the corresponding name.
199 * <CODE>null</CODE> otherwise.
200 */
201 public JInternalFrame getInternalFrame(String name) {
202 return (JInternalFrame) listOpened.get(name);
203 }
204
205 /***
206 * Gets the active inner frame in the desktop.
207 * @return the active inner frame if any.
208 * <CODE>null</CODE> otherwise.
209 */
210 public JInternalFrame getActiveInternalFrame() {
211 return getActiveInternalFrameFrom(JLayeredPane.DEFAULT_LAYER.intValue());
212 }
213
214 /***
215 * Gets the active palette in the desktop.
216 * @return the active inner frame if any. <CODE>null</CODE> otherwise.
217 */
218 public JInternalFrame getActivePalette() {
219 return getActiveInternalFrameFrom(JLayeredPane.PALETTE_LAYER.intValue());
220 }
221
222 /***
223 * Gets the active inner frame in the desktop in the specified layer.
224 * @param layer the layer indentifier.
225 * @return the active inner frame if any. <CODE>null</CODE> otherwise.
226 */
227 private JInternalFrame getActiveInternalFrameFrom(int layer) {
228 JInternalFrame[] allJifs = getAllFramesInLayer(layer);
229 JInternalFrame jif = null;
230
231 for (int i = 0; i < allJifs.length; i++) {
232 jif = allJifs[i];
233 if (jif.isSelected())
234 break;
235 }
236
237 return jif;
238 }
239
240 /***
241 * Selects the inner frame with the specified name
242 * @param name the specified name.
243 */
244 public void setSelected(String name) {
245 JInternalFrame jif = (JInternalFrame) listOpened.get(name);
246 if (jif != null) {
247 try {
248 jif.setSelected(true);
249 } catch (PropertyVetoException pve) {
250 pve.printStackTrace();
251 }
252 }
253 }
254
255 /***
256 * Renames the inner frame.
257 * @param currentName the current name of the inner frame.
258 * @param newName the new name of the inner frame.
259 */
260 public void rename(String currentName, String newName) throws Exception {
261 JInternalFrame jif = (JInternalFrame) listOpened.remove(currentName);
262 if (jif != null) {
263 if (listOpened.containsKey(newName))
264 throw new Exception("There already exists an inner frame with the name : " + newName);
265 else {
266 listOpened.put(newName, jif);
267 jif.setTitle(newName);
268 }
269 }
270 else
271 throw new Exception("There is no inner frame with the name : " + currentName);
272
273 }
274
275 /***
276 * Creates a new inner frame with the specified content pane.
277 * @param contentPane the specified content pane.
278 * @return the inner frame added.
279 */
280 private JInternalFrame createJInternalFrame(Container contentPane) {
281 JInternalFrame jif = new MyJInternalFrame("Untitled-" + ++nbUntitled, this);
282 jif.setResizable(true);
283 jif.setClosable(true);
284 jif.setIconifiable(true);
285 jif.setMaximizable(true);
286 jif.setContentPane(contentPane);
287 return jif;
288 }
289
290 /***
291 * Compute the postion of the next inner frame created.
292 */
293 private void computeNextFramePosition() {
294 Dimension size = getSize();
295 framePosition.x = (framePosition.x > size.width/2) ? 0 : framePosition.x + 25;
296 framePosition.y = (framePosition.y > size.height/2) ? 0 : framePosition.y + 30;
297 }
298
299 /***
300 * Sets all the inner frames to their maximum size.
301 */
302 public void setAllMaximum() {
303 String key;
304 JInternalFrame value;
305 for (Enumeration e = listOpened.keys(); e.hasMoreElements(); ) {
306 key = (String) e.nextElement();
307 value = (JInternalFrame) listOpened.get(key);
308 try {
309 value.setMaximum(maximize);
310 } catch (PropertyVetoException pve) {
311 pve.printStackTrace();
312 }
313 }
314
315 }
316
317 /***
318 *
319 */
320 public void setMaximize(boolean maximize) {
321 this.maximize = maximize;
322 }
323
324 /***
325 * Cascades the frame in the desktop.
326 */
327 public void cascade() {
328 framePosition.x = 0;
329 framePosition.y = 0;
330 JInternalFrame jif;
331 for (Enumeration e = orderOpened.elements(); e.hasMoreElements(); ) {
332 jif = (JInternalFrame) e.nextElement();
333 jif.setLocation(framePosition.x, framePosition.y);
334 setSelected(jif.getTitle());
335 computeNextFramePosition();
336 }
337 }
338
339 /***
340 * Closes all the frames in the desktop.
341 */
342 public void closeAll() {
343 JInternalFrame[] allJifs = getAllFramesInLayer(JLayeredPane.DEFAULT_LAYER.intValue());
344 for (int i = 0; i < allJifs.length; i++) {
345 try {
346 allJifs[i].setClosed(true);
347 }
348 catch (PropertyVetoException pve) {
349 }
350 }
351 framePosition.x = 0;
352 framePosition.y = 0;
353 }
354
355 }