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