Lines 39-45
Link Here
|
39 |
* |
39 |
* |
40 |
* Portions Copyrighted 2009 - 2010 Sun Microsystems, Inc. |
40 |
* Portions Copyrighted 2009 - 2010 Sun Microsystems, Inc. |
41 |
*/ |
41 |
*/ |
42 |
|
|
|
43 |
/* |
42 |
/* |
44 |
* SQLHistoryPanel.java |
43 |
* SQLHistoryPanel.java |
45 |
* |
44 |
* |
Lines 48-299
Link Here
|
48 |
package org.netbeans.modules.db.sql.execute.ui; |
47 |
package org.netbeans.modules.db.sql.execute.ui; |
49 |
|
48 |
|
50 |
import java.awt.Component; |
49 |
import java.awt.Component; |
51 |
import java.awt.Cursor; |
|
|
52 |
import java.awt.Window; |
53 |
import java.awt.event.ActionEvent; |
50 |
import java.awt.event.ActionEvent; |
54 |
import java.awt.event.ActionListener; |
51 |
import java.awt.event.ActionListener; |
55 |
import java.awt.event.MouseAdapter; |
52 |
import java.awt.event.MouseAdapter; |
56 |
import java.awt.event.MouseEvent; |
53 |
import java.awt.event.MouseEvent; |
57 |
import java.awt.event.WindowAdapter; |
|
|
58 |
import java.awt.event.WindowEvent; |
59 |
import java.text.DateFormat; |
54 |
import java.text.DateFormat; |
60 |
import java.util.ArrayList; |
55 |
import java.util.ArrayList; |
61 |
import java.util.Collections; |
|
|
62 |
import java.util.Comparator; |
63 |
import java.util.Date; |
56 |
import java.util.Date; |
64 |
import java.util.HashMap; |
|
|
65 |
import java.util.HashSet; |
66 |
import java.util.List; |
57 |
import java.util.List; |
67 |
import java.util.Map; |
|
|
68 |
import java.util.Set; |
69 |
import java.util.logging.Level; |
58 |
import java.util.logging.Level; |
70 |
import java.util.logging.Logger; |
59 |
import java.util.logging.Logger; |
71 |
import javax.swing.DefaultComboBoxModel; |
60 |
import javax.swing.DefaultComboBoxModel; |
72 |
import javax.swing.DefaultListCellRenderer; |
61 |
import javax.swing.InputVerifier; |
73 |
import javax.swing.JComponent; |
62 |
import javax.swing.JComponent; |
74 |
import javax.swing.JEditorPane; |
63 |
import javax.swing.JEditorPane; |
75 |
import javax.swing.JLabel; |
|
|
76 |
import javax.swing.JList; |
77 |
import javax.swing.JTable; |
64 |
import javax.swing.JTable; |
78 |
import javax.swing.SwingUtilities; |
65 |
import javax.swing.JTextField; |
79 |
import javax.swing.UIManager; |
66 |
import javax.swing.ListSelectionModel; |
|
|
67 |
import javax.swing.RowFilter; |
68 |
import javax.swing.RowFilter.Entry; |
69 |
import javax.swing.RowSorter.SortKey; |
70 |
import javax.swing.SortOrder; |
80 |
import javax.swing.event.DocumentEvent; |
71 |
import javax.swing.event.DocumentEvent; |
81 |
import javax.swing.event.DocumentListener; |
72 |
import javax.swing.event.DocumentListener; |
|
|
73 |
import javax.swing.event.ListSelectionEvent; |
74 |
import javax.swing.event.ListSelectionListener; |
82 |
import javax.swing.event.TableModelEvent; |
75 |
import javax.swing.event.TableModelEvent; |
83 |
import javax.swing.event.TableModelListener; |
76 |
import javax.swing.event.TableModelListener; |
|
|
77 |
import javax.swing.table.AbstractTableModel; |
84 |
import javax.swing.table.DefaultTableCellRenderer; |
78 |
import javax.swing.table.DefaultTableCellRenderer; |
85 |
import javax.swing.table.DefaultTableModel; |
|
|
86 |
import javax.swing.table.JTableHeader; |
87 |
import javax.swing.table.TableCellRenderer; |
79 |
import javax.swing.table.TableCellRenderer; |
88 |
import javax.swing.table.TableColumn; |
80 |
import javax.swing.table.TableColumn; |
89 |
import javax.swing.table.TableColumnModel; |
81 |
import javax.swing.table.TableColumnModel; |
|
|
82 |
import javax.swing.table.TableRowSorter; |
90 |
import javax.swing.text.BadLocationException; |
83 |
import javax.swing.text.BadLocationException; |
91 |
import javax.swing.text.Caret; |
84 |
import javax.swing.text.Caret; |
92 |
import javax.swing.text.Document; |
85 |
import javax.swing.text.Document; |
93 |
import org.netbeans.api.db.explorer.ConnectionManager; |
|
|
94 |
import org.netbeans.api.db.explorer.DatabaseConnection; |
95 |
import org.netbeans.api.editor.EditorRegistry; |
86 |
import org.netbeans.api.editor.EditorRegistry; |
96 |
import org.netbeans.modules.db.sql.history.SQLHistory; |
87 |
import org.netbeans.modules.db.sql.history.SQLHistoryEntry; |
97 |
import org.netbeans.modules.db.sql.history.SQLHistoryException; |
|
|
98 |
import org.netbeans.modules.db.sql.history.SQLHistoryManager; |
88 |
import org.netbeans.modules.db.sql.history.SQLHistoryManager; |
99 |
import org.netbeans.modules.db.sql.history.SQLHistoryModel; |
|
|
100 |
import org.netbeans.modules.db.sql.history.SQLHistoryModelImpl; |
101 |
import org.netbeans.modules.db.sql.history.SQLHistoryPersistenceManager; |
102 |
import org.netbeans.modules.db.sql.loader.SQLDataLoader; |
89 |
import org.netbeans.modules.db.sql.loader.SQLDataLoader; |
103 |
import org.openide.awt.MouseUtils; |
90 |
import org.openide.awt.MouseUtils; |
104 |
import org.openide.filesystems.FileObject; |
|
|
105 |
import org.openide.filesystems.FileUtil; |
106 |
import org.openide.util.Exceptions; |
91 |
import org.openide.util.Exceptions; |
107 |
import org.openide.util.NbBundle; |
92 |
import org.openide.util.NbBundle; |
108 |
import org.openide.util.NbPreferences; |
93 |
import org.openide.util.NotImplementedException; |
109 |
import org.openide.util.RequestProcessor; |
|
|
110 |
import org.openide.util.RequestProcessor.Task; |
111 |
|
94 |
|
112 |
/** |
95 |
/** |
113 |
* |
96 |
* |
114 |
* @author John Baker |
97 |
* @author John Baker |
115 |
*/ |
98 |
*/ |
116 |
public class SQLHistoryPanel extends javax.swing.JPanel { |
99 |
public class SQLHistoryPanel extends javax.swing.JPanel { |
|
|
100 |
|
101 |
private TableRowSorter<HistoryTableModel> rowSorter; |
102 |
private HistoryTableModel htm = new HistoryTableModel(); |
117 |
public static final String SAVE_STATEMENTS_CLEARED = ""; // NOI18N |
103 |
public static final String SAVE_STATEMENTS_CLEARED = ""; // NOI18N |
118 |
public static final int SAVE_STATEMENTS_MAX_LIMIT = 10000; |
|
|
119 |
public static final int TABLE_DATA_WIDTH_SQL = 125; |
120 |
public static final Logger LOGGER = Logger.getLogger(SQLHistoryPanel.class.getName()); |
104 |
public static final Logger LOGGER = Logger.getLogger(SQLHistoryPanel.class.getName()); |
121 |
private static final FileObject USERDIR = FileUtil.getConfigRoot(); |
|
|
122 |
private static final FileObject historyRoot = USERDIR.getFileObject(SQLHistoryManager.SQL_HISTORY_FOLDER); |
123 |
private static Object[][] data; |
124 |
private Set<String> currentConnections = new HashSet<String>(); |
125 |
private SQLHistoryView view; |
126 |
private JEditorPane editorPane; |
105 |
private JEditorPane editorPane; |
127 |
private Map<String,String> urlAliasMap = new HashMap<String, String>(); |
106 |
final private ListSelectionModel sqlTableSelektion; |
128 |
private Map<String,String> aliasUrlMap = new HashMap<String, String>(); |
107 |
final private DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); |
129 |
private static RequestProcessor RP = new RequestProcessor(SQLHistoryPanel.class); |
|
|
130 |
private final Task initTask; |
131 |
|
108 |
|
132 |
/** Creates new form SQLHistoryPanel */ |
109 |
/** Creates new form SQLHistoryPanel */ |
133 |
public SQLHistoryPanel(final JEditorPane editorPane) { |
110 |
public SQLHistoryPanel(final JEditorPane editorPane) { |
134 |
this.editorPane = editorPane; |
111 |
this.editorPane = editorPane; |
135 |
|
112 |
|
136 |
// dummy model |
113 |
initComponents(); |
137 |
view = new SQLHistoryView(new SQLHistoryModel() { |
114 |
|
|
|
115 |
rowSorter = new TableRowSorter<HistoryTableModel>(htm); |
116 |
sqlHistoryTable.setRowSorter(rowSorter); |
117 |
List<SortKey> sortKeys = new ArrayList<SortKey>(); |
118 |
sortKeys.add(new SortKey(2, SortOrder.DESCENDING)); |
119 |
rowSorter.setSortKeys(sortKeys); |
120 |
rowSorter.setSortsOnUpdates(true); |
121 |
rowSorter.sort(); |
122 |
|
123 |
sqlTableSelektion = sqlHistoryTable.getSelectionModel(); |
124 |
|
125 |
updateURLList(); |
126 |
|
127 |
htm.addTableModelListener(new TableModelListener() { |
138 |
|
128 |
|
139 |
@Override |
129 |
@Override |
140 |
public void initialize() { |
130 |
public void tableChanged(TableModelEvent e) { |
|
|
131 |
updateURLList(); |
132 |
} |
133 |
}); |
134 |
|
135 |
searchTextField.getDocument().addDocumentListener(new DocumentListener() { |
136 |
|
137 |
@Override |
138 |
public void insertUpdate(DocumentEvent e) { |
139 |
updateFilter(); |
141 |
} |
140 |
} |
142 |
|
141 |
|
143 |
@Override |
142 |
@Override |
144 |
public void setFilter(String filter) { |
143 |
public void removeUpdate(DocumentEvent e) { |
|
|
144 |
updateFilter(); |
145 |
} |
145 |
} |
146 |
|
146 |
|
147 |
@Override |
147 |
@Override |
148 |
public String getFilter() { |
148 |
public void changedUpdate(DocumentEvent e) { |
149 |
return ""; // NOI18N |
149 |
updateFilter(); |
150 |
} |
|
|
151 |
|
152 |
@Override |
153 |
public List<SQLHistory> getSQLHistoryList() throws SQLHistoryException { |
154 |
return Collections.emptyList(); |
155 |
} |
156 |
|
157 |
@Override |
158 |
public void setSQLHistoryList(List<SQLHistory> sqlHistoryList) { |
159 |
} |
150 |
} |
160 |
}); |
151 |
}); |
161 |
|
152 |
|
162 |
initSQLHistoryTableData(); |
153 |
sqlHistoryTable.addMouseListener(new MouseAdapter() { |
163 |
initComponents(); |
|
|
164 |
setupSQLSaveLimit(); |
165 |
initTask = RP.post(new Runnable() { |
166 |
|
154 |
|
167 |
@Override |
155 |
@Override |
168 |
public void run() { |
|
|
169 |
lazyInit(); |
170 |
} |
171 |
}); |
172 |
} |
173 |
|
174 |
public void lazyInit() { |
175 |
view = new SQLHistoryView(new SQLHistoryModelImpl()); |
176 |
for (DatabaseConnection existingConnection : ConnectionManager.getDefault().getConnections()) { |
177 |
urlAliasMap.put(existingConnection.getDatabaseURL(), existingConnection.getDisplayName()); |
178 |
aliasUrlMap.put(existingConnection.getDisplayName(), existingConnection.getDatabaseURL()); |
179 |
} |
180 |
// Adjust table column width |
181 |
SwingUtilities.invokeLater(new Runnable() { |
182 |
|
183 |
@Override |
184 |
public void run() { |
185 |
setWaitingState(false); |
186 |
initSQLHistoryTableData(); |
187 |
initComponentData(); |
188 |
adjustColumnPreferredWidths(sqlHistoryTable); |
189 |
sqlHistoryTable.revalidate(); |
190 |
} |
191 |
}); |
192 |
} |
193 |
|
194 |
@Override |
195 |
public void addNotify() { |
196 |
super.addNotify(); |
197 |
//show progress for initialize method |
198 |
final Window w = findWindowParent(); |
199 |
if (w != null) { |
200 |
w.addWindowListener(new WindowAdapter() { |
201 |
|
202 |
@Override |
203 |
public void windowOpened(WindowEvent e) { |
204 |
setWaitingState(! initTask.isFinished()); |
205 |
} |
206 |
}); |
207 |
} |
208 |
} |
209 |
|
210 |
private void setWaitingState(boolean waitingState) { |
211 |
boolean enabled = !waitingState; |
212 |
Component parent = getParent(); |
213 |
Component rootPane = getRootPane(); |
214 |
if (parent != null) { |
215 |
parent.setEnabled(enabled); |
216 |
} |
217 |
if (rootPane != null) { |
218 |
if (enabled) { |
219 |
rootPane.setCursor(null); |
220 |
} else { |
221 |
rootPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
222 |
} |
223 |
} |
224 |
} |
225 |
|
226 |
private Window findWindowParent() { |
227 |
Component c = this; |
228 |
while (c != null) { |
229 |
c = c.getParent(); |
230 |
if (c instanceof Window) { |
231 |
return (Window) c; |
232 |
} |
233 |
} |
234 |
return null; |
235 |
} |
236 |
|
237 |
private void setupSQLSaveLimit() { |
238 |
// SQL statments save limit |
239 |
String savedLimit = NbPreferences.forModule(SQLHistoryPanel.class).get("SQL_STATEMENTS_SAVED_FOR_HISTORY", ""); // NOI18N |
240 |
if (null != savedLimit && !savedLimit.equals(SAVE_STATEMENTS_CLEARED)) { |
241 |
sqlLimitTextField.setText(savedLimit); |
242 |
} else { |
243 |
sqlLimitTextField.setText(SQLHistoryManager.SAVE_STATEMENTS_MAX_LIMIT_ENTERED); |
244 |
savedLimit = SQLHistoryManager.SAVE_STATEMENTS_MAX_LIMIT_ENTERED; |
245 |
NbPreferences.forModule(SQLHistoryPanel.class).put("SQL_STATEMENTS_SAVED_FOR_HISTORY", SQLHistoryManager.SAVE_STATEMENTS_MAX_LIMIT_ENTERED); // NOI18N |
246 |
} |
247 |
} |
248 |
|
249 |
private void initComponentData() { |
250 |
searchTextField.getDocument().addDocumentListener((HistoryTableModel) sqlHistoryTable.getModel()); |
251 |
sqlHistoryTable.getColumnModel().getColumn(0).setHeaderValue(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_SQLTableTitle")); |
252 |
sqlHistoryTable.getColumnModel().getColumn(1).setHeaderValue(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_DateTableTitle")); |
253 |
// Add mouse listener to listen for mouse click on the table header so columns can be sorted |
254 |
JTableHeader header = sqlHistoryTable.getTableHeader(); |
255 |
header.addMouseListener(new ColumnListener()); |
256 |
|
257 |
// Add mouse listener for the case when a user double-clicks on a row to insert SQL |
258 |
sqlHistoryTable.addMouseListener(new MouseAdapter() { |
259 |
@Override |
260 |
public void mouseClicked(MouseEvent e) { |
156 |
public void mouseClicked(MouseEvent e) { |
261 |
if (MouseUtils.isDoubleClick(e)) { |
157 |
if (MouseUtils.isDoubleClick(e)) { |
262 |
insertSQL(); |
158 |
insertSQL(); |
263 |
e.consume(); |
159 |
e.consume(); |
|
|
160 |
} |
161 |
} |
162 |
}); |
163 |
|
164 |
sqlTableSelektion.addListSelectionListener( |
165 |
new ListSelectionListener() { |
166 |
|
167 |
@Override |
168 |
public void valueChanged(ListSelectionEvent e) { |
169 |
if (sqlTableSelektion.isSelectionEmpty()) { |
170 |
insertSQLButton.setEnabled(false); |
171 |
} else { |
172 |
insertSQLButton.setEnabled(true); |
173 |
} |
174 |
} |
175 |
}); |
176 |
|
177 |
connectionUrlComboBox.addActionListener(new ActionListener() { |
178 |
|
179 |
@Override |
180 |
public void actionPerformed(ActionEvent e) { |
181 |
updateFilter(); |
182 |
} |
183 |
}); |
184 |
} |
185 |
|
186 |
private void updateFilter() { |
187 |
List<RowFilter<HistoryTableModel, Integer>> rowFilter = new ArrayList<RowFilter<HistoryTableModel, Integer>>(); |
188 |
|
189 |
String url = (String) connectionUrlComboBox.getSelectedItem(); |
190 |
|
191 |
if ( url != null && !url.equals(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_URLComboBoxAllConnectionsItem"))) { |
192 |
rowFilter.add(new EqualsFilter(url, 0)); |
193 |
} |
194 |
|
195 |
if (!searchTextField.getText().equals("")) { |
196 |
rowFilter.add(new ContainsInsensitiveFilter(searchTextField.getText(), 1)); |
197 |
} |
198 |
|
199 |
if (rowFilter.size() > 0) { |
200 |
rowSorter.setRowFilter(RowFilter.andFilter(rowFilter)); |
201 |
} else { |
202 |
rowSorter.setRowFilter(null); |
264 |
} |
203 |
} |
265 |
} |
204 |
} |
266 |
}); |
|
|
267 |
|
205 |
|
268 |
// Initialize sql column data |
206 |
private void updateURLList() { |
269 |
connectionUrlComboBox.addActionListener((HistoryTableModel) sqlHistoryTable.getModel()); |
207 |
List<String> urls = new ArrayList<String>(htm.getJdbcURLs()); |
270 |
view.updateConnectionUrl(); |
208 |
urls.add(0, NbBundle.getMessage(SQLHistoryPanel.class, "LBL_URLComboBoxAllConnectionsItem")); |
271 |
} |
209 |
Object selected = connectionUrlComboBox.getSelectedItem(); |
272 |
|
210 |
connectionUrlComboBox.setModel(new DefaultComboBoxModel(urls.toArray())); |
273 |
private void initSQLHistoryTableData() { |
211 |
if(selected != null && urls.contains(selected)) { |
274 |
if (initTask == null || ! initTask.isFinished()) { |
212 |
connectionUrlComboBox.setSelectedItem(selected); |
275 |
data = new Object[1][2]; |
213 |
} else { |
276 |
data[0][0] = NbBundle.getMessage(SQLHistoryPanel.class, "SQLHistoryPanel_PleaseWait"); |
214 |
connectionUrlComboBox.setSelectedIndex(0); |
277 |
return ; |
|
|
278 |
} |
279 |
// Initialize sql column data |
280 |
List<String> sqlList = view.getSQLList(null); |
281 |
List<String> dateList = view.getDateList(null); |
282 |
data = new Object[sqlList.size()][2]; |
283 |
int row = 0; |
284 |
int maxLength; |
285 |
int length; |
286 |
for (String sql : sqlList) { |
287 |
length = sql.trim().length(); |
288 |
maxLength = length > TABLE_DATA_WIDTH_SQL ? TABLE_DATA_WIDTH_SQL : length; |
289 |
data[row][0] = sql.trim().substring(0, maxLength).replaceAll("\n", " "); |
290 |
row++; |
291 |
} |
292 |
// Initialize data |
293 |
row = 0; |
294 |
for (String date : dateList) { |
295 |
data[row][1] = date; |
296 |
row++; |
297 |
} |
215 |
} |
298 |
} |
216 |
} |
299 |
|
217 |
|
Lines 318-324
Link Here
|
318 |
Component c = super.prepareRenderer(renderer, rowIndex, vColIndex); |
236 |
Component c = super.prepareRenderer(renderer, rowIndex, vColIndex); |
319 |
if (c instanceof JComponent) { |
237 |
if (c instanceof JComponent) { |
320 |
JComponent jc = (JComponent)c; |
238 |
JComponent jc = (JComponent)c; |
321 |
jc.setToolTipText(view.getSQLHistoryTooltipValue(rowIndex, vColIndex)); |
239 |
jc.setToolTipText(getSQLHistoryTooltipValue(sqlHistoryTable, rowIndex, vColIndex)); |
322 |
} |
240 |
} |
323 |
return c; |
241 |
return c; |
324 |
} |
242 |
} |
Lines 330-337
Link Here
|
330 |
|
248 |
|
331 |
jLabel1.setText(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_Connection")); // NOI18N |
249 |
jLabel1.setText(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_Connection")); // NOI18N |
332 |
|
250 |
|
333 |
connectionUrlComboBox.setRenderer(new ConnectionUrlRenderer()); |
|
|
334 |
|
335 |
jLabel2.setLabelFor(searchTextField); |
251 |
jLabel2.setLabelFor(searchTextField); |
336 |
org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_Match")); // NOI18N |
252 |
org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_Match")); // NOI18N |
337 |
|
253 |
|
Lines 345-360
Link Here
|
345 |
} |
261 |
} |
346 |
}); |
262 |
}); |
347 |
|
263 |
|
348 |
sqlHistoryTable.setModel(new HistoryTableModel()); |
264 |
sqlHistoryTable.setAutoCreateColumnsFromModel(false); |
349 |
sqlHistoryTable.setGridColor(java.awt.Color.lightGray); |
265 |
sqlHistoryTable.setModel(htm); |
350 |
jScrollPane1.setViewportView(sqlHistoryTable); |
266 |
jScrollPane1.setViewportView(sqlHistoryTable); |
351 |
sqlHistoryTable.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSN_History")); // NOI18N |
267 |
sqlHistoryTable.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSN_History")); // NOI18N |
352 |
sqlHistoryTable.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSD_History")); // NOI18N |
268 |
sqlHistoryTable.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSD_History")); // NOI18N |
|
|
269 |
int dateColumnWidth1 = new JTextField(dateTimeFormat.format(new Date())).getPreferredSize().width; |
270 |
int dateColumnWidth2 = new JTextField(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_DateTableTitle") + "XXXXX").getPreferredSize().width; |
271 |
int dateColumnWidth = Math.max(dateColumnWidth1, dateColumnWidth2); |
272 |
|
273 |
TableColumnModel sqlHistoryTableTCM = sqlHistoryTable.getColumnModel(); |
274 |
|
275 |
TableColumn sqlHistoryTableColumn; |
276 |
|
277 |
sqlHistoryTableColumn = new TableColumn(1); |
278 |
sqlHistoryTableColumn.setHeaderValue(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_SQLTableTitle")); |
279 |
|
280 |
sqlHistoryTableTCM.addColumn(sqlHistoryTableColumn); |
281 |
|
282 |
sqlHistoryTableColumn = new TableColumn(2); |
283 |
sqlHistoryTableColumn.setMinWidth(dateColumnWidth); |
284 |
sqlHistoryTableColumn.setPreferredWidth(dateColumnWidth); |
285 |
sqlHistoryTableColumn.setMaxWidth(dateColumnWidth); |
286 |
sqlHistoryTableColumn.setHeaderValue(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_DateTableTitle")); |
287 |
sqlHistoryTableColumn.setCellRenderer(new DefaultTableCellRenderer() { |
288 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
289 |
if(value instanceof Date) { |
290 |
value = dateTimeFormat.format((Date) value); |
291 |
} |
292 |
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
293 |
} |
294 |
}); |
295 |
|
296 |
sqlHistoryTableTCM.addColumn(sqlHistoryTableColumn); |
353 |
|
297 |
|
354 |
sqlLimitLabel.setLabelFor(sqlLimitTextField); |
298 |
sqlLimitLabel.setLabelFor(sqlLimitTextField); |
355 |
org.openide.awt.Mnemonics.setLocalizedText(sqlLimitLabel, org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_SqlLimit")); // NOI18N |
299 |
org.openide.awt.Mnemonics.setLocalizedText(sqlLimitLabel, org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_SqlLimit")); // NOI18N |
356 |
|
300 |
|
357 |
sqlLimitTextField.setText(SQLHistoryManager.SAVE_STATEMENTS_MAX_LIMIT_ENTERED); |
301 |
sqlLimitTextField.setText(Integer.toString(SQLHistoryManager.getInstance().getListSize())); |
358 |
sqlLimitTextField.setMinimumSize(new java.awt.Dimension(18, 22)); |
302 |
sqlLimitTextField.setMinimumSize(new java.awt.Dimension(18, 22)); |
359 |
|
303 |
|
360 |
org.openide.awt.Mnemonics.setLocalizedText(sqlLimitButton, org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_ApplyButton")); // NOI18N |
304 |
org.openide.awt.Mnemonics.setLocalizedText(sqlLimitButton, org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_ApplyButton")); // NOI18N |
Lines 378-388
Link Here
|
378 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
322 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
379 |
.addGroup(layout.createSequentialGroup() |
323 |
.addGroup(layout.createSequentialGroup() |
380 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
324 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
381 |
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 655, Short.MAX_VALUE) |
325 |
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE) |
382 |
.addGroup(layout.createSequentialGroup() |
326 |
.addGroup(layout.createSequentialGroup() |
383 |
.addComponent(jLabel1) |
327 |
.addComponent(jLabel1) |
384 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) |
328 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) |
385 |
.addComponent(connectionUrlComboBox, 0, 306, Short.MAX_VALUE) |
329 |
.addComponent(connectionUrlComboBox, 0, 301, Short.MAX_VALUE) |
386 |
.addGap(18, 18, 18) |
330 |
.addGap(18, 18, 18) |
387 |
.addComponent(jLabel2) |
331 |
.addComponent(jLabel2) |
388 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) |
332 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) |
Lines 413-419
Link Here
|
413 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
357 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
414 |
.addComponent(insertSQLButton) |
358 |
.addComponent(insertSQLButton) |
415 |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
359 |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
416 |
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE) |
360 |
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE) |
417 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
361 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
418 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
362 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) |
419 |
.addComponent(sqlLimitTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) |
363 |
.addComponent(sqlLimitTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) |
Lines 432-437
Link Here
|
432 |
insertSQLButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSD_Insert")); // NOI18N |
376 |
insertSQLButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSD_Insert")); // NOI18N |
433 |
sqlLimitTextField.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSN_Save")); // NOI18N |
377 |
sqlLimitTextField.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSN_Save")); // NOI18N |
434 |
sqlLimitTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSD_Save")); // NOI18N |
378 |
sqlLimitTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSD_Save")); // NOI18N |
|
|
379 |
sqlLimitTextField.setInputVerifier(new InputVerifier() { |
380 |
|
381 |
public boolean verify(JComponent input) { |
382 |
JTextField tf = (JTextField) input; |
383 |
return tf.getText().matches("^\\d+$"); |
384 |
} |
385 |
}); |
435 |
sqlLimitButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSN_Apply")); // NOI18N |
386 |
sqlLimitButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSN_Apply")); // NOI18N |
436 |
sqlLimitButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSD_Apply")); // NOI18N |
387 |
sqlLimitButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "ACSD_Apply")); // NOI18N |
437 |
}// </editor-fold>//GEN-END:initComponents |
388 |
}// </editor-fold>//GEN-END:initComponents |
Lines 444-565
Link Here
|
444 |
verifySQLLimit(); |
395 |
verifySQLLimit(); |
445 |
}//GEN-LAST:event_sqlLimitButtonActionPerformed |
396 |
}//GEN-LAST:event_sqlLimitButtonActionPerformed |
446 |
|
397 |
|
447 |
|
|
|
448 |
private void insertSQL() { |
398 |
private void insertSQL() { |
449 |
try { |
399 |
try { |
450 |
// Make sure to insert the entire SQL, not just what appears in the Table |
400 |
JEditorPane pane = (JEditorPane) EditorRegistry.lastFocusedComponent(); |
451 |
List<SQLHistory> sqlHistoryList = view.getCurrentSQLHistoryList(); |
|
|
452 |
int i = 0; |
453 |
String sqlToInsert = ""; // NOI18N |
454 |
InsertSQLUtility insertUtility = new InsertSQLUtility(); |
455 |
for (SQLHistory sqlHistory : sqlHistoryList) { |
456 |
if (sqlHistoryTable.isRowSelected(i)) { |
457 |
sqlToInsert = sqlHistory.getSql().trim(); |
458 |
JEditorPane pane = (JEditorPane)EditorRegistry.lastFocusedComponent(); |
459 |
String mime = pane.getContentType(); |
401 |
String mime = pane.getContentType(); |
460 |
if (mime.equals(SQLDataLoader.SQL_MIME_TYPE)) { |
402 |
if (mime.equals(SQLDataLoader.SQL_MIME_TYPE)) { |
461 |
editorPane = pane; |
403 |
editorPane = pane; |
462 |
} |
404 |
} |
463 |
insertUtility.insert(sqlToInsert, editorPane); |
405 |
int min = sqlTableSelektion.getMinSelectionIndex(); |
|
|
406 |
int max = sqlTableSelektion.getMaxSelectionIndex(); |
407 |
for (int i = min; i <= max; i++) { |
408 |
if (sqlHistoryTable.isRowSelected(i)) { |
409 |
int modelIndex = sqlHistoryTable.convertRowIndexToModel(i); |
410 |
String sql = ((String) htm.getValueAt(modelIndex, 1)).trim(); |
411 |
insertIntoDocument(sql, editorPane); |
464 |
} |
412 |
} |
465 |
// increment for the next row |
|
|
466 |
i++; |
467 |
} |
413 |
} |
468 |
|
|
|
469 |
} catch (BadLocationException ex) { |
414 |
} catch (BadLocationException ex) { |
470 |
Exceptions.printStackTrace(ex); |
415 |
Exceptions.printStackTrace(ex); |
471 |
} |
416 |
} |
472 |
} |
417 |
} |
473 |
|
418 |
|
474 |
private void verifySQLLimit() { |
419 |
private void verifySQLLimit() { |
475 |
String enteredLimit = sqlLimitTextField.getText(); |
420 |
String enteredLimitString = sqlLimitTextField.getText(); |
476 |
int iLimit = 0; |
421 |
String currentLimit = Integer.toString(SQLHistoryManager.getInstance().getListSize()); |
477 |
if (enteredLimit.equals(SAVE_STATEMENTS_CLEARED)) { |
422 |
String maxLimit = Integer.toString(SQLHistoryManager.MAX_SQL_STATEMENTS_SAVED_FOR_HISTORY); |
478 |
updateSaveLimitUponClear(iLimit); |
423 |
if (enteredLimitString.equals(SAVE_STATEMENTS_CLEARED)) { |
479 |
inputWarningLabel.setText(""); // NOI18N |
424 |
sqlLimitTextField.setText(currentLimit); |
480 |
} else { // user enters a value to limit the number of SQL statements to save |
425 |
return; |
481 |
updateSaveLimitUponReset(enteredLimit); |
426 |
} |
|
|
427 |
try { |
428 |
Integer enteredLimit = Integer.valueOf(enteredLimitString); |
429 |
if (enteredLimit > SQLHistoryManager.MAX_SQL_STATEMENTS_SAVED_FOR_HISTORY) { |
430 |
inputWarningLabel.setText(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_NumberInputWarningLabel")); |
431 |
sqlLimitTextField.setText(maxLimit); |
432 |
SQLHistoryManager.getInstance().setListSize(SQLHistoryManager.MAX_SQL_STATEMENTS_SAVED_FOR_HISTORY); |
433 |
} else { |
434 |
inputWarningLabel.setText(""); // NOI18N |
435 |
SQLHistoryManager.getInstance().setListSize(enteredLimit); |
436 |
} |
437 |
} catch (NumberFormatException ex) { |
438 |
inputWarningLabel.setText(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_NumberInputWarningLabel")); |
439 |
sqlLimitTextField.setText(currentLimit); |
482 |
} |
440 |
} |
483 |
} |
441 |
htm.refresh(); |
|
|
442 |
} |
484 |
|
443 |
|
485 |
private void updateSaveLimitUponClear(int iLimit) { |
444 |
public String getSQLHistoryTooltipValue(JTable historyTable, int row, int col) { |
486 |
iLimit = SAVE_STATEMENTS_MAX_LIMIT; |
445 |
HistoryTableModel historyTableModel = (HistoryTableModel) historyTable.getModel(); |
487 |
List<SQLHistory> sqlHistoryList = new ArrayList<SQLHistory>(); |
446 |
int modelColumn = historyTable.convertColumnIndexToModel(col); |
488 |
try { |
447 |
int modelRow = historyTable.convertRowIndexToModel(row); |
489 |
view.setSQLHistoryList(SQLHistoryPersistenceManager.getInstance().updateSQLSaved(iLimit, historyRoot)); |
448 |
|
490 |
sqlHistoryList = SQLHistoryPersistenceManager.getInstance().retrieve(historyRoot); |
449 |
if (String.class.isAssignableFrom(historyTableModel.getColumnClass(modelColumn))) { |
491 |
view.setCurrentSQLHistoryList(sqlHistoryList); |
450 |
String data = (String) historyTableModel.getValueAt(modelRow, modelColumn); |
492 |
} catch (ClassNotFoundException ex) { |
451 |
return "<html>" + data.trim().replace("\n", "<br>") + "</html>"; // NOI18N |
493 |
Exceptions.printStackTrace(ex); |
452 |
} else if (Date.class.isAssignableFrom(historyTableModel.getColumnClass(modelColumn))) { |
494 |
} catch (SQLHistoryException ex) { |
453 |
Date data = (Date) historyTableModel.getValueAt(modelRow, modelColumn); |
495 |
handleSQLHistoryException(); |
454 |
return DateFormat.getInstance().format(data); |
496 |
} |
|
|
497 |
((HistoryTableModel) sqlHistoryTable.getModel()).refreshTable(sqlHistoryList); |
498 |
NbPreferences.forModule(SQLHistoryPanel.class).put("SQL_STATEMENTS_SAVED_FOR_HISTORY", Integer.toString(iLimit)); // NOI18N |
499 |
sqlLimitTextField.setText(SQLHistoryManager.SAVE_STATEMENTS_MAX_LIMIT_ENTERED); |
500 |
} |
501 |
|
502 |
private void updateSaveLimitUponReset(String enteredLimit) { |
503 |
try { |
504 |
int iLimit; |
505 |
if (enteredLimit.trim().length() < 10) { |
506 |
iLimit = Integer.parseInt(enteredLimit); |
507 |
} else { |
455 |
} else { |
508 |
// too long number |
456 |
return null; |
509 |
iLimit = SAVE_STATEMENTS_MAX_LIMIT + 1; |
|
|
510 |
} |
511 |
String savedLimit = NbPreferences.forModule(SQLHistoryPanel.class).get("SQL_STATEMENTS_SAVED_FOR_HISTORY", SAVE_STATEMENTS_CLEARED); // NOI18N |
512 |
if (iLimit < 0) { |
513 |
inputWarningLabel.setText(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_TextInputWarningLabel")); |
514 |
if (savedLimit != null) { |
515 |
sqlLimitTextField.setText(savedLimit); |
516 |
} else { |
517 |
sqlLimitTextField.setText(SQLHistoryManager.SAVE_STATEMENTS_MAX_LIMIT_ENTERED); |
518 |
} |
519 |
} else if (iLimit > SAVE_STATEMENTS_MAX_LIMIT) { |
520 |
sqlLimitButton.setEnabled(true); |
521 |
inputWarningLabel.setText(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_NumberInputWarningLabel")); |
522 |
// reset user's input |
523 |
if (savedLimit != null) { |
524 |
sqlLimitTextField.setText(savedLimit); |
525 |
} else { |
526 |
sqlLimitTextField.setText(SAVE_STATEMENTS_CLEARED); |
527 |
sqlLimitTextField.setText(SQLHistoryManager.SAVE_STATEMENTS_MAX_LIMIT_ENTERED); |
528 |
} |
529 |
} else { |
530 |
inputWarningLabel.setText(""); // NOI18N |
531 |
if (SQLHistoryPersistenceManager.getInstance().updateSQLSaved(iLimit, historyRoot).size() > 0) { |
532 |
List<SQLHistory> sqlHistoryList = SQLHistoryPersistenceManager.getInstance().retrieve(historyRoot); |
533 |
view.setCurrentSQLHistoryList(sqlHistoryList); |
534 |
((HistoryTableModel) sqlHistoryTable.getModel()).refreshTable(sqlHistoryList); |
535 |
view.updateConnectionUrl(); |
536 |
NbPreferences.forModule(SQLHistoryPanel.class).put("SQL_STATEMENTS_SAVED_FOR_HISTORY", Integer.toString(iLimit)); // NOI18N |
537 |
} |
457 |
} |
538 |
} |
458 |
} |
539 |
} catch (ClassNotFoundException ex) { |
459 |
|
540 |
Exceptions.printStackTrace(ex); |
460 |
private int insertIntoDocument(String s, JEditorPane target) |
541 |
} catch (SQLHistoryException ex) { |
461 |
throws BadLocationException { |
542 |
handleSQLHistoryException(); |
462 |
Document doc = target.getDocument(); |
543 |
} catch (NumberFormatException ne) { |
463 |
if (s == null) { |
544 |
inputWarningLabel.setText(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_TextInputWarningLabel")); |
464 |
s = ""; |
545 |
// reset user's input |
|
|
546 |
String savedLimit = NbPreferences.forModule(SQLHistoryPanel.class).get("SQL_STATEMENTS_SAVED_FOR_HISTORY", ""); // NOI18N |
547 |
if (savedLimit != null) { |
548 |
sqlLimitTextField.setText(savedLimit); |
549 |
} else { |
550 |
sqlLimitTextField.setText(SQLHistoryManager.SAVE_STATEMENTS_MAX_LIMIT_ENTERED); |
551 |
} |
465 |
} |
|
|
466 |
if (doc == null) { |
467 |
return -1; |
552 |
} |
468 |
} |
|
|
469 |
int start = -1; |
470 |
try { |
471 |
Caret caret = target.getCaret(); |
472 |
int p0 = Math.min(caret.getDot(), caret.getMark()); |
473 |
int p1 = Math.max(caret.getDot(), caret.getMark()); |
474 |
doc.remove(p0, p1 - p0); |
475 |
start = caret.getDot(); |
476 |
doc.insertString(start, s + ";\n", null); // NOI18N |
477 |
} catch (BadLocationException ble) { |
478 |
LOGGER.log(Level.WARNING, org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_InsertAtLocationError") + ble); |
553 |
} |
479 |
} |
554 |
|
480 |
return start; |
555 |
private void handleSQLHistoryException() { |
|
|
556 |
LOGGER.log(Level.WARNING, NbBundle.getMessage(SQLHistoryPanel.class, "LBL_ErrorParsingSQLHistory")); |
557 |
List<SQLHistory> sqlHistoryList = SQLHistoryPersistenceManager.getInstance().retrieve(); |
558 |
view.setCurrentSQLHistoryList(sqlHistoryList); |
559 |
((HistoryTableModel) sqlHistoryTable.getModel()).refreshTable(sqlHistoryList); |
560 |
view.updateConnectionUrl(); |
561 |
} |
481 |
} |
562 |
|
|
|
563 |
// Variables declaration - do not modify//GEN-BEGIN:variables |
482 |
// Variables declaration - do not modify//GEN-BEGIN:variables |
564 |
private javax.swing.JComboBox connectionUrlComboBox; |
483 |
private javax.swing.JComboBox connectionUrlComboBox; |
565 |
private javax.swing.JLabel inputWarningLabel; |
484 |
private javax.swing.JLabel inputWarningLabel; |
Lines 574-773
Link Here
|
574 |
private javax.swing.JTextField sqlLimitTextField; |
493 |
private javax.swing.JTextField sqlLimitTextField; |
575 |
// End of variables declaration//GEN-END:variables |
494 |
// End of variables declaration//GEN-END:variables |
576 |
|
495 |
|
577 |
private class SQLHistoryView { |
496 |
private final class HistoryTableModel extends AbstractTableModel { |
578 |
private SQLHistoryModel model; |
|
|
579 |
List<SQLHistory> sqlHistoryList = new ArrayList<SQLHistory>(); |
580 |
List<SQLHistory> currentSQLHistoryList = new ArrayList<SQLHistory>(); |
581 |
public static final String MATCH_EMPTY = ""; // NOI18N |
582 |
|
583 |
public SQLHistoryView(SQLHistoryModel model) { |
584 |
this.model = model; |
585 |
init(); |
586 |
} |
587 |
|
497 |
|
588 |
private void init() { |
498 |
private List<SQLHistoryEntry> sqlList; |
589 |
try { |
499 |
private List<String> jdbcURLs = new ArrayList<String>(); |
590 |
this.sqlHistoryList = model.getSQLHistoryList(); |
500 |
private SQLHistoryManager shm = SQLHistoryManager.getInstance(); |
591 |
this.currentSQLHistoryList = model.getSQLHistoryList(); |
|
|
592 |
} catch (SQLHistoryException ex) { |
593 |
LOGGER.log(Level.INFO, NbBundle.getMessage(SQLHistoryPanel.class, "LBL_ErrorParsingSQLHistory"), ex); // NOI18N |
594 |
sqlHistoryList = SQLHistoryPersistenceManager.getInstance().retrieve(); |
595 |
setCurrentSQLHistoryList(sqlHistoryList); |
596 |
} |
597 |
} |
598 |
|
599 |
public void setCurrentSQLHistoryList(List<SQLHistory> sqlHistoryList) { |
600 |
currentSQLHistoryList = sqlHistoryList; |
601 |
} |
602 |
|
603 |
public List<SQLHistory> getCurrentSQLHistoryList() { |
604 |
return currentSQLHistoryList; |
605 |
} |
606 |
|
607 |
public List<SQLHistory> getSQLHistoryList() { |
608 |
return sqlHistoryList; |
609 |
} |
610 |
|
611 |
public void setSQLHistoryList(List<SQLHistory> sqlHistoryList) { |
612 |
this.sqlHistoryList = sqlHistoryList; |
613 |
} |
614 |
|
615 |
/** |
616 |
* Get the SQL statement string at the row,col position in the table and convert the string to html |
617 |
* @param row - table row |
618 |
* @param col - table column |
619 |
* @return - formatted SQL statement for the specified row, col |
620 |
*/ |
621 |
public String getSQLHistoryTooltipValue(int row, int col) { |
622 |
List<SQLHistory> sqlHistoryListForTooltip = view.getCurrentSQLHistoryList(); |
623 |
if (sqlHistoryListForTooltip != null && row < sqlHistoryListForTooltip.size()) { |
624 |
if (col == 0) { |
625 |
String sqlRow = sqlHistoryListForTooltip.get(row).getSql().trim(); |
626 |
while (sqlRow.indexOf("\n") != -1) { // NOI18N |
627 |
sqlRow = replace(sqlRow, "\n", "<br>"); // NOI18N |
628 |
} |
629 |
return "<html>" + sqlRow + "</html>"; // NOI18N |
630 |
} else { |
631 |
return DateFormat.getInstance().format(sqlHistoryListForTooltip.get(row).getDate()); |
632 |
} |
633 |
} else { |
634 |
return null; |
635 |
} |
636 |
} |
637 |
|
638 |
/** |
639 |
* Convert sql statement to html for proper rendering in the table's tooltip |
640 |
* @param target - original string |
641 |
* @param from - string to replace |
642 |
* @param to - string to replace with |
643 |
* @return - updated string |
644 |
*/ |
645 |
public String replace(String target, String from, String to) { |
646 |
int start = target.indexOf(from); |
647 |
if (start == -1) { |
648 |
return target; |
649 |
} |
650 |
int lf = from.length(); |
651 |
char[] targetChars = target.toCharArray(); |
652 |
StringBuilder buffer = new StringBuilder(); |
653 |
int copyFrom = 0; |
654 |
while (start != -1) { |
655 |
buffer.append(targetChars, copyFrom, start - copyFrom); |
656 |
buffer.append(to); |
657 |
copyFrom = start + lf; |
658 |
start = target.indexOf(from, copyFrom); |
659 |
} |
660 |
buffer.append(targetChars, copyFrom, targetChars.length - copyFrom); |
661 |
return buffer.toString(); |
662 |
} |
663 |
|
501 |
|
664 |
public List<String> getSQLList(List<SQLHistory> sqlHistoryList) { |
502 |
public HistoryTableModel() { |
665 |
List<String> sqlList = new ArrayList<String>(); |
503 |
refresh(); |
666 |
if (sqlHistoryList == null) { |
|
|
667 |
sqlHistoryList = getSQLHistoryList(); |
668 |
} |
504 |
} |
669 |
|
505 |
|
670 |
for (SQLHistory sqlHistory : sqlHistoryList) { |
506 |
public void refresh() { |
671 |
String sql = sqlHistory.getSql(); |
507 |
sqlList = new ArrayList<SQLHistoryEntry>(shm.getSQLHistory()); |
672 |
if (!sqlList.contains(sql)) { |
508 |
for (SQLHistoryEntry sqe : sqlList) { |
673 |
sqlList.add(sql); |
509 |
String url = sqe.getUrl(); |
|
|
510 |
if (!jdbcURLs.contains(url)) { |
511 |
jdbcURLs.add(url); |
674 |
} |
512 |
} |
675 |
} |
513 |
} |
676 |
return sqlList; |
514 |
fireTableDataChanged(); |
677 |
} |
515 |
} |
678 |
|
516 |
|
679 |
public List<String> getDateList(List<SQLHistory> sqlHistoryList) { |
|
|
680 |
List<String> dateList = new ArrayList<String>(); |
681 |
List<String> sqlList = new ArrayList<String>(); |
682 |
if (sqlHistoryList == null) { |
683 |
sqlHistoryList = getSQLHistoryList(); |
684 |
} |
685 |
|
686 |
for (SQLHistory sqlHistory : sqlHistoryList) { |
687 |
String date = DateFormat.getInstance().format(sqlHistory.getDate()); |
688 |
String sql = sqlHistory.getSql(); |
689 |
// need to make sure that the date is the one that belongs with the SQL |
690 |
if (!sqlList.contains(sql)) { |
691 |
sqlList.add(sql); |
692 |
dateList.add(date); |
693 |
} |
694 |
} |
695 |
return dateList; |
696 |
} |
697 |
|
698 |
public void updateConnectionUrl() { |
699 |
// Initialize combo box data |
700 |
currentConnections.clear(); |
701 |
// Set default item in the combo box |
702 |
String defaultSelectedItem = NbBundle.getMessage(SQLHistoryPanel.class, "LBL_URLComboBoxAllConnectionsItem"); |
703 |
currentConnections.add(defaultSelectedItem); |
704 |
|
705 |
for (SQLHistory sqlHistory : sqlHistoryList) { |
706 |
String url = sqlHistory.getUrl(); |
707 |
if (urlAliasMap.containsKey(url)) { |
708 |
// add connection display name |
709 |
currentConnections.add(urlAliasMap.get(url)); |
710 |
} else { |
711 |
// add URL |
712 |
currentConnections.add(url); |
713 |
} |
714 |
} |
715 |
// Initialize combo box |
716 |
connectionUrlComboBox.setModel(new DefaultComboBoxModel(currentConnections.toArray())); |
717 |
connectionUrlComboBox.setSelectedItem(defaultSelectedItem); |
718 |
connectionUrlComboBox.revalidate(); |
719 |
} |
720 |
|
721 |
private List<SQLHistory> filterSQLHistoryList() { |
722 |
List<SQLHistory> filteredSqlHistoryList = new ArrayList<SQLHistory>(); |
723 |
String match = searchTextField.getText(); |
724 |
String url = (String)connectionUrlComboBox.getSelectedItem(); |
725 |
if (aliasUrlMap.containsKey(url)) { |
726 |
url = aliasUrlMap.get(url); |
727 |
} |
728 |
// modify list of SQL to reflect a selection from the Connection dropdown or if a match text entered |
729 |
for (SQLHistory sqlHistory : sqlHistoryList) { |
730 |
if (sqlHistory.getUrl().equals(url) || url.equals(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_URLComboBoxAllConnectionsItem"))) { |
731 |
if (!match.equals(MATCH_EMPTY)) { |
732 |
if (sqlHistory.getSql().toLowerCase().indexOf(match.toLowerCase()) != -1) { |
733 |
filteredSqlHistoryList.add(sqlHistory); |
734 |
} |
735 |
} else { |
736 |
filteredSqlHistoryList.add(sqlHistory); |
737 |
} |
738 |
} |
739 |
} |
740 |
currentSQLHistoryList = filteredSqlHistoryList; |
741 |
return filteredSqlHistoryList; |
742 |
} |
743 |
} |
744 |
|
745 |
private final class HistoryTableModel extends DefaultTableModel implements ActionListener, DocumentListener { |
746 |
List<String> sqlList; |
747 |
List<String> dateList; |
748 |
int sortCol = 1; |
749 |
boolean sortAsc = false; |
750 |
|
751 |
@Override |
517 |
@Override |
752 |
public int getRowCount() { |
518 |
public int getRowCount() { |
753 |
if (sqlHistoryTable.getSelectedRow() == -1) { |
519 |
return sqlList.size(); |
754 |
insertSQLButton.setEnabled(false); |
|
|
755 |
} |
756 |
return data.length; |
757 |
} |
520 |
} |
758 |
|
521 |
|
759 |
@Override |
522 |
@Override |
760 |
public int getColumnCount() { |
523 |
public int getColumnCount() { |
761 |
return 2; |
524 |
return 4; |
762 |
} |
525 |
} |
763 |
|
526 |
|
764 |
@Override |
527 |
@Override |
765 |
public Class<?> getColumnClass(int c) { |
528 |
public Class<?> getColumnClass(int c) { |
766 |
Object value = getValueAt(0, c); |
529 |
switch (c) { |
767 |
if (value == null) { |
530 |
case 0: |
768 |
return String.class; |
531 |
return String.class; |
769 |
} else { |
532 |
case 1: |
770 |
return getValueAt(0, c).getClass(); |
533 |
return String.class; |
|
|
534 |
case 2: |
535 |
return Date.class; |
536 |
case 3: |
537 |
return SQLHistoryEntry.class; |
538 |
default: |
539 |
return Object.class; |
771 |
} |
540 |
} |
772 |
} |
541 |
} |
773 |
|
542 |
|
Lines 778-1127
Link Here
|
778 |
|
547 |
|
779 |
@Override |
548 |
@Override |
780 |
public Object getValueAt(int row, int col) { |
549 |
public Object getValueAt(int row, int col) { |
781 |
if (sqlHistoryTable.isRowSelected(row)) { |
550 |
switch (col) { |
782 |
insertSQLButton.setEnabled(true); |
551 |
case 0: |
|
|
552 |
return sqlList.get(row).getUrl(); |
553 |
case 1: |
554 |
return sqlList.get(row).getSql(); |
555 |
case 2: |
556 |
return sqlList.get(row).getDate(); |
557 |
case 3: |
558 |
return sqlList.get(row); |
559 |
default: |
560 |
return null; |
783 |
} |
561 |
} |
784 |
return data[row][col]; |
|
|
785 |
} |
562 |
} |
786 |
|
563 |
|
787 |
@Override |
564 |
@Override |
788 |
public void setValueAt(Object value, int row, int col) { |
565 |
public void setValueAt(Object value, int row, int col) { |
789 |
adjustColumnPreferredWidths(sqlHistoryTable); |
566 |
throw new NotImplementedException(); |
790 |
fireTableCellUpdated(row, col); |
567 |
} |
|
|
568 |
|
569 |
public List<String> getJdbcURLs() { |
570 |
return jdbcURLs; |
571 |
} |
572 |
} |
573 |
|
574 |
private class EqualsFilter extends RowFilter<HistoryTableModel, Integer> { |
575 |
|
576 |
private String referenz; |
577 |
private int referenzColumn; |
578 |
|
579 |
public EqualsFilter(String referenz, int referenzColumn) { |
580 |
this.referenz = referenz; |
581 |
this.referenzColumn = referenzColumn; |
582 |
} |
583 |
|
584 |
@Override |
585 |
public boolean include(Entry<? extends HistoryTableModel, ? extends Integer> entry) { |
586 |
return ((String) entry.getModel().getValueAt(entry.getIdentifier(), referenzColumn)).equals(referenz); |
587 |
} |
588 |
}; |
589 |
|
590 |
private class ContainsInsensitiveFilter extends RowFilter<HistoryTableModel, Integer> { |
591 |
|
592 |
private String referenz; |
593 |
private int referenzColumn; |
594 |
|
595 |
public ContainsInsensitiveFilter(String referenz, int referenzColumn) { |
596 |
this.referenz = referenz.toLowerCase(); |
597 |
this.referenzColumn = referenzColumn; |
791 |
} |
598 |
} |
792 |
|
599 |
|
793 |
@Override |
600 |
@Override |
794 |
public void addTableModelListener(TableModelListener arg0) { |
601 |
public boolean include(Entry<? extends HistoryTableModel, ? extends Integer> entry) { |
795 |
// not used |
602 |
return ((String) entry.getModel().getValueAt(entry.getIdentifier(), referenzColumn)).toLowerCase().contains(referenz); |
796 |
} |
|
|
797 |
|
798 |
@Override |
799 |
public void removeTableModelListener(TableModelListener arg0) { |
800 |
// not used |
801 |
} |
802 |
|
803 |
public void adjustColumnPreferredWidths(JTable table) { |
804 |
// Get max width for cells in column and make that the preferred width |
805 |
TableColumnModel columnModel = table.getColumnModel(); |
806 |
for (int col = 0; col < table.getColumnCount(); col++) { |
807 |
|
808 |
int maxwidth = 0; |
809 |
for (int row = 0; row < table.getRowCount(); row++) { |
810 |
TableCellRenderer rend = |
811 |
table.getCellRenderer(row, col); |
812 |
Object value = table.getValueAt(row, col); |
813 |
Component comp = |
814 |
rend.getTableCellRendererComponent(table, |
815 |
value, |
816 |
false, |
817 |
false, |
818 |
row, |
819 |
col); |
820 |
maxwidth = Math.max(comp.getPreferredSize().width, maxwidth); |
821 |
} |
822 |
TableColumn column = columnModel.getColumn(col); |
823 |
column.setPreferredWidth(maxwidth); |
824 |
column.setHeaderRenderer(createDefaultRenderer()); |
825 |
} |
826 |
} |
827 |
|
828 |
@Override |
829 |
public void actionPerformed(ActionEvent evt) { |
830 |
processUpdate(); |
831 |
} |
832 |
|
833 |
public void refreshTable(List<SQLHistory> sqlHistoryList) { |
834 |
if (initTask == null) { |
835 |
return ; |
836 |
} |
837 |
String url; |
838 |
// Get the connection url from the combo box |
839 |
if (sqlHistoryList.size() > 0) { |
840 |
url = connectionUrlComboBox.getSelectedItem().toString(); |
841 |
if (aliasUrlMap.containsKey(url)) { |
842 |
url = aliasUrlMap.get(url); |
843 |
} |
844 |
} else { |
845 |
url = NbBundle.getMessage(SQLHistoryPanel.class, "LBL_URLComboBoxAllConnectionsItem"); |
846 |
} |
847 |
sqlList = new ArrayList<String>(); |
848 |
dateList = new ArrayList<String>(); |
849 |
connectionUrlComboBox.setToolTipText(url); |
850 |
int length; |
851 |
int maxLength; |
852 |
for (SQLHistory sqlHistory : sqlHistoryList) { |
853 |
if (url.equals(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_URLComboBoxAllConnectionsItem")) || |
854 |
url.equals(sqlHistory.getUrl())) { |
855 |
length = sqlHistory.getSql().trim().length(); |
856 |
maxLength = length > TABLE_DATA_WIDTH_SQL ? TABLE_DATA_WIDTH_SQL : length; |
857 |
sqlList.add(sqlHistory.getSql().trim().substring(0, maxLength).replaceAll("\n", " ")); |
858 |
dateList.add(DateFormat.getInstance().format(sqlHistory.getDate())); |
859 |
} |
860 |
} |
861 |
// Initialize sql column data |
862 |
data = null; |
863 |
data = new Object[sqlList.size()][2]; |
864 |
int row = 0; |
865 |
for (String sql : sqlList) { |
866 |
length = sql.trim().length(); |
867 |
maxLength = length > TABLE_DATA_WIDTH_SQL ? TABLE_DATA_WIDTH_SQL : length; |
868 |
data[row][0] = sql.trim().substring(0, maxLength).replaceAll("\n", " "); |
869 |
row++; |
870 |
} |
871 |
// Initialize date column data |
872 |
row = 0; |
873 |
for (String date : dateList) { |
874 |
data[row++][1] = date; |
875 |
} |
876 |
// Refresh table |
877 |
if (data.length > 0) { |
878 |
sqlHistoryTable.revalidate(); |
879 |
} else { |
880 |
sqlHistoryTable.revalidate(); |
881 |
insertSQLButton.setEnabled(false); |
882 |
} |
883 |
} |
884 |
|
885 |
@Override |
886 |
public void insertUpdate(DocumentEvent evt) { |
887 |
processUpdate(); |
888 |
} |
889 |
|
890 |
@Override |
891 |
public void removeUpdate(DocumentEvent evt) { |
892 |
processUpdate(); |
893 |
} |
894 |
|
895 |
private void processUpdate() { |
896 |
view.setCurrentSQLHistoryList(view.filterSQLHistoryList()); |
897 |
sqlHistoryTable.repaint(); |
898 |
sqlHistoryTable.clearSelection(); |
899 |
refreshTable(view.getCurrentSQLHistoryList()); |
900 |
} |
901 |
|
902 |
@Override |
903 |
public void changedUpdate(DocumentEvent arg0) { |
904 |
// unused |
905 |
} |
906 |
|
907 |
public List<SQLHistory> sortData() { |
908 |
// Refresh the table |
909 |
List<SQLHistory> filteredSQLHistoryList = view.filterSQLHistoryList(); |
910 |
SQLComparator sqlComparator = new SQLComparator(sortCol, sortAsc); |
911 |
Collections.sort(filteredSQLHistoryList, sqlComparator); |
912 |
view.setCurrentSQLHistoryList(filteredSQLHistoryList); |
913 |
refreshTable( filteredSQLHistoryList); |
914 |
return filteredSQLHistoryList; |
915 |
} |
916 |
} |
603 |
} |
917 |
|
604 |
}; |
918 |
public static String read(Document doc) throws InterruptedException, Exception { |
|
|
919 |
Renderer r = new Renderer(doc); |
920 |
doc.render(r); |
921 |
|
922 |
synchronized (r) { |
923 |
while (!r.done) { |
924 |
r.wait(); |
925 |
if (r.err != null) { |
926 |
throw new Exception(r.err); |
927 |
} |
928 |
} |
929 |
} |
930 |
return r.result; |
931 |
} |
932 |
|
933 |
private static class Renderer implements Runnable { |
934 |
Document doc; |
935 |
String result; |
936 |
Throwable err; |
937 |
boolean done; |
938 |
|
939 |
Renderer(Document doc) { |
940 |
this.doc = doc; |
941 |
} |
942 |
|
943 |
@Override |
944 |
public synchronized void run() { |
945 |
try { |
946 |
result = doc.getText(0, doc.getLength()); |
947 |
} catch (Throwable e) { |
948 |
err = e; |
949 |
Exceptions.printStackTrace(e); |
950 |
} |
951 |
done = true; |
952 |
notify(); |
953 |
} |
954 |
} |
955 |
|
956 |
private class InsertSQLUtility { |
957 |
|
958 |
public InsertSQLUtility() { |
959 |
} |
960 |
|
961 |
public void insert(String s, JEditorPane target) |
962 |
throws BadLocationException { |
963 |
insert(s, target, false); |
964 |
} |
965 |
|
966 |
public void insert(String s, JEditorPane target, boolean reformat) |
967 |
throws BadLocationException { |
968 |
|
969 |
if (s == null) { |
970 |
s = ""; // NOI18N |
971 |
} |
972 |
|
973 |
Document doc = target.getDocument(); |
974 |
if (doc != null) { |
975 |
insert(s, target, doc); |
976 |
} |
977 |
} |
978 |
|
979 |
private int insert(String s, JEditorPane target, Document doc) |
980 |
throws BadLocationException { |
981 |
|
982 |
int start = -1; |
983 |
try { |
984 |
Caret caret = target.getCaret(); |
985 |
int p0 = Math.min(caret.getDot(), caret.getMark()); |
986 |
int p1 = Math.max(caret.getDot(), caret.getMark()); |
987 |
doc.remove(p0, p1 - p0); |
988 |
start = caret.getDot(); |
989 |
doc.insertString(start, s + ";\n", null); // NOI18N |
990 |
} catch (BadLocationException ble) { |
991 |
LOGGER.log(Level.WARNING, org.openide.util.NbBundle.getMessage(SQLHistoryPanel.class, "LBL_InsertAtLocationError") + ble); |
992 |
} |
993 |
return start; |
994 |
} |
995 |
} |
996 |
|
997 |
private static void adjustColumnPreferredWidths(JTable table) { |
998 |
TableColumnModel columnModel = table.getColumnModel(); |
999 |
for (int col = 0; col < table.getColumnCount(); col++) { |
1000 |
|
1001 |
int maxwidth = 0; |
1002 |
for (int row = 0; row < table.getRowCount(); row++) { |
1003 |
TableCellRenderer rend = |
1004 |
table.getCellRenderer(row, col); |
1005 |
Object value = table.getValueAt(row, col); |
1006 |
Component comp = |
1007 |
rend.getTableCellRendererComponent(table, |
1008 |
value, |
1009 |
false, |
1010 |
false, |
1011 |
row, |
1012 |
col); |
1013 |
maxwidth = Math.max(comp.getPreferredSize().width, maxwidth); |
1014 |
} |
1015 |
TableColumn column = columnModel.getColumn(col); |
1016 |
column.setPreferredWidth(maxwidth); |
1017 |
} |
1018 |
} |
1019 |
|
1020 |
private static final class ConnectionUrlRenderer extends DefaultListCellRenderer { |
1021 |
|
1022 |
@Override |
1023 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
1024 |
JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
1025 |
component.setToolTipText((String) value); |
1026 |
return component; |
1027 |
} |
1028 |
} |
1029 |
|
1030 |
private TableCellRenderer createDefaultRenderer() { |
1031 |
DefaultTableCellRenderer label = new DefaultTableCellRenderer() { |
1032 |
|
1033 |
@Override |
1034 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
1035 |
if (table != null) { |
1036 |
JTableHeader header = table.getTableHeader(); |
1037 |
if (header != null) { |
1038 |
setForeground(header.getForeground()); |
1039 |
setBackground(header.getBackground()); |
1040 |
setFont(header.getFont()); |
1041 |
} |
1042 |
} |
1043 |
setText((value == null) ? "" : value.toString()); |
1044 |
setBorder(UIManager.getBorder("TableHeader.cellBorder")); |
1045 |
return SQLHistoryPanel.this; |
1046 |
} |
1047 |
}; |
1048 |
label.setHorizontalAlignment(JLabel.CENTER); |
1049 |
return label; |
1050 |
} |
1051 |
|
1052 |
private class ColumnListener extends MouseAdapter { |
1053 |
@Override |
1054 |
public void mouseClicked(MouseEvent e) { |
1055 |
HistoryTableModel model = (HistoryTableModel)sqlHistoryTable.getModel(); |
1056 |
TableColumnModel colModel = sqlHistoryTable.getColumnModel(); |
1057 |
int colModelIndex = colModel.getColumnIndexAtX(e.getX()); |
1058 |
int modelIndex = colModel.getColumn(colModelIndex).getModelIndex(); |
1059 |
if (modelIndex < 0) { |
1060 |
return; |
1061 |
} |
1062 |
if (model.sortCol == modelIndex) { |
1063 |
model.sortAsc = !model.sortAsc; |
1064 |
} else { |
1065 |
model.sortCol = modelIndex; |
1066 |
} |
1067 |
model.sortData(); |
1068 |
sqlHistoryTable.tableChanged(new TableModelEvent(model)); |
1069 |
sqlHistoryTable.repaint(); |
1070 |
} |
1071 |
} |
1072 |
|
1073 |
private class SQLComparator implements Comparator<SQLHistory> { |
1074 |
|
1075 |
protected int sortCol; |
1076 |
protected boolean sortAsc; |
1077 |
|
1078 |
public SQLComparator(int sortCol, boolean sortAsc) { |
1079 |
this.sortCol = sortCol; |
1080 |
this.sortAsc = sortAsc; |
1081 |
} |
1082 |
|
1083 |
@Override |
1084 |
public int compare(SQLHistory sql1, SQLHistory sql2) { |
1085 |
int result = 0; |
1086 |
if (!(sql1 instanceof SQLHistory) || !(sql2 instanceof SQLHistory)) { |
1087 |
return result; |
1088 |
} |
1089 |
SQLHistory sqlHistory1 = sql1; |
1090 |
SQLHistory sqlHistory2 = sql2; |
1091 |
|
1092 |
switch (sortCol) { |
1093 |
case 0: // SQL |
1094 |
String s1 = sqlHistory1.getSql().trim().toLowerCase(); |
1095 |
String s2 = sqlHistory2.getSql().trim().toLowerCase(); |
1096 |
result = s1.compareTo(s2); |
1097 |
break; |
1098 |
case 1: // Date |
1099 |
Date d1 = sqlHistory1.getDate(); |
1100 |
Date d2 = sqlHistory2.getDate(); |
1101 |
result = d1.compareTo(d2); |
1102 |
break; |
1103 |
} |
1104 |
if (!sortAsc) { |
1105 |
result = -result; |
1106 |
} |
1107 |
return result; |
1108 |
} |
1109 |
|
1110 |
@Override |
1111 |
public boolean equals(Object obj) { |
1112 |
if (obj instanceof SQLComparator) { |
1113 |
SQLComparator compObj = (SQLComparator) obj; |
1114 |
return (compObj.sortCol == sortCol) && (compObj.sortAsc == sortAsc); |
1115 |
} |
1116 |
return false; |
1117 |
} |
1118 |
|
1119 |
@Override |
1120 |
public int hashCode() { |
1121 |
int hash = 3; |
1122 |
hash = 17 * hash + this.sortCol; |
1123 |
hash = 17 * hash + (this.sortAsc ? 1 : 0); |
1124 |
return hash; |
1125 |
} |
1126 |
} |
1127 |
} |
605 |
} |