001: package net.sourceforge.squirrel_sql.plugins.codecompletion.prefs;
002:
003: import net.sourceforge.squirrel_sql.client.IApplication;
004: import net.sourceforge.squirrel_sql.client.preferences.INewSessionPropertiesPanel;
005: import net.sourceforge.squirrel_sql.client.session.ISession;
006: import net.sourceforge.squirrel_sql.client.session.properties.ISessionPropertiesPanel;
007: import net.sourceforge.squirrel_sql.fw.util.StringManager;
008: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
009:
010: import javax.swing.table.TableColumn;
011: import javax.swing.table.TableCellEditor;
012: import javax.swing.*;
013: import java.awt.*;
014: import java.awt.event.ActionListener;
015: import java.awt.event.ActionEvent;
016:
017: public class CodeCompletionPreferencesController implements
018: INewSessionPropertiesPanel, ISessionPropertiesPanel {
019: private static final StringManager s_stringMgr = StringManagerFactory
020: .getStringManager(CodeCompletionPreferencesController.class);
021:
022: private CodeCompletionPreferences _prefs;
023:
024: private CodeCompletionPreferencesPanel _panel;
025:
026: public CodeCompletionPreferencesController(
027: CodeCompletionPreferences prefs) {
028: _panel = new CodeCompletionPreferencesPanel();
029: _prefs = prefs;
030:
031: switch (_prefs.getGeneralCompletionConfig()) {
032: case CodeCompletionPreferences.CONFIG_SP_WITH_PARARMS:
033: _panel.optSPWithParams.setSelected(true);
034: break;
035: case CodeCompletionPreferences.CONFIG_SP_WITHOUT_PARARMS:
036: _panel.optSPWithoutParams.setSelected(true);
037: break;
038: case CodeCompletionPreferences.CONFIG_UDF_WITH_PARARMS:
039: _panel.optUDFWithParams.setSelected(true);
040: break;
041: case CodeCompletionPreferences.CONFIG_UDF_WITHOUT_PARARMS:
042: _panel.optUDFWithoutParams.setSelected(true);
043: break;
044: }
045:
046: PrefixesTableModel tm = new PrefixesTableModel(_prefs
047: .getPrefixedConfigs());
048: _panel.tblPrefixes.setModel(tm);
049:
050: TableColumn tcPrefix = new TableColumn();
051: // i18n[codecompletion.prefs.table.col.prefix=Prefix]
052: tcPrefix.setHeaderValue(s_stringMgr
053: .getString("codecompletion.prefs.table.col.prefix"));
054: tcPrefix.setModelIndex(0);
055: _panel.tblPrefixes.addColumn(tcPrefix);
056:
057: TableColumn tcCompletionConfig = new TableColumn();
058: // i18n[codecompletion.prefs.table.col.config=Configuration]
059: tcCompletionConfig.setHeaderValue(s_stringMgr
060: .getString("codecompletion.prefs.table.col.config"));
061:
062: JComboBox cboConfigs = new JComboBox(ConfigCboItem.items);
063: cboConfigs.setSelectedIndex(0);
064: tcCompletionConfig.setCellEditor(new DefaultCellEditor(
065: cboConfigs));
066: tcCompletionConfig.setModelIndex(1);
067: _panel.tblPrefixes.addColumn(tcCompletionConfig);
068:
069: _panel.txtMaxLastSelectedCompletionNamesPanel.setText(""
070: + _prefs.getMaxLastSelectedCompletionNames());
071:
072: _panel.btnNewRow.addActionListener(new ActionListener() {
073: public void actionPerformed(ActionEvent e) {
074: onAddRow();
075: }
076: });
077:
078: _panel.btnDeleteRows.addActionListener(new ActionListener() {
079: public void actionPerformed(ActionEvent e) {
080: onDeleteRows();
081: }
082: });
083: }
084:
085: private void onDeleteRows() {
086: stopEditing();
087: PrefixesTableModel tm = (PrefixesTableModel) _panel.tblPrefixes
088: .getModel();
089: int[] selRows = _panel.tblPrefixes.getSelectedRows();
090: tm.removeRows(selRows);
091: }
092:
093: private void onAddRow() {
094: PrefixesTableModel tm = (PrefixesTableModel) _panel.tblPrefixes
095: .getModel();
096: tm.addNewConfig();
097: }
098:
099: public void initialize(IApplication app) {
100: }
101:
102: public void initialize(IApplication app, ISession session) {
103: }
104:
105: /**
106: * Return the component to be displayed in the Preferences dialog.
107: *
108: * @return the component to be displayed in the Preferences dialog.
109: */
110: public Component getPanelComponent() {
111: return _panel;
112: }
113:
114: /**
115: * User has pressed OK or Apply in the dialog so save data from
116: * panel.
117: */
118: public void applyChanges() {
119:
120: if (_panel.optSPWithParams.isSelected()) {
121: _prefs
122: .setGeneralCompletionConfig(CodeCompletionPreferences.CONFIG_SP_WITH_PARARMS);
123: } else if (_panel.optSPWithoutParams.isSelected()) {
124: _prefs
125: .setGeneralCompletionConfig(CodeCompletionPreferences.CONFIG_SP_WITHOUT_PARARMS);
126: } else if (_panel.optUDFWithParams.isSelected()) {
127: _prefs
128: .setGeneralCompletionConfig(CodeCompletionPreferences.CONFIG_UDF_WITH_PARARMS);
129: } else if (_panel.optUDFWithoutParams.isSelected()) {
130: _prefs
131: .setGeneralCompletionConfig(CodeCompletionPreferences.CONFIG_UDF_WITHOUT_PARARMS);
132: } else {
133: throw new IllegalStateException("No valid config selected");
134: }
135:
136: stopEditing();
137:
138: PrefixesTableModel tm = (PrefixesTableModel) _panel.tblPrefixes
139: .getModel();
140:
141: _prefs.setPrefixedConfigs(tm.getData());
142:
143: try {
144: _prefs
145: .setMaxLastSelectedCompletionNames(Math
146: .max(
147: 0,
148: Integer
149: .parseInt(_panel.txtMaxLastSelectedCompletionNamesPanel
150: .getText())));
151: } catch (NumberFormatException e) {
152:
153: }
154:
155: }
156:
157: private void stopEditing() {
158: TableCellEditor cellEditor = _panel.tblPrefixes.getCellEditor();
159: if (null != cellEditor) {
160: cellEditor.stopCellEditing();
161: }
162: }
163:
164: /**
165: * Return the title for this panel.
166: *
167: * @return the title for this panel.
168: */
169: public String getTitle() {
170: // i18n[codeCompletion.PrefsTabTitle=Code Completion]
171: return s_stringMgr.getString("codeCompletion.PrefsTabTitle");
172: }
173:
174: /**
175: * Return the hint for this panel.
176: *
177: * @return the hint for this panel.
178: */
179: public String getHint() {
180: // i18n[codeCompletion.PrefsTabTitleHint=Configure Code Completion]
181: return s_stringMgr
182: .getString("codeCompletion.PrefsTabTitleHint");
183: }
184:
185: }
|