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