Revision: 6635
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=6635&view=rev
Author: gerdwagner
Date: 2012-06-19 18:19:23 +0000 (Tue, 19 Jun 2012)
Log Message:
-----------
Made SQL formating configurable
Modified Paths:
--------------
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlConfigPrefsTab.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlController.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPanel.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPrefCtrl.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformatorConfigFactory.java
trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/preferences/codereformat/I18NStrings.properties
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlConfigPrefsTab.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlConfigPrefsTab.java 2012-06-18 19:17:57 UTC (rev 6634)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlConfigPrefsTab.java 2012-06-19 18:19:23 UTC (rev 6635)
@@ -39,13 +39,13 @@
@Override
public String getTitle()
{
- return s_stringMgr.getString("editextras.FormatSqlConfigPrefsTab.title");
+ return s_stringMgr.getString("codereformat.FormatSqlConfigPrefsTab.title");
}
@Override
public String getHint()
{
- return s_stringMgr.getString("editextras.FormatSqlConfigPrefsTab.hint");
+ return s_stringMgr.getString("codereformat.FormatSqlConfigPrefsTab.hint");
}
@Override
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlController.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlController.java 2012-06-18 19:17:57 UTC (rev 6634)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlController.java 2012-06-19 18:19:23 UTC (rev 6635)
@@ -1,10 +1,14 @@
package net.sourceforge.squirrel_sql.client.preferences.codereformat;
import net.sourceforge.squirrel_sql.client.IApplication;
+import net.sourceforge.squirrel_sql.client.util.codereformat.CodeReformator;
+import net.sourceforge.squirrel_sql.client.util.codereformat.CodeReformatorConfigFactory;
+import net.sourceforge.squirrel_sql.fw.gui.FontInfo;
import net.sourceforge.squirrel_sql.fw.util.StringManager;
import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
+import java.awt.event.*;
import java.util.ArrayList;
public class FormatSqlController
@@ -20,65 +24,132 @@
public FormatSqlController(IApplication app)
{
_app = app;
+
_formatSqlPref = FormatSqlPrefReader.loadPref();
+ _formatSqlPanel = new FormatSqlPanel(_formatSqlPref.getKeywordBehaviourPrefs());
- _formatSqlPanel = new FormatSqlPanel(_formatSqlPref.getKeywordBehaviourPrefs());
+ FontInfo fontInfo = app.getSquirrelPreferences().getSessionProperties().getFontInfo();
+ _formatSqlPanel.txtExampleSqls.setEditable(false);
+ _formatSqlPanel.txtExampleSqls.setFont(fontInfo.createFont());
+ refreshExampleSql(_formatSqlPref);
+
+
+
_formatSqlPanel.txtIndentCount.setValue(_formatSqlPref.getIndent());
+
+ _formatSqlPanel.txtIndentCount.addFocusListener(new FocusAdapter()
+ {
+ @Override
+ public void focusLost(FocusEvent e)
+ {
+ refreshExampleSql(createFormatSqlPrefFromGui());
+ }
+ });
+
_formatSqlPanel.txtPreferedLineLength.setValue(_formatSqlPref.getPreferedLineLength());
+ _formatSqlPanel.txtPreferedLineLength.addFocusListener(new FocusAdapter()
+ {
+ @Override
+ public void focusLost(FocusEvent e)
+ {
+ refreshExampleSql(createFormatSqlPrefFromGui());
+ }
+ });
+
+ ActionListener actionListener = new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ refreshExampleSql(createFormatSqlPrefFromGui());
+ }
+ };
+
+ for (KeywordBehaviourPrefCtrl keywordBehaviourPrefCtrl : _formatSqlPanel.keywordBehaviourPrefCtrls)
+ {
+ keywordBehaviourPrefCtrl.addKeyWordBehaviourChangedListener(actionListener);
+ }
+
}
+ private void refreshExampleSql(FormatSqlPref formatSqlPref)
+ {
+ String sqls;CodeReformator codeReformator = new CodeReformator(CodeReformatorConfigFactory.createConfig(formatSqlPref));
+ sqls = codeReformator.reformat("SELECT table1.id, table2.number, SUM(table1.amount) FROM table1 INNER JOIN table2 ON table.id = table2.table1_id WHERE table1.id IN (SELECT table1_id FROM table3 WHERE table3.name = 'Foo Bar' and table3.type = 'unknown_type') GROUP BY table1.id, table2.number ORDER BY table1.id");
+ sqls += "\n\n";
+ sqls += codeReformator.reformat("UPDATE table1 SET name = 'Hello', number = '1456-789' WHERE id = 42");
+ sqls += "\n\n";
+ sqls += codeReformator.reformat("INSERT INTO table1 (name, number) SELECT name, number FROM table1_bak");
+ sqls += "\n\n";
+ sqls += codeReformator.reformat("INSERT INTO table1 (name, number, type) VALUES ('Foo', 42, 'VA')");
+ sqls += "\n\n";
+ sqls += codeReformator.reformat("DELETE FROM table1 WHERE name = 'Hello' OR number = '1456-789'");
+
+ _formatSqlPanel.txtExampleSqls.setText(sqls);
+ }
+
+
public void applyChanges()
{
try
{
- if (null != _formatSqlPanel.txtIndentCount.getText())
+ _formatSqlPref = createFormatSqlPrefFromGui();
+
+ XMLBeanWriter bw = new XMLBeanWriter(_formatSqlPref);
+ bw.save(FormatSqlPrefReader.getPrefsFile());
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private FormatSqlPref createFormatSqlPrefFromGui()
+ {
+ FormatSqlPref ret = new FormatSqlPref();
+
+ if (null != _formatSqlPanel.txtIndentCount.getText())
+ {
+ try
{
- try
+ Integer indent = Integer.valueOf(_formatSqlPanel.txtIndentCount.getText());
+ if (indent >= 0)
{
- Integer indent = Integer.valueOf(_formatSqlPanel.txtIndentCount.getText());
- if (indent >= 0)
- {
- _formatSqlPref.setIndent(indent);
- }
+ ret.setIndent(indent);
}
- catch (NumberFormatException e)
- {
- // ignore
- }
}
+ catch (NumberFormatException e)
+ {
+ // ignore
+ }
+ }
- if (null != _formatSqlPanel.txtPreferedLineLength.getText())
+ if (null != _formatSqlPanel.txtPreferedLineLength.getText())
+ {
+ try
{
- try
+ Integer preferedLineLength = Integer.valueOf(_formatSqlPanel.txtPreferedLineLength.getText());
+ if (preferedLineLength >= 0)
{
- Integer preferedLineLength = Integer.valueOf(_formatSqlPanel.txtPreferedLineLength.getText());
- if (preferedLineLength >= 0)
- {
- _formatSqlPref.setPreferedLineLength(preferedLineLength);
- }
+ ret.setPreferedLineLength(preferedLineLength);
}
- catch (NumberFormatException e)
- {
- // ignore
- }
}
-
- ArrayList<KeywordBehaviourPref> buf = new ArrayList<KeywordBehaviourPref>();
- for (KeywordBehaviourPrefCtrl keywordBehaviourPrefCtrl : _formatSqlPanel._keywordBehaviourPrefCtrls)
+ catch (NumberFormatException e)
{
- keywordBehaviourPrefCtrl.applyChanges();
- buf.add(keywordBehaviourPrefCtrl.getKeywordBehaviourPref());
+ // ignore
}
- _formatSqlPref.setKeywordBehaviourPrefs(buf.toArray(new KeywordBehaviourPref[buf.size()]));
+ }
- XMLBeanWriter bw = new XMLBeanWriter(_formatSqlPref);
- bw.save(FormatSqlPrefReader.getPrefsFile());
- }
- catch (Exception e)
+ ArrayList<KeywordBehaviourPref> buf = new ArrayList<KeywordBehaviourPref>();
+ for (KeywordBehaviourPrefCtrl keywordBehaviourPrefCtrl : _formatSqlPanel.keywordBehaviourPrefCtrls)
{
- throw new RuntimeException(e);
+ keywordBehaviourPrefCtrl.applyChanges();
+ buf.add(keywordBehaviourPrefCtrl.getKeywordBehaviourPref());
}
+ ret.setKeywordBehaviourPrefs(buf.toArray(new KeywordBehaviourPref[buf.size()]));
+
+ return ret;
}
public FormatSqlPanel getPanel()
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPanel.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPanel.java 2012-06-18 19:17:57 UTC (rev 6634)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/FormatSqlPanel.java 2012-06-19 18:19:23 UTC (rev 6635)
@@ -17,9 +17,9 @@
public static enum KeywordBehaviour
{
- ALONE_IN_LINE(1, s_stringMgr.getString("editextras.FormatSqlPanel.aloneInLine"), PieceMarkerSpec.TYPE_PIECE_MARKER_IN_OWN_PIECE),
- START_NEW_LINE(2, s_stringMgr.getString("editextras.FormatSqlPanel.startNewLine"), PieceMarkerSpec.TYPE_PIECE_MARKER_AT_BEGIN),
- NO_INFLUENCE_ON_NEW_LINE(3, s_stringMgr.getString("editextras.FormatSqlPanel.noInfluenceOnNewLine"), null);
+ ALONE_IN_LINE(1, s_stringMgr.getString("codereformat.aloneInLine"), PieceMarkerSpec.TYPE_PIECE_MARKER_IN_OWN_PIECE),
+ START_NEW_LINE(2, s_stringMgr.getString("codereformat.startNewLine"), PieceMarkerSpec.TYPE_PIECE_MARKER_AT_BEGIN),
+ NO_INFLUENCE_ON_NEW_LINE(3, s_stringMgr.getString("codereformat.noInfluenceOnNewLine"), null);
private String _title;
private Integer _pieceMarkerSpecType;
@@ -65,53 +65,65 @@
JFormattedTextField txtIndentCount;
JFormattedTextField txtPreferedLineLength;
- ArrayList<KeywordBehaviourPrefCtrl> _keywordBehaviourPrefCtrls = new ArrayList<KeywordBehaviourPrefCtrl>();
+ ArrayList<KeywordBehaviourPrefCtrl> keywordBehaviourPrefCtrls = new ArrayList<KeywordBehaviourPrefCtrl>();
+ JTextArea txtExampleSqls = new JTextArea();
+
public FormatSqlPanel(KeywordBehaviourPref[] keywordBehaviourPrefs)
{
- setLayout(new GridBagLayout());
+ setLayout(new BorderLayout());
+ add(createControlsPanel(keywordBehaviourPrefs), BorderLayout.WEST);
+ add(new JScrollPane(txtExampleSqls), BorderLayout.CENTER);
+ }
+
+ private JPanel createControlsPanel(KeywordBehaviourPref[] keywordBehaviourPrefs)
+ {
+ JPanel ret = new JPanel(new GridBagLayout());
+
GridBagConstraints gbc;
gbc = new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
- add(new JLabel(s_stringMgr.getString("editextras.FormatSqlPanel.indent")), gbc);
+ ret.add(new JLabel(s_stringMgr.getString("codereformat.FormatSqlPanel.indent")), gbc);
gbc = new GridBagConstraints(1,0,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
txtIndentCount = new JFormattedTextField(NumberFormat.getInstance());
txtIndentCount.setColumns(7);
- add(txtIndentCount, gbc);
+ ret.add(txtIndentCount, gbc);
gbc = new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
- add(new JLabel(s_stringMgr.getString("editextras.FormatSqlPanel.preferedLineLen")), gbc);
+ ret.add(new JLabel(s_stringMgr.getString("codereformat.FormatSqlPanel.preferedLineLen")), gbc);
gbc = new GridBagConstraints(1,1,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
txtPreferedLineLength = new JFormattedTextField(NumberFormat.getInstance());
txtPreferedLineLength.setColumns(7);
- add(txtPreferedLineLength, gbc);
+ ret.add(txtPreferedLineLength, gbc);
gbc = new GridBagConstraints(0,2,2,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(30,5,5,5),0,0);
- add(new JLabel(s_stringMgr.getString("editextras.FormatSqlPanel.keywordBehavior")), gbc);
+ ret.add(new JLabel(s_stringMgr.getString("codereformat.FormatSqlPanel.keywordBehavior")), gbc);
int gridy = 2;
for (KeywordBehaviourPref keywordBehaviourPref : keywordBehaviourPrefs)
{
- _keywordBehaviourPrefCtrls.add(createKeywordBehaviourPrefCtrl(keywordBehaviourPref, ++gridy));
+ keywordBehaviourPrefCtrls.add(createKeywordBehaviourPrefCtrl(ret, keywordBehaviourPref, ++gridy));
}
+
+ return ret;
}
- private KeywordBehaviourPrefCtrl createKeywordBehaviourPrefCtrl(KeywordBehaviourPref keywordBehaviourPref, int gridy)
+ private KeywordBehaviourPrefCtrl createKeywordBehaviourPrefCtrl(JPanel toAddTo, KeywordBehaviourPref keywordBehaviourPref, int gridy)
{
GridBagConstraints gbc;
gbc = new GridBagConstraints(0, gridy,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
- add(new JLabel(keywordBehaviourPref.getKeyWord()), gbc);
+ toAddTo.add(new JLabel(keywordBehaviourPref.getKeyWord()), gbc);
gbc = new GridBagConstraints(1, gridy,1,1,0,0,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0);
JComboBox cbo = new JComboBox();
- add(cbo, gbc);
+ toAddTo.add(cbo, gbc);
return new KeywordBehaviourPrefCtrl(cbo, keywordBehaviourPref);
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPrefCtrl.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPrefCtrl.java 2012-06-18 19:17:57 UTC (rev 6634)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/preferences/codereformat/KeywordBehaviourPrefCtrl.java 2012-06-19 18:19:23 UTC (rev 6635)
@@ -1,6 +1,7 @@
package net.sourceforge.squirrel_sql.client.preferences.codereformat;
import javax.swing.*;
+import java.awt.event.ActionListener;
public class KeywordBehaviourPrefCtrl
{
@@ -30,4 +31,9 @@
{
return _keywordBehaviourPref;
}
+
+ public void addKeyWordBehaviourChangedListener(ActionListener l)
+ {
+ _cbo.addActionListener(l);
+ }
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformatorConfigFactory.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformatorConfigFactory.java 2012-06-18 19:17:57 UTC (rev 6634)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/util/codereformat/CodeReformatorConfigFactory.java 2012-06-19 18:19:23 UTC (rev 6635)
@@ -11,6 +11,15 @@
public class CodeReformatorConfigFactory
{
+
+ public static final CommentSpec[] DEFAULT_COMMENT_SPECS = new CommentSpec[]
+ {
+ new CommentSpec("/*", "*/"),
+ new CommentSpec("--", StringUtilities.getEolStr())
+ };
+
+ public static final String DEFAULT_STATEMENT_SEPARATOR = ";";
+
public static CodeReformatorConfig createConfig(ISession sess)
{
String statementSep = sess.getQueryTokenizer().getSQLStatementSeparator();
@@ -19,22 +28,22 @@
public static CodeReformatorConfig createConfig(String statementSep)
{
- CommentSpec[] commentSpecs =
- new CommentSpec[]
- {
- new CommentSpec("/*", "*/"),
- new CommentSpec("--", StringUtilities.getEolStr())
- };
-
- return createConfig(statementSep, commentSpecs);
+ return createConfig(statementSep, DEFAULT_COMMENT_SPECS);
}
public static CodeReformatorConfig createConfig(String statementSeparator, CommentSpec[] commentSpecs)
{
-
FormatSqlPref formatSqlPref = FormatSqlPrefReader.loadPref();
+ return createConfig(statementSeparator, commentSpecs, formatSqlPref);
+ }
+ public static CodeReformatorConfig createConfig(FormatSqlPref formatSqlPref)
+ {
+ return createConfig(DEFAULT_STATEMENT_SEPARATOR, DEFAULT_COMMENT_SPECS, formatSqlPref);
+ }
+ public static CodeReformatorConfig createConfig(String statementSeparator, CommentSpec[] commentSpecs, FormatSqlPref formatSqlPref)
+ {
String indent = "";
for (int i = 0; i < formatSqlPref.getIndent(); i++)
{
Modified: trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/preferences/codereformat/I18NStrings.properties
===================================================================
--- trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/preferences/codereformat/I18NStrings.properties 2012-06-18 19:17:57 UTC (rev 6634)
+++ trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/preferences/codereformat/I18NStrings.properties 2012-06-19 18:19:23 UTC (rev 6635)
@@ -1,8 +1,8 @@
-editextras.FormatSqlConfigPrefsTab.title=SQL formating
-editextras.FormatSqlConfigPrefsTab.hint= Option for SQL formating (ctrl+f)
-editextras.FormatSqlPanel.indent=Indent space count
-editextras.FormatSqlPanel.preferedLineLen=Prefered line length
-editextras.FormatSqlPanel.keywordBehavior=Keyword behavior:
-editextras.FormatSqlPanel.aloneInLine=Alone in line
-editextras.FormatSqlPanel.startNewLine=Start new Line
-editextras.FormatSqlPanel.noInfluenceOnNewLine=No influence on line break
\ No newline at end of file
+codereformat.FormatSqlConfigPrefsTab.title=SQL formating
+codereformat.FormatSqlConfigPrefsTab.hint= Option for SQL formating (ctrl+f)
+codereformat.FormatSqlPanel.indent=Indent space count
+codereformat.FormatSqlPanel.preferedLineLen=Prefered line length
+codereformat.FormatSqlPanel.keywordBehavior=Keyword behavior:
+codereformat.aloneInLine=Alone in line
+codereformat.startNewLine=Start new Line
+codereformat.noInfluenceOnNewLine=No influence on line break
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|