001: package net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs;
002:
003: /*
004: * Copyright (C) 2002 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.sql.PreparedStatement;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026:
027: import javax.swing.JScrollPane;
028: import javax.swing.JTextArea;
029:
030: import net.sourceforge.squirrel_sql.client.session.ISession;
031: import net.sourceforge.squirrel_sql.fw.util.StringManager;
032: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
033: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
034: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
035:
036: public abstract class BaseSourceTab extends BaseObjectTab {
037: /** Internationalized strings for this class. */
038: private static final StringManager s_stringMgr = StringManagerFactory
039: .getStringManager(BaseSourceTab.class);
040:
041: /** Logger for this class. */
042: private final static ILogger s_log = LoggerController
043: .createLogger(BaseSourceTab.class);
044:
045: /** Hint to display for tab. */
046: private final String _hint;
047:
048: /** Title of the tab */
049: private String _title;
050:
051: /** Component to display in tab. */
052: private BaseSourcePanel _comp;
053:
054: /** Scrolling pane for <TT>_comp. */
055: private JScrollPane _scroller;
056:
057: public BaseSourceTab(String hint) {
058: this (null, hint);
059: }
060:
061: public BaseSourceTab(String title, String hint) {
062: super ();
063: if (title != null) {
064: _title = title;
065: } else {
066: //i18n[BaseSourceTab.title=Source]
067: _title = s_stringMgr.getString("BaseSourceTab.title");
068: }
069:
070: _hint = hint != null ? hint : _title;
071: }
072:
073: /**
074: * Return the title for the tab.
075: *
076: * @return The title for the tab.
077: */
078: public String getTitle() {
079: return _title;
080: }
081:
082: /**
083: * Return the hint for the tab.
084: *
085: * @return The hint for the tab.
086: */
087: public String getHint() {
088: return _hint;
089: }
090:
091: public void clear() {
092: }
093:
094: public Component getComponent() {
095: if (_scroller == null) {
096: if (_comp == null) {
097: _comp = new DefaultSourcePanel();
098: }
099: _scroller = new JScrollPane(_comp);
100: LineNumber lineNumber = new LineNumber(_comp);
101: _scroller.setRowHeaderView(lineNumber);
102: _scroller.getVerticalScrollBar().setUnitIncrement(10);
103: }
104: return _scroller;
105: }
106:
107: /**
108: * Subclasses can use this to override the default behavior provided by the
109: * DefaultSourcePanel, with a subclass of BaseSourcePanel.
110: *
111: * @param panel
112: */
113: public void setSourcePanel(BaseSourcePanel panel) {
114: _comp = panel;
115: }
116:
117: protected void refreshComponent() {
118: ISession session = getSession();
119: if (session == null) {
120: throw new IllegalStateException("Null ISession");
121: }
122: try {
123: PreparedStatement pstmt = createStatement();
124: try {
125: _comp.load(getSession(), pstmt);
126: } finally {
127: try {
128: pstmt.close();
129: } catch (SQLException ex) {
130: s_log.error(ex);
131: }
132: }
133: } catch (SQLException ex) {
134: s_log.error(ex);
135: session.showErrorMessage(ex);
136: }
137: }
138:
139: /**
140: * Sub-classes should override this method to return a PreparedStatement
141: * which will yield the source code of the object returned by
142: * getDatabaseObjectInfo.
143: *
144: * @return a PreparedStatement already with bound variables, ready to be
145: * executed
146: * @throws SQLException
147: * if any error occurs.
148: */
149: protected abstract PreparedStatement createStatement()
150: throws SQLException;
151:
152: private final class DefaultSourcePanel extends BaseSourcePanel {
153: private static final long serialVersionUID = 1L;
154:
155: private JTextArea _ta;
156:
157: DefaultSourcePanel() {
158: super (new BorderLayout());
159: createUserInterface();
160: }
161:
162: public void load(ISession session, PreparedStatement stmt) {
163: _ta.setText("");
164: try {
165: ResultSet rs = stmt.executeQuery();
166: StringBuffer buf = new StringBuffer(4096);
167: while (rs.next()) {
168: buf.append(rs.getString(1));
169: }
170: _ta.setText(buf.toString());
171: _ta.setCaretPosition(0);
172: } catch (SQLException ex) {
173: session.showErrorMessage(ex);
174: }
175:
176: }
177:
178: private void createUserInterface() {
179: _ta = new JTextArea();
180: _ta.setEditable(false);
181: add(_ta, BorderLayout.CENTER);
182: }
183: }
184: }
|