Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jbreport/core/SectionBreak.java


1   /*
2    * $Id: SectionBreak.java,v 1.1 2000/08/31 13:53:17 grantfin Exp $
3    *
4    * jbReport - A reporting library for Java
5    * Copyright (C) 2000 Grant Finnemore <grantfin@users.sourceforge.net>
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package jbreport.core;
22  
23  import org.xml.sax.Attributes;
24  
25  import jbreport.ReportElement;
26  import jbreport.ReportException;
27  import jbreport.data.QueryResult;
28  import jbreport.util.ReportBreak;
29  
30  /**
31   * This provides for breaking a section into sub-sections based on various 
32   * events.
33   *
34   * @author Grant Finnemore
35   * @version $Revision: 1.1 $
36   */
37  class SectionBreak extends AbstractReportElement implements ReportBreak {
38  
39     /** The type of event that will trigger this break */
40     private String breakEvent;
41  
42     /** The current value of the condition variable */
43     private String value;
44  
45     /** The QueryResult variable that will be checked */
46     private String id;
47  
48     /** The query that this is attached to */
49     private String queryName;
50  
51     //
52     // Constructors
53     //
54  
55     //
56     // Accessor methods
57     //
58  
59     public String getQueryName() {
60        return queryName;
61     }
62  
63     //
64     // Implementation of the ReportBreak interface
65     //
66  
67     public String getBreakEvent() {
68        return breakEvent;
69     }
70  
71     public boolean isConditionTrue(Object helper) throws ReportException {
72        boolean result = false;
73        if(helper instanceof QueryResult) {
74           String curVal = ((QueryResult)helper).getString(id);
75           if(curVal == null || !curVal.equals(value)) {
76              result = true;
77              value = curVal;
78           }
79        }
80        else {
81           System.err.println(this + " doesn't know how to deal with " + helper);
82        }
83        return result;
84     }
85  
86     //
87     // Implementation of the Comparable interface
88     //
89  
90     public int compareTo(Object obj) {
91        // The interface says that this can throw a ClassCastException
92        SectionBreak oth = (SectionBreak)obj;
93        // The ordering depends on the level within the document. ie. a shallower
94        // break is considered greater than a deeper break.
95        return getDepth(oth) - getDepth(this);
96     }
97  
98     //
99     // Methods overloaded from AbstractReportElement
100    //
101 
102    public void accept(ReportVisitor visitor, ReportVisitorState state) 
103       throws ReportException {
104       visitor.visitSectionBreak(this, state);
105    }
106 
107    public void xmlInitialize(String localName, Attributes attributes) 
108       throws ReportException {
109       
110       super.xmlInitialize(localName, attributes);
111       breakEvent = attributes.getValue("event");
112       assertNotNull(breakEvent, "breakEvent");
113       id = attributes.getValue("id");
114       assertNotNull(id, "id");
115       queryName = attributes.getValue("query");
116       assertNotNull(queryName, "query");
117    }
118 
119    //
120    // Implementation methods
121    //
122 
123    private static int getDepth(SectionBreak sb) {
124       int result = 0;
125       for(ReportElement elem = sb; elem != null; elem = elem.getParent()) {
126          result++;
127       }
128       return result;
129    }
130 
131 }