001: package net.sourceforge.squirrel_sql.plugins.postgres.tab;
002:
003: /*
004: * Copyright (C) 2007 Rob Manning
005: * manningr@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.sql.PreparedStatement;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025:
026: import javax.swing.JTextArea;
027:
028: import net.sourceforge.squirrel_sql.client.session.ISession;
029: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseSourcePanel;
030: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseSourceTab;
031: import net.sourceforge.squirrel_sql.fw.codereformat.CodeReformator;
032: import net.sourceforge.squirrel_sql.fw.codereformat.CommentSpec;
033: import net.sourceforge.squirrel_sql.fw.util.StringManager;
034: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
035: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
036: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
037:
038: /**
039: *
040: * @author manningr
041: *
042: */
043: public abstract class PostgresSourceTab extends BaseSourceTab {
044:
045: private static final StringManager s_stringMgr = StringManagerFactory
046: .getStringManager(PostgresSourceTab.class);
047:
048: public static final int VIEW_TYPE = 0;
049: public static final int STORED_PROC_TYPE = 1;
050: public static final int TRIGGER_TYPE = 2;
051:
052: protected int sourceType = VIEW_TYPE;
053:
054: /** Logger for this class. */
055: private final static ILogger s_log = LoggerController
056: .createLogger(PostgresSourceTab.class);
057:
058: private static CommentSpec[] commentSpecs = new CommentSpec[] {
059: new CommentSpec("/*", "*/"), new CommentSpec("--", "\n") };
060:
061: private static CodeReformator formatter = new CodeReformator(";",
062: commentSpecs);
063:
064: public PostgresSourceTab(String hint) {
065: super (hint);
066: super .setSourcePanel(new InformixSourcePanel());
067: }
068:
069: private final class InformixSourcePanel extends BaseSourcePanel {
070: private JTextArea _ta;
071:
072: InformixSourcePanel() {
073: super (new BorderLayout());
074: createUserInterface();
075: }
076:
077: public void load(ISession session, PreparedStatement stmt) {
078: _ta.setText("");
079: _ta.setWrapStyleWord(true);
080: try {
081: ResultSet rs = stmt.executeQuery();
082: StringBuffer buf = new StringBuffer(4096);
083: while (rs.next()) {
084: if (sourceType == STORED_PROC_TYPE) {
085: String tmpProcData = rs.getString(1);
086: buf.append(tmpProcData);
087: }
088: if (sourceType == TRIGGER_TYPE) {
089: String data = rs.getString(1);
090: buf.append(data.trim() + " ");
091: }
092: if (sourceType == VIEW_TYPE) {
093: String line = rs.getString(1);
094: buf.append(line.trim() + " ");
095: }
096: }
097: // Stored Procedures can have comments embedded in them, so
098: // don't line-wrap them.
099: if (sourceType == VIEW_TYPE
100: || sourceType == TRIGGER_TYPE) {
101: if (s_log.isDebugEnabled()) {
102: s_log.debug("View source before formatting: "
103: + buf.toString());
104: }
105: _ta.setText(formatter.reformat(buf.toString()));
106: } else {
107: _ta.setText(buf.toString());
108: }
109: _ta.setCaretPosition(0);
110: } catch (SQLException ex) {
111: session.showErrorMessage(ex);
112: }
113:
114: }
115:
116: private void createUserInterface() {
117: _ta = new JTextArea();
118: _ta.setEditable(false);
119: add(_ta, BorderLayout.CENTER);
120: }
121: }
122:
123: }
|