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