KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > command > FormatCommand


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  * FormatCommand.java
28  *
29  */

30
31 package it.businesslogic.ireport.gui.command;
32
33 import it.businesslogic.ireport.Band;
34 import it.businesslogic.ireport.CrosstabReportElement;
35 import it.businesslogic.ireport.OperationType;
36 import it.businesslogic.ireport.ReportElement;
37 import it.businesslogic.ireport.gui.JReportFrame;
38 import it.businesslogic.ireport.gui.MainFrame;
39 import it.businesslogic.ireport.undo.FormatElementsOperation;
40
41 import java.util.Enumeration JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Vector JavaDoc;
44
45 /**
46  * FormatCommand.java. Created on 9 mei 2005, 21:32 By Robert Lamping Using
47  * the Command pattern
48  */

49 /**
50  * @author Fourdim
51  */

52 public class FormatCommand
53         implements ICommand
54 {
55     
56     /**
57      * The attribute sleepUndo is introduced to repeat multiple commands. When
58      * sleepUndo = true then it prevents the storage of multiple undo
59      * transitions.
60      */

61     static boolean sleepUndo = false;
62
63     
64     
65     /** mf is a reference to the Mainframe instance. */
66     private MainFrame mf = null;
67     
68     /** mf is a reference to the current JReportFrame instance. */
69     JReportFrame jrf = null;
70     Vector JavaDoc oldSelectedElements = null;
71     
72     /** e is the enumeration of the selected elements. */
73     Enumeration JavaDoc e = null;
74     
75     boolean treatAllElements = false;
76         
77     ReportElement re = null;
78     int operationType = 0;
79     int currentElementPosition = 0;
80     FormatElementsOperation undoOp = null;
81     
82     
83     public Vector JavaDoc getSelectedElements()
84     {
85         jrf = MainFrame.getMainInstance().getActiveReportFrame();
86         if (jrf == null) return null;
87         return MainFrame.getMainInstance().getActiveElementSelection(jrf);
88     }
89     
90     public void setSelectedElements(Vector JavaDoc elements)
91     {
92         jrf = MainFrame.getMainInstance().getActiveReportFrame();
93         if (jrf == null) return;
94         
95         if (jrf.getSelectedCrosstabEditorPanel() != null)
96         {
97             jrf.getSelectedCrosstabEditorPanel().setSelectedElements(elements);
98         }
99         else
100         {
101             jrf.setSelectedElements(elements);
102         }
103     }
104     
105     
106     /**
107      * DOCUMENT ME!
108      */

109     public void execute()
110     {
111         mf = MainFrame.getMainInstance();
112         
113         if (mf.getJMDIDesktopPane().getSelectedFrame() != null &&
114                 mf.getJMDIDesktopPane().getSelectedFrame() instanceof JReportFrame)
115         {
116             // everytime the current selectedFrame must be chosen
117
jrf = (JReportFrame)mf.getJMDIDesktopPane().getSelectedFrame();
118           
119             if ( getTreatAllElements() ){
120                 oldSelectedElements = (Vector JavaDoc) getSelectedElements().clone();
121                 this.setSelectedElements((Vector JavaDoc) jrf.getReport().getElements().clone() );
122             }
123             
124             // TODO: Buttons should listen to number of selectelements and
125
// enable or disable themselves.
126
if (getSelectedElements().isEmpty())
127             {
128                 // do nothing
129
}
130             else
131             {
132                 if (preCondition())
133                 {
134                     re = null;
135                     CrosstabReportElement cre = null;
136                     if (jrf.getSelectedCrosstabEditorPanel() != null)
137                     {
138                         cre = jrf.getSelectedCrosstabEditorPanel().getCrosstabElement();
139                     }
140                     undoOp = new FormatElementsOperation(jrf, cre, operationType);
141                     executeDeeper();
142                     
143                     //Compulsary:
144
postAction();
145                 }
146             }
147             
148             // reset selectedElements selection
149
if ( getTreatAllElements() ){
150                 this.setSelectedElements((Vector JavaDoc) oldSelectedElements.clone() );
151             }
152         }
153     }
154     
155     /**
156      * DOCUMENT ME!
157      */

158     void executeDeeper()
159     {
160         resetEnumeration();
161         preparation(); // <========= method in subclass is called if present
162
resetEnumeration();
163         processElements();
164     }
165     
166     /**
167      * This is the regular processElements(0 in which the current selected elements
168      * are processed
169      * It is possible to use the variant where you pass your own selection of elements
170      */

171     void processElements()
172     {
173         processElements(this.e);
174     }
175     
176     
177     /**
178      * Register the elements in the UndoOp instaed of misusing processElements.
179      * in registerElements the method modify() is not executed.
180      * It is use for multi command FormatCommands, such as FormatCommandOrganizeAsATable.
181      * @param e DOCUMENT ME!
182      */

183     void registerElements()
184     {
185         Enumeration JavaDoc e = getSelectedElements().elements();
186         while (e.hasMoreElements())
187         {
188             undoOp.addElement( e.nextElement() );
189         }
190     }
191     
192     /**
193      * DOCUMENT ME!
194      *
195      * @param e DOCUMENT ME!
196      */

197     void processElements(Enumeration JavaDoc e)
198     {
199         
200         currentElementPosition = -1;
201         
202         while (e.hasMoreElements())
203         {
204             currentElementPosition++;
205             re = (ReportElement)e.nextElement();
206             
207             // Undo preparation, phase 1 (pre element transition)
208
// Rectangle oldBounds = new Rectangle(re.getBounds());
209

210             if ( ! getSleepUndo() )
211             {
212                 undoOp.addElement(re);
213             }
214             // else: "multicommandmode' more than one command is execute in one Undo Operation
215

216             //undoOp.captureCurrent(re); // acts on last element on stack
217

218             // mf.logOnConsole( re.toString() );
219
modify(); // <========= method in subclass is called
220

221             // adjusting the boundary of the report element
222
// to the current position, width and height.
223
re.updateBounds();
224             
225             // Undo preparation, phase 2 (post element transition)
226
// Rectangle newBounds = new Rectangle(re.getBounds());
227

228             // se FormatCommand.java for undoOp.
229
//mf.logOnConsole("NewBounds" + re.toString() + "\n");
230

231             if ( ! getSleepUndo() )
232             {
233                 undoOp.captureUniqueModified(re);
234             }
235             
236             //undoOp.addElement(re, oldBounds, newBounds);
237
}
238     }
239     
240     /**
241      * DOCUMENT ME!
242      */

243     void updateElements()
244     {
245         Enumeration JavaDoc e = getSelectedElements().elements();
246         
247         while (e.hasMoreElements())
248         {
249             try
250             {
251                 // modify this so that also JReportFrame objects can be passed
252
undoOp.captureUniqueModified( (ReportElement) e.nextElement() );
253             }
254             catch (Exception JavaDoc ex)
255             {
256                 // exception occurs when element is diffent from ReportElement
257
}
258         }
259     }
260     
261     /**
262      * DOCUMENT ME!
263      *
264      * @return DOCUMENT ME!
265      */

266     int getCurrentElementPosition()
267     {
268         
269         return currentElementPosition;
270     }
271     
272     /**
273      * DOCUMENT ME!
274      */

275     void preparation()
276     {
277         //dummy
278
}
279     
280     // TODO: Restrict access, make it private check with interface
281

282     /**
283      * DOCUMENT ME!
284      */

285     void modify()
286     {
287         // if the subclass has a method modify then that method will be executed
288
// otherwise this one.
289
// Fake, the method of the subclass will normally be performed.
290
}
291     
292     /**
293      * DOCUMENT ME!
294      */

295     void resetEnumeration()
296     {
297         e = getSelectedElements().elements();
298     }
299     
300     /**
301      * DOCUMENT ME!
302      *
303      * @return DOCUMENT ME!
304      */

305     boolean preCondition()
306     {
307         
308         return true;
309     }
310     
311     /**
312      * DOCUMENT ME!
313      */

314     void postAction()
315     {
316         if (jrf.getSelectedCrosstabEditorPanel() == null)
317         {
318             jrf.getReportPanel().repaint();
319
320             if (!sleepUndo)
321             {
322                 jrf.addUndoOperation(undoOp);
323
324                 mf.getDocumentStructurePanel().updateDocumentStructureTree(jrf);
325
326                 // this part should be move to a listener
327
jrf.getReportPanel().repaint();
328             }
329         }
330         else
331         {
332             jrf.getSelectedCrosstabEditorPanel().repaint();
333             if (!sleepUndo)
334             {
335                 jrf.addUndoOperation(undoOp);
336
337                 //mf.getDocumentStructurePanel().updateDocumentStructureTree(jrf);
338

339                 // this part should be move to a listener
340
//jrf.getReportPanel().repaint();
341
}
342         }
343              
344     }
345     
346     /**
347      * DOCUMENT ME!
348      *
349      * @param enum2 DOCUMENT ME!
350      * @return DOCUMENT ME!
351      */

352     Vector JavaDoc sortYX(Enumeration JavaDoc enum2)
353     {
354         
355         Vector JavaDoc myElements = new Vector JavaDoc();
356         
357         while (enum2.hasMoreElements())
358         {
359             re = (ReportElement)enum2.nextElement();
360             
361             // insert this element in the right position...
362
if (myElements.size() == 0)
363             {
364                 myElements.add(re);
365             }
366             else
367             {
368                 
369                 boolean inserted = false;
370                 
371                 for (int i = 0; i < myElements.size(); ++i)
372                 {
373                     
374                     ReportElement re2 = (ReportElement)myElements.elementAt(i);
375                     
376                     if (re.getPosition().y < re2.getPosition().y)
377                     {
378                         myElements.insertElementAt(re, i);
379                         inserted = true;
380                         
381                         break;
382                     }
383                     else if (re.getPosition().y == re2.getPosition().y)
384                     {
385                         
386                         if (re.getPosition().x < re2.getPosition().x)
387                         {
388                             myElements.insertElementAt(re, i);
389                             inserted = true;
390                             
391                             break;
392                         }
393                     }
394                 }
395                 
396                 if (!inserted)
397                 {
398                     myElements.addElement(re);
399                 }
400             }
401         }
402         
403         return myElements;
404     }
405     
406     /**
407      * DOCUMENT ME!
408      *
409      * @param enum2 DOCUMENT ME!
410      * @return DOCUMENT ME!
411      */

412     Vector JavaDoc sortXY(Enumeration JavaDoc enum2)
413     {
414         
415         Vector JavaDoc myElements = new Vector JavaDoc();
416         
417         while (enum2.hasMoreElements())
418         {
419             re = (ReportElement)enum2.nextElement();
420             
421             // insert this element in the right position...
422
if (myElements.size() == 0)
423             {
424                 myElements.add(re);
425             }
426             else
427             {
428                 
429                 boolean inserted = false;
430                 
431                 for (int i = 0; i < myElements.size(); ++i)
432                 {
433                     
434                     ReportElement re2 = (ReportElement)myElements.elementAt(i);
435                     
436                     if (re.getPosition().x < re2.getPosition().x)
437                     {
438                         myElements.insertElementAt(re, i);
439                         inserted = true;
440                         
441                         break;
442                     }
443                     else if (re.getPosition().x == re2.getPosition().x)
444                     {
445                         
446                         if (re.getPosition().y < re2.getPosition().y)
447                         {
448                             myElements.insertElementAt(re, i);
449                             inserted = true;
450                             
451                             break;
452                         }
453                     }
454                 }
455                 
456                 if (!inserted)
457                 {
458                     myElements.addElement(re);
459                 }
460             }
461         }
462         
463         return myElements;
464     }
465     
466     /**
467      * DOCUMENT ME!
468      *
469      * @param b DOCUMENT ME!
470      * @return DOCUMENT ME!
471      */

472     Vector JavaDoc getBandElements(Band b)
473     {
474         
475         Vector JavaDoc bandElements = new Vector JavaDoc();
476         
477         for (Iterator JavaDoc i = getSelectedElements().iterator(); i.hasNext();)
478         {
479             
480             ReportElement re = (ReportElement)i.next();
481             
482             if (re.getBand() == b)
483             {
484                 bandElements.add(re);
485             }
486         }
487         
488         return bandElements;
489     }
490     
491     /*
492      * getBands() returns a unique list of bands that have selected elements in it.
493      **/

494     
495     /**
496      * getBands() selects all the bands that have selected elements in it.
497      *
498      * @return Vector with all the bands that have selected elements in it
499      */

500     Vector JavaDoc getBands()
501     {
502         
503         Vector JavaDoc bands = new Vector JavaDoc();
504         
505         for (Iterator JavaDoc h = getSelectedElements().iterator(); h.hasNext();)
506         {
507             
508             ReportElement bandRe = (ReportElement)h.next();
509             
510             if (bandRe.getBand() != null && !bands.contains(bandRe.getBand()))
511             {
512                 bands.add(bandRe.getBand());
513             }
514         }
515         
516         return bands;
517     }
518     
519     static public FormatCommand getCommand(int operationType)
520     {
521         
522         // this must be move to a factory
523
// MainFrame.getMainInstance().logOnConsole("" + operationType);
524
switch (operationType)
525         {
526             
527             case OperationType.ALIGN_TOP:
528                 return (new FormatCommandAlignTop());
529                 
530             case OperationType.ALIGN_BOTTOM:
531                 return (new FormatCommandAlignBottom());
532                 
533             case OperationType.ALIGN_LEFT:
534                 return (new FormatCommandAlignLeft());
535                 
536             case OperationType.ALIGN_RIGHT:
537                 return (new FormatCommandAlignRight());
538                 
539             case OperationType.ALIGN_HORIZONTAL_AXIS:
540                 return (new FormatCommandAlignHorizontalAxis());
541                 
542             case OperationType.ALIGN_VERTICAL_AXIS:
543                 return (new FormatCommandAlignVerticalAxis());
544                 
545             case OperationType.ALIGN_CENTER_HORIZONTALLY:
546                 return (new FormatCommandAlignCenterHorizontally());
547                 
548             case OperationType.ALIGN_CENTER_VERTICALLY:
549                 return (new FormatCommandAlignCenterVertically());
550                 
551             case OperationType.ALIGN_CENTER:
552                 return (new FormatCommandAlignCenter());
553                 
554             case OperationType.SAME_HEIGHT:
555                 return (new FormatCommandSameHeight());
556                 
557             case OperationType.SAME_HEIGHT_MAX:
558                 return (new FormatCommandSameHeightMax());
559                 
560             case OperationType.SAME_HEIGHT_MIN:
561                 return (new FormatCommandSameHeightMin());
562                 
563             case OperationType.SAME_WIDTH:
564                 return (new FormatCommandSameWidth());
565                 
566             case OperationType.SAME_WIDTH_MAX:
567                 return (new FormatCommandSameWidthMax());
568                 
569             case OperationType.SAME_WIDTH_MIN:
570                 return (new FormatCommandSameWidthMin());
571                 
572             case OperationType.SAME_SIZE:
573                 return (new FormatCommandSameSize());
574                 
575             case OperationType.ALIGN_TOP_TO_BAND:
576                 return (new FormatCommandAlignTopToBand());
577                 
578             case OperationType.ALIGN_BOTTOM_TO_BAND:
579                 return (new FormatCommandAlignBottomToBand());
580                 
581             case OperationType.ALIGN_TO_LEFT_MARGIN:
582                 return (new FormatCommandAlignToLeftMargin());
583                 
584             case OperationType.ALIGN_TO_RIGHT_MARGIN:
585                 return (new FormatCommandAlignToRightMargin());
586                 
587             case OperationType.MOVE_TO_LEFT_MARGIN:
588                 return (new FormatCommandMoveToLeftMargin());
589                 
590             case OperationType.MOVE_TO_RIGHT_MARGIN:
591                 return (new FormatCommandMoveToRightMargin());
592                 
593             case OperationType.JOIN_LEFT:
594                 return (new FormatCommandJoinLeft());
595                 
596             case OperationType.JOIN_RIGHT:
597                 return (new FormatCommandJoinRight());
598                 
599             case OperationType.EQUALS_SPACE_H:
600                 return (new FormatCommandEqualsSpaceH());
601                 
602             case OperationType.EQUALS_SPACE_V:
603                 return (new FormatCommandEqualsSpaceV());
604                 
605             case OperationType.INCREASE_SPACE_V:
606                 return (new FormatCommandIncreaseSpaceV());
607                 
608             case OperationType.DECREASE_SPACE_V:
609                 return (new FormatCommandDecreaseSpaceV());
610                 
611             case OperationType.DECREASE_SPACE_H:
612                 return (new FormatCommandDecreaseSpaceH());
613                 
614             case OperationType.INCREASE_SPACE_H:
615                 return (new FormatCommandIncreaseSpaceH());
616                 
617             case OperationType.REMOVE_SPACE_V:
618                 return (new FormatCommandRemoveSpaceV());
619                 
620             case OperationType.ORGANIZE_AS_A_TABLE: // multicommand
621
return (new FormatCommandOrganizeAsATable());
622                 
623             case OperationType.ELEMENT_MAXIMIZE:
624                 return (new FormatCommandElementMaximize());
625                 
626             case OperationType.ELEMENT_MAXIMIZE_H:
627                 return (new FormatCommandElementMaximizeH());
628                 
629             case OperationType.ELEMENT_MAXIMIZE_V:
630                 return (new FormatCommandElementMaximizeV());
631                 
632             case OperationType.CENTER_IN_BAND:
633                 return (new FormatCommandCenterInBand());
634                 
635             case OperationType.CENTER_IN_BAND_V:
636                 return (new FormatCommandCenterInBandV());
637                 
638             case OperationType.CENTER_IN_BAND_H:
639                 return (new FormatCommandCenterInBandH());
640                 
641             case OperationType.SHRINK:
642                 return (new FormatCommandShrink());
643                 
644             case OperationType.SHRINK_ALL:
645                 return (new FormatCommandShrink( OperationType.SHRINK_ALL ));
646                 
647             case OperationType.CENTER_IN_BACKGROUND:
648                 return (new FormatCommandCenterInBackground());
649             default:
650                 return null;
651         }
652     }
653     
654     /**
655      * DOCUMENT ME!
656      *
657      * @return DOCUMENT ME!
658      */

659     public MainFrame getMainFrame()
660     {
661         
662         return mf;
663     }
664     
665     /**
666      * DOCUMENT ME!
667      *
668      * @param mf DOCUMENT ME!
669      */

670     void setMainFrame(MainFrame mf)
671     {
672         this.mf = mf;
673     }
674     
675     /**
676      * Setter for variable sleepUndo
677      *
678      * @param bool. Boolean value to set sleepUndo
679      */

680     void setSleepUndo(boolean bool)
681     {
682         this.sleepUndo = bool;
683     }
684     boolean getSleepUndo()
685     {
686         return this.sleepUndo;
687     }
688     
689     
690     void setTreatAllElements(boolean bool)
691     {
692         this.treatAllElements = bool;
693     }
694     boolean getTreatAllElements()
695     {
696         return this.treatAllElements;
697     }
698
699     
700     
701 }
702
Popular Tags