preparing new release
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / DeleteAllRowsAction.java
1 package com.quantum.actions;
2
3 import java.sql.SQLException;
4
5 import com.quantum.Messages;
6 import com.quantum.model.ConnectionException;
7 import com.quantum.model.Table;
8 import com.quantum.ui.dialog.ExceptionDisplayDialog;
9 import com.quantum.ui.dialog.SQLExceptionDialog;
10 import com.quantum.view.bookmark.EntityNode;
11
12 import org.eclipse.jface.dialogs.MessageDialog;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.ui.IViewPart;
16 import org.eclipse.ui.actions.SelectionListenerAction;
17
18 /**
19  * @author root
20  *
21  */
22 public class DeleteAllRowsAction extends SelectionListenerAction {
23     private IViewPart view;
24     
25         /**
26          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
27          */
28         public DeleteAllRowsAction(IViewPart view) {
29         super(Messages.getString(DeleteAllRowsAction.class.getName() + ".text"));
30                 this.view = view;
31         }
32
33         public void run() {
34         Table table = getTable();
35         try {
36                         if (table != null) {
37                                 boolean flag = MessageDialog.openConfirm(
38                     view.getSite().getShell(), 
39                     Messages.getString(DeleteAllRowsAction.class.getName() + ".confirmTitle"),
40                     Messages.getString(DeleteAllRowsAction.class.getName() + ".confirmText",  
41                     new Object[] { table.getQualifiedName() }));
42                                 if (flag) {
43                                         table.deleteAllRows();
44                                 }
45                         }
46         } catch (SQLException e) {
47             SQLExceptionDialog.openException(getShell(), 
48                         table == null ? null : table.getBookmark(), e);
49         } catch (ConnectionException e) {
50             ExceptionDisplayDialog.openError(getShell(), 
51                 Messages.getString("ExecuteAgainstAction.title"), 
52                 Messages.getString("ExecuteAgainstAction.ConnectionException"), e);
53         }
54         }
55
56     private Table getTable() {
57         EntityNode node = (EntityNode) getSelectedNonResources().get(0);
58         return node == null ? null : (Table) node.getEntity();
59     }
60     
61     
62
63     protected Shell getShell() {
64         return this.view.getViewSite().getShell();
65     }
66
67     /* (non-Javadoc)
68      * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
69      */
70     protected boolean updateSelection(IStructuredSelection selection) {
71         boolean enabled = super.updateSelection(selection);
72         return enabled && selection.size() == 1 && 
73             (selection.getFirstElement() instanceof EntityNode) &&
74             ((EntityNode) selection.getFirstElement()).isTable();
75     }
76
77 }