001: package org.jsqltool.gui.tablepanel;
002:
003: import java.awt.*;
004: import javax.swing.*;
005: import javax.swing.table.*;
006: import javax.swing.event.*;
007: import org.jsqltool.conn.*;
008: import java.awt.event.*;
009: import java.sql.*;
010: import java.io.*;
011: import java.util.ArrayList;
012: import org.jsqltool.model.*;
013: import org.jsqltool.gui.*;
014: import org.jsqltool.*;
015: import org.jsqltool.gui.tableplugins.*;
016: import org.jsqltool.utils.ImageLoader;
017: import org.jsqltool.utils.Options;
018:
019: /**
020: * <p>Title: JSqlTool Project</p>
021: * <p>Description: Panel used to show a table detail.
022: * </p>
023: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
024: *
025: * <p> This file is part of JSqlTool project.
026: * This library is free software; you can redistribute it and/or
027: * modify it under the terms of the (LGPL) Lesser General Public
028: * License as published by the Free Software Foundation;
029: *
030: * GNU LESSER GENERAL PUBLIC LICENSE
031: * Version 2.1, February 1999
032: *
033: * This library is distributed in the hope that it will be useful,
034: * but WITHOUT ANY WARRANTY; without even the implied warranty of
035: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
036: * Library General Public License for more details.
037: *
038: * You should have received a copy of the GNU Library General Public
039: * License along with this library; if not, write to the Free
040: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
041: *
042: * The author may be contacted at:
043: * maurocarniel@tin.it</p>
044: *
045: * @author Mauro Carniel
046: * @version 1.0
047: */
048: public class TableDetailPanel extends JPanel implements ChangeListener {
049: BorderLayout borderLayout1 = new BorderLayout();
050: JPanel mainPanel = new JPanel();
051: JTabbedPane tabbedPane = new JTabbedPane();
052: BorderLayout borderLayout2 = new BorderLayout();
053: JPanel buttonsPanel = new JPanel();
054: JButton refreshTablesButton = new JButton();
055: JButton refreshDetailButton = new JButton();
056: ImageIcon refreshTablesImage;
057: ImageIcon refreshDetailImage;
058: FlowLayout flowLayout1 = new FlowLayout();
059: private TableListFrame parentFrame = null;
060: private String tableName = null;
061: private DbConnectionUtil dbConnUtil = null;
062: private MainFrame parent = null;
063:
064: public TableDetailPanel() {
065: this (null, null, null);
066: }
067:
068: public TableDetailPanel(MainFrame parent,
069: DbConnectionUtil dbConnUtil, TableListFrame parentFrame) {
070: this .dbConnUtil = dbConnUtil;
071: this .parentFrame = parentFrame;
072: this .parent = parent;
073: try {
074: init();
075: jbInit();
076: } catch (Exception ex) {
077: ex.printStackTrace();
078: }
079: }
080:
081: void jbInit() throws Exception {
082: this .setLayout(borderLayout1);
083: mainPanel.setLayout(borderLayout2);
084: buttonsPanel.setLayout(flowLayout1);
085: flowLayout1.setAlignment(FlowLayout.LEFT);
086: flowLayout1.setHgap(0);
087: refreshTablesButton
088: .addActionListener(new TableDetailPanel_refreshTablesButton_actionAdapter(
089: this ));
090: refreshDetailButton
091: .addActionListener(new TableDetailPanel_refreshDetailButton_actionAdapter(
092: this ));
093: refreshTablesButton.setBorder(null);
094: refreshTablesButton.setMinimumSize(new Dimension(24, 24));
095: refreshTablesButton.setPreferredSize(new Dimension(24, 24));
096: refreshDetailButton.setBorder(null);
097: refreshDetailButton.setPreferredSize(new Dimension(24, 24));
098: this .add(mainPanel, BorderLayout.CENTER);
099: mainPanel.add(tabbedPane, BorderLayout.CENTER);
100: mainPanel.add(buttonsPanel, BorderLayout.NORTH);
101:
102: refreshTablesImage = ImageLoader.getInstance().getIcon(
103: "refreshtables.gif");
104: refreshDetailImage = ImageLoader.getInstance().getIcon(
105: "refresh.gif");
106: refreshTablesButton.setIcon(refreshTablesImage);
107: refreshTablesButton.setToolTipText(Options.getInstance()
108: .getResource("refresh all"));
109: refreshTablesButton.setMaximumSize(new Dimension(24, 24));
110: refreshDetailButton.setIcon(refreshDetailImage);
111: refreshDetailButton.setToolTipText(Options.getInstance()
112: .getResource("refresh detail"));
113: refreshDetailButton.setMaximumSize(new Dimension(24, 24));
114: buttonsPanel.add(refreshTablesButton, null);
115: buttonsPanel.add(refreshDetailButton, null);
116: }
117:
118: public void updateContent(final String tableName) {
119: this .tableName = tableName;
120: ProgressDialog.getInstance().startProgressNoClose();
121: Thread t = new Thread() {
122: public void run() {
123: try {
124: if (tableName != null
125: && !tableName
126: .equals(((TablePlugin) tabbedPane
127: .getSelectedComponent())
128: .getTableName()))
129: ((TablePlugin) tabbedPane
130: .getSelectedComponent())
131: .setTableName(tableName);
132: ((TablePlugin) tabbedPane.getSelectedComponent())
133: .updateContent();
134: // for (int i = 0; i < tabbedPane.getComponentCount(); i++) {
135: // if (i != tabbedPane.getSelectedIndex()) {
136: // ( (TablePlugin) tabbedPane.getComponent(i)).updateContent(tableName);
137: // }
138: // }
139: } catch (Throwable ex) {
140: ex.printStackTrace();
141: }
142: ProgressDialog.getInstance().forceStopProgress();
143: }
144: };
145: t.start();
146: }
147:
148: void refreshTablesButton_actionPerformed(ActionEvent e) {
149: new Thread() {
150: public void run() {
151: ProgressDialog.getInstance().startProgress();
152: try {
153: parentFrame.updateLists();
154: refreshDetailButton_actionPerformed(null);
155: } catch (Throwable ex) {
156: } finally {
157: ProgressDialog.getInstance().stopProgress();
158: }
159: }
160: }.start();
161:
162: }
163:
164: void refreshDetailButton_actionPerformed(ActionEvent e) {
165: updateContent(tableName);
166: }
167:
168: private void init() {
169: ArrayList sortPanels = getSortPanels();
170: TablePlugin panel = null;
171: for (int i = 0; i < sortPanels.size(); i++)
172: try {
173: panel = (TablePlugin) sortPanels.get(i);
174: tabbedPane.add((JPanel) panel, panel.getTabbedName());
175: tabbedPane.addChangeListener(this );
176: panel.initPanel(parent, dbConnUtil);
177: } catch (Exception ex1) {
178: ex1.printStackTrace();
179: }
180:
181: }
182:
183: /**
184: * Invoked when the target of the listener has changed its state.
185: * @param e a ChangeEvent object
186: */
187: public void stateChanged(ChangeEvent e) {
188: if (tableName != null
189: && !tableName.equals(((TablePlugin) tabbedPane
190: .getSelectedComponent()).getTableName())) {
191: System.out.println(((TablePlugin) tabbedPane
192: .getSelectedComponent()).getName());
193: ((TablePlugin) tabbedPane.getSelectedComponent())
194: .setTableName(tableName);
195: Thread t = new Thread() {
196: public void run() {
197: try {
198: ((TablePlugin) tabbedPane
199: .getSelectedComponent())
200: .updateContent();
201: } catch (Throwable ex) {
202: ex.printStackTrace();
203: }
204: ProgressDialog.getInstance().forceStopProgress();
205: }
206: };
207: t.start();
208: }
209: }
210:
211: public static ArrayList getSortPanels() {
212: String path = MainApp.class.getClassLoader().getResource(".")
213: .getPath().substring(1);
214: File dir = new File(path + "org/jsqltool/gui/tableplugins/");
215: String[] files = dir.list();
216: File file = null;
217: TablePlugin panel = null;
218: ArrayList panels = new ArrayList();
219: panels.add(new ColumnsTablePanel());
220: panels.add(new ConstraintsTablePanel());
221: panels.add(new DataTablePanel());
222: panels.add(new IndexesTablePanel());
223: panels.add(new ScriptTablePanel());
224: panels.add(new TriggersTablePanel());
225: if (files != null)
226: for (int i = 0; i < files.length; i++) {
227: file = new File(files[i]);
228: try {
229: if (file.getName().endsWith(".class")) {
230: panel = (TablePlugin) Class.forName(
231: "org.jsqltool.gui.tableplugins."
232: + file.getName().substring(
233: 0,
234: file.getName().indexOf(
235: ".")))
236: .newInstance();
237: if (!(panel instanceof JPanel))
238: continue;
239: if (!panels.contains(panel))
240: panels.add(panel);
241: }
242: } catch (Exception ex) {
243: } catch (Error er) {
244: }
245: }
246: ArrayList sortPanels = new ArrayList();
247: for (int i = 0; i < panels.size(); i++)
248: sortPanels.add(null);
249: for (int i = 0; i < panels.size(); i++) {
250: panel = (TablePlugin) panels.get(i);
251: sortPanels.set(panel.getTabbedPosition(), panel);
252: }
253: return sortPanels;
254: }
255:
256: }
257:
258: class TableDetailPanel_refreshTablesButton_actionAdapter implements
259: java.awt.event.ActionListener {
260: TableDetailPanel adaptee;
261:
262: TableDetailPanel_refreshTablesButton_actionAdapter(
263: TableDetailPanel adaptee) {
264: this .adaptee = adaptee;
265: }
266:
267: public void actionPerformed(ActionEvent e) {
268: adaptee.refreshTablesButton_actionPerformed(e);
269: }
270: }
271:
272: class TableDetailPanel_refreshDetailButton_actionAdapter implements
273: java.awt.event.ActionListener {
274: TableDetailPanel adaptee;
275:
276: TableDetailPanel_refreshDetailButton_actionAdapter(
277: TableDetailPanel adaptee) {
278: this .adaptee = adaptee;
279: }
280:
281: public void actionPerformed(ActionEvent e) {
282: adaptee.refreshDetailButton_actionPerformed(e);
283: }
284: }
|