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