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