KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JMDIMenuBar.java
28  *
29  * Created on 5 febbraio 2003, 22.57
30  *
31  */

32
33 package it.businesslogic.ireport.gui;
34
35 /**
36  * Similar to a javax.swing.JMenuBar, this menu bar is able to handle close, minimize e AltMaximize buttons for
37  * JMDIFrame contained in a JMDIDesktopPane.
38  * @author Administrator
39  */

40 public class JMDIMenuBar extends javax.swing.JMenuBar JavaDoc implements java.awt.event.ComponentListener JavaDoc, java.awt.event.ActionListener JavaDoc {
41     
42     javax.swing.JButton JavaDoc closeButton = null;
43     javax.swing.JButton JavaDoc resizeButton = null;
44     javax.swing.JButton JavaDoc iconifyButton = null;
45     JMDIFrame activeFrame = null;
46     boolean isMaximized = false;
47     int closeCount=0;
48         
49     /** Creates a new instance of JMDIMenuBar */
50     public JMDIMenuBar() {
51         closeButton = new javax.swing.JButton JavaDoc(javax.swing.plaf.metal.MetalIconFactory.getInternalFrameCloseIcon(16));
52         closeButton.setBorder( new javax.swing.border.EmptyBorder JavaDoc( new java.awt.Insets JavaDoc( 0,0,0,0 ) ));
53         resizeButton = new javax.swing.JButton JavaDoc(javax.swing.plaf.metal.MetalIconFactory.getInternalFrameAltMaximizeIcon(16));
54         resizeButton.setBorder( new javax.swing.border.EmptyBorder JavaDoc( new java.awt.Insets JavaDoc( 0,0,0,0 ) ));
55         iconifyButton = new javax.swing.JButton JavaDoc(javax.swing.plaf.metal.MetalIconFactory.getInternalFrameMinimizeIcon(16) );
56         iconifyButton.setBorder( new javax.swing.border.EmptyBorder JavaDoc( new java.awt.Insets JavaDoc( 0,0,0,0 ) ));
57  
58         this.addComponentListener(this);
59     }
60        
61        public void componentHidden(java.awt.event.ComponentEvent JavaDoc e) {}
62        public void componentMoved(java.awt.event.ComponentEvent JavaDoc e) {}
63        public void componentResized(java.awt.event.ComponentEvent JavaDoc e) {
64        if (this.getParent()!= null && closeButton != null) closeButton.setBounds(this.getParent().getWidth()-16-2,(this.getHeight()-16)/2,16,16);
65        if (this.getParent()!= null && closeButton != null) resizeButton.setBounds(this.getParent().getWidth()-32-6,(this.getHeight()-16)/2,16,16);
66        if (this.getParent()!= null && closeButton != null) iconifyButton.setBounds(this.getParent().getWidth()-48-6,(this.getHeight()-16)/2,16,16);
67     }
68     
69     public void removeFrameButtons()
70     {
71            isMaximized = false;
72            activeFrame = null;
73            closeButton.removeActionListener(this);
74            resizeButton.removeActionListener(this);
75            iconifyButton.removeActionListener(this);
76            this.remove( closeButton);
77            this.remove( resizeButton);
78            this.remove( iconifyButton);
79            //restoreButtonsPosition();
80
this.update(this.getGraphics());
81     }
82
83     public void setMaximizedFrame(JMDIFrame jf)
84     {
85         if (jf == null)
86         {
87            removeFrameButtons();
88         }
89         else
90         {
91            
92             if (!isMaximized)
93             {
94                 resizeButton.addActionListener(this);
95                 iconifyButton.addActionListener(this);
96                 closeButton.addActionListener(this);
97                 this.add ( iconifyButton );
98                 this.add( resizeButton);
99                 this.add( closeButton);
100                 this.restoreButtonsPosition();
101                 this.update(this.getGraphics());
102             }
103             activeFrame = jf;
104             isMaximized = true;
105         }
106     }
107     
108     public JMDIFrame getMaximizedFrame()
109     {
110         return this.activeFrame;
111     }
112
113     public void componentShown(java.awt.event.ComponentEvent JavaDoc e) {}
114
115     /** Paints this component.
116      * <p>
117      * This method is called when the contents of the component should
118      * be painted in response to the component first being shown or
119      * damage needing repair. The clip rectangle in the
120      * <code>Graphics</code> parameter will be set to the area
121      * which needs to be painted.
122      * Subclasses of Component that override this method need not call
123      * super.paint(g).
124      * <p>
125      * For performance reasons, Components with zero width or height
126      * aren't considered to need painting when they are first shown,
127      * and also aren't considered to need repair.
128      *
129      * @param g the graphics context to use for painting
130      * @see #update
131      * @since JDK1.0
132      *
133      */

134     public void paint(java.awt.Graphics JavaDoc g) {
135         componentResized(null);
136         super.paint(g);
137     }
138     
139     /** Invoked when an action occurs.
140      * This method handle the minimize,alt-maximize and close buttons
141      * for frames maximized in JMDIDestopPane
142      */

143     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
144         
145         this.activeFrame = MainFrame.getMainInstance().getActiveReportFrame();
146         // The simpler case...
147
closeCount++;
148         if (e. getSource() == resizeButton)
149         {
150             if (this.activeFrame != null)
151             {
152                 try {
153                    activeFrame.setMaximum( false);
154                    activeFrame.getDesktopPane().getDesktopManager().activateFrame(activeFrame);
155                 } catch (Exception JavaDoc ex){}
156             }
157         }
158         // A bit more complex...
159
else if (e. getSource() == iconifyButton)
160         {
161             if (this.activeFrame != null)
162             {
163                 try {
164                    //activeFrame.setMaximum( false);
165
activeFrame.setIcon(true);
166                 } catch (Exception JavaDoc ex){}
167             }
168         }
169         // the camplex case....the user want close this form!
170
// What we have to do?
171
//
172
else if (e. getSource() == closeButton)
173         {
174             if (this.activeFrame != null)
175             {
176                 /*
177                 if (activeFrame instanceof JReportFrame)
178                 {
179                     JReportFrame jrf = (JReportFrame)activeFrame;
180                     //System.out.println( activeFrame.getTitle() );
181                     boolean saveIt = true;
182                     if (jrf.getReport().isModified() && !MainFrame.getMainInstance().getProperties().getProperty("AskToSave","true").equals("false"))
183                     {
184                         String message = "Would you like to save the report before exiting?";
185                         String caption = "Unsaved file" + (jrf.getReport().isModified()?" (Changed)": " (Unchanged)") ;
186                         int ret = javax.swing.JOptionPane.showConfirmDialog(this, message, caption, javax.swing.JOptionPane.YES_NO_CANCEL_OPTION);
187                         
188                         switch(ret)
189                         {
190                             case javax.swing.JOptionPane.YES_OPTION:
191                                 saveIt = true;
192                                 break;
193                             case javax.swing.JOptionPane.NO_OPTION:
194                                 saveIt = false;
195                                 break;
196                             default:
197                                 return;
198                         }
199                     }
200                     if (saveIt)
201                     {
202                         java.awt.event.ActionEvent ae = new java.awt.event.ActionEvent(this,0, "");
203                         MainFrame.getMainInstance().jMenuItemSaveActionPerformed(ae);
204                         if (jrf.getReport().isModified()) return;
205                     }
206                 }
207                 */

208                 try {
209                     
210                     //System.out.println(closeCount+" "+activeFrame.getTitle());
211
//System.out.flush();
212

213                     javax.swing.JInternalFrame JavaDoc f = activeFrame;
214                     int frames = 0;
215                     if (activeFrame != null && activeFrame.getDesktopPane() != null)
216                     {
217                        frames = activeFrame.getDesktopPane().getAllFrames().length;
218                     }
219                     f.doDefaultCloseAction();
220                     if (f.isClosed() && frames <= 1)
221                     {
222                         removeFrameButtons();
223                     }
224                     
225                     MainFrame.getMainInstance().updateMenuWindowList();
226                     //activeFrame.setMaximum( false);
227
//activeFrame.setIcon(true);
228
} catch (Exception JavaDoc ex){
229                     ex.printStackTrace();
230                 }
231             }
232         }
233     }
234     
235     public void restoreButtonsPosition()
236     {
237         componentResized(null);
238     }
239     
240     public void closeFrame( javax.swing.JInternalFrame JavaDoc frameToClose)
241     {
242          closeFrame( frameToClose, false );
243     }
244     
245     public void closeFrame( javax.swing.JInternalFrame JavaDoc frameToClose, boolean force )
246     {
247         try {
248             
249         javax.swing.JInternalFrame JavaDoc oldActive = this.activeFrame;
250         
251         if (oldActive == frameToClose) oldActive = null;
252         if (frameToClose != null)
253             {
254                 
255                 //if (!((JMDIFrame)frameToClose).closingFrame(force)) return;
256
//System.out.println("Closing...!!!");
257
try {
258                     
259                     //System.out.println(closeCount+" "+activeFrame.getTitle());
260
//System.out.flush();
261
int frames = frameToClose.getDesktopPane().getAllFrames().length;
262                     frameToClose.setDefaultCloseOperation(frameToClose.DISPOSE_ON_CLOSE);
263                     frameToClose.doDefaultCloseAction();
264                     
265                     if (frameToClose.isClosed() && frames <= 1)
266                     {
267                         removeFrameButtons();
268                     }
269                     
270                     //activeFrame.setMaximum( false);
271
//activeFrame.setIcon(true);
272
} catch (Exception JavaDoc ex){
273                     ex.printStackTrace();;
274                 }
275             }
276
277             if (oldActive != null && oldActive instanceof JReportFrame)
278             {
279                 MainFrame.getMainInstance().setActiveReportForm( (JReportFrame)oldActive );
280             }
281         } finally
282         {
283             MainFrame.getMainInstance().updateMenuWindowList();
284         }
285     }
286 }
287
Popular Tags