KickJava   Java API By Example, From Geeks To Geeks.

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


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

32
33 package it.businesslogic.ireport.gui;
34
35 import it.businesslogic.ireport.gui.event.*;
36 import java.beans.*;
37 import javax.swing.*;
38 import java.awt.datatransfer.DataFlavor JavaDoc;
39 import java.awt.datatransfer.Transferable JavaDoc;
40 import java.awt.dnd.DnDConstants JavaDoc;
41 import java.awt.dnd.DropTarget JavaDoc;
42 import java.awt.dnd.DropTargetContext JavaDoc;
43 import java.awt.dnd.DropTargetDragEvent JavaDoc;
44 import java.awt.dnd.DropTargetDropEvent JavaDoc;
45 import java.awt.dnd.DropTargetEvent JavaDoc;
46 import java.awt.dnd.DropTargetListener JavaDoc;
47
48 /**
49  * JMDIDesktopPane is an evolution of the very trivial javax.swing.JDesktopPane.
50  * This class add a lot of event for really handle a DesktopPane. With this
51  * improvements we will able to handle internal windows menu list, activated window events,
52  * and more...
53  *
54  *
55  * @author Administrator
56  */

57 public class JMDIDesktopPane extends javax.swing.JDesktopPane JavaDoc implements java.io.Serializable JavaDoc {
58     
59    private DropTarget JavaDoc dropTarget;
60    private DropTargetListener JavaDoc dtListener;
61    private int acceptableActions = DnDConstants.ACTION_COPY_OR_MOVE;
62
63   
64     private static final String JavaDoc PROP_SAMPLE_PROPERTY = "SampleProperty";
65     
66     private String JavaDoc sampleProperty;
67     
68     private PropertyChangeSupport propertySupport;
69     
70     /** Utility field used by event firing mechanism. */
71     private javax.swing.event.EventListenerList JavaDoc listenerList = null;
72     
73     /** Creates new JMDIDesktopPane */
74     public JMDIDesktopPane() {
75         super();
76         propertySupport = new PropertyChangeSupport( this );
77         
78         this.dtListener = new DTListener();
79     
80         // component, ops, listener, accepting
81
this.dropTarget = new DropTarget JavaDoc(this,
82             this.acceptableActions,
83             this.dtListener,
84         true);
85     }
86     
87     public void internalFrameActivated(JMDIFrame jMDIFrame)
88     {
89         this.fireInternalFrameActivatedListenerInternalFrameActivated(new InternalFrameActivatedEvent(jMDIFrame));
90     }
91     
92     public void internalFrameClosed(JMDIFrame jMDIFrame)
93     {
94         if (this.getAllFrames().length == 1)
95         {
96             this.fireInternalFrameActivatedListenerInternalFrameActivated(new InternalFrameActivatedEvent(null) );
97         }
98         this.fireInternalFrameActivatedListenerInternalFrameActivated(new InternalFrameActivatedEvent(jMDIFrame, InternalFrameActivatedEvent.CLOSED ) );
99     }
100     
101     
102     public String JavaDoc getSampleProperty() {
103         return sampleProperty;
104     }
105     
106     public void setSampleProperty(String JavaDoc value) {
107         String JavaDoc oldValue = sampleProperty;
108         sampleProperty = value;
109         propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
110     }
111     
112     
113     public void addPropertyChangeListener(PropertyChangeListener listener) {
114         if (listener == null || propertySupport == null) return;
115         propertySupport.addPropertyChangeListener(listener);
116     }
117     
118     public void removePropertyChangeListener(PropertyChangeListener listener) {
119         propertySupport.removePropertyChangeListener(listener);
120     }
121     
122     /** Registers InternalFrameActivatedListener to receive events.
123      * @param listener The listener to register.
124      *
125      */

126     public synchronized void addInternalFrameActivatedListener(it.businesslogic.ireport.gui.event.InternalFrameActivatedListener listener) {
127         if (listenerList == null ) {
128             listenerList = new javax.swing.event.EventListenerList JavaDoc();
129         }
130         listenerList.add(it.businesslogic.ireport.gui.event.InternalFrameActivatedListener.class, listener);
131     }
132     
133     /** Removes InternalFrameActivatedListener from the list of listeners.
134      * @param listener The listener to remove.
135      *
136      */

137     public synchronized void removeInternalFrameActivatedListener(it.businesslogic.ireport.gui.event.InternalFrameActivatedListener listener) {
138         listenerList.remove(it.businesslogic.ireport.gui.event.InternalFrameActivatedListener.class, listener);
139     }
140     
141     /** Notifies all registered listeners about the event.
142      *
143      * @param event The event to be fired
144      *
145      */

146     private void fireInternalFrameActivatedListenerInternalFrameActivated(it.businesslogic.ireport.gui.event.InternalFrameActivatedEvent event) {
147         if (listenerList == null) return;
148         Object JavaDoc[] listeners = listenerList.getListenerList();
149         for (int i = listeners.length-2; i>=0; i-=2) {
150             if (listeners[i]==it.businesslogic.ireport.gui.event.InternalFrameActivatedListener.class) {
151                 ((it.businesslogic.ireport.gui.event.InternalFrameActivatedListener)listeners[i+1]).internalFrameActivated(event);
152             }
153         }
154     }
155     
156     public void cascade()
157     {
158         JInternalFrame[] frames = this.getAllFrames();
159         if (frames.length == 0) return;
160         
161         JInternalFrame activeframe = this.getSelectedFrame();
162         
163         int position =0;
164         for (int i=0; i<frames.length; ++i)
165         {
166             try {
167                     frames[i].setMaximum(false);
168                     frames[i].setIcon(false);
169                     frames[i].moveToFront();
170                 } catch (Exception JavaDoc ex){}
171             if (frames[i] != activeframe)
172             {
173                 frames[i].setLocation(position,position);
174                 
175                 position += 24;
176             }
177         }
178         
179         if (activeframe != null)
180         {
181             activeframe.moveToFront();
182             activeframe.setLocation(position,position);
183         }
184     }
185     
186     /* This method is ported from kdevelop'QextMdiChildArea
187      */

188     public void tileVertically()
189     {
190         JInternalFrame[] frames_tmp = this.getAllFrames();
191         
192         if (frames_tmp.length == 0) return;
193
194         JInternalFrame[] frames = new JInternalFrame[frames_tmp.length];
195         if (this.getSelectedFrame() == null && frames_tmp[0].isIcon())
196         {
197             try {
198             frames_tmp[0].setIcon(false);
199             frames_tmp[0].setSelected(true);
200             } catch (Exception JavaDoc ex){ return; }
201         }
202         frames[0] = this.getSelectedFrame();
203         
204         int k=1;
205         for (int i=0; i<frames_tmp.length; ++i)
206         {
207             if (frames_tmp[i] == frames[0]) continue;
208             frames[k] = frames_tmp[i];
209             k++;
210         }
211
212         //adjust the arry to have the selected win in the first position....
213

214         
215         int w = getWidth() / frames.length;
216     int lastWidth = 0;
217     if( frames.length > 1)
218             lastWidth = getWidth() - (w * (frames.length - 1));
219         else
220             lastWidth = w;
221     
222         int h = getHeight();
223     int posX = 0;
224     int countVisible = 0;
225
226         for (int i=0; i<frames.length; ++i)
227         {
228             try {
229                     frames[i].setMaximum(false);
230                     frames[i].setIcon(false);
231                     frames[i].moveToFront();
232                 } catch (Exception JavaDoc ex){}
233             
234             if( i < frames.length) {
235                 frames[i].setLocation(posX, 0);
236                 frames[i].setSize(w, h);
237                 posX += w;
238             }
239             else { // last visible childframe
240
frames[i].setLocation(posX, 0);
241                 frames[i].setSize(lastWidth, h);
242             }
243         }
244     }
245     
246     /* This method is ported from kdevelop'QextMdiChildArea
247      */

248     public void tileHorizontally()
249     {
250         JInternalFrame[] frames_tmp = this.getAllFrames();
251         
252         if (frames_tmp.length == 0) return;
253
254         JInternalFrame[] frames = new JInternalFrame[frames_tmp.length];
255          if (this.getSelectedFrame() == null && frames_tmp[0].isIcon())
256         {
257             try {
258             frames_tmp[0].setIcon(false);
259             frames_tmp[0].setSelected(true);
260             } catch (Exception JavaDoc ex){ return; }
261         }
262         frames[0] = this.getSelectedFrame();
263         
264         int k=1;
265         for (int i=0; i<frames_tmp.length; ++i)
266         {
267             if (frames_tmp[i] == frames[0]) continue;
268             else
269             {
270                 frames[k] = frames_tmp[i];
271                 k++;
272             }
273         }
274
275         //adjust the arry to have the selected win in the first position....
276

277         
278         int w = getWidth() ;
279     
280     
281         int h = getHeight() / frames.length;
282         
283         int lastHeight = 0;
284     if( frames.length > 1)
285             lastHeight = getHeight() - (h * (frames.length - 1));
286         else
287             lastHeight = h;
288         
289     int posY = 0;
290     int countVisible = 0;
291
292         for (int i=0; i<frames.length; ++i)
293         {
294             try {
295                     frames[i].setMaximum(false);
296                     frames[i].setIcon(false);
297                     frames[i].moveToFront();
298                 } catch (Exception JavaDoc ex){}
299             
300             if( i < frames.length) {
301                 frames[i].setLocation(0,posY);
302                 frames[i].setSize(w, h);
303                 posY += h;
304             }
305             else { // last visible childframe
306
frames[i].setLocation(0,posY);
307                 frames[i].setSize(w,lastHeight);
308             }
309         }
310         
311     }
312     
313     /* This method is ported from kdevelop'QextMdiChildArea
314      */

315     public void tileAnodine()
316     {
317         JInternalFrame[] frames_tmp = this.getAllFrames();
318         
319         if (frames_tmp.length == 0) return;
320
321         JInternalFrame[] frames = new JInternalFrame[frames_tmp.length];
322         
323         if (this.getSelectedFrame() == null && frames_tmp[0].isIcon())
324         {
325             try {
326             frames_tmp[0].setIcon(false);
327             frames_tmp[0].setSelected(true);
328             } catch (Exception JavaDoc ex){ return; }
329         }
330         
331         frames[0] = this.getSelectedFrame();
332         int k=1;
333         for (int i=0; i<frames_tmp.length; ++i)
334         {
335             if (frames_tmp[i] == frames[0]) continue;
336             frames[k] = frames_tmp[i];
337             k++;
338         }
339         
340     int numVisible=frames.length;
341     if(numVisible<1)return;
342     int numCols= (int)Math.sqrt(numVisible); // set columns to square root of visible count
343
// create an array to form grid layout
344
int[] numRows=new int[numCols];
345     int numCurCol=0;
346     while(numCurCol<numCols){
347         numRows[numCurCol]=numCols; // create primary grid values
348
numCurCol++;
349     }
350     int numDiff=numVisible-(numCols*numCols); // count extra rows
351
int numCurDiffCol=numCols; // set column limiting for grid updates
352
while(numDiff>0){
353         numCurDiffCol--;
354         numRows[numCurDiffCol]++; // add extra rows to column grid
355
if(numCurDiffCol<1)numCurDiffCol=numCols; // rotate through the grid
356
numDiff--;
357     }
358     numCurCol=0;
359     int numCurRow=0;
360     int curX=0;
361     int curY=0;
362     // the following code will size everything based on my grid above
363
// there is no limit to the number of windows it will handle
364
// it's great when a kick-ass theory works!!! // Pragma :)
365
int xQuantum= getWidth()/numCols;
366     int yQuantum= getHeight()/numRows[numCurCol];
367                
368     for (int i=0; i<frames.length; ++i)
369         {
370                try {
371                     frames[i].setMaximum(false);
372                     frames[i].setIcon(false);
373                     frames[i].moveToFront();
374                 } catch (Exception JavaDoc ex){}
375
376                 frames[i].setLocation(curX,curY);
377                 frames[i].setSize(xQuantum, yQuantum);
378         numCurRow++;
379         curY+=yQuantum;
380         if(numCurRow==numRows[numCurCol]){
381                 numCurRow=0;
382                 numCurCol++;
383                 curY=0;
384                 curX+=xQuantum;
385                 if(numCurCol!=numCols)yQuantum= getHeight()/numRows[numCurCol];
386             }
387         }
388     }
389     
390
391     
392 }
393   
394
Popular Tags