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.awt.*;
026: import java.awt.event.*;
027: import java.net.*;
028: import java.sql.*;
029: import java.util.*;
030: import javax.swing.*;
031:
032: public class ActionControl_G extends ActionDataPanel implements
033: MouseListener {
034: JFrame selectXYData = null;
035: JButton oK = null;
036: JButton cancel = null;
037: JComboBox xData = null;
038: JComboBox yData = null;
039: JToggleButton tbBar = null;
040: JToggleButton tbBar3d = null;
041: JToggleButton tbPie3d = null;
042: JToggleButton tbPie = null;
043: JToggleButton tbXY = null;
044:
045: boolean closeOnDispose = false;
046: int chartType = Chart.BAR3D;
047: Vector xVector = null;
048: Vector yVector = null;
049:
050: //Thread th = null;
051: //DataEngineGraph pml = null;
052:
053: public ActionControl_G(String action) {
054: super (action);
055: }
056:
057: public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
058: if (actionEvent == null) {
059: closeOnDispose = true;
060: }
061:
062: URL imgIconURLBar = DataEngineGraph.class
063: .getResource("img/bar.jpg");
064: URL imgIconURLBar3d = DataEngineGraph.class
065: .getResource("img/bar3d.jpg");
066: URL imgIconURLPie = DataEngineGraph.class
067: .getResource("img/pie.jpg");
068: URL imgIconURLPie3d = DataEngineGraph.class
069: .getResource("img/pie3d.jpg");
070: URL imgIconURLXYLine = DataEngineGraph.class
071: .getResource("img/xyline.jpg");
072:
073: ImageIcon imgBar = new ImageIcon(java.awt.Toolkit
074: .getDefaultToolkit().getImage(imgIconURLBar));
075: ImageIcon imgBar3d = new ImageIcon(java.awt.Toolkit
076: .getDefaultToolkit().getImage(imgIconURLBar3d));
077: ImageIcon imgPie = new ImageIcon(java.awt.Toolkit
078: .getDefaultToolkit().getImage(imgIconURLPie));
079: ImageIcon imgPie3d = new ImageIcon(java.awt.Toolkit
080: .getDefaultToolkit().getImage(imgIconURLPie3d));
081: ImageIcon imgXYLine = new ImageIcon(java.awt.Toolkit
082: .getDefaultToolkit().getImage(imgIconURLXYLine));
083:
084: selectXYData = new JFrame("Select Chart");
085: selectXYData.setBounds(0, 0, 400, 200);
086: DataEngine.centerWindow(selectXYData, null);
087: selectXYData.setResizable(false);
088:
089: Vector xyColumns = this .dataPanel.getColumns();
090: String columns[] = new String[xyColumns.size()];
091:
092: for (int i = 0; i < xyColumns.size(); i++) {
093: columns[i] = ((JTextField) xyColumns.get(i)).getText();
094: }
095:
096: JLabel x = new JLabel(" X");
097: x.setBounds(2, 10, 20, 25);
098: x.setPreferredSize(new Dimension(20, 25));
099:
100: JLabel y = new JLabel(" Y");
101: y.setBounds(2, 60, 20, 25);
102: y.setPreferredSize(new Dimension(20, 25));
103:
104: xData = new JComboBox(columns);
105: xData.setEditable(true);
106: xData.setBounds(30, 10, 350, 25);
107: xData.setPreferredSize(new Dimension(100, 50));
108:
109: yData = new JComboBox(columns);
110: yData.setEditable(true);
111: yData.setBounds(30, 60, 350, 25);
112: yData.setPreferredSize(new Dimension(100, 50));
113:
114: oK = new JButton("Ok");
115: oK.setBounds(280, 150, 50, 25);
116: oK.setPreferredSize(new Dimension(50, 25));
117: oK.addMouseListener(this );
118:
119: cancel = new JButton("Cancel");
120: cancel.setBounds(340, 150, 50, 25);
121: cancel.setPreferredSize(new Dimension(50, 40));
122: cancel.addMouseListener(this );
123:
124: tbBar = new JToggleButton(imgBar, false);
125: tbBar.setBounds(38, 100, 50, 40);
126: tbBar.setPreferredSize(new Dimension(50, 40));
127: tbBar.addMouseListener(this );
128:
129: tbPie = new JToggleButton(imgPie, false);
130: tbPie.setBounds(98, 100, 50, 40);
131: tbPie.setPreferredSize(new Dimension(50, 40));
132: tbPie.addMouseListener(this );
133:
134: tbBar3d = new JToggleButton(imgBar3d, true);
135: tbBar3d.setBounds(158, 100, 50, 40);
136: tbBar3d.setPreferredSize(new Dimension(50, 40));
137: tbBar3d.setBorder(BorderFactory.createLoweredBevelBorder());
138: tbBar3d.addMouseListener(this );
139:
140: tbPie3d = new JToggleButton(imgPie3d, false);
141: tbPie3d.setBounds(218, 100, 50, 40);
142: tbPie3d.setPreferredSize(new Dimension(50, 40));
143: tbPie3d.addMouseListener(this );
144:
145: tbXY = new JToggleButton(imgXYLine, false);
146: tbXY.setBounds(278, 100, 50, 40);
147: tbXY.setPreferredSize(new Dimension(50, 40));
148: tbXY.addMouseListener(this );
149:
150: selectXYData.getContentPane().setLayout(null);
151: selectXYData.getContentPane().add(tbBar);
152: selectXYData.getContentPane().add(tbBar3d);
153: selectXYData.getContentPane().add(tbPie);
154: selectXYData.getContentPane().add(tbPie3d);
155: selectXYData.getContentPane().add(tbXY);
156: selectXYData.getContentPane().add(x);
157: selectXYData.getContentPane().add(y);
158: selectXYData.getContentPane().add(oK);
159: selectXYData.getContentPane().add(cancel);
160: selectXYData.getContentPane().add(xData);
161: selectXYData.getContentPane().add(yData);
162: selectXYData.setVisible(true);
163: }
164:
165: public void mouseClicked(MouseEvent e) {
166: Object button = e.getSource();
167:
168: if (button == oK) {
169: this .createGraphics();
170:
171: } else if (button == tbBar) {
172: chartType = Chart.BAR;
173: tbBar.setBorder(BorderFactory.createLoweredBevelBorder());
174: tbBar3d.setSelected(false);
175: tbBar3d.setBorder(BorderFactory.createEtchedBorder());
176: tbPie.setSelected(false);
177: tbPie.setBorder(BorderFactory.createEtchedBorder());
178: tbPie3d.setSelected(false);
179: tbPie3d.setBorder(BorderFactory.createEtchedBorder());
180: tbXY.setSelected(false);
181: tbXY.setBorder(BorderFactory.createEtchedBorder());
182: } else if (button == tbBar3d) {
183: chartType = Chart.BAR3D;
184: tbBar3d.setBorder(BorderFactory.createLoweredBevelBorder());
185: tbBar.setSelected(false);
186: tbBar.setBorder(BorderFactory.createEtchedBorder());
187: tbPie.setSelected(false);
188: tbPie.setBorder(BorderFactory.createEtchedBorder());
189: tbPie3d.setSelected(false);
190: tbPie3d.setBorder(BorderFactory.createEtchedBorder());
191: tbXY.setSelected(false);
192: tbXY.setBorder(BorderFactory.createEtchedBorder());
193: } else if (button == tbPie) {
194: chartType = Chart.PIE;
195: tbPie.setBorder(BorderFactory.createLoweredBevelBorder());
196: tbPie3d.setSelected(false);
197: tbPie3d.setBorder(BorderFactory.createEtchedBorder());
198: tbBar.setSelected(false);
199: tbBar.setBorder(BorderFactory.createEtchedBorder());
200: tbBar3d.setSelected(false);
201: tbBar3d.setBorder(BorderFactory.createEtchedBorder());
202: tbXY.setSelected(false);
203: tbXY.setBorder(BorderFactory.createEtchedBorder());
204: } else if (button == tbPie3d) {
205: chartType = Chart.PIE3D;
206: tbPie3d.setBorder(BorderFactory.createLoweredBevelBorder());
207: tbPie.setSelected(false);
208: tbPie.setBorder(BorderFactory.createEtchedBorder());
209: tbBar.setSelected(false);
210: tbBar.setBorder(BorderFactory.createEtchedBorder());
211: tbBar3d.setSelected(false);
212: tbBar3d.setBorder(BorderFactory.createEtchedBorder());
213: tbXY.setSelected(false);
214: tbXY.setBorder(BorderFactory.createEtchedBorder());
215: } else if (button == tbXY) {
216: chartType = Chart.XYLINE;
217: tbXY.setBorder(BorderFactory.createLoweredBevelBorder());
218: tbBar.setSelected(false);
219: tbBar.setBorder(BorderFactory.createEtchedBorder());
220: tbBar3d.setSelected(false);
221: tbBar3d.setBorder(BorderFactory.createEtchedBorder());
222: tbPie.setSelected(false);
223: tbPie.setBorder(BorderFactory.createEtchedBorder());
224: tbPie3d.setSelected(false);
225: tbPie3d.setBorder(BorderFactory.createEtchedBorder());
226: } else {
227: selectXYData.dispose();
228: }
229: }
230:
231: public void mouseEntered(MouseEvent e) {
232: }
233:
234: public void mouseExited(MouseEvent e) {
235: }
236:
237: public void mousePressed(MouseEvent e) {
238: }
239:
240: public void mouseReleased(MouseEvent e) {
241: }
242:
243: public void createGraphics() {
244: try {
245: Connection conn = Connessione.getConnessione();
246: if (conn != null) {
247: DataPanel dtpGraph = new DataPanel(false);
248:
249: String yColumn = ((String) yData.getSelectedItem())
250: .toUpperCase();
251: String xColumn = (String) xData.getSelectedItem();
252:
253: // int indexSum = yColumn.indexOf("SUM(");
254: // int indexCount = yColumn.indexOf("COUNT(");
255:
256: String sql = "";
257:
258: // if (indexSum!=-1 || indexCount!=-1)
259: // {
260: JTextField desc = new JTextField(xColumn);
261: dtpGraph.addGroup();
262: ((JPanel) dtpGraph.getHeaderGroupPanel(1)).add(desc);
263:
264: JTextField com = new JTextField(yColumn);
265: com.setName("Computed");
266: ((JPanel) dtpGraph.getTrailerGroupPanel(1)).add(com);
267:
268: sql = this .dataPanel.getSql().toUpperCase();
269:
270: int index = sql.indexOf("ORDER BY");
271:
272: int lastindex = index;
273: String searchStr = sql;
274:
275: while (lastindex != -1) {
276: lastindex = sql.indexOf("ORDER BY", lastindex + 8);
277:
278: if (lastindex != -1) {
279: index = lastindex;
280: }
281: }
282:
283: if (sql.indexOf("|", index) != -1) {
284: index = -1;
285: }
286:
287: if (index != -1) {
288: sql = sql.substring(0, index) + " ORDER BY "
289: + xColumn;
290: } else {
291: sql = sql + " ORDER BY " + xColumn;
292: }
293: // }else
294: // {
295: // JTextField field1 = new JTextField(xColumn);
296: // ((JPanel)dtpGraph.getDetailPanel()).add(field1);
297: // JTextField field2 = new JTextField(yColumn);
298: // ((JPanel)dtpGraph.getDetailPanel()).add(field2);
299: // sql = this.dataPanel.getSql().toUpperCase();
300: // int index = sql.indexOf("ORDER BY");
301: //
302: // if (index != -1)
303: // {
304: // sql = sql.substring(0,index) + " ORDER BY " + xColumn;
305: // }else
306: // {
307: // sql = sql + " ORDER BY " + xColumn;
308: // }
309: // }
310:
311: dtpGraph.setSql(sql);
312:
313: DataEngineGraph pml = new DataEngineGraph(conn,
314: dtpGraph);
315:
316: if (closeOnDispose) {
317: pml.setCloseSystem(true);
318: }
319:
320: pml.setChartType(chartType);
321: Thread th = new Thread(pml);
322: th.start();
323: }
324:
325: } catch (SQLException sqlEx) {
326: System.out.println(sqlEx.getMessage());
327: }
328: }
329: }
|