misc
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / RenameAction.java
1 package com.quantum.view.bookmark;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5
6 import org.eclipse.jface.viewers.IStructuredSelection;
7 import org.eclipse.jface.wizard.Wizard;
8 import org.eclipse.jface.wizard.WizardDialog;
9 import org.eclipse.ui.IViewPart;
10 import org.eclipse.ui.actions.SelectionListenerAction;
11
12 import com.quantum.Messages;
13 import com.quantum.model.Bookmark;
14 import com.quantum.wizards.BookmarkNameWizardPage;
15
16 /**
17  * @author BC
18  */
19 public class RenameAction extends SelectionListenerAction {
20         
21         public class RenameWizard extends Wizard implements PropertyChangeListener {
22                 
23                 private BookmarkNameWizardPage page;
24                 private String name;
25                 
26                 public RenameWizard(String name) {
27                         this.name = name;
28                 }
29                 
30                 public void addPages() {
31                         super.addPages();
32                         this.page = new BookmarkNameWizardPage("pageName", this.name);
33                         this.page.addPropertyChangeListener(this);
34                         addPage(this.page);
35                 }
36                 
37                 public void dispose() {
38                         this.page.removePropertyChangeListener(this);
39                         super.dispose();
40                 }
41                 public boolean performFinish() {
42                         return true;
43                 }
44
45                 public void propertyChange(PropertyChangeEvent event) {
46                         if ("name".equals(event.getPropertyName())) {
47                                 setName((String) event.getNewValue());
48                         }
49                 }
50                 
51                 public String getName() {
52                         return this.name;
53                 }
54                 public void setName(String name) {
55                         this.name = name;
56                 }
57         }
58         
59
60     private IViewPart viewPart;
61
62     /**
63      * @param text
64      */
65     protected RenameAction(IViewPart viewPart) {
66         super(Messages.getString(RenameAction.class.getName() + ".text"));
67         this.viewPart = viewPart;
68     }
69
70     /**
71      * @see org.eclipse.ui.actions.SelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
72      */
73     protected boolean updateSelection(IStructuredSelection selection) {
74         boolean enabled = super.updateSelection(selection);
75         return enabled & selection.size() == 1 & 
76             selection.getFirstElement() instanceof BookmarkNode;
77     }
78     
79     private Bookmark getBookmark() {
80         return ((BookmarkNode) getSelectedNonResources().get(0)).getBookmark();
81     }
82
83     /**
84      * @see org.eclipse.jface.action.IAction#run()
85      */
86     public void run() {
87         RenameWizard wizard = new RenameWizard(getBookmark().getName());
88         WizardDialog dialog = new WizardDialog(this.viewPart.getSite().getShell(), wizard);
89         int result = dialog.open();
90         if (result == WizardDialog.OK) {
91             getBookmark().setName(wizard.getName());
92         }
93     }
94 }