001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Component;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.MouseEvent;
024: import java.awt.event.KeyEvent;
025:
026: import java.awt.print.PrinterJob;
027:
028: import javax.swing.*;
029:
030: import net.sourceforge.squirrel_sql.fw.gui.action.*;
031: import net.sourceforge.squirrel_sql.fw.util.StringManager;
032: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
033: import net.sourceforge.squirrel_sql.fw.datasetviewer.IDataSetUpdateableModel;
034: import net.sourceforge.squirrel_sql.fw.datasetviewer.DataSetViewerTablePanel;
035:
036: public class TablePopupMenu extends BasePopupMenu {
037: /** Internationalized strings for this class. */
038: private static final StringManager s_stringMgr = StringManagerFactory
039: .getStringManager(TablePopupMenu.class);
040:
041: public interface IOptionTypes {
042: int COPY = 0;
043: int COPY_WITH_HEADERS = 1;
044: int COPY_HTML = 2;
045: int COPY_IN_STATEMENT = 3;
046: int COPY_EXPORT_CSV = 4;
047: int SELECT_ALL = 5;
048: int ADOPT_ALL_COL_WIDTHS_ACTION = 6;
049: int ALWAYS_ADOPT_ALL_COL_WIDTHS_ACTION = 7;
050: int SHOW_ROW_NUMBERS = 8;
051: int LAST_ENTRY = 9;
052: }
053:
054: private static final KeyStroke COPY_STROKE = KeyStroke
055: .getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK);
056:
057: private final JMenuItem[] _menuItems = new JMenuItem[IOptionTypes.LAST_ENTRY + 1];
058:
059: private JTable _table;
060:
061: private JCheckBoxMenuItem _alwaysAdoptAllColWidtshActionItem;
062: private JCheckBoxMenuItem _showRowNumbersItem;
063:
064: private CopyAction _copy = new CopyAction();
065: private CopyWithHeadersAction _copyWithHeaders = new CopyWithHeadersAction();
066: private CopyHtmlAction _copyHtml = new CopyHtmlAction();
067: private CopyInStatementAction _copyInStatement = new CopyInStatementAction();
068: private ExportCsvAction _exportCvs = new ExportCsvAction();
069: private AdoptAllColWidthsAction _adoptAllColWidthsAction = new AdoptAllColWidthsAction();
070: private AlwaysAdoptAllColWidthsAction _alwaysAdoptAllColWidthsAction = new AlwaysAdoptAllColWidthsAction();
071: private ShowRowNumbersAction _showRowNumbersAction = new ShowRowNumbersAction();
072:
073: protected MakeEditableAction _makeEditable = new MakeEditableAction();
074: private UndoMakeEditableAction _undoMakeEditable = new UndoMakeEditableAction();
075: private DeleteRowsAction _deleteRows = new DeleteRowsAction();
076: protected InsertRowAction _insertRow = new InsertRowAction();
077: private SelectAllAction _select = new SelectAllAction();
078: private PrintAction _print = new PrintAction();
079:
080: // The following pointer is needed to allow the "Make Editable button
081: // to tell the application to set up an editable display panel
082: private IDataSetUpdateableModel _updateableModel = null;
083:
084: // pointer to the viewer
085: // This is needed for insert and delete operations
086: private DataSetViewerTablePanel _viewer = null;
087:
088: /**
089: * Constructor used when caller wants to be able to make table editable.
090: * We need both parameters because there is at least one case where the
091: * underlying data model is updateable, but we do not want to allow the
092: * user to enter editing mode because they are already in edit mode.
093: * The caller needs to determine whether or not to allow a request for edit mode.
094: */
095: public TablePopupMenu(boolean allowEditing,
096: IDataSetUpdateableModel updateableModel,
097: DataSetViewerTablePanel viewer) {
098: super ();
099: // save the pointer needed to enable editing of data on-demand
100: _updateableModel = updateableModel;
101:
102: // save the pointer needed for insert and delete operations
103: _viewer = viewer;
104:
105: // add the menu items to the menu
106: _menuItems[IOptionTypes.COPY] = add(_copy);
107: _menuItems[IOptionTypes.COPY].setAccelerator(COPY_STROKE);
108: _menuItems[IOptionTypes.COPY_WITH_HEADERS] = add(_copyWithHeaders);
109: _menuItems[IOptionTypes.COPY_HTML] = add(_copyHtml);
110: _menuItems[IOptionTypes.COPY_IN_STATEMENT] = add(_copyInStatement);
111: _menuItems[IOptionTypes.COPY_IN_STATEMENT] = add(_exportCvs);
112: addSeparator();
113: _menuItems[IOptionTypes.ADOPT_ALL_COL_WIDTHS_ACTION] = add(_adoptAllColWidthsAction);
114:
115: _alwaysAdoptAllColWidtshActionItem = new JCheckBoxMenuItem();
116: _alwaysAdoptAllColWidtshActionItem
117: .setSelected(ButtonTableHeader
118: .isAlwaysAdoptAllColWidths());
119: _alwaysAdoptAllColWidtshActionItem
120: .setAction(_alwaysAdoptAllColWidthsAction);
121: _menuItems[IOptionTypes.ALWAYS_ADOPT_ALL_COL_WIDTHS_ACTION] = add(_alwaysAdoptAllColWidtshActionItem);
122:
123: addSeparator();
124:
125: _showRowNumbersItem = new JCheckBoxMenuItem();
126: _showRowNumbersItem.setSelected(false);
127: _showRowNumbersItem.setAction(_showRowNumbersAction);
128: _menuItems[IOptionTypes.SHOW_ROW_NUMBERS] = add(_showRowNumbersItem);
129:
130: if (allowEditing) {
131: addSeparator();
132: add(_makeEditable);
133: }
134: // if ( ! allowEditing )
135: if (updateableModel != null
136: && updateableModel.editModeIsForced()) {
137: add(_undoMakeEditable);
138: }
139: addSeparator();
140: _menuItems[IOptionTypes.SELECT_ALL] = add(_select);
141:
142: // add entries for insert and delete rows
143: // only if table is updateable and already editable (ie. allowEditing is false)
144: if (_updateableModel != null && allowEditing == false) {
145: addSeparator();
146: add(_insertRow);
147: add(_deleteRows);
148: }
149:
150: addSeparator();
151: add(_print);
152: }
153:
154: /**
155: * Constructor used when creating menu for use in cell editor.
156: */
157: public TablePopupMenu(IDataSetUpdateableModel updateableModel,
158: DataSetViewerTablePanel viewer, JTable table) {
159: super ();
160: // save the pointer needed to enable editing of data on-demand
161: _updateableModel = updateableModel;
162:
163: // save the pointer needed for insert and delete operations
164: _viewer = viewer;
165:
166: _table = table;
167: replaceStandardTableCopyAction();
168:
169: // Cut and Paste need to be worked on, so for now do not include them
170: // Also, the copy operations do not seem to work right - we may need special
171: // versions for the cellEditor menu.
172: // add(_cut);
173: // add(_copy);
174: // add(_copyHtml);
175: // add(_paste);
176: // addSeparator();
177: add(_select);
178: addSeparator();
179: add(_insertRow);
180: add(_deleteRows);
181:
182: addSeparator();
183: add(_print);
184: }
185:
186: public void setTable(JTable value) {
187: _table = value;
188: replaceStandardTableCopyAction();
189: }
190:
191: public void reset() {
192: _showRowNumbersItem.setSelected(false);
193: }
194:
195: private void replaceStandardTableCopyAction() {
196: _table.getInputMap(
197: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
198: COPY_STROKE, "CopyAction");
199: _table.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
200: COPY_STROKE, "CopyAction");
201: _table.getInputMap(JComponent.WHEN_FOCUSED).put(COPY_STROKE,
202: "CopyAction");
203: _table.getActionMap().put("CopyAction", _copy);
204: }
205:
206: /**
207: * Show the menu.
208: */
209: public void show(Component invoker, int x, int y) {
210: super .show(invoker, x, y);
211: }
212:
213: public void show(MouseEvent evt) {
214: super .show(evt);
215: }
216:
217: private class CopyAction extends BaseAction {
218: CopyAction() {
219: super (s_stringMgr.getString("TablePopupMenu.copy"));
220: }
221:
222: public void actionPerformed(ActionEvent evt) {
223: if (_table != null) {
224: new TableCopyCommand(_table, false).execute();
225: }
226: }
227: }
228:
229: private class CopyWithHeadersAction extends BaseAction {
230: CopyWithHeadersAction() {
231: super (s_stringMgr
232: .getString("TablePopupMenu.copyWithHeaders"));
233: }
234:
235: public void actionPerformed(ActionEvent evt) {
236: if (_table != null) {
237: new TableCopyCommand(_table, true).execute();
238: }
239: }
240: }
241:
242: private class CopyHtmlAction extends BaseAction {
243: CopyHtmlAction() {
244: super (s_stringMgr.getString("TablePopupMenu.copyashtml"));
245: }
246:
247: public void actionPerformed(ActionEvent evt) {
248: if (_table != null) {
249: new TableCopyHtmlCommand(_table).execute();
250: }
251: }
252: }
253:
254: private class CopyInStatementAction extends BaseAction {
255: CopyInStatementAction() {
256: super (s_stringMgr
257: .getString("TablePopupMenu.copyasinstatement"));
258: }
259:
260: public void actionPerformed(ActionEvent evt) {
261: if (_table != null) {
262: new TableCopyInStatementCommand(_table).execute();
263: }
264: }
265: }
266:
267: private class ExportCsvAction extends BaseAction {
268: ExportCsvAction() {
269: // i18n[TablePopupMenu.export=Export CSV / MS Excel ...]
270: super (s_stringMgr.getString("TablePopupMenu.export"));
271: }
272:
273: public void actionPerformed(ActionEvent evt) {
274: if (_table != null) {
275: new TableExportCsvCommand(_table).execute();
276: }
277: }
278: }
279:
280: private class AdoptAllColWidthsAction extends BaseAction {
281: AdoptAllColWidthsAction() {
282: super (
283: s_stringMgr
284: .getString("TablePopupMenu.adoptAllColWidthsAction"));
285: }
286:
287: public void actionPerformed(ActionEvent evt) {
288: if (_table != null) {
289: if (_table.getTableHeader() instanceof ButtonTableHeader) {
290: ((ButtonTableHeader) _table.getTableHeader())
291: .adoptAllColWidths(true);
292: }
293: }
294: }
295: }
296:
297: private class AlwaysAdoptAllColWidthsAction extends BaseAction {
298: AlwaysAdoptAllColWidthsAction() {
299: super (s_stringMgr
300: .getString("TablePopupMenu.alwaysAdoptAllColWiths"));
301: }
302:
303: public void actionPerformed(ActionEvent evt) {
304: ButtonTableHeader
305: .setAlwaysAdoptAllColWidths(_alwaysAdoptAllColWidtshActionItem
306: .isSelected());
307: if (_table != null
308: && _alwaysAdoptAllColWidtshActionItem.isSelected()) {
309: ((ButtonTableHeader) _table.getTableHeader())
310: .adoptAllColWidths(true);
311: }
312: }
313: }
314:
315: private class ShowRowNumbersAction extends BaseAction {
316: ShowRowNumbersAction() {
317: super (s_stringMgr
318: .getString("TablePopupMenu.showRowNumbers"));
319: }
320:
321: public void actionPerformed(ActionEvent evt) {
322: if (_table != null) {
323: JCheckBoxMenuItem mnu = (JCheckBoxMenuItem) evt
324: .getSource();
325: new ShowRowNumbersCommand(_viewer, mnu.isSelected())
326: .execute();
327: }
328: }
329: }
330:
331: private class MakeEditableAction extends BaseAction {
332: MakeEditableAction() {
333: super (s_stringMgr.getString("TablePopupMenu.makeeditable"));
334: }
335:
336: public void actionPerformed(ActionEvent evt) {
337: if (_updateableModel != null) {
338: new MakeEditableCommand(_updateableModel).execute();
339: }
340: }
341: }
342:
343: private class UndoMakeEditableAction extends BaseAction {
344: UndoMakeEditableAction() {
345: super (s_stringMgr
346: .getString("TablePopupMenu.undomakeeditable"));
347: }
348:
349: public void actionPerformed(ActionEvent evt) {
350: if (_updateableModel != null) {
351: new UndoMakeEditableCommand(_updateableModel).execute();
352: }
353: }
354: }
355:
356: private class DeleteRowsAction extends BaseAction {
357: DeleteRowsAction() {
358: super (s_stringMgr.getString("TablePopupMenu.deleterows"));
359: }
360:
361: public void actionPerformed(ActionEvent evt) {
362: if (_table != null) {
363: int selectedRows[] = _table.getSelectedRows();
364:
365: // Tell the DataSetViewer to delete the rows
366: // Note: rows are indexes in the SORTABLE model, not the ACTUAL model
367: _viewer.deleteRows(selectedRows);
368: }
369: }
370: }
371:
372: private class InsertRowAction extends BaseAction {
373: InsertRowAction() {
374: super (s_stringMgr.getString("TablePopupMenu.insertrow"));
375: }
376:
377: public void actionPerformed(ActionEvent evt) {
378: _viewer.insertRow();
379: }
380: }
381:
382: private class SelectAllAction extends BaseAction {
383: SelectAllAction() {
384: super (s_stringMgr.getString("TablePopupMenu.selectall"));
385: }
386:
387: public void actionPerformed(ActionEvent evt) {
388: if (_table != null) {
389: new TableSelectAllCellsCommand(_table).execute();
390: }
391: }
392: }
393:
394: private class PrintAction extends BaseAction {
395: PrintAction() {
396: super (s_stringMgr.getString("TablePopupMenu.print"));
397: }
398:
399: public void actionPerformed(ActionEvent evt) {
400: if (_table != null) {
401: try {
402:
403: PrinterJob printerJob = PrinterJob.getPrinterJob();
404:
405: printerJob.setPrintable(_viewer);
406:
407: if (printerJob.printDialog()) {
408: printerJob.print();
409: }
410: } catch (Exception e) {
411: throw new RuntimeException(e);
412: }
413: }
414: }
415: }
416: }
|