001: package net.sourceforge.squirrel_sql.plugins.hibernate;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.client.session.mainpanel.SQLResultExecuterPanel;
005: import net.sourceforge.squirrel_sql.client.session.event.SessionAdapter;
006: import net.sourceforge.squirrel_sql.client.session.event.SessionEvent;
007: import net.sourceforge.squirrel_sql.fw.codereformat.CommentSpec;
008: import net.sourceforge.squirrel_sql.fw.codereformat.CodeReformator;
009:
010: import javax.swing.*;
011: import java.util.ArrayList;
012: import java.util.prefs.Preferences;
013: import java.awt.*;
014: import java.awt.event.ActionListener;
015: import java.awt.event.ActionEvent;
016:
017: public class SQLPanelManager extends EntryPanelManagerBase {
018: private static final String PREF_KEY_APPEND_SQL = "SquirrelSQL.hibernate.sqlAppendSql";
019: private static final String PREF_KEY_FORMAT_SQL = "SquirrelSQL.hibernate.sqlFormatSql";
020: private static final String PREF_KEY_EXECUTE_SQL = "SquirrelSQL.hibernate.sqlExecuteSql";
021:
022: private HibernateSQLPanel _hibernateSQLPanel;
023:
024: SQLResultExecuterPanel _resultExecuterPanel;
025:
026: public SQLPanelManager(final ISession session) {
027: super (session);
028: init(null, null);
029:
030: _resultExecuterPanel = new SQLResultExecuterPanel(session);
031: _hibernateSQLPanel = new HibernateSQLPanel(
032: super .getComponent(), _resultExecuterPanel);
033:
034: session.getApplication().getSessionManager()
035: .addSessionListener(new SessionAdapter() {
036:
037: public void sessionClosing(SessionEvent evt) {
038: onSessionClosing();
039: session.getApplication().getSessionManager()
040: .removeSessionListener(this );
041: }
042: });
043:
044: _hibernateSQLPanel._btnFormatSql
045: .addActionListener(new ActionListener() {
046: public void actionPerformed(ActionEvent e) {
047: onFormatSql();
048: }
049: });
050:
051: _hibernateSQLPanel._chkAppendSql.setSelected(Preferences
052: .userRoot().getBoolean(PREF_KEY_APPEND_SQL, false));
053: _hibernateSQLPanel._chkAlwaysFormatSql.setSelected(Preferences
054: .userRoot().getBoolean(PREF_KEY_FORMAT_SQL, false));
055: _hibernateSQLPanel._chkAlwaysExecuteSql.setSelected(Preferences
056: .userRoot().getBoolean(PREF_KEY_EXECUTE_SQL, false));
057: }
058:
059: private void onFormatSql() {
060: if (null != getEntryPanel().getText()) {
061: getEntryPanel().setText(format(getEntryPanel().getText()));
062: }
063: }
064:
065: private void onSessionClosing() {
066: Preferences.userRoot().putBoolean(PREF_KEY_APPEND_SQL,
067: _hibernateSQLPanel._chkAppendSql.isSelected());
068: Preferences.userRoot().putBoolean(PREF_KEY_FORMAT_SQL,
069: _hibernateSQLPanel._chkAlwaysFormatSql.isSelected());
070: Preferences.userRoot().putBoolean(PREF_KEY_EXECUTE_SQL,
071: _hibernateSQLPanel._chkAlwaysExecuteSql.isSelected());
072: }
073:
074: public JComponent getComponent() {
075: return _hibernateSQLPanel;
076: }
077:
078: public void displaySqls(ArrayList<String> sqls) {
079: String allSqls = createAllSqlsString(sqls);
080:
081: if (_hibernateSQLPanel._chkAlwaysExecuteSql.isSelected()) {
082: _hibernateSQLPanel._tabResult_code
083: .setSelectedComponent(_resultExecuterPanel);
084: displaySqlResult(allSqls);
085: } else {
086: _hibernateSQLPanel._tabResult_code
087: .setSelectedComponent(super .getComponent());
088: }
089: displaySqlCode(allSqls);
090: }
091:
092: private void displaySqlResult(String allSqls) {
093: _resultExecuterPanel.executeSQL(allSqls);
094: }
095:
096: private void displaySqlCode(String allSqls) {
097:
098: if (_hibernateSQLPanel._chkAlwaysFormatSql.isSelected()) {
099: allSqls = format(allSqls);
100: }
101:
102: if (_hibernateSQLPanel._chkAppendSql.isSelected()) {
103: getEntryPanel().appendText(allSqls);
104:
105: scrollEntryPlanel(allSqls);
106:
107: } else {
108: getEntryPanel().setText(allSqls, false);
109: }
110: }
111:
112: private String createAllSqlsString(ArrayList<String> sqls) {
113: String allSqls = "";
114:
115: String sep = getSession().getProperties()
116: .getSQLStatementSeparator();
117:
118: for (String sql : sqls) {
119: allSqls += sql;
120:
121: if (1 < sep.length()) {
122: allSqls += "\n";
123: }
124: allSqls += (sep + "\n\n");
125: }
126: return allSqls;
127: }
128:
129: private String format(String sqls) {
130: CommentSpec[] commentSpecs = new CommentSpec[] {
131: new CommentSpec("/*", "*/"),
132: new CommentSpec("--", "\n") };
133:
134: String statementSep = getSession().getQueryTokenizer()
135: .getSQLStatementSeparator();
136:
137: CodeReformator cr = new CodeReformator(statementSep,
138: commentSpecs);
139:
140: sqls = cr.reformat(sqls) + "\n";
141: return sqls;
142: }
143:
144: private void scrollEntryPlanel(String allSqls) {
145: getEntryPanel().getTextComponent().setCaretPosition(
146: getEntryPanel().getText().length() - allSqls.length());
147: Point p = getEntryPanel().getTextComponent().getCaret()
148: .getMagicCaretPosition();
149: if (p != null) {
150: getEntryPanel().getTextComponent().scrollRectToVisible(
151: new Rectangle(p.x, p.y, 1, 100));
152: }
153: }
154: }
|