001: package org.dbbrowser.ui.widget;
002:
003: import infrastructure.internationalization.InternationalizationManager;
004: import infrastructure.propertymanager.PropertyManager;
005: import java.awt.Component;
006: import java.awt.FontMetrics;
007: import java.awt.Toolkit;
008: import java.awt.datatransfer.StringSelection;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.awt.event.MouseAdapter;
012: import java.awt.event.MouseEvent;
013: import java.awt.event.MouseListener;
014: import javax.swing.*;
015: import javax.swing.table.AbstractTableModel;
016: import javax.swing.table.TableColumn;
017: import org.dbbrowser.ui.*;
018: import org.dbbrowser.ui.helper.DBTableDataTableModel;
019: import org.dbbrowser.ui.helper.exporthelper.wizard.DataExporterUsingWizard;
020: import org.dbbrowser.util.TableSorter;
021: import org.dbbrowser.help.HelpManager;
022:
023: /**
024: * A table which can be sorted by clicking on the row header
025: */
026: public class Table extends JPanel implements ActionListener {
027: private static final long serialVersionUID = UIControllerForQueries.version;
028:
029: private static final String POP_UP_MENU_COPY_MENU_ITEM_LABEL = InternationalizationManager
030: .getInstance()
031: .getMessage(
032: "dbbrowser-ui",
033: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-popup-menu-copy-menu-item-label",
034: null);;
035: private static final String POP_UP_MENU_EXPORT_MENU_ITEM_LABEL = InternationalizationManager
036: .getInstance()
037: .getMessage(
038: "dbbrowser-ui",
039: "dbbrowser-ui-dbbrowser-window-tools-menu-copy-label",
040: null);;
041: private static final String POP_UP_MENU_WHATS_THIS_MENU_ITEM_LABEL = InternationalizationManager
042: .getInstance()
043: .getMessage(
044: "dbbrowser-ui",
045: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-popup-menu-whats-this-menu-item-label",
046: null);;
047: private static final String POP_UP_MENU_HELP_MENU_ITEM_LABEL = InternationalizationManager
048: .getInstance()
049: .getMessage(
050: "dbbrowser-ui",
051: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-popup-menu-help-menu-item-label",
052: null);;
053:
054: private static final String COPY_POPUP_ITEM_ICON_FILENAME = PropertyManager
055: .getInstance().getProperty(
056: "dbbrowser-popup-menu-copy-icon-filename");
057: private static final String EXPORT_POPUP_ITEM_ICON_FILENAME = PropertyManager
058: .getInstance()
059: .getProperty(
060: "dbbrowser-ui-dbbrowser-window-toolbar-export-icon");
061: private static final String WHATS_THIS_POPUP_ITEM_ICON_FILENAME = PropertyManager
062: .getInstance().getProperty(
063: "dbbrowser-popup-menu-whats-this-icon-filename");
064: private static final String HELP_POPUP_ITEM_ICON_FILENAME = PropertyManager
065: .getInstance().getProperty(
066: "dbbrowser-ui-dbbrowser-window-toolbar-help-icon");
067:
068: private UIControllerForQueries uiControllerForQueries = null;
069: private UIControllerForUpdates uiControllerForUpdates = null;
070: private AbstractTableModel abstractTableModel = null;
071:
072: //Widgets in the panel
073: private JScrollPane paneForTable = null;
074: private TableSorter sorter = null;
075: private JTable tableForTableData = null;
076:
077: //Popup menu
078: private JPopupMenu popupMenuForResultsTable = null;
079:
080: /**
081: * Constructer
082: * @param uiControllerForQueries
083: * @param uiControllerForUpdates
084: */
085: public Table(UIControllerForQueries uiControllerForQueries,
086: UIControllerForUpdates uiControllerForUpdates) {
087: this .uiControllerForQueries = uiControllerForQueries;
088: this .uiControllerForUpdates = uiControllerForUpdates;
089: this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
090: }
091:
092: /**
093: * Returns the selected row
094: * @return
095: */
096: public Integer getSelectedRow() {
097: int selectedRow = this .tableForTableData.getSelectedRow();
098: Integer returnValue = null;
099: if (selectedRow >= 0) {
100: int modelIndex = sorter.modelIndex(selectedRow);
101: returnValue = new Integer(modelIndex);
102: }
103: return returnValue;
104: }
105:
106: /**
107: * Update the table
108: */
109: public void update() {
110: this .abstractTableModel.fireTableDataChanged();
111: this .tableForTableData.updateUI();
112: }
113:
114: /**
115: * Set up table for display
116: * @param abstractTableModel
117: */
118: public void initializeTable(AbstractTableModel abstractTableModel) {
119: this .abstractTableModel = abstractTableModel;
120:
121: //If the panel already had a table(scrollpane), remove it
122: if (this .tableForTableData != null) {
123: this .remove(this .paneForTable);
124: this .paneForTable = null;
125: this .tableForTableData = null;
126: }
127:
128: //Wrap the table model around a sorter
129: sorter = new TableSorter(this .abstractTableModel);
130:
131: //Build a new JTable
132: this .tableForTableData = new JTable(sorter);
133: sorter.setTableHeader(this .tableForTableData.getTableHeader());
134:
135: //Add listener for Double clicks
136: this .tableForTableData
137: .addMouseListener(new TableMouseListener());
138:
139: //Allow cell selection
140: this .tableForTableData.setCellSelectionEnabled(true);
141:
142: //Other values
143: this .tableForTableData
144: .setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //if we dont set this, scrollbar is never shown
145:
146: //Set the min width of each column so it as wide as it needs to be
147: //Set the table header renderer for the columns which contain the primary key
148: FontMetrics fontMetrics = this .getGraphics().getFontMetrics();
149:
150: for (int i = 0; i < this .abstractTableModel.getColumnCount(); i++) {
151: TableColumn tc = this .tableForTableData.getColumnModel()
152: .getColumn(i);
153:
154: //Set the width of first column
155: if (i == 0) {
156: tc.setPreferredWidth(80);
157: } else {
158: //Set the width of the subsequent columns to be little more than width of the column header
159: String columnName = this .abstractTableModel
160: .getColumnName(i);
161: int columnWidth = fontMetrics.stringWidth(columnName) + 60;
162: tc.setPreferredWidth(columnWidth);
163: }
164: }
165:
166: //Set the renderer for the first column
167: //TableColumn firstColumn = this.tableForTableData.getColumnModel().getColumn(0);
168: //firstColumn.setCellRenderer(new RowHeaderCellRenderer());
169:
170: //Wrap the table in a scrollpane and add the scrollpane to the panel
171: this .paneForTable = new JScrollPane(this .tableForTableData);
172: this .add(this .paneForTable);
173:
174: //Add the popup menu
175: popupMenuForResultsTable = new JPopupMenu();
176: JMenuItem copyMenuItem = new JMenuItem(
177: POP_UP_MENU_COPY_MENU_ITEM_LABEL, new ImageIcon(
178: COPY_POPUP_ITEM_ICON_FILENAME));
179: copyMenuItem.addActionListener(this );
180: popupMenuForResultsTable.add(copyMenuItem);
181: JMenuItem exportMenuItem = new JMenuItem(
182: POP_UP_MENU_EXPORT_MENU_ITEM_LABEL, new ImageIcon(
183: EXPORT_POPUP_ITEM_ICON_FILENAME));
184: exportMenuItem.addActionListener(this );
185: popupMenuForResultsTable.add(exportMenuItem);
186: popupMenuForResultsTable.addSeparator();
187: JMenuItem whatsThisMenuItem = new JMenuItem(
188: POP_UP_MENU_WHATS_THIS_MENU_ITEM_LABEL, new ImageIcon(
189: WHATS_THIS_POPUP_ITEM_ICON_FILENAME));
190: HelpManager.getInstance().registerCSH(whatsThisMenuItem, this ,
191: "data_browser");
192: popupMenuForResultsTable.add(whatsThisMenuItem);
193: JMenuItem helpMenuItem = new JMenuItem(
194: POP_UP_MENU_HELP_MENU_ITEM_LABEL, new ImageIcon(
195: HELP_POPUP_ITEM_ICON_FILENAME));
196: helpMenuItem.addActionListener(HelpManager.getInstance()
197: .getActionListenerForHelpEvents());
198: popupMenuForResultsTable.add(helpMenuItem);
199:
200: //Add listener to components that can bring up popup menus.
201: MouseListener popupListenerForResultsTable = new BasicPopupListener(
202: this .popupMenuForResultsTable);
203: this .tableForTableData
204: .addMouseListener(popupListenerForResultsTable);
205:
206: //Update the table
207: this .abstractTableModel.fireTableDataChanged();
208: this .tableForTableData.doLayout();
209: this .updateUI();
210: }
211:
212: public void actionPerformed(ActionEvent e) {
213: //If the user wants to copy something
214: if (POP_UP_MENU_COPY_MENU_ITEM_LABEL.equals(e
215: .getActionCommand())) {
216: int[] selectedRows = this .tableForTableData
217: .getSelectedRows();
218: int[] selectedColumns = this .tableForTableData
219: .getSelectedColumns();
220:
221: StringBuffer buffer = new StringBuffer();
222: for (int i = 0; i < selectedRows.length; i++) {
223: for (int j = 0; j < selectedColumns.length; j++) {
224: Object selectedCell = this .tableForTableData
225: .getValueAt(selectedRows[i],
226: selectedColumns[j]);
227: String selectedValue = selectedCell.toString();
228:
229: if (j == selectedColumns.length - 1) {
230: buffer.append(selectedValue);
231: } else {
232: buffer.append(selectedValue + "\t");
233: }
234: }
235:
236: if (i != selectedRows.length - 1) {
237: buffer.append("\n");
238: }
239: }
240:
241: //Copy the contents to the system clipboard
242: StringSelection ss = new StringSelection(buffer.toString());
243: Toolkit.getDefaultToolkit().getSystemClipboard()
244: .setContents(ss, ss);
245: }
246:
247: //If the user wants to copy something
248: if (POP_UP_MENU_EXPORT_MENU_ITEM_LABEL.equals(e
249: .getActionCommand())) {
250: DataExporterUsingWizard dataExporterUsingWizard = new DataExporterUsingWizard();
251: dataExporterUsingWizard.export(this .abstractTableModel);
252: }
253: }
254:
255: /**
256: * Listener for double clicks
257: * @author amangat
258: */
259: private class TableMouseListener extends MouseAdapter {
260: public void mouseClicked(MouseEvent evt) {
261: if (evt.getClickCount() == 2) {
262: Component sourceComponent = evt.getComponent();
263:
264: //if the source is a jtabel, get the selected row
265: if (sourceComponent instanceof JTable) {
266: JTable aTable = (JTable) sourceComponent;
267: int selectedRow = aTable.getSelectedRow();
268:
269: //if the source is a row in the table
270: if (selectedRow != -1) {
271: //if there are any rows to display
272: if (abstractTableModel.getRowCount() != 0) {
273: if (abstractTableModel instanceof DBTableDataTableModel) {
274: DBTableDataTableModel dbTableDataTableModel = (DBTableDataTableModel) abstractTableModel;
275:
276: //Get the index of the row selected from the model
277: int modelIndex = sorter
278: .modelIndex(selectedRow);
279:
280: //Build the view record window
281: ViewRecordWindow viewRecordWindow = new ViewRecordWindow(
282: uiControllerForUpdates,
283: dbTableDataTableModel
284: .getDBTable(),
285: new Integer(modelIndex));
286: viewRecordWindow.show();
287: }
288: }
289: }
290: }
291: }
292: }
293: }
294: }
|