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;
023:
024: import java.awt.Cursor;
025: import java.awt.Dimension;
026: import java.awt.event.WindowEvent;
027: import javax.swing.*;
028:
029: public class JPreviewFrame extends javax.swing.JFrame implements
030: java.awt.event.ActionListener, java.awt.event.WindowListener,
031: javax.swing.event.ChangeListener {
032: private java.awt.Toolkit objToolkit;
033: private Dimension dScSize;
034: private int page;
035:
036: private JButton next;
037: private JButton prev;
038: private JButton print;
039: private JButton export;
040: private JSlider slider;
041:
042: private JPreviewPanel jPrewPanel;
043:
044: private boolean bCloseSystem = false;
045:
046: public JPreviewFrame(JPreviewPanel jPrewPanel) {
047: super ("Preview");
048:
049: objToolkit = java.awt.Toolkit.getDefaultToolkit();
050: dScSize = objToolkit.getScreenSize();
051:
052: this .addWindowListener(this );
053: this .jPrewPanel = jPrewPanel;
054: float fpage = (float) jPrewPanel.getRowsCount()
055: / (float) jPrewPanel.getRowsForPage();
056: page = Math.round(fpage);
057:
058: this .createButton();
059: }
060:
061: private void createButton() {
062: JPanel buttonPanel = new JPanel();
063: buttonPanel.setLayout(new javax.swing.BoxLayout(buttonPanel,
064: javax.swing.BoxLayout.X_AXIS));
065: ImageIcon right = new ImageIcon(JPreviewFrame.class
066: .getResource("img/next.png"));
067: next = new JButton(right);
068: next.setPreferredSize(new Dimension(80, 40));
069: next.setLocation(0, 0);
070: next.setToolTipText("Next page");
071: next.addActionListener(this );
072:
073: if (jPrewPanel.getRowsCount() > 0) {
074: slider = new JSlider(JSlider.HORIZONTAL, 1, jPrewPanel
075: .getRowsCount(), jPrewPanel.getCursorPosition() + 1);
076: slider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
077: slider.setPaintTicks(true);
078: slider.setMajorTickSpacing(jPrewPanel.getRowsCount() / 2);
079: slider.setMinorTickSpacing(jPrewPanel.getRowsCount() / 10);
080: slider.setPaintLabels(true);
081: slider.setSnapToTicks(false);
082: slider.setPreferredSize(new Dimension(1100, 40));
083:
084: if (page > 1) {
085: if (jPrewPanel.getRowsCount() > 10) {
086: slider.setLabelTable(slider
087: .createStandardLabels(jPrewPanel
088: .getRowsCount() / 10));
089: } else {
090: slider
091: .setLabelTable(slider
092: .createStandardLabels(1));
093: }
094:
095: slider.setLabelTable(slider.getLabelTable());
096: slider.addChangeListener(this );
097: } else {
098: slider.setEnabled(false);
099: }
100: } else {
101: slider = new JSlider(JSlider.HORIZONTAL, 0, 1, 0);
102: slider.setEnabled(false);
103: }
104:
105: ImageIcon left = new ImageIcon(JPreviewFrame.class
106: .getResource("img/prev.png"));
107: prev = new JButton(left);
108: prev.setEnabled(false);
109: prev.setPreferredSize(new Dimension(80, 40));
110: prev.setLocation(101, 0);
111: prev.setToolTipText("Previous page");
112: prev.addActionListener(this );
113:
114: ImageIcon printers = new ImageIcon(JPreviewFrame.class
115: .getResource("img/printers.png"));
116: print = new JButton(printers);
117: print.setEnabled(true);
118: print.setPreferredSize(new Dimension(100, 40));
119: print.setLocation(202, 0);
120: print.setToolTipText("Print");
121: print.addActionListener(this );
122:
123: if (jPrewPanel.getRowsCount() == jPrewPanel.getCursorPosition() + 1
124: || jPrewPanel.getRowsCount() == 0) {
125: next.setEnabled(false);
126: }
127:
128: JPanel sliderPanel = new JPanel();
129: sliderPanel.setLayout(new javax.swing.BoxLayout(sliderPanel,
130: javax.swing.BoxLayout.X_AXIS));
131: sliderPanel.setPreferredSize(new Dimension(1000, 50));
132: sliderPanel.setBorder(javax.swing.BorderFactory
133: .createLoweredBevelBorder());
134: sliderPanel.add(slider);
135:
136: buttonPanel.add(sliderPanel);
137:
138: ImageIcon exp = new ImageIcon(JPreviewFrame.class
139: .getResource("img/export.png"));
140: export = new JButton(exp);
141: export.setEnabled(true);
142: export.setPreferredSize(new Dimension(100, 40));
143: export.setLocation(202, 0);
144: export.setToolTipText("Export");
145: export.addActionListener(this );
146:
147: JPanel button = new JPanel();
148: button.setLayout(new java.awt.GridLayout(1, 4));
149: button.setPreferredSize(new Dimension(260, 50));
150: button.add(prev);
151: button.add(next);
152: button.add(print);
153: button.add(export);
154:
155: buttonPanel.add(button);
156: this .getContentPane().add(buttonPanel,
157: java.awt.BorderLayout.NORTH);
158: }
159:
160: public void setCloseSystemOnCloseWindow(boolean close) {
161: this .bCloseSystem = close;
162: }
163:
164: public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
165: if (actionEvent.getSource().equals(next)) {
166: try {
167: this .setCursor(new Cursor(Cursor.WAIT_CURSOR));
168:
169: jPrewPanel.nextPage();
170: jPrewPanel.repaint();
171: prev.setEnabled(true);
172:
173: if (this .slider.getMaximum() == jPrewPanel
174: .getCursorPosition() + 1) {
175: next.setEnabled(false);
176: this .slider.setEnabled(false);
177: }
178:
179: this .slider.setValue(jPrewPanel.getCursorPosition());
180: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
181:
182: } catch (java.sql.SQLException sqle) {
183: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
184: javax.swing.JOptionPane.showMessageDialog(this , sqle
185: .getMessage());
186: }
187: }
188:
189: if (actionEvent.getSource().equals(prev)) {
190: try {
191: this .setCursor(new Cursor(Cursor.WAIT_CURSOR));
192:
193: jPrewPanel.prevPage();
194: jPrewPanel.repaint();
195:
196: next.setEnabled(true);
197: this .slider.setEnabled(true);
198:
199: this .slider.setValue(jPrewPanel.getCursorPosition());
200: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
201:
202: } catch (java.sql.SQLException sqle) {
203: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
204: javax.swing.JOptionPane.showMessageDialog(this , sqle
205: .getMessage());
206: }
207: }
208:
209: if (actionEvent.getSource().equals(export)) {
210: ExportThread windowBar = new ExportThread(this , jPrewPanel,
211: page);
212: new Thread(windowBar).start();
213: }
214:
215: if (actionEvent.getSource().equals(print)) {
216: PrintThread printJPrew = new PrintThread(this , jPrewPanel,
217: page);
218: new Thread(printJPrew).start();
219: }
220: }
221:
222: public void windowOpened(WindowEvent e) {
223: }
224:
225: public void windowClosing(WindowEvent e) {
226: try {
227: java.sql.ResultSet rsDati = jPrewPanel.getResultSet();
228:
229: if (rsDati != null) {
230: java.sql.Statement stm = rsDati.getStatement();
231: java.sql.Connection connection = stm.getConnection();
232:
233: rsDati.close();
234: stm.close();
235:
236: boolean bMltStm = ConfigurationProperties.properties()
237: .getUseMultiTransaction(false);
238:
239: if (!bMltStm) {
240: connection.close();
241: }
242: } else {
243: if (jPrewPanel != null) {
244: if (jPrewPanel.getConnection() != null) {
245: boolean bMltStm = ConfigurationProperties
246: .properties().getUseMultiTransaction(
247: false);
248:
249: if (!bMltStm) {
250: jPrewPanel.getConnection().close();
251: }
252: }
253: }
254: }
255:
256: ConfigurationProperties.properties().setWndHeight(
257: this .getHeight());
258: ConfigurationProperties.properties().setWndWidth(
259: this .getWidth());
260: ConfigurationProperties.properties().setWndX(this .getX());
261: ConfigurationProperties.properties().setWndY(this .getY());
262:
263: if (bCloseSystem) {
264: System.exit(0);
265: }
266: } catch (java.sql.SQLException s) {
267: System.out.println("Chiusura Preview: " + s.getMessage());
268: }
269: }
270:
271: public void windowClosed(WindowEvent e) {
272: }
273:
274: public void windowIconified(WindowEvent e) {
275: }
276:
277: public void windowDeiconified(WindowEvent e) {
278: }
279:
280: public void windowActivated(WindowEvent e) {
281: }
282:
283: public void windowDeactivated(WindowEvent e) {
284: }
285:
286: public void stateChanged(javax.swing.event.ChangeEvent e) {
287: try {
288: JSlider s1 = (JSlider) e.getSource();
289: int value = s1.getValue();
290: int cursor = jPrewPanel.getCursorPosition();
291:
292: jPrewPanel.setCursorPosition(value);
293:
294: if (cursor != value) {
295: jPrewPanel.nextPage();
296: }
297:
298: s1.setValue(jPrewPanel.getCursorPosition());
299:
300: jPrewPanel.repaint();
301:
302: prev.setEnabled(true);
303:
304: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
305:
306: } catch (java.sql.SQLException sqle) {
307: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
308: javax.swing.JOptionPane.showMessageDialog(this, sqle
309: .getMessage());
310: }
311: }
312: }
|