001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * FormatCommand.java
028: *
029: */
030:
031: package it.businesslogic.ireport.gui.command;
032:
033: import it.businesslogic.ireport.Band;
034: import it.businesslogic.ireport.CrosstabReportElement;
035: import it.businesslogic.ireport.OperationType;
036: import it.businesslogic.ireport.ReportElement;
037: import it.businesslogic.ireport.gui.JReportFrame;
038: import it.businesslogic.ireport.gui.MainFrame;
039: import it.businesslogic.ireport.undo.FormatElementsOperation;
040:
041: import java.util.Enumeration;
042: import java.util.Iterator;
043: import java.util.Vector;
044:
045: /**
046: * FormatCommand.java. Created on 9 mei 2005, 21:32 By Robert Lamping Using
047: * the Command pattern
048: */
049: /**
050: * @author Fourdim
051: */
052: public class FormatCommand implements ICommand {
053:
054: /**
055: * The attribute sleepUndo is introduced to repeat multiple commands. When
056: * sleepUndo = true then it prevents the storage of multiple undo
057: * transitions.
058: */
059: static boolean sleepUndo = false;
060:
061: /** mf is a reference to the Mainframe instance. */
062: private MainFrame mf = null;
063:
064: /** mf is a reference to the current JReportFrame instance. */
065: JReportFrame jrf = null;
066: Vector oldSelectedElements = null;
067:
068: /** e is the enumeration of the selected elements. */
069: Enumeration e = null;
070:
071: boolean treatAllElements = false;
072:
073: ReportElement re = null;
074: int operationType = 0;
075: int currentElementPosition = 0;
076: FormatElementsOperation undoOp = null;
077:
078: public Vector getSelectedElements() {
079: jrf = MainFrame.getMainInstance().getActiveReportFrame();
080: if (jrf == null)
081: return null;
082: return MainFrame.getMainInstance().getActiveElementSelection(
083: jrf);
084: }
085:
086: public void setSelectedElements(Vector elements) {
087: jrf = MainFrame.getMainInstance().getActiveReportFrame();
088: if (jrf == null)
089: return;
090:
091: if (jrf.getSelectedCrosstabEditorPanel() != null) {
092: jrf.getSelectedCrosstabEditorPanel().setSelectedElements(
093: elements);
094: } else {
095: jrf.setSelectedElements(elements);
096: }
097: }
098:
099: /**
100: * DOCUMENT ME!
101: */
102: public void execute() {
103: mf = MainFrame.getMainInstance();
104:
105: if (mf.getJMDIDesktopPane().getSelectedFrame() != null
106: && mf.getJMDIDesktopPane().getSelectedFrame() instanceof JReportFrame) {
107: // everytime the current selectedFrame must be chosen
108: jrf = (JReportFrame) mf.getJMDIDesktopPane()
109: .getSelectedFrame();
110:
111: if (getTreatAllElements()) {
112: oldSelectedElements = (Vector) getSelectedElements()
113: .clone();
114: this .setSelectedElements((Vector) jrf.getReport()
115: .getElements().clone());
116: }
117:
118: // TODO: Buttons should listen to number of selectelements and
119: // enable or disable themselves.
120: if (getSelectedElements().isEmpty()) {
121: // do nothing
122: } else {
123: if (preCondition()) {
124: re = null;
125: CrosstabReportElement cre = null;
126: if (jrf.getSelectedCrosstabEditorPanel() != null) {
127: cre = jrf.getSelectedCrosstabEditorPanel()
128: .getCrosstabElement();
129: }
130: undoOp = new FormatElementsOperation(jrf, cre,
131: operationType);
132: executeDeeper();
133:
134: //Compulsary:
135: postAction();
136: }
137: }
138:
139: // reset selectedElements selection
140: if (getTreatAllElements()) {
141: this .setSelectedElements((Vector) oldSelectedElements
142: .clone());
143: }
144: }
145: }
146:
147: /**
148: * DOCUMENT ME!
149: */
150: void executeDeeper() {
151: resetEnumeration();
152: preparation(); // <========= method in subclass is called if present
153: resetEnumeration();
154: processElements();
155: }
156:
157: /**
158: * This is the regular processElements(0 in which the current selected elements
159: * are processed
160: * It is possible to use the variant where you pass your own selection of elements
161: */
162: void processElements() {
163: processElements(this .e);
164: }
165:
166: /**
167: * Register the elements in the UndoOp instaed of misusing processElements.
168: * in registerElements the method modify() is not executed.
169: * It is use for multi command FormatCommands, such as FormatCommandOrganizeAsATable.
170: * @param e DOCUMENT ME!
171: */
172: void registerElements() {
173: Enumeration e = getSelectedElements().elements();
174: while (e.hasMoreElements()) {
175: undoOp.addElement(e.nextElement());
176: }
177: }
178:
179: /**
180: * DOCUMENT ME!
181: *
182: * @param e DOCUMENT ME!
183: */
184: void processElements(Enumeration e) {
185:
186: currentElementPosition = -1;
187:
188: while (e.hasMoreElements()) {
189: currentElementPosition++;
190: re = (ReportElement) e.nextElement();
191:
192: // Undo preparation, phase 1 (pre element transition)
193: // Rectangle oldBounds = new Rectangle(re.getBounds());
194:
195: if (!getSleepUndo()) {
196: undoOp.addElement(re);
197: }
198: // else: "multicommandmode' more than one command is execute in one Undo Operation
199:
200: //undoOp.captureCurrent(re); // acts on last element on stack
201:
202: // mf.logOnConsole( re.toString() );
203: modify(); // <========= method in subclass is called
204:
205: // adjusting the boundary of the report element
206: // to the current position, width and height.
207: re.updateBounds();
208:
209: // Undo preparation, phase 2 (post element transition)
210: // Rectangle newBounds = new Rectangle(re.getBounds());
211:
212: // se FormatCommand.java for undoOp.
213: //mf.logOnConsole("NewBounds" + re.toString() + "\n");
214:
215: if (!getSleepUndo()) {
216: undoOp.captureUniqueModified(re);
217: }
218:
219: //undoOp.addElement(re, oldBounds, newBounds);
220: }
221: }
222:
223: /**
224: * DOCUMENT ME!
225: */
226: void updateElements() {
227: Enumeration e = getSelectedElements().elements();
228:
229: while (e.hasMoreElements()) {
230: try {
231: // modify this so that also JReportFrame objects can be passed
232: undoOp.captureUniqueModified((ReportElement) e
233: .nextElement());
234: } catch (Exception ex) {
235: // exception occurs when element is diffent from ReportElement
236: }
237: }
238: }
239:
240: /**
241: * DOCUMENT ME!
242: *
243: * @return DOCUMENT ME!
244: */
245: int getCurrentElementPosition() {
246:
247: return currentElementPosition;
248: }
249:
250: /**
251: * DOCUMENT ME!
252: */
253: void preparation() {
254: //dummy
255: }
256:
257: // TODO: Restrict access, make it private check with interface
258:
259: /**
260: * DOCUMENT ME!
261: */
262: void modify() {
263: // if the subclass has a method modify then that method will be executed
264: // otherwise this one.
265: // Fake, the method of the subclass will normally be performed.
266: }
267:
268: /**
269: * DOCUMENT ME!
270: */
271: void resetEnumeration() {
272: e = getSelectedElements().elements();
273: }
274:
275: /**
276: * DOCUMENT ME!
277: *
278: * @return DOCUMENT ME!
279: */
280: boolean preCondition() {
281:
282: return true;
283: }
284:
285: /**
286: * DOCUMENT ME!
287: */
288: void postAction() {
289: if (jrf.getSelectedCrosstabEditorPanel() == null) {
290: jrf.getReportPanel().repaint();
291:
292: if (!sleepUndo) {
293: jrf.addUndoOperation(undoOp);
294:
295: mf.getDocumentStructurePanel()
296: .updateDocumentStructureTree(jrf);
297:
298: // this part should be move to a listener
299: jrf.getReportPanel().repaint();
300: }
301: } else {
302: jrf.getSelectedCrosstabEditorPanel().repaint();
303: if (!sleepUndo) {
304: jrf.addUndoOperation(undoOp);
305:
306: //mf.getDocumentStructurePanel().updateDocumentStructureTree(jrf);
307:
308: // this part should be move to a listener
309: //jrf.getReportPanel().repaint();
310: }
311: }
312:
313: }
314:
315: /**
316: * DOCUMENT ME!
317: *
318: * @param enum2 DOCUMENT ME!
319: * @return DOCUMENT ME!
320: */
321: Vector sortYX(Enumeration enum2) {
322:
323: Vector myElements = new Vector();
324:
325: while (enum2.hasMoreElements()) {
326: re = (ReportElement) enum2.nextElement();
327:
328: // insert this element in the right position...
329: if (myElements.size() == 0) {
330: myElements.add(re);
331: } else {
332:
333: boolean inserted = false;
334:
335: for (int i = 0; i < myElements.size(); ++i) {
336:
337: ReportElement re2 = (ReportElement) myElements
338: .elementAt(i);
339:
340: if (re.getPosition().y < re2.getPosition().y) {
341: myElements.insertElementAt(re, i);
342: inserted = true;
343:
344: break;
345: } else if (re.getPosition().y == re2.getPosition().y) {
346:
347: if (re.getPosition().x < re2.getPosition().x) {
348: myElements.insertElementAt(re, i);
349: inserted = true;
350:
351: break;
352: }
353: }
354: }
355:
356: if (!inserted) {
357: myElements.addElement(re);
358: }
359: }
360: }
361:
362: return myElements;
363: }
364:
365: /**
366: * DOCUMENT ME!
367: *
368: * @param enum2 DOCUMENT ME!
369: * @return DOCUMENT ME!
370: */
371: Vector sortXY(Enumeration enum2) {
372:
373: Vector myElements = new Vector();
374:
375: while (enum2.hasMoreElements()) {
376: re = (ReportElement) enum2.nextElement();
377:
378: // insert this element in the right position...
379: if (myElements.size() == 0) {
380: myElements.add(re);
381: } else {
382:
383: boolean inserted = false;
384:
385: for (int i = 0; i < myElements.size(); ++i) {
386:
387: ReportElement re2 = (ReportElement) myElements
388: .elementAt(i);
389:
390: if (re.getPosition().x < re2.getPosition().x) {
391: myElements.insertElementAt(re, i);
392: inserted = true;
393:
394: break;
395: } else if (re.getPosition().x == re2.getPosition().x) {
396:
397: if (re.getPosition().y < re2.getPosition().y) {
398: myElements.insertElementAt(re, i);
399: inserted = true;
400:
401: break;
402: }
403: }
404: }
405:
406: if (!inserted) {
407: myElements.addElement(re);
408: }
409: }
410: }
411:
412: return myElements;
413: }
414:
415: /**
416: * DOCUMENT ME!
417: *
418: * @param b DOCUMENT ME!
419: * @return DOCUMENT ME!
420: */
421: Vector getBandElements(Band b) {
422:
423: Vector bandElements = new Vector();
424:
425: for (Iterator i = getSelectedElements().iterator(); i.hasNext();) {
426:
427: ReportElement re = (ReportElement) i.next();
428:
429: if (re.getBand() == b) {
430: bandElements.add(re);
431: }
432: }
433:
434: return bandElements;
435: }
436:
437: /*
438: * getBands() returns a unique list of bands that have selected elements in it.
439: **/
440:
441: /**
442: * getBands() selects all the bands that have selected elements in it.
443: *
444: * @return Vector with all the bands that have selected elements in it
445: */
446: Vector getBands() {
447:
448: Vector bands = new Vector();
449:
450: for (Iterator h = getSelectedElements().iterator(); h.hasNext();) {
451:
452: ReportElement bandRe = (ReportElement) h.next();
453:
454: if (bandRe.getBand() != null
455: && !bands.contains(bandRe.getBand())) {
456: bands.add(bandRe.getBand());
457: }
458: }
459:
460: return bands;
461: }
462:
463: static public FormatCommand getCommand(int operationType) {
464:
465: // this must be move to a factory
466: // MainFrame.getMainInstance().logOnConsole("" + operationType);
467: switch (operationType) {
468:
469: case OperationType.ALIGN_TOP:
470: return (new FormatCommandAlignTop());
471:
472: case OperationType.ALIGN_BOTTOM:
473: return (new FormatCommandAlignBottom());
474:
475: case OperationType.ALIGN_LEFT:
476: return (new FormatCommandAlignLeft());
477:
478: case OperationType.ALIGN_RIGHT:
479: return (new FormatCommandAlignRight());
480:
481: case OperationType.ALIGN_HORIZONTAL_AXIS:
482: return (new FormatCommandAlignHorizontalAxis());
483:
484: case OperationType.ALIGN_VERTICAL_AXIS:
485: return (new FormatCommandAlignVerticalAxis());
486:
487: case OperationType.ALIGN_CENTER_HORIZONTALLY:
488: return (new FormatCommandAlignCenterHorizontally());
489:
490: case OperationType.ALIGN_CENTER_VERTICALLY:
491: return (new FormatCommandAlignCenterVertically());
492:
493: case OperationType.ALIGN_CENTER:
494: return (new FormatCommandAlignCenter());
495:
496: case OperationType.SAME_HEIGHT:
497: return (new FormatCommandSameHeight());
498:
499: case OperationType.SAME_HEIGHT_MAX:
500: return (new FormatCommandSameHeightMax());
501:
502: case OperationType.SAME_HEIGHT_MIN:
503: return (new FormatCommandSameHeightMin());
504:
505: case OperationType.SAME_WIDTH:
506: return (new FormatCommandSameWidth());
507:
508: case OperationType.SAME_WIDTH_MAX:
509: return (new FormatCommandSameWidthMax());
510:
511: case OperationType.SAME_WIDTH_MIN:
512: return (new FormatCommandSameWidthMin());
513:
514: case OperationType.SAME_SIZE:
515: return (new FormatCommandSameSize());
516:
517: case OperationType.ALIGN_TOP_TO_BAND:
518: return (new FormatCommandAlignTopToBand());
519:
520: case OperationType.ALIGN_BOTTOM_TO_BAND:
521: return (new FormatCommandAlignBottomToBand());
522:
523: case OperationType.ALIGN_TO_LEFT_MARGIN:
524: return (new FormatCommandAlignToLeftMargin());
525:
526: case OperationType.ALIGN_TO_RIGHT_MARGIN:
527: return (new FormatCommandAlignToRightMargin());
528:
529: case OperationType.MOVE_TO_LEFT_MARGIN:
530: return (new FormatCommandMoveToLeftMargin());
531:
532: case OperationType.MOVE_TO_RIGHT_MARGIN:
533: return (new FormatCommandMoveToRightMargin());
534:
535: case OperationType.JOIN_LEFT:
536: return (new FormatCommandJoinLeft());
537:
538: case OperationType.JOIN_RIGHT:
539: return (new FormatCommandJoinRight());
540:
541: case OperationType.EQUALS_SPACE_H:
542: return (new FormatCommandEqualsSpaceH());
543:
544: case OperationType.EQUALS_SPACE_V:
545: return (new FormatCommandEqualsSpaceV());
546:
547: case OperationType.INCREASE_SPACE_V:
548: return (new FormatCommandIncreaseSpaceV());
549:
550: case OperationType.DECREASE_SPACE_V:
551: return (new FormatCommandDecreaseSpaceV());
552:
553: case OperationType.DECREASE_SPACE_H:
554: return (new FormatCommandDecreaseSpaceH());
555:
556: case OperationType.INCREASE_SPACE_H:
557: return (new FormatCommandIncreaseSpaceH());
558:
559: case OperationType.REMOVE_SPACE_V:
560: return (new FormatCommandRemoveSpaceV());
561:
562: case OperationType.ORGANIZE_AS_A_TABLE: // multicommand
563: return (new FormatCommandOrganizeAsATable());
564:
565: case OperationType.ELEMENT_MAXIMIZE:
566: return (new FormatCommandElementMaximize());
567:
568: case OperationType.ELEMENT_MAXIMIZE_H:
569: return (new FormatCommandElementMaximizeH());
570:
571: case OperationType.ELEMENT_MAXIMIZE_V:
572: return (new FormatCommandElementMaximizeV());
573:
574: case OperationType.CENTER_IN_BAND:
575: return (new FormatCommandCenterInBand());
576:
577: case OperationType.CENTER_IN_BAND_V:
578: return (new FormatCommandCenterInBandV());
579:
580: case OperationType.CENTER_IN_BAND_H:
581: return (new FormatCommandCenterInBandH());
582:
583: case OperationType.SHRINK:
584: return (new FormatCommandShrink());
585:
586: case OperationType.SHRINK_ALL:
587: return (new FormatCommandShrink(OperationType.SHRINK_ALL));
588:
589: case OperationType.CENTER_IN_BACKGROUND:
590: return (new FormatCommandCenterInBackground());
591: default:
592: return null;
593: }
594: }
595:
596: /**
597: * DOCUMENT ME!
598: *
599: * @return DOCUMENT ME!
600: */
601: public MainFrame getMainFrame() {
602:
603: return mf;
604: }
605:
606: /**
607: * DOCUMENT ME!
608: *
609: * @param mf DOCUMENT ME!
610: */
611: void setMainFrame(MainFrame mf) {
612: this .mf = mf;
613: }
614:
615: /**
616: * Setter for variable sleepUndo
617: *
618: * @param bool. Boolean value to set sleepUndo
619: */
620: void setSleepUndo(boolean bool) {
621: this .sleepUndo = bool;
622: }
623:
624: boolean getSleepUndo() {
625: return this .sleepUndo;
626: }
627:
628: void setTreatAllElements(boolean bool) {
629: this .treatAllElements = bool;
630: }
631:
632: boolean getTreatAllElements() {
633: return this.treatAllElements;
634: }
635:
636: }
|