synchronized from quantum plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / BookmarkWizard.java
1 package com.quantum.wizards;
2
3 import org.eclipse.jface.dialogs.MessageDialog;
4 import org.eclipse.jface.wizard.Wizard;
5 import org.eclipse.jface.wizard.WizardPage;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.events.SelectionAdapter;
8 import org.eclipse.swt.events.SelectionEvent;
9 import org.eclipse.swt.events.SelectionListener;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Button;
13 import org.eclipse.swt.widgets.Combo;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.FileDialog;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Text;
18
19 import com.quantum.Messages;
20 import com.quantum.QuantumPlugin;
21 import com.quantum.adapters.AdapterFactory;
22 import com.quantum.adapters.DriverInfo;
23 import com.quantum.model.Bookmark;
24 import com.quantum.model.BookmarkCollection;
25 import com.quantum.model.JDBCDriver;
26
27 public class BookmarkWizard extends Wizard {
28         BookmarkPage mainPage;
29
30         public void init() {
31                 System.out.println("Initing workbench"); //$NON-NLS-1$
32                 setWindowTitle(Messages.getString("BookmarkWizard.NewBookmark")); //$NON-NLS-1$
33         }
34         public boolean performFinish() {
35                 System.out.println("perform finish workbench"); //$NON-NLS-1$
36                 String bookmarkName = mainPage.name.getText();
37                 if ( BookmarkCollection.getInstance().find(bookmarkName) != null){
38                         MessageDialog.openInformation(
39                                 this.getShell(),Messages.getString("BookmarkWizard.Error"),Messages.getString("BookmarkWizard.bookmarkAlreadyExists"));  //$NON-NLS-1$ //$NON-NLS-2$
40                                 return false;
41                 }
42
43                 mainPage.performFinish();
44                 return true;
45         }
46         public void addPages() {
47                 System.out.println("adding pages"); //$NON-NLS-1$
48 //              WizardPage page = new NewBookmarkPage1(Messages.getString("BookmarkWizard.Testing")); //$NON-NLS-1$
49 //              addPage(page);
50                 mainPage = new BookmarkPage(Messages.getString("BookmarkWizard.Testing")); //$NON-NLS-1$
51                 addPage(mainPage);
52                 System.out.println("adding pages"); //$NON-NLS-1$
53         }
54 }
55
56 class BookmarkPage extends WizardPage {
57         public static final String ADD = "ADD"; //$NON-NLS-1$
58         String action = ADD;
59         Text name;
60         Text username;
61         Text password;
62         Text schema;
63         Text connect;
64         Text driver;
65         //List driverList;
66         Combo type;
67         Text driverFile;
68     Button prompt;
69
70         FileDialog dialog;
71
72         DriverInfo[] drivers = AdapterFactory.getInstance().getDriverList();
73         /**
74          * Constructor for BookmarkPage.
75          * @param pageName
76          */
77         public BookmarkPage(String pageName) {
78                 super(pageName);
79         }
80         public void createControl(Composite parent) {
81                 System.out.println("page create control"); //$NON-NLS-1$
82                 dialog = new FileDialog(getContainer().getShell(), SWT.OPEN);
83                 dialog.setFilterExtensions(new String[]{"*.jar", "*.zip","*.*"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
84                 dialog.setFilterNames(new String[]{Messages.getString("BookmarkWizard.JarFiles"),Messages.getString("BookmarkWizard.ZipFiles"), Messages.getString("BookmarkWizard.AllFiles")}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
85                 Composite container = new Composite(parent, SWT.NULL);
86                 GridLayout layout = new GridLayout();
87                 container.setLayout(layout);
88                 layout.numColumns = 2;
89                 layout.verticalSpacing = 9;
90
91
92                 Label label = new Label(container, SWT.NULL);
93                 label.setText(Messages.getString("BookmarkWizard.BookmarkNameAst")); //$NON-NLS-1$
94                 name = new Text(container, SWT.BORDER | SWT.SINGLE);
95                 GridData fullHorizontal = new GridData();
96                 fullHorizontal.horizontalAlignment = GridData.FILL;
97                 name.setLayoutData(fullHorizontal);
98
99                 label = new Label(container, SWT.NULL);
100                 label.setText(Messages.getString("BookmarkWizard.UsernameAst")); //$NON-NLS-1$
101                 username = new Text(container, SWT.BORDER | SWT.SINGLE);
102                 fullHorizontal = new GridData();
103                 fullHorizontal.horizontalAlignment = GridData.FILL;
104                 username.setLayoutData(fullHorizontal);
105
106                 label = new Label(container, SWT.NULL);
107                 label.setText(Messages.getString("BookmarkWizard.PasswordAst")); //$NON-NLS-1$
108                 password = new Text(container, SWT.BORDER | SWT.SINGLE);
109                 password.setEchoChar('*');
110                 fullHorizontal = new GridData();
111                 fullHorizontal.horizontalAlignment = GridData.FILL;
112                 password.setLayoutData(fullHorizontal);
113  
114         this.prompt = new Button(container, SWT.CHECK);
115         this.prompt.setText(Messages.getString("BookmarkWizard.Prompt")); //$NON-NLS-1$
116         fullHorizontal = new GridData();
117         fullHorizontal.horizontalAlignment = GridData.FILL;
118         fullHorizontal.horizontalSpan = 2;
119         this.prompt.setLayoutData(fullHorizontal);
120  
121                 label = new Label(container, SWT.NULL);
122                 label.setText(Messages.getString("BookmarkWizard.Schema")); //$NON-NLS-1$
123                 schema = new Text(container, SWT.BORDER | SWT.SINGLE);
124                 fullHorizontal = new GridData();
125                 fullHorizontal.horizontalAlignment = GridData.FILL;
126                 schema.setLayoutData(fullHorizontal);
127
128                 label = new Label(container, SWT.NULL);
129                 label.setText(Messages.getString("BookmarkWizard.ConnectAst")); //$NON-NLS-1$
130                 connect = new Text(container, SWT.BORDER | SWT.SINGLE);
131                 fullHorizontal = new GridData();
132                 fullHorizontal.horizontalAlignment = GridData.FILL;
133                 connect.setLayoutData(fullHorizontal);
134
135                 label = new Label(container, SWT.NULL);
136                 label.setText(Messages.getString("BookmarkWizard.DriverAst")); //$NON-NLS-1$
137                 driver = new Text(container, SWT.BORDER | SWT.SINGLE);
138                 fullHorizontal = new GridData();
139                 fullHorizontal.horizontalAlignment = GridData.FILL;
140                 driver.setLayoutData(fullHorizontal);
141
142                 label = new Label(container, SWT.NULL);
143                 label.setText(Messages.getString("BookmarkWizard.TypeAst")); //$NON-NLS-1$
144                 type = new Combo(container, SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY);
145                 String driverNames[] = new String[drivers.length];
146                 for (int i = 0; i < drivers.length; i++) {
147                         driverNames[i] = drivers[i].getDisplayName();
148                 }
149                 type.setItems(driverNames);
150                 type.select(0);
151                 fullHorizontal = new GridData();
152                 fullHorizontal.horizontalAlignment = GridData.FILL;
153                 type.setLayoutData(fullHorizontal);
154
155                 label = new Label(container, SWT.NULL);
156                 label.setText(Messages.getString("BookmarkWizard.DriverFilenameAst")); //$NON-NLS-1$
157                 driverFile = new Text(container, SWT.BORDER | SWT.SINGLE);
158                 fullHorizontal = new GridData();
159                 fullHorizontal.horizontalAlignment = GridData.FILL;
160                 driverFile.setLayoutData(fullHorizontal);
161
162                 Button button = new Button(container, SWT.PUSH);
163                 button.setText(Messages.getString("BookmarkWizard.Browse")); //$NON-NLS-1$
164                 
165                 button.addSelectionListener(new SelectionListener() {
166                         public void widgetDefaultSelected(SelectionEvent e) {
167                         }
168                         public void widgetSelected(SelectionEvent e) {
169                                 // We retrieve the last saved path
170                                 dialog.setFilterPath(QuantumPlugin.getDefault().getPreferenceStore().getString("quantum.dialogs.bookmarkwizard.path")); //$NON-NLS-1$
171                                 String filename = dialog.open();
172                                 if (filename != null) {
173                                         driverFile.setText(filename);
174                                 //      We save the used path
175                                          QuantumPlugin.getDefault().getPreferenceStore().setValue("quantum.dialogs.bookmarkwizard.path", filename); //$NON-NLS-1$
176                 
177                                 }
178                         }
179                 });
180
181         prompt.addSelectionListener(new SelectionAdapter() {
182             public void widgetSelected(SelectionEvent event) {
183                 BookmarkPage.this.password.setEditable(!prompt.getSelection());
184             }
185         });
186
187                 setControl(container);
188
189                 setPageComplete(true);
190         }
191         public void updateDriverList() {
192                         /*try {
193                                 JarFile file = new JarFile(driverFile.getText());
194                                 Enumeration enum = file.entries();
195                                 while (enum.hasMoreElements()) {
196                                         JarEntry entry = (JarEntry) enum.nextElement();
197                                         String className = entry.getName().replace('/', '.');
198                                         if (className.endsWith("Driver.class")) {
199                                                 className = className.substring(0, className.lastIndexOf('.'));
200                                                 //driverList.add(className);
201                                         }
202                                 }
203                         } catch (IOException ex) {
204                                 //driverList.removeAll();
205                         }*/
206         }
207         public void performFinish() {
208                 Bookmark initialData = new Bookmark();
209                 initialData.setName(name.getText());
210                 initialData.setUsername(username.getText());
211         initialData.setPromptForPassword(this.prompt.getSelection());
212         if (initialData.getPromptForPassword()) {
213             initialData.setPassword(""); //$NON-NLS-1$
214         } else {
215                 initialData.setPassword(password.getText());
216         }
217                 initialData.addSchema(schema.getText());
218                 initialData.setConnect(connect.getText());
219                 JDBCDriver jdbcDriver = BookmarkCollection.getInstance().findDriver(
220                                 driver.getText(), driverFile.getText());
221                 initialData.setJDBCDriver(jdbcDriver);
222                 int selection = type.getSelectionIndex();
223                 if (selection >= 0) {
224                         initialData.setType(drivers[selection].getDriverType());
225                 }
226         BookmarkCollection.getInstance().addBookmark(initialData);
227         }
228 }