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