001: package org.dbbrowser.ui.panel.dbbrowserwindow;
002:
003: import infrastructure.internationalization.InternationalizationManager;
004: import infrastructure.propertymanager.PropertyManager;
005: import infrastructure.logging.Log;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.util.ArrayList;
009: import java.util.HashMap;
010: import java.util.Map;
011: import javax.swing.*;
012: import org.dbbrowser.db.engine.exception.DBEngineException;
013: import org.dbbrowser.db.engine.model.DBTable;
014: import org.dbbrowser.db.engine.model.DBRow;
015: import org.dbbrowser.db.engine.model.filter.Filter;
016: import org.dbbrowser.ui.*;
017: import org.dbbrowser.ui.helper.DBTableDataTableModel;
018: import org.dbbrowser.ui.panel.ButtonsPanel;
019: import org.dbbrowser.ui.widget.Button;
020: import org.dbbrowser.ui.widget.Table;
021:
022: public class TableDataPanel extends JPanel implements ActionListener {
023: private static final long serialVersionUID = UIControllerForQueries.version;
024: private static final String TITLE = InternationalizationManager
025: .getInstance().getMessage("dbbrowser-ui",
026: "dbbrowser-ui-dbbrowser-window-title-label", null);
027:
028: private String addNewRecordIconFileName = PropertyManager
029: .getInstance()
030: .getProperty(
031: "dbbrowser-ui-dbbrowser-window-add-new-record-icon");
032: private String deleteRecordIconFileName = PropertyManager
033: .getInstance().getProperty(
034: "dbbrowser-ui-dbbrowser-window-delete-record-icon");
035: private String addEditFilterIconFileName = PropertyManager
036: .getInstance()
037: .getProperty(
038: "dbbrowser-ui-dbbrowser-window-add-edit-filter-icon");
039:
040: private Icon iconForAddNewRecordButton = new ImageIcon(
041: addNewRecordIconFileName);
042: private Icon iconForDeleteRecordButton = new ImageIcon(
043: deleteRecordIconFileName);
044: private Icon iconForAddEditFilterButton = new ImageIcon(
045: addEditFilterIconFileName);
046:
047: private String firstRecordIconFileName = PropertyManager
048: .getInstance()
049: .getProperty(
050: "dbbrowser-ui-view-record-window-first-record-icon");
051: private String lastRecordIconFileName = PropertyManager
052: .getInstance().getProperty(
053: "dbbrowser-ui-view-record-window-last-record-icon");
054: private String previousRecordIconFileName = PropertyManager
055: .getInstance()
056: .getProperty(
057: "dbbrowser-ui-view-record-window-previous-record-icon");
058: private String nextRecordIconFileName = PropertyManager
059: .getInstance().getProperty(
060: "dbbrowser-ui-view-record-window-next-record-icon");
061:
062: private Icon iconForFirstPageButton = new ImageIcon(
063: firstRecordIconFileName);
064: private Icon iconForLastPageButton = new ImageIcon(
065: lastRecordIconFileName);
066: private Icon iconForPreviousPageButton = new ImageIcon(
067: previousRecordIconFileName);
068: private Icon iconForNextPageButton = new ImageIcon(
069: nextRecordIconFileName);
070:
071: private static final String ADD_NEW_RECORD_BUTTON_LABEL = InternationalizationManager
072: .getInstance()
073: .getMessage(
074: "dbbrowser-ui",
075: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-add-new-record-button-label",
076: null);
077: private static final String DELETE_RECORD_BUTTON_LABEL = InternationalizationManager
078: .getInstance()
079: .getMessage(
080: "dbbrowser-ui",
081: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-delete-record-button-label",
082: null);
083: private static final String ADD_FILTER_BUTTON_LABEL = InternationalizationManager
084: .getInstance()
085: .getMessage(
086: "dbbrowser-ui",
087: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-add-new-filter-button-label",
088: null);
089:
090: private static Integer pagingSize = new Integer(Integer
091: .parseInt(PropertyManager.getInstance().getProperty(
092: "dbbrowser-ui-browser-window-paging-size")));
093: private static final String FIRST_PAGE_BUTTON_LABEL = InternationalizationManager
094: .getInstance()
095: .getMessage(
096: "dbbrowser-ui",
097: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-first-page-button-label",
098: new Integer[] { pagingSize });
099: private static final String LAST_PAGE_BUTTON_LABEL = InternationalizationManager
100: .getInstance()
101: .getMessage(
102: "dbbrowser-ui",
103: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-last-page-button-label",
104: new Integer[] { pagingSize });
105: private static final String PREVIOUS_PAGE_BUTTON_LABEL = InternationalizationManager
106: .getInstance()
107: .getMessage(
108: "dbbrowser-ui",
109: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-previous-page-button-label",
110: new Integer[] { pagingSize });
111: private static final String NEXT_PAGE_BUTTON_LABEL = InternationalizationManager
112: .getInstance()
113: .getMessage(
114: "dbbrowser-ui",
115: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-next-page-button-label",
116: new Integer[] { pagingSize });
117: private static final String DELETE_RECORD_CONFIRMATION_MESSAGE = InternationalizationManager
118: .getInstance()
119: .getMessage(
120: "dbbrowser-ui",
121: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-delete-record-confirm-message",
122: null);
123:
124: private UIControllerForQueries uiControllerForQueries = null;
125: private UIControllerForUpdates uiControllerForUpdates = null;
126:
127: private Table queryResultsTablePanel = null;
128: private ButtonsPanel pagingButtonsPanel = null;
129: private ButtonsPanel buttonsPanel = null;
130: private JPanel panelForFilterSetMessage = null;
131:
132: private DBTable dbTable = null;
133: //private Filter filter = null;
134:
135: private Map mapOfTableNameToFilter = new HashMap();
136:
137: public TableDataPanel(
138: UIControllerForQueries uiControllerForQueries,
139: UIControllerForUpdates uiControllerForUpdates) {
140: this .uiControllerForQueries = uiControllerForQueries;
141: this .uiControllerForUpdates = uiControllerForUpdates;
142: }
143:
144: public Filter getFilter(String tableName) {
145: Object o = this .mapOfTableNameToFilter.get(tableName);
146: if (o != null) {
147: return (Filter) o;
148: }
149: return null;
150: }
151:
152: public void initializeTable(DBTable dbTable) {
153: //If the panels have not been set, set them up
154: if (this .pagingButtonsPanel == null) {
155: this .initialize();
156: }
157:
158: this .dbTable = dbTable;
159: DBTableDataTableModel dbTableDataTableModel = new DBTableDataTableModel(
160: dbTable);
161: this .queryResultsTablePanel
162: .initializeTable(dbTableDataTableModel);
163:
164: Integer offset = this .dbTable.getOffset();
165: Integer numberOfPagesToReturn = new Integer(this .dbTable
166: .getNumberOfRowsToReturn().intValue()
167: + offset.intValue());
168: Integer numberOfRowsInTable = this .dbTable
169: .getNumberOfRowsInTable();
170:
171: //if the numberOfPagesToReturn greater than number of rows, then display the number of rows in the table
172: Integer[] i = null;
173: if (numberOfPagesToReturn.intValue() > numberOfRowsInTable
174: .intValue()) {
175: i = new Integer[] { offset, numberOfRowsInTable,
176: numberOfRowsInTable };
177: } else {
178: i = new Integer[] { offset, numberOfPagesToReturn,
179: numberOfRowsInTable };
180: }
181:
182: String TABLE_PANEL_LABEL = InternationalizationManager
183: .getInstance()
184: .getMessage(
185: "dbbrowser-ui",
186: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-panel-title",
187: i);
188:
189: this .queryResultsTablePanel.setBorder(BorderFactory
190: .createTitledBorder(this .dbTable.getTableName() + " - "
191: + TABLE_PANEL_LABEL));
192:
193: //Rebuild the buttons panel
194: Object o = this .mapOfTableNameToFilter.get(this .dbTable
195: .getTableName());
196: if (o != null) {
197: if (this .panelForFilterSetMessage == null) {
198: this .panelForFilterSetMessage = new JPanel();
199: JLabel labelForFilterSetMessage = new JLabel(
200: "Data has been filtered");
201: labelForFilterSetMessage
202: .setIcon(iconForAddEditFilterButton);
203: this .panelForFilterSetMessage
204: .add(labelForFilterSetMessage);
205: this .add(this .panelForFilterSetMessage, 1);
206: this .remove(this .pagingButtonsPanel);
207: }
208: } else {
209: if (this .panelForFilterSetMessage != null) {
210: this .add(this .pagingButtonsPanel, 1);
211: this .remove(this .panelForFilterSetMessage);
212: this .panelForFilterSetMessage = null;
213: }
214: }
215: this .updateUI();
216: }
217:
218: private void initialize() {
219: //Set the layout
220: this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
221:
222: //Add the buttons for paging to the button panel
223: Button firstPageButton = new Button(FIRST_PAGE_BUTTON_LABEL,
224: this , FIRST_PAGE_BUTTON_LABEL, iconForFirstPageButton,
225: Boolean.FALSE);
226: Button lastPageButton = new Button(LAST_PAGE_BUTTON_LABEL,
227: this , LAST_PAGE_BUTTON_LABEL, iconForLastPageButton,
228: Boolean.FALSE);
229: Button previousPageButton = new Button(
230: PREVIOUS_PAGE_BUTTON_LABEL, this ,
231: PREVIOUS_PAGE_BUTTON_LABEL, iconForPreviousPageButton,
232: Boolean.FALSE);
233: Button nextPageButton = new Button(NEXT_PAGE_BUTTON_LABEL,
234: this , NEXT_PAGE_BUTTON_LABEL, iconForNextPageButton,
235: Boolean.FALSE);
236: ArrayList listOfButtons = new ArrayList();
237: listOfButtons.add(firstPageButton);
238: listOfButtons.add(previousPageButton);
239: listOfButtons.add(nextPageButton);
240: listOfButtons.add(lastPageButton);
241: this .pagingButtonsPanel = new ButtonsPanel(listOfButtons);
242:
243: //Add the buttons to the button panel
244: Button addNewRecordButton = new Button(
245: ADD_NEW_RECORD_BUTTON_LABEL, this ,
246: ADD_NEW_RECORD_BUTTON_LABEL, iconForAddNewRecordButton,
247: Boolean.FALSE);
248: Button deleteRecordButton = new Button(
249: DELETE_RECORD_BUTTON_LABEL, this ,
250: DELETE_RECORD_BUTTON_LABEL, iconForDeleteRecordButton,
251: Boolean.FALSE);
252: Button addFilterButton = new Button(ADD_FILTER_BUTTON_LABEL,
253: this , ADD_FILTER_BUTTON_LABEL,
254: iconForAddEditFilterButton, Boolean.FALSE);
255: listOfButtons = new ArrayList();
256: listOfButtons.add(addNewRecordButton);
257: listOfButtons.add(deleteRecordButton);
258: listOfButtons.add(addFilterButton);
259: this .buttonsPanel = new ButtonsPanel(listOfButtons);
260:
261: //Setup the query results panel
262: queryResultsTablePanel = new Table(this .uiControllerForQueries,
263: this .uiControllerForUpdates);
264:
265: //Add the panels to this panel
266: this .add(this .queryResultsTablePanel);
267: this .add(this .pagingButtonsPanel);
268: this .add(this .buttonsPanel);
269: }
270:
271: public void actionPerformed(ActionEvent e) {
272: if (FIRST_PAGE_BUTTON_LABEL.equals(e.getActionCommand())) {
273: try {
274: DBTable dbTable = this .uiControllerForQueries
275: .getAllDataInATable(this .dbTable
276: .getSchemaName(), this .dbTable
277: .getTableName(), new Integer(0),
278: this .dbTable.getNumberOfRowsToReturn());
279: this .initializeTable(dbTable);
280: } catch (DBEngineException exc) {
281: String errorMessage = InternationalizationManager
282: .getInstance()
283: .getMessage(
284: "dbbrowser-ui",
285: "dbbrowser-ui-dbbrowser-window-sql-failed",
286: null);
287: JOptionPane.showMessageDialog(null, errorMessage
288: + " - " + exc.getMessage(), TITLE,
289: JOptionPane.ERROR_MESSAGE);
290: }
291: }
292:
293: if (LAST_PAGE_BUTTON_LABEL.equals(e.getActionCommand())) {
294: try {
295: int offset = this .dbTable.getNumberOfRowsInTable()
296: .intValue()
297: - this .dbTable.getNumberOfRowsToReturn()
298: .intValue();
299:
300: DBTable dbTable = this .uiControllerForQueries
301: .getAllDataInATable(this .dbTable
302: .getSchemaName(), this .dbTable
303: .getTableName(), new Integer(offset),
304: this .dbTable.getNumberOfRowsToReturn());
305: this .initializeTable(dbTable);
306: } catch (DBEngineException exc) {
307: String errorMessage = InternationalizationManager
308: .getInstance()
309: .getMessage(
310: "dbbrowser-ui",
311: "dbbrowser-ui-dbbrowser-window-sql-failed",
312: null);
313: JOptionPane.showMessageDialog(null, errorMessage
314: + " - " + exc.getMessage(), TITLE,
315: JOptionPane.ERROR_MESSAGE);
316: }
317: }
318:
319: if (NEXT_PAGE_BUTTON_LABEL.equals(e.getActionCommand())) {
320: int currentRecordNumber = this .dbTable.getOffset()
321: .intValue()
322: + this .dbTable.getNumberOfRowsToReturn().intValue();
323:
324: //If there are more rows to return
325: if (currentRecordNumber < this .dbTable
326: .getNumberOfRowsInTable().intValue()) {
327: int offset = this .dbTable.getOffset().intValue()
328: + this .dbTable.getNumberOfRowsToReturn()
329: .intValue();
330: try {
331: DBTable dbTable = this .uiControllerForQueries
332: .getAllDataInATable(this .dbTable
333: .getSchemaName(), this .dbTable
334: .getTableName(),
335: new Integer(offset), this .dbTable
336: .getNumberOfRowsToReturn());
337: this .initializeTable(dbTable);
338: } catch (DBEngineException exc) {
339: String errorMessage = InternationalizationManager
340: .getInstance()
341: .getMessage(
342: "dbbrowser-ui",
343: "dbbrowser-ui-dbbrowser-window-sql-failed",
344: null);
345: JOptionPane.showMessageDialog(null, errorMessage
346: + " - " + exc.getMessage(), TITLE,
347: JOptionPane.ERROR_MESSAGE);
348: }
349: }
350: }
351:
352: if (PREVIOUS_PAGE_BUTTON_LABEL.equals(e.getActionCommand())) {
353: int currentRecordNumber = this .dbTable.getOffset()
354: .intValue()
355: + this .dbTable.getNumberOfRowsToReturn().intValue();
356:
357: //If this is the first row
358: if (this .dbTable.getOffset().intValue() != 0) {
359: int offset = this .dbTable.getOffset().intValue()
360: - pagingSize.intValue();
361:
362: //if offset is less than 0, make it zero
363: if (offset < 0) {
364: offset = 0;
365: }
366:
367: try {
368: DBTable dbTable = this .uiControllerForQueries
369: .getAllDataInATable(this .dbTable
370: .getSchemaName(), this .dbTable
371: .getTableName(),
372: new Integer(offset), this .dbTable
373: .getNumberOfRowsToReturn());
374: this .initializeTable(dbTable);
375: } catch (DBEngineException exc) {
376: String errorMessage = InternationalizationManager
377: .getInstance()
378: .getMessage(
379: "dbbrowser-ui",
380: "dbbrowser-ui-dbbrowser-window-sql-failed",
381: null);
382: JOptionPane.showMessageDialog(null, errorMessage
383: + " - " + exc.getMessage(), TITLE,
384: JOptionPane.ERROR_MESSAGE);
385: }
386: }
387: }
388:
389: //Delete the selected record
390: if (DELETE_RECORD_BUTTON_LABEL.equals(e.getActionCommand())) {
391: Integer selectedRowInteger = this .queryResultsTablePanel
392: .getSelectedRow();
393: if (selectedRowInteger == null) {
394: String errorMessage = InternationalizationManager
395: .getInstance()
396: .getMessage(
397: "dbbrowser-ui",
398: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-delete-record-no-row-selected-message",
399: null);
400: JOptionPane.showMessageDialog(null, errorMessage,
401: TITLE, JOptionPane.ERROR_MESSAGE);
402: } else {
403: int selectedRow = selectedRowInteger.intValue();
404: if (selectedRow != -1) {
405: int ans = JOptionPane.showConfirmDialog(null,
406: DELETE_RECORD_CONFIRMATION_MESSAGE, TITLE,
407: JOptionPane.YES_NO_OPTION,
408: JOptionPane.QUESTION_MESSAGE);
409: if (ans == 0) {
410: DBRow dbRow = (DBRow) this .dbTable
411: .getListOfRows().get(selectedRow);
412: try {
413: //Delete the row
414: this .uiControllerForUpdates.deleteRow(
415: this .dbTable.getSchemaName(),
416: this .dbTable.getTableName(), dbRow);
417:
418: //Get the auto commit flag. If autocommit if off, commit
419: String autoCommitFlag = PropertyManager
420: .getInstance().getProperty(
421: "dbbrowser-auto-commit");
422: if ("false".equals(autoCommitFlag)) {
423: this .uiControllerForUpdates.commit();
424: }
425:
426: //remove the selected row from the list of rows
427: this .dbTable.getListOfRows().remove(
428: selectedRow);
429:
430: //Update the UI
431: this .queryResultsTablePanel.update();
432: this .queryResultsTablePanel.updateUI();
433: } catch (DBEngineException exc) {
434: String errorMessage = InternationalizationManager
435: .getInstance()
436: .getMessage(
437: "dbbrowser-ui",
438: "dbbrowser-ui-dbbrowser-window-sql-failed",
439: null);
440: JOptionPane.showMessageDialog(null,
441: errorMessage + " - "
442: + exc.getMessage(), TITLE,
443: JOptionPane.ERROR_MESSAGE);
444: }
445: }
446: }
447: }
448: }
449:
450: //Add filter
451: if (ADD_FILTER_BUTTON_LABEL.equals(e.getActionCommand())) {
452: SetFilterWindow setFilterWindow = null;
453: Object o = this .mapOfTableNameToFilter.get(this .dbTable
454: .getTableName());
455: if (o != null) {
456: Filter cachedFilter = (Filter) o;
457: setFilterWindow = new SetFilterWindow(this .dbTable
458: .getListOfColumnInfos(), cachedFilter);
459: } else {
460: setFilterWindow = new SetFilterWindow(this .dbTable
461: .getListOfColumnInfos(), null);
462: }
463:
464: setFilterWindow.show();
465:
466: //Get the filter
467: Filter filter = setFilterWindow.getFilter();
468:
469: //if filter has been set, use it
470: if (filter != null) {
471: Log.getInstance().debugMessage(
472: "Filter is: " + filter.getSQLString(),
473: this .getClass().getName());
474:
475: //Get the data from the UI Controller
476: try {
477: this .dbTable = this .uiControllerForQueries
478: .getFilteredDataInATable(this .dbTable
479: .getSchemaName(), this .dbTable
480: .getTableName(), filter);
481:
482: if (this .panelForFilterSetMessage == null) {
483: this .panelForFilterSetMessage = new JPanel();
484: JLabel labelForFilterSetMessage = new JLabel(
485: "Data has been filtered");
486: labelForFilterSetMessage
487: .setIcon(iconForAddEditFilterButton);
488: this .panelForFilterSetMessage
489: .add(labelForFilterSetMessage);
490: this .add(this .panelForFilterSetMessage, 1);
491: this .remove(this .pagingButtonsPanel);
492: }
493: } catch (DBEngineException exc) {
494: Log.getInstance().fatalMessage(
495: exc.getClass().getName() + " - "
496: + exc.getMessage(),
497: BrowserPanel.class.getName());
498: String sqlFailedLabel = InternationalizationManager
499: .getInstance()
500: .getMessage(
501: "dbbrowser-ui",
502: "dbbrowser-ui-dbbrowser-window-sql-failed",
503: null);
504: JOptionPane.showMessageDialog(null, sqlFailedLabel
505: + " - " + exc.getMessage(), TITLE,
506: JOptionPane.ERROR_MESSAGE);
507: }
508: } else {
509: Log.getInstance().debugMessage(
510: "Filter has been cleared",
511: this .getClass().getName());
512:
513: //Get the data from the UI Controller
514: try {
515: //Get the results from the database and show the data in the table
516: dbTable = this .uiControllerForQueries
517: .getAllDataInATable(this .dbTable
518: .getSchemaName(), this .dbTable
519: .getTableName(), new Integer(0),
520: pagingSize);
521:
522: if (this .panelForFilterSetMessage != null) {
523: this .add(this .pagingButtonsPanel, 1);
524: this .remove(this .panelForFilterSetMessage);
525: this .panelForFilterSetMessage = null;
526: }
527: } catch (DBEngineException exc) {
528: Log.getInstance().fatalMessage(
529: exc.getClass().getName() + " - "
530: + exc.getMessage(),
531: BrowserPanel.class.getName());
532: String sqlFailedLabel = InternationalizationManager
533: .getInstance()
534: .getMessage(
535: "dbbrowser-ui",
536: "dbbrowser-ui-dbbrowser-window-sql-failed",
537: null);
538: JOptionPane.showMessageDialog(null, sqlFailedLabel
539: + " - " + exc.getMessage(), TITLE,
540: JOptionPane.ERROR_MESSAGE);
541: }
542: }
543:
544: //Store the filter
545: this .mapOfTableNameToFilter.put(
546: this .dbTable.getTableName(), filter);
547:
548: //Display the table
549: this .initializeTable(this .dbTable);
550: }
551:
552: //Add new record
553: if (ADD_NEW_RECORD_BUTTON_LABEL.equals(e.getActionCommand())) {
554: AddNewRecordWindow addNewRecordWindow = new AddNewRecordWindow(
555: this .uiControllerForUpdates, this .dbTable);
556: addNewRecordWindow.show();
557:
558: //Refresh the data
559: try {
560: //Get the results from the database and show the data in the table
561: this .dbTable = this .uiControllerForQueries
562: .getAllDataInATable(this .dbTable
563: .getSchemaName(), this .dbTable
564: .getTableName(), this .dbTable
565: .getOffset(), this .dbTable
566: .getNumberOfRowsToReturn());
567:
568: //Show the results
569: this .initializeTable(this .dbTable);
570: } catch (DBEngineException exc) {
571: String errorMessage = InternationalizationManager
572: .getInstance()
573: .getMessage(
574: "dbbrowser-ui",
575: "dbbrowser-ui-dbbrowser-window-sql-failed",
576: null);
577: JOptionPane.showMessageDialog(null, errorMessage
578: + " - " + exc.getMessage(), TITLE,
579: JOptionPane.ERROR_MESSAGE);
580: }
581: }
582: }
583: }
|