KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > AReport


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.print;
15
16 import java.sql.*;
17 import java.util.*;
18
19 import java.awt.*;
20 import java.awt.event.*;
21 import javax.swing.*;
22
23 import org.compiere.util.*;
24 import org.compiere.model.*;
25 import org.compiere.apps.*;
26
27 /**
28  * Application Report Launcher.
29  * Called from APanel; Queries available Reports for Table.
30  * If no report available, a new one is created and launched.
31  * If there is one report, it is launched.
32  * If there are more, the available reports are displayed and the selected is launched.
33  *
34  * @author Jorg Janke
35  * @version $Id: AReport.java,v 1.9 2003/10/27 15:22:03 jjanke Exp $
36  */

37 public class AReport implements ActionListener
38 {
39     /**
40      * Constructor
41      *
42      * @param AD_Table_ID table
43      * @param invoker component to display popup (optional)
44      * @param query query
45      */

46     public AReport (int AD_Table_ID, JComponent invoker, MQuery query)
47     {
48         Log.trace(Log.l3_Util, "AReport", "AD_Table_ID=" + AD_Table_ID + " " + query);
49         if (!MRole.getDefault().isCanReport(AD_Table_ID))
50         {
51             ADialog.error(0, invoker, "AccessCannotReport", query.getTableName());
52             return;
53         }
54
55         m_query = query;
56
57         // See What is there
58
getPrintFormats (AD_Table_ID, invoker);
59     } // AReport
60

61     /** The Query */
62     private MQuery m_query;
63     /** The Popup */
64     private JPopupMenu m_popup = new JPopupMenu("MyMenu");
65     /** The Option List */
66     private ArrayList m_list = new ArrayList();
67
68
69     /**
70      * Get the Print Formats for the table.
71      * Fill the list and the popup menu
72      * @param AD_Table_ID table
73      * @param invoker component to display popup (optional)
74      */

75     private void getPrintFormats (int AD_Table_ID, JComponent invoker)
76     {
77         int AD_Client_ID = Env.getContextAsInt(Env.getCtx(), "#AD_Client_ID");
78         //
79
String JavaDoc sql = MRole.getDefault().addAccessSQL (
80             "SELECT AD_PrintFormat_ID, Name, AD_Client_ID "
81                 + "FROM AD_PrintFormat "
82                 + "WHERE AD_Table_ID=? AND IsTableBased='Y' "
83                 + "ORDER BY AD_Client_ID DESC", // Own First
84
"AD_PrintFormat", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
85         KeyNamePair pp = null;
86         try
87         {
88             PreparedStatement pstmt = DB.prepareStatement(sql);
89             pstmt.setInt(1, AD_Table_ID);
90             ResultSet rs = pstmt.executeQuery();
91             while (rs.next())
92             {
93                 pp = new KeyNamePair (rs.getInt(1), rs.getString(2));
94                 if (rs.getInt(3) == AD_Client_ID)
95                 {
96                     m_list.add(pp);
97                     m_popup.add(pp.toString()).addActionListener(this);
98                 }
99             }
100             rs.close();
101             pstmt.close();
102         }
103         catch (SQLException e)
104         {
105             Log.error("AReport.getPrintFormats", e);
106         }
107
108         // No Format exists - create it
109
if (m_list.size() == 0)
110         {
111             if (pp == null)
112                 createNewFormat (AD_Table_ID); // calls launch
113
else
114                 copyFormat(pp.getKey(), AD_Client_ID);
115         }
116         // One Format exists or no invoker - show it
117
else if (m_list.size() == 1 || invoker == null)
118             launchReport ((KeyNamePair)m_list.get(0));
119         // Multiple Formats exist - show selection
120
else
121             m_popup.show(invoker, 0, invoker.getHeight()); // below button
122
} // getPrintFormats
123

124     /**
125      * Create and Launch new Format for table
126      * @param AD_Table_ID table
127      */

128     private void createNewFormat (int AD_Table_ID)
129     {
130         MPrintFormat pf = MPrintFormat.createFromTable(Env.getCtx(), AD_Table_ID);
131         launchReport (pf);
132     } // createNewFormat
133

134     /**
135      * Copy existing Format
136      * @param AD_PrintFormat_ID print format
137      * @param To_Client_ID to client
138      */

139     private void copyFormat (int AD_PrintFormat_ID, int To_Client_ID)
140     {
141         MPrintFormat pf = MPrintFormat.copyToClient(Env.getCtx(), AD_PrintFormat_ID, To_Client_ID);
142         launchReport (pf);
143     } // copyFormatFromClient
144

145     /**
146      * Launch Report
147      * @param pp Key=AD_PrintFormat_ID
148      */

149     private void launchReport (KeyNamePair pp)
150     {
151         MPrintFormat pf = MPrintFormat.get(pp.getKey(), false);
152         launchReport (pf);
153     } // launchReport
154

155     /**
156      * Launch Report
157      * @param pf print format
158      */

159     private void launchReport (MPrintFormat pf)
160     {
161         ReportEngine re = new ReportEngine (Env.getCtx(), pf, m_query);
162         new Viewer(re);
163     // if (m_popup.isVisible())
164
// m_popup.setVisible(false);
165
} // launchReport
166

167     /**
168      * Action Listener
169      * @param e event
170      */

171     public void actionPerformed(ActionEvent e)
172     {
173         m_popup.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
174         String JavaDoc cmd = e.getActionCommand();
175         for (int i = 0; i < m_list.size(); i++)
176         {
177             KeyNamePair pp = (KeyNamePair)m_list.get(i);
178             if (cmd.equals(pp.getName()))
179             {
180                 launchReport (pp);
181                 return;
182             }
183         }
184     } // actionPerformed
185

186     /*************************************************************************/
187
188     /**
189      * Get AD_Table_ID for Table Name
190      * @param TableName table name
191      * @return AD_Table_ID or 0
192      */

193     static public int getAD_Table_ID (String JavaDoc TableName)
194     {
195         int AD_Table_ID = 0;
196         String JavaDoc sql = "SELECT AD_Table_ID FROM AD_Table WHERE TableName=?";
197         try
198         {
199             PreparedStatement pstmt = DB.prepareStatement(sql);
200             pstmt.setString(1, TableName);
201             ResultSet rs = pstmt.executeQuery();
202             if (rs.next())
203                 AD_Table_ID = rs.getInt(1);
204             rs.close();
205             pstmt.close();
206         }
207         catch (SQLException e)
208         {
209             Log.error("AReport.getAD_Table_ID", e);
210         }
211         return AD_Table_ID;
212     } // getAD_Table_ID
213

214 } // AReport
215

216
Popular Tags