001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * 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, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb.action;
023:
024: import it.frb.*;
025: import java.util.*;
026: import javax.swing.JComponent;
027:
028: public class ActionControl_I extends ActionDataPanel {
029: String sSql = "";
030: ArrayList labels = null;
031: ArrayList columns = null;
032:
033: public ActionControl_I(String action) {
034: super (action);
035: }
036:
037: public void setHeaderDetailComponents(ArrayList labels,
038: ArrayList columns) {
039: this .labels = labels;
040: this .columns = columns;
041: }
042:
043: public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
044: java.sql.Statement stm = null;
045: java.sql.ResultSet rst = null;
046: java.sql.Connection conn = null;
047: java.sql.ResultSetMetaData rstMD = null;
048:
049: int countColumn = 0;
050:
051: if (labels != null || columns != null) {
052: if (labels != null) {
053: int x = 0;
054:
055: for (ListIterator iter = labels.listIterator(); iter
056: .hasNext();) {
057: Object objLabel = this .addJComponent("JLabel",
058: "Header");
059: javax.swing.JLabel jlb = (javax.swing.JLabel) objLabel;
060: jlb.setText(iter.next().toString());
061: jlb.setSize(100, 20);
062: jlb.setLocation(x, 1);
063: jlb.setForeground(java.awt.Color.red);
064: dataPanel.repaint(x, 20, 50, 20);
065:
066: x = x + 101;
067: }
068: }
069:
070: if (columns != null) {
071: int x = 0;
072:
073: for (ListIterator iter = labels.listIterator(); iter
074: .hasNext();) {
075: Object obj = this .addJComponent("JTextField",
076: "Detail");
077: javax.swing.JTextField jtf = (javax.swing.JTextField) obj;
078: jtf.setText(iter.next().toString());
079: jtf.setSize(100, 20);
080: jtf.setEditable(false);
081: jtf.setLocation(x, 1);
082:
083: dataPanel.repaint(x, 20, 50, 20);
084:
085: x = x + 101;
086: }
087: }
088: } else {
089: sSql = dataPanel.getSql().toUpperCase();
090: if (!sSql.equals("")) {
091: try {
092: conn = Connessione.getConnessione();
093:
094: stm = conn.createStatement();
095: rst = stm.executeQuery(sSql);
096: rstMD = rst.getMetaData();
097: countColumn = rstMD.getColumnCount();
098:
099: int x = 10;
100:
101: for (int i = 1; i <= countColumn; i++) {
102: Object obj = this .addJComponent("JTextField",
103: "Detail");
104: javax.swing.JTextField jtf = (javax.swing.JTextField) obj;
105:
106: String sName = "";
107: String sColumnNameFromMetadata = rstMD
108: .getTableName(i);
109:
110: if (sColumnNameFromMetadata.equals("")) {
111: String sTableName = getTableNameFromColumn(
112: sSql, rstMD.getColumnName(i));
113: if (!sTableName.equals("")) {
114: if (sTableName.indexOf("\"") == -1) {
115: sName = sTableName + "."
116: + rstMD.getColumnName(i);
117: } else {
118: sName = sTableName + ".\""
119: + rstMD.getColumnName(i)
120: + "\"";
121: }
122: } else {
123: sName = rstMD.getColumnName(i);
124: }
125: } else {
126: sName = rstMD.getTableName(i) + "."
127: + rstMD.getColumnName(i);
128: }
129:
130: int columnSize = rstMD.getColumnDisplaySize(i);
131:
132: if (columnSize > 300) {
133: columnSize = 100;
134: }
135:
136: jtf.setText(sName);
137: jtf.setSize(columnSize * 10, 20);
138: jtf.setEditable(false);
139: jtf.setLocation(x, 1);
140:
141: Object objLabel = this .addJComponent("JLabel",
142: "Header");
143: javax.swing.JLabel jlb = (javax.swing.JLabel) objLabel;
144: jlb.setText(rstMD.getColumnLabel(i));
145:
146: jlb.setSize(columnSize * 10, 20);
147: jlb.setLocation(x, 1);
148: jlb.setForeground(java.awt.Color.red);
149:
150: x = x + columnSize * 10;
151:
152: dataPanel.repaint(x, 20, 50, 20);
153: dataPanel.getTextArea().setPreferredSize(
154: new java.awt.Dimension(x, 10000));
155: }
156:
157: rst.close();
158: stm.close();
159:
160: boolean bMltStm = ConfigurationProperties
161: .properties().getUseMultiTransaction(false);
162:
163: if (!bMltStm) {
164: conn.close();
165: }
166:
167: } catch (java.sql.SQLException s) {
168: javax.swing.JOptionPane.showMessageDialog(null, s
169: .getMessage());
170: } finally {
171: try {
172: stm.close();
173:
174: boolean bMltStm = ConfigurationProperties
175: .properties().getUseMultiTransaction(
176: false);
177:
178: if (!bMltStm) {
179: conn.close();
180: }
181:
182: } catch (java.sql.SQLException s) {
183: javax.swing.JOptionPane.showMessageDialog(null,
184: s.getMessage());
185: }
186: }
187: }
188: }
189: }
190:
191: private String getTableNameFromColumn(String sql, String columnName) {
192: String sTableName = "";
193:
194: int indexColumn = sql.indexOf(columnName);
195:
196: if (indexColumn != -1) {
197: String mark = sql.substring(indexColumn - 1, indexColumn);
198:
199: if (mark.equals(".")) {
200: int index = indexColumn - 1;
201:
202: while (!sql.substring(index - 1, index).equals(" ")
203: && !sql.substring(index - 1, index)
204: .equals("\t")) {
205: index = index - 1;
206: }
207:
208: sTableName = sql.substring(index, indexColumn - 1);
209: //1.4
210: //String sSql = sql.replaceFirst(sTableName + "." + columnName," ");
211: String str = new String(sTableName + "." + columnName);
212: int sl = str.length();
213: index = sql.indexOf(str);
214: sSql = sql.substring(0, index) + " "
215: + sql.substring(index + sl);
216:
217: } else if (mark.equals("\"")) {
218: int index = indexColumn - 2;
219:
220: while (!sql.substring(index - 1, index).equals(" ")
221: && !sql.substring(index - 1, index)
222: .equals("\t")) {
223: index = index - 1;
224: }
225:
226: sTableName = sql.substring(index, indexColumn - 2);
227: //1.4
228: //String sSql = sql.replaceFirst(sTableName + ".\"" + columnName + "\""," ");
229: String str = new String(sTableName + ".\"" + columnName
230: + "\"");
231: int sl = str.length();
232: index = sql.indexOf(str);
233: sSql = sql.substring(0, index) + " "
234: + sql.substring(index + sl);
235:
236: }
237: }
238:
239: return sTableName;
240: }
241:
242: private Object addJComponent(String className, String panelName) {
243: Object obj = null;
244: Class cObj = null;
245:
246: try {
247: cObj = Class.forName("javax.swing." + className);
248: obj = cObj.newInstance();
249:
250: } catch (InstantiationException a) {
251: System.out.println(a.getMessage());
252:
253: } catch (IllegalAccessException b) {
254: System.out.println(b.getMessage());
255:
256: } catch (ClassNotFoundException e) {
257: System.out.println(e.getMessage());
258: }
259:
260: ((JComponent) obj).addMouseListener(drg);
261: ((JComponent) obj).addMouseMotionListener(drg);
262: ((JComponent) obj).addKeyListener(drg);
263:
264: if (panelName.equalsIgnoreCase("header")) {
265: dataPanel.addToHeader((JComponent) obj);
266: } else if (panelName.equalsIgnoreCase("detail")) {
267: dataPanel.addToDetail((JComponent) obj);
268: } else if (panelName.equalsIgnoreCase("summary")) {
269: dataPanel.addToSummary((JComponent) obj);
270: } else if (panelName.equalsIgnoreCase("footer")) {
271: dataPanel.addToFooter((JComponent) obj);
272: }
273:
274: return obj;
275: }
276: }
|