001: package org.jsqltool.gui;
002:
003: import java.util.*;
004: import javax.swing.*;
005: import javax.swing.event.*;
006: import java.awt.event.*;
007: import java.awt.Component;
008: import java.sql.*;
009: import java.beans.*;
010: import org.jsqltool.conn.*;
011: import org.jsqltool.utils.Options;
012: import org.jsqltool.utils.ImageLoader;
013:
014: /**
015: * <p>Title: JSqlTool Project</p>
016: * <p>Description: Class used to collect internal frames and to listen internal frame events.
017: * </p>
018: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
019: *
020: * <p> This file is part of JSqlTool project.
021: * This library is free software; you can redistribute it and/or
022: * modify it under the terms of the (LGPL) Lesser General Public
023: * License as published by the Free Software Foundation;
024: *
025: * GNU LESSER GENERAL PUBLIC LICENSE
026: * Version 2.1, February 1999
027: *
028: * This library is distributed in the hope that it will be useful,
029: * but WITHOUT ANY WARRANTY; without even the implied warranty of
030: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
031: * Library General Public License for more details.
032: *
033: * You should have received a copy of the GNU Library General Public
034: * License along with this library; if not, write to the Free
035: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
036: *
037: * The author may be contacted at:
038: * maurocarniel@tin.it</p>
039: *
040: * @author Mauro Carniel
041: * @version 1.0
042: */
043: public class ConnWindows implements InternalFrameListener {
044:
045: /** DbConnectionUtil objects list */
046: private ArrayList conns = new ArrayList();
047:
048: /** DbConnWindow objects list */
049: private ArrayList wins = new ArrayList();
050:
051: /** MDI frame desktop */
052: private JDesktopPane desktop = null;
053:
054: /** MDI frame */
055: private MainFrame frame = null;
056:
057: /** internal frame that currently has focus */
058: private JInternalFrame currentFrame = null;
059:
060: /** database connection used by the internal frame that currently has focus */
061: private DbConnectionUtil currentDbConnUtil = null;
062:
063: /** status bar viewed in the bottom area of the MDI frame */
064: private JPanel statusBar = null;
065:
066: public ConnWindows(MainFrame frame, JDesktopPane desktop,
067: JPanel statusBar) {
068: this .frame = frame;
069: this .desktop = desktop;
070: this .statusBar = statusBar;
071: }
072:
073: /**
074: * Method used to register an internal frame.
075: * @param window internal frame
076: */
077: public void addWindow(final DbConnWindow window) {
078: // add a listener to the internal frame...
079: ((JInternalFrame) window).addInternalFrameListener(this );
080:
081: // make visibile the internal frame...
082: ((JInternalFrame) window).setSize(desktop.getSize().width,
083: desktop.getSize().height - 4);
084: ((JInternalFrame) window).setFrameIcon(ImageLoader
085: .getInstance().getIcon("logo.gif"));
086: desktop.add((JInternalFrame) window);
087: String title = ((JInternalFrame) window).getTitle();
088: int num = 1;
089: for (int i = 0; i < statusBar.getComponentCount(); i++)
090: if (((JToggleButton) statusBar.getComponent(i)).getName()
091: .equals(title + (num == 1 ? "" : " [" + num + "]")))
092: num++;
093: if (num > 1)
094: title += " [" + num + "]";
095: ((JInternalFrame) window).setTitle(title);
096:
097: // add window name to the status bar...
098: final JToggleButton button = new JToggleButton(title);
099: button.setName(title);
100: toggleButton(button);
101: button.addActionListener(new ActionListener() {
102: public void actionPerformed(ActionEvent e) {
103: try {
104: toggleButton(button);
105: ((JInternalFrame) window).toFront();
106: ((JInternalFrame) window).setSelected(true);
107: } catch (PropertyVetoException ex) {
108: }
109: }
110: });
111: statusBar.add(button);
112:
113: // add window name to the "Window" menu...
114: JMenuItem menu = new JMenuItem(title);
115: frame.winMenu.add(menu);
116: menu.setName(title);
117: menu.addActionListener(new ActionListener() {
118: public void actionPerformed(ActionEvent e) {
119: try {
120: toggleButton(button);
121: ((JInternalFrame) window).toFront();
122: ((JInternalFrame) window).setSelected(true);
123: } catch (PropertyVetoException ex) {
124: }
125: }
126: });
127:
128: ((JInternalFrame) window).setVisible(true);
129: try {
130: ((JInternalFrame) window).setSelected(true);
131: } catch (PropertyVetoException ex) {
132: }
133:
134: // create the link between the internal frame and the connection...
135: DbConnectionUtil c = window.getDbConnectionUtil();
136: for (int i = 0; i < conns.size(); i++)
137: if (conns.get(i).equals(c)) {
138: ((ArrayList) wins.get(i)).add(window);
139: return;
140: }
141: conns.add(c);
142: ArrayList winList = new ArrayList();
143: winList.add(window);
144: wins.add(winList);
145: }
146:
147: private void toggleButton(JToggleButton button) {
148: for (int i = 0; i < statusBar.getComponentCount(); i++)
149: ((JToggleButton) statusBar.getComponent(i))
150: .setSelected(false);
151: button.setSelected(true);
152: }
153:
154: public void removeWindow(DbConnWindow window) {
155: // remove the current internal frame from the "Window" menu...
156: Component[] menus = (Component[]) frame.winMenu
157: .getMenuComponents();
158: for (int i = 0; i < menus.length; i++)
159: if (menus[i].getName() != null
160: && menus[i].getName().equals(
161: ((JInternalFrame) window).getTitle()))
162: frame.winMenu.remove(i);
163:
164: // remove the window name from the status bar...
165: Component[] buttons = (Component[]) statusBar.getComponents();
166: for (int i = 0; i < buttons.length; i++)
167: if (buttons[i].getName() != null
168: && buttons[i].getName().equals(
169: ((JInternalFrame) window).getTitle()))
170: statusBar.remove(i);
171: statusBar.revalidate();
172: statusBar.repaint();
173:
174: DbConnectionUtil c = window.getDbConnectionUtil();
175: for (int i = 0; i < conns.size(); i++)
176: if (conns.get(i).equals(c)) {
177: ArrayList winList = ((ArrayList) wins.get(i));
178: winList.remove(window);
179: ((JInternalFrame) window).setVisible(false);
180: desktop.remove((JInternalFrame) window);
181: if (winList.size() == 0) {
182: try {
183: c.getConn().close();
184: } catch (SQLException ex) {
185: ex.printStackTrace();
186: JOptionPane
187: .showMessageDialog(
188: frame,
189: Options
190: .getInstance()
191: .getResource(
192: "error on closing connection."),
193: Options.getInstance()
194: .getResource("error"),
195: JOptionPane.ERROR_MESSAGE);
196: }
197: wins.remove(i);
198: conns.remove(i);
199: }
200: return;
201: }
202: }
203:
204: public List getConnectionsList() {
205: return conns;
206: }
207:
208: public void closeConnectionAndWindows(DbConnectionUtil dbConnUtil) {
209: for (int i = 0; i < conns.size(); i++)
210: if (conns.get(i).equals(dbConnUtil)) {
211: try {
212: dbConnUtil.getConn().close();
213: } catch (SQLException ex) {
214: ex.printStackTrace();
215: JOptionPane.showMessageDialog(frame, Options
216: .getInstance().getResource(
217: "error on closing connection."),
218: Options.getInstance().getResource("error"),
219: JOptionPane.ERROR_MESSAGE);
220: }
221: conns.remove(i);
222: ArrayList winList = (ArrayList) wins.get(i);
223: for (int j = 0; j < winList.size(); j++) {
224: Component[] menus = (Component[]) frame.winMenu
225: .getMenuComponents();
226: for (int k = 0; k < menus.length; k++)
227: if (menus[k].getName() != null
228: && menus[k].getName().equals(
229: ((JInternalFrame) winList
230: .get(j)).getTitle()))
231: frame.winMenu.remove(k);
232:
233: // remove window name from status bar...
234: Component[] buttons = (Component[]) statusBar
235: .getComponents();
236: for (int k = 0; k < buttons.length; k++)
237: if (buttons[k].getName() != null
238: && buttons[k].getName().equals(
239: ((JInternalFrame) winList
240: .get(j)).getTitle()))
241: statusBar.remove(k);
242: statusBar.revalidate();
243: statusBar.repaint();
244:
245: ((JInternalFrame) winList.get(j)).setVisible(false);
246: desktop.remove((JInternalFrame) winList.get(j));
247: }
248: wins.remove(i);
249: return;
250: }
251: }
252:
253: public void closeAll() {
254: while (conns.size() > 0) {
255: closeConnectionAndWindows((DbConnectionUtil) conns.get(0));
256: }
257: currentDbConnUtil = null;
258: currentFrame = null;
259: }
260:
261: /**
262: * Invoked when an internal frame is activated.
263: */
264: public void internalFrameActivated(InternalFrameEvent e) {
265: currentDbConnUtil = ((DbConnWindow) e.getInternalFrame())
266: .getDbConnectionUtil();
267: currentFrame = e.getInternalFrame();
268:
269: Component[] buttons = (Component[]) statusBar.getComponents();
270: for (int i = 0; i < buttons.length; i++)
271: if (buttons[i].getName() != null
272: && buttons[i].getName().equals(
273: (e.getInternalFrame()).getTitle()))
274: toggleButton((JToggleButton) buttons[i]);
275: }
276:
277: /**
278: * Invoked when an internal frame has been closed.
279: */
280: public void internalFrameClosed(InternalFrameEvent e) {
281: removeWindow((DbConnWindow) e.getInternalFrame());
282: // currentDbConnUtil = null;
283: }
284:
285: /**
286: * Invoked when an internal frame is in the process of being closed.
287: */
288: public void internalFrameClosing(InternalFrameEvent e) {
289: currentDbConnUtil = ((DbConnWindow) e.getInternalFrame())
290: .getDbConnectionUtil();
291: currentFrame = e.getInternalFrame();
292: if (!currentDbConnUtil.getDbConnection().isAutoCommit()) {
293: try {
294: if (JOptionPane.showConfirmDialog(frame, Options
295: .getInstance().getResource(
296: "commit before closing window?"),
297: Options.getInstance().getResource(
298: "commit transaction"),
299: JOptionPane.YES_NO_OPTION,
300: JOptionPane.QUESTION_MESSAGE) == 0) {
301: if (currentDbConnUtil.getConn() != null)
302: currentDbConnUtil.getConn().commit();
303: }
304: } catch (SQLException ex) {
305: ex.printStackTrace();
306: JOptionPane.showMessageDialog(frame, Options
307: .getInstance().getResource(
308: "error on commit connection."), Options
309: .getInstance().getResource("error"),
310: JOptionPane.ERROR_MESSAGE);
311: }
312: }
313: }
314:
315: /**
316: * Invoked when an internal frame is de-activated.
317: */
318: public void internalFrameDeactivated(InternalFrameEvent e) {
319: currentDbConnUtil = null;
320: currentFrame = null;
321: }
322:
323: /**
324: * Invoked when an internal frame is de-iconified.
325: */
326: public void internalFrameDeiconified(InternalFrameEvent e) {
327:
328: }
329:
330: /**
331: * Invoked when an internal frame is iconified.
332: */
333: public void internalFrameIconified(InternalFrameEvent e) {
334:
335: }
336:
337: public void internalFrameOpened(InternalFrameEvent e) {
338:
339: }
340:
341: public DbConnectionUtil getCurrentDbConnUtil() {
342: return currentDbConnUtil;
343: }
344:
345: public JInternalFrame getCurrentFrame() {
346: return currentFrame;
347: }
348:
349: }
|