001: /*
002: * TriggerDisplayPanel.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.dbobjects;
013:
014: import java.awt.BorderLayout;
015:
016: import javax.swing.JPanel;
017: import javax.swing.JSplitPane;
018: import javax.swing.ListSelectionModel;
019: import javax.swing.border.EtchedBorder;
020: import javax.swing.event.ListSelectionEvent;
021: import javax.swing.event.ListSelectionListener;
022:
023: import workbench.db.DbMetadata;
024: import workbench.db.TableIdentifier;
025: import workbench.db.WbConnection;
026: import workbench.gui.WbSwingUtilities;
027: import workbench.gui.components.DataStoreTableModel;
028: import workbench.gui.components.WbScrollPane;
029: import workbench.gui.components.WbSplitPane;
030: import workbench.gui.components.WbTable;
031: import workbench.gui.sql.EditorPanel;
032: import workbench.interfaces.Resettable;
033: import workbench.resource.Settings;
034: import workbench.storage.DataStore;
035:
036: /**
037: *
038: * @author support@sql-workbench.net
039: */
040: public class TriggerDisplayPanel extends JPanel implements
041: ListSelectionListener, Resettable {
042: private WbConnection dbConnection;
043: private WbTable triggers;
044: private EditorPanel source;
045: private WbSplitPane splitPane;
046: private String triggerSchema;
047: private String triggerCatalog;
048:
049: public TriggerDisplayPanel() {
050: this .triggers = new WbTable();
051: WbScrollPane scroll = new WbScrollPane(this .triggers);
052: scroll.setBorder(new EtchedBorder());
053: //scroll.setBorder(WbSwingUtilities.EMPTY_BORDER);
054:
055: this .source = EditorPanel.createSqlEditor();
056: this .source.setEditable(false);
057: this .source.setBorder(WbSwingUtilities.EMPTY_BORDER);
058: this .setBorder(WbSwingUtilities.EMPTY_BORDER);
059:
060: this .setLayout(new BorderLayout());
061: this .splitPane = new WbSplitPane(JSplitPane.VERTICAL_SPLIT,
062: scroll, this .source);
063: this .add(splitPane, BorderLayout.CENTER);
064: this .triggers.getSelectionModel()
065: .addListSelectionListener(this );
066: this .triggers.getSelectionModel().setSelectionMode(
067: ListSelectionModel.SINGLE_SELECTION);
068: }
069:
070: public void saveSettings() {
071: Settings.getInstance().setProperty(
072: this .getClass().getName() + ".divider",
073: this .splitPane.getDividerLocation());
074: }
075:
076: public void restoreSettings() {
077: int loc = Settings.getInstance().getIntProperty(
078: this .getClass().getName() + ".divider", 200);
079: this .splitPane.setDividerLocation(loc);
080: }
081:
082: public void setConnection(WbConnection aConnection) {
083: this .dbConnection = aConnection;
084: this .source.setDatabaseConnection(aConnection);
085: this .reset();
086: }
087:
088: public void reset() {
089: this .triggers.reset();
090: this .source.setText("");
091: this .triggerSchema = null;
092: this .triggerCatalog = null;
093: }
094:
095: public void readTriggers(TableIdentifier table) {
096: try {
097: if (table == null)
098: return;
099: DbMetadata metaData = this .dbConnection.getMetadata();
100: DataStore trg = metaData.getTableTriggers(table);
101: DataStoreTableModel rs = new DataStoreTableModel(trg);
102: triggers.setModel(rs, true);
103: triggers.adjustOrOptimizeColumns();
104: this .triggerCatalog = table.getCatalog();
105: this .triggerSchema = table.getSchema();
106: if (triggers.getRowCount() > 0)
107: this .triggers.getSelectionModel().setSelectionInterval(
108: 0, 0);
109: else
110: this .source.setText("");
111: } catch (Exception e) {
112: this .reset();
113: }
114: }
115:
116: /**
117: * Called whenever the value of the selection changes.
118: * @param e the event that characterizes the change.
119: */
120: public void valueChanged(ListSelectionEvent e) {
121: if (e.getValueIsAdjusting())
122: return;
123: int row = this .triggers.getSelectedRow();
124: if (row < 0)
125: return;
126:
127: try {
128: DbMetadata metaData = this .dbConnection.getMetadata();
129: String triggerName = this .triggers.getValueAsString(row,
130: DbMetadata.COLUMN_IDX_TABLE_TRIGGERLIST_TRG_NAME);
131: String sql = metaData.getTriggerSource(this .triggerCatalog,
132: this .triggerSchema, triggerName);
133: this .source.setText(sql);
134: this .source.setCaretPosition(0);
135: } catch (Exception ex) {
136: ex.printStackTrace();
137: this .source.setText("");
138: }
139: }
140:
141: }
|