intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / BookmarkNameWizardPage.java
1 package com.quantum.wizards;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.events.ModifyEvent;
5 import org.eclipse.swt.events.ModifyListener;
6 import org.eclipse.swt.layout.GridData;
7 import org.eclipse.swt.layout.GridLayout;
8 import org.eclipse.swt.widgets.Composite;
9 import org.eclipse.swt.widgets.Label;
10 import org.eclipse.swt.widgets.Text;
11
12 import com.quantum.Messages;
13 import com.quantum.model.BookmarkCollection;
14
15
16 /**
17  * @author BC
18  */
19 public class BookmarkNameWizardPage extends PropertyChangeWizardPage {
20         
21         private String name;
22         private final String originalName;
23
24         public BookmarkNameWizardPage(String pageName) {
25                 this(pageName, null);
26         }
27
28         /**
29          * @param pageName
30          */
31         public BookmarkNameWizardPage(String pageName, String originalName) {
32                 super(pageName);
33                 this.originalName = originalName;
34                 setTitle(Messages.getString(getClass(), "title"));
35                 setDescription(Messages.getString(getClass(), "description"));
36         }
37
38         public void createControl(Composite parent) {
39                 setPageComplete(false);
40                 
41                 Composite composite = new Composite(parent, SWT.NONE);
42                 GridLayout layout = new GridLayout();
43                 layout.numColumns = 2;
44                 composite.setLayout(layout);
45                 
46                 Label label = new Label(composite, SWT.NONE);
47                 label.setText(Messages.getString(getClass(), "name"));
48                 
49                 Text name = new Text(composite, SWT.BORDER | SWT.SINGLE);
50                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
51                 name.setLayoutData(data);
52                 if (this.originalName != null) {
53                         name.setText(this.originalName);
54                 }
55                 name.addModifyListener(new ModifyListener() {
56                         public void modifyText(ModifyEvent event) {
57                                 String name = ((Text) event.getSource()).getText();
58                                 setName(name);
59                         }
60                 });
61                 setControl(composite);
62         }
63
64         /**
65          * 
66          */
67         private void setName(String bookmarkName) {
68                 String errorMessage = null;
69                 boolean done = true;
70                 if (bookmarkName == null || bookmarkName.trim().length() == 0) {
71                         errorMessage = Messages.getString(getClass(), "mustProvide");
72                         done = false;
73                 } else if (this.originalName != null && this.originalName.equals(bookmarkName)) {
74                         done = false;
75                 } else if (BookmarkCollection.getInstance().find(bookmarkName) != null) {
76                         errorMessage = Messages.getString(getClass(), "alreadyExists");
77                         done = false;
78                 }
79                 
80                 setErrorMessage(errorMessage);
81                 setPageComplete(done);
82
83                 if (errorMessage == null && !bookmarkName.equals(this.name)) {
84                         String original = this.name;
85                         this.name = bookmarkName;
86                         firePropertyChange("name", original, bookmarkName);
87                 }
88         }
89
90         /**
91          * @return Returns the name.
92          */
93         public String getName() {
94                 return this.name;
95         }
96 }