KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > JMDIFrame


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * JMDIFrame.java
28  *
29  * Created on 6 febbraio 2003, 0.31
30  *
31  */

32
33 package it.businesslogic.ireport.gui;
34 import com.lowagie.text.Rectangle;
35 import java.awt.Dimension JavaDoc;
36 /**
37  * JMDIFrame is an evolution of the very trivial javax.swing.JInternalFrame.
38  * This class implements an MDI window handling similar to Win32, with
39  * the maximized frame really maximized (without the title bar, and with the
40  * minimize, maximize and close buttons on menu bar, that must be a JMDIMenuBar)
41  *
42  * @author Administrator
43  */

44 public class JMDIFrame extends javax.swing.JInternalFrame JavaDoc {
45     
46     javax.swing.border.Border JavaDoc borderNormal = null;
47     javax.swing.JComponent JavaDoc northPane = null;
48     java.awt.Rectangle JavaDoc northPaneBounds = null;
49     it.businesslogic.ireport.gui.JMDIMenuBar menu = null;
50     
51     private boolean borderNone = false;
52     /** Creates new form JMDIFrame */
53     /** OLD
54     public JMDIFrame() {
55         initComponents();
56         
57         northPane = ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).getNorthPane();
58         northPaneBounds = northPane.getBounds();
59         setNormalSize( this.getPreferredSize());
60         borderNone = false;
61         jPanel1.setBackground(new java.awt.Color((int)( Math.random()* 0xffffff)));
62     }
63     **/

64     /**
65         Fixed version for OSX
66     */

67     public JMDIFrame() {
68         initComponents();
69         
70         northPane = ((javax.swing.plaf.basic.BasicInternalFrameUI JavaDoc) this.getUI()).getNorthPane();
71         if(northPane == null){//change made here
72
javax.swing.plaf.basic.BasicInternalFrameUI JavaDoc aUI = new javax.swing.plaf.basic.BasicInternalFrameUI JavaDoc(this);
73            this.setUI(aUI);
74             northPane = ((javax.swing.plaf.basic.BasicInternalFrameUI JavaDoc) this.getUI()).getNorthPane();
75         } //change ends here
76
northPaneBounds = northPane.getBounds();
77         setNormalSize( this.getPreferredSize());
78         borderNone = false;
79        jPanel1.setBackground(new java.awt.Color JavaDoc((int)( Math.random()* 0xffffff)));
80     }
81
82
83     
84     /** This method is called from within the constructor to
85      * initialize the form.
86      * WARNING: Do NOT modify this code. The content of this method is
87      * always regenerated by the Form Editor.
88      */

89     private void initComponents() {//GEN-BEGIN:initComponents
90
jPanel1 = new javax.swing.JPanel JavaDoc();
91
92         setClosable(true);
93         setIconifiable(true);
94         setMaximizable(true);
95         setResizable(true);
96         setTitle("JMDIFrame");
97         setPreferredSize(new java.awt.Dimension JavaDoc(400, 400));
98         addComponentListener(new java.awt.event.ComponentAdapter JavaDoc() {
99             public void componentResized(java.awt.event.ComponentEvent JavaDoc evt) {
100                 formComponentResized(evt);
101             }
102         });
103
104         addHierarchyBoundsListener(new java.awt.event.HierarchyBoundsListener JavaDoc() {
105             public void ancestorMoved(java.awt.event.HierarchyEvent JavaDoc evt) {
106             }
107             public void ancestorResized(java.awt.event.HierarchyEvent JavaDoc evt) {
108                 formAncestorResized(evt);
109             }
110         });
111
112         addInternalFrameListener(new javax.swing.event.InternalFrameListener JavaDoc() {
113             public void internalFrameOpened(javax.swing.event.InternalFrameEvent JavaDoc evt) {
114             }
115             public void internalFrameClosing(javax.swing.event.InternalFrameEvent JavaDoc evt) {
116                 formInternalFrameClosing(evt);
117             }
118             public void internalFrameClosed(javax.swing.event.InternalFrameEvent JavaDoc evt) {
119                 formInternalFrameClosed(evt);
120             }
121             public void internalFrameIconified(javax.swing.event.InternalFrameEvent JavaDoc evt) {
122             }
123             public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent JavaDoc evt) {
124             }
125             public void internalFrameActivated(javax.swing.event.InternalFrameEvent JavaDoc evt) {
126                 formInternalFrameActivated(evt);
127             }
128             public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent JavaDoc evt) {
129             }
130         });
131
132         addPropertyChangeListener(new java.beans.PropertyChangeListener JavaDoc() {
133             public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
134                 formPropertyChange(evt);
135             }
136         });
137
138         jPanel1.setBackground(new java.awt.Color JavaDoc(255, 204, 204));
139         jPanel1.addHierarchyBoundsListener(new java.awt.event.HierarchyBoundsListener JavaDoc() {
140             public void ancestorMoved(java.awt.event.HierarchyEvent JavaDoc evt) {
141             }
142             public void ancestorResized(java.awt.event.HierarchyEvent JavaDoc evt) {
143                 jPanel1AncestorResized(evt);
144             }
145         });
146
147         getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
148
149         pack();
150     }//GEN-END:initComponents
151

152     private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent JavaDoc evt) {//GEN-FIRST:event_formInternalFrameClosing
153
// If there is no more frames, reset menu buttons...
154
// javax.swing.JOptionPane.showMessageDialog(this,"Frames: "+this.getDesktopPane().getAllFrames().length);
155
// if (this.getDesktopPane().getAllFrames().length == 1)
156
// {
157
// try {
158
// this.setIcon(true);
159
// } catch (Exception ex) {}
160
//if (menu != null) menu.removeFrameButtons();
161
// }
162

163         if (!closingFrame(false))
164         {
165             setDefaultCloseOperation( this.DO_NOTHING_ON_CLOSE );
166         }
167         else
168         {
169             setDefaultCloseOperation( this.DISPOSE_ON_CLOSE );
170              if ( this.getDesktopPane() instanceof JMDIDesktopPane)
171             {
172                 ((JMDIDesktopPane)this.getDesktopPane()).internalFrameClosed(this);
173             }
174         }
175     }//GEN-LAST:event_formInternalFrameClosing
176

177     private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent JavaDoc evt) {//GEN-FIRST:event_formInternalFrameClosed
178

179     }//GEN-LAST:event_formInternalFrameClosed
180

181     private void jPanel1AncestorResized(java.awt.event.HierarchyEvent JavaDoc evt) {//GEN-FIRST:event_jPanel1AncestorResized
182

183          
184     }//GEN-LAST:event_jPanel1AncestorResized
185

186     private void formInternalFrameActivated(javax.swing.event.InternalFrameEvent JavaDoc evt) {//GEN-FIRST:event_formInternalFrameActivated
187
// We must notify the JMDIDesktopPane...
188
if ( this.getDesktopPane() instanceof JMDIDesktopPane)
189         {
190             ((JMDIDesktopPane)this.getDesktopPane()).internalFrameActivated(this);
191         }
192        
193     }//GEN-LAST:event_formInternalFrameActivated
194

195     private void formComponentResized(java.awt.event.ComponentEvent JavaDoc evt) {//GEN-FIRST:event_formComponentResized
196

197         //((javax.swing.plaf.basic.BasicInternalFrameUI)getUI()).setNorthPane(null);
198
if (this.isMaximum())
199         {
200             double h = ((javax.swing.plaf.basic.BasicInternalFrameUI JavaDoc)getUI()).getNorthPane().getSize().getHeight();
201             
202             java.awt.Rectangle JavaDoc rr = this.getBounds();
203             if (rr.getY() >= 0)
204             {
205                 rr.setRect(rr.getX(),(int)rr.getY()-h, (int)rr.getWidth(), rr.getHeight() + h);
206                 this.setBounds(rr);
207             }
208         }
209
210     }//GEN-LAST:event_formComponentResized
211

212
213     private void formPropertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {//GEN-FIRST:event_formPropertyChange
214
// Add your handling code here
215
if (evt.getPropertyName().equals("maximum"))
216                         onMaximizeMinimize();
217             else if (evt.getPropertyName().equals("selected"))
218                         onSelect();
219     }//GEN-LAST:event_formPropertyChange
220

221     private void onMaximizeMinimize()
222     {
223          if (borderNormal == null)
224          {
225             borderNormal = this.getBorder();
226          }
227          
228          //javax.swing.JOptionPane.showMessageDialog(this,"xx2");
229
if ( this.isMaximum)
230         {
231             if (!borderNone)
232             {
233                 
234                 /* BUG #955403 fixed - Recursive calls on maximize a internal frame
235                   
236                  JDesktopPane desktopPane = this.getDesktopPane();
237                 if(desktopPane != null){
238                     javax.swing.JInternalFrame[] jfs = desktopPane.getAllFrames();
239                     for (int i=0; i<jfs.length; ++i)
240                     {
241                        if (jfs[i] != this)
242                        {
243                            try {
244                                jfs[i].setMaximum(true);
245                            } catch (Exception ex){}
246                        }
247                    }
248                 }*/

249                  
250                  try {
251                     this.setSelected(true);
252                  } catch (Exception JavaDoc ex){}
253                  
254                 this.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc( 0,0,0,0 )));
255                 borderNone = true;
256                 
257                                 
258                 try {
259                     //((javax.swing.plaf.basic.BasicInternalFrameUI)getUI()).setNorthPane(null);
260
double h = ((javax.swing.plaf.basic.BasicInternalFrameUI JavaDoc)getUI()).getNorthPane().getSize().getHeight();
261                     java.awt.Rectangle JavaDoc rr = this.getBounds();
262                     rr.setRect(rr.getX(),(int)rr.getY()-h, (int)rr.getWidth(), rr.getHeight() + h);
263                     this.setBounds(rr);
264                     ((javax.swing.plaf.basic.BasicInternalFrameUI JavaDoc)getUI()).getNorthPane().setPreferredSize( new java.awt.Dimension JavaDoc(0,0) );
265
266                     //((javax.swing.plaf.basic.BasicInternalFrameUI)getUI()).getNorthPane().setSize( new java.awt.Dimension(0,0) );
267

268                 } catch (Exception JavaDoc ex)
269                 {
270                     ex.printStackTrace();
271                 }
272
273                 //System.out.println( ((javax.swing.plaf.basic.BasicInternalFrameUI)getUI()).getNorthPane() );
274

275                 //northPane = ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).getNorthPane();
276
//((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane( null );
277
if (menu != null) menu.setMaximizedFrame(this);
278             }
279        }
280         else if ( !this.isMaximum && borderNone)
281         {
282          
283             // System.out.println("Set maximum: "+this.getSize());
284
setNormalSize( this.getSize() );
285                
286             /* BUG #955403 fixed - Recursive calls on maximize a internal frame
287               
288              JDesktopPane desktopPane = this.getDesktopPane();
289             if(desktopPane != null){
290
291                 // set not maximum all frames...
292                 javax.swing.JInternalFrame[] jfs = desktopPane.getAllFrames();
293                 for (int i=0; i<jfs.length; ++i)
294                 {
295                     if (jfs[i] != this)
296                     {
297                         try {
298                         jfs[i].setMaximum(false);
299                         } catch (Exception ex){}
300                     }
301                 }
302             }*/

303             
304             borderNone = false;
305             
306             
307             //((javax.swing.plaf.basic.BasicInternalFrameUI)getUI()).setNorthPane( northPane );
308
((javax.swing.plaf.basic.BasicInternalFrameUI JavaDoc)getUI()).getNorthPane().setPreferredSize( new java.awt.Dimension JavaDoc(this.getNormalSize().width ,23) );
309             setPreferredSize(this.getNormalSize());
310             setSize(this.getNormalSize());
311                       
312              this.setBorder(borderNormal);
313              if (menu != null) menu.setMaximizedFrame(null);
314              //this.getDesktopPane().getDesktopManager().activateFrame(this);
315
try {
316              this.setSelected(true);
317              } catch (Exception JavaDoc ex){};
318         }
319     }
320     
321     
322     /**
323      * This method set the activeForm to the menu. It is useful when the user
324      * maximize windows and then close one. The frame that will activated will
325      * set activeForm in JMDIMenuBar class trought this method.
326      *
327      */

328     private void onSelect()
329     {
330         if ( this.isMaximum)
331         {
332             if (menu != null) menu.setMaximizedFrame(this);
333         }
334         
335     }
336     
337     
338     private void formAncestorResized(java.awt.event.HierarchyEvent JavaDoc evt) {//GEN-FIRST:event_formAncestorResized
339

340     }//GEN-LAST:event_formAncestorResized
341

342     /** Getter for property menu.
343      * @return Value of property menu.
344      *
345      */

346     public it.businesslogic.ireport.gui.JMDIMenuBar getMenu() {
347         return menu;
348     }
349     
350     /** Setter for property menu.
351      * We need a JMDIMenuBar to add, if needed, buttons to resize, close, minimize this
352      * window when maximized.
353      * @param menu New value of property menu.
354      *
355      */

356     public void setMenu(it.businesslogic.ireport.gui.JMDIMenuBar menu) {
357         this.menu = menu;
358     }
359     
360     /** Getter for property normalSize.
361      * @return Value of property normalSize.
362      *
363      */

364     public java.awt.Dimension JavaDoc getNormalSize() {
365         return normalSize;
366     }
367     
368     /** Setter for property normalSize.
369      * @param normalSize New value of property normalSize.
370      *
371      */

372     public void setNormalSize(java.awt.Dimension JavaDoc normalSize) {
373         this.normalSize = normalSize;
374     }
375     
376     // Variables declaration - do not modify//GEN-BEGIN:variables
377
private javax.swing.JPanel JavaDoc jPanel1;
378     // End of variables declaration//GEN-END:variables
379

380     private java.awt.Dimension JavaDoc normalSize;
381    /*
382     public void setMaximum(boolean bMaximum) throws java.beans.PropertyVetoException
383     {
384         try {
385         //((javax.swing.plaf.basic.BasicInternalFrameUI)getUI()).setNorthPane( northPane );
386         super.setMaximum(bMaximum);
387         
388         } catch (Exception ex0)
389         {
390             
391         }
392     }
393     */

394     
395     /**
396      * On close event.
397      * Please return true on force.
398      * Return false to stop the window close.
399      */

400     public boolean closingFrame(boolean force)
401     {
402         return true;
403     }
404 }
405
Popular Tags