KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > wizards > NewConfigurationWizardPage


1 package org.hibernate.eclipse.console.wizards;
2
3 import org.eclipse.core.resources.IContainer;
4 import org.eclipse.core.resources.IFile;
5 import org.eclipse.core.resources.IResource;
6 import org.eclipse.core.resources.ResourcesPlugin;
7 import org.eclipse.core.runtime.Path;
8 import org.eclipse.jface.dialogs.IDialogPage;
9 import org.eclipse.jface.viewers.ISelection;
10 import org.eclipse.jface.wizard.WizardPage;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.ModifyEvent;
13 import org.eclipse.swt.events.ModifyListener;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Combo;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Text;
20 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
21 import org.hibernate.eclipse.console.utils.DriverClassHelpers;
22
23 /**
24  * Wizard for creating basic hibernate.cfg.xml
25  */

26
27 public class NewConfigurationWizardPage extends WizardPage {
28     final private DriverClassHelpers helper = new DriverClassHelpers();
29
30     private Label containerText;
31
32     private Label fileText;
33
34     private Text sessionFactoryNameText;
35
36     private Combo dialectCombo;
37
38     private Combo driver_classCombo;
39
40     private Text usernameText;
41
42     private Text passwordText;
43
44     private Combo urlCombo;
45
46     private Text datasourceName;
47     private Text jndiURL;
48     private Text jndiClassname;
49     
50     
51     private ISelection selection;
52
53     private final WizardNewFileCreationPage fileCreation;
54
55     private boolean beenShown = false;
56
57     /**
58      * Constructor for SampleNewWizardPage.
59      * @param page
60      *
61      * @param pageName
62      */

63     public NewConfigurationWizardPage(ISelection selection, WizardNewFileCreationPage page) {
64         super("wizardPage");
65         this.fileCreation = page;
66         setTitle("Hibernate Configuration File (cfg.xml)");
67         setDescription("This wizard creates a new configuration file to use with Hibernate.");
68         this.selection = selection;
69     }
70
71     /**
72      * @see IDialogPage#createControl(Composite)
73      */

74     public void createControl(Composite parent) {
75
76         ModifyListener listener = new ModifyListener() {
77             public void modifyText(ModifyEvent e) {
78                 dialogChanged();
79             }
80         };
81
82         Composite container = new Composite(parent, SWT.NULL);
83         GridLayout layout = new GridLayout();
84         container.setLayout(layout);
85         layout.numColumns = 3;
86         layout.verticalSpacing = 9;
87         Label label = new Label(container, SWT.NULL);
88         label.setText("&Container:");
89
90         containerText = new Label(container, SWT.BORDER | SWT.SINGLE);
91         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
92         containerText.setLayoutData(gd);
93         fillLabel(container);
94         
95         label = new Label(container, SWT.NULL);
96         label.setText("&File name:");
97
98         fileText = new Label(container, SWT.BORDER | SWT.SINGLE);
99         gd = new GridData(GridData.FILL_HORIZONTAL);
100         fileText.setLayoutData(gd);
101         
102         fillLabel(container);
103
104         label = new Label(container, SWT.NULL);
105         label.setText("&Session factory name:");
106         sessionFactoryNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
107         gd = new GridData(GridData.FILL_HORIZONTAL);
108         sessionFactoryNameText.setLayoutData(gd);
109         sessionFactoryNameText.addModifyListener(listener);
110         fillLabel(container);
111
112         label = new Label(container, SWT.NULL);
113         label.setText("&Database dialect:");
114         dialectCombo = new Combo(container, SWT.NULL);
115         fillHerUp(dialectCombo, helper.getDialectNames());
116         dialectCombo.select(0);
117         gd = new GridData(GridData.FILL_HORIZONTAL);
118         gd.grabExcessHorizontalSpace = true;
119         dialectCombo.setLayoutData(gd);
120         dialectCombo.addModifyListener(new ModifyListener() {
121             public void modifyText(ModifyEvent e) {
122                 String JavaDoc[] driverClasses = helper.getDriverClasses(helper
123                         .getDialectClass(dialectCombo.getText()));
124                 fillHerUp(driver_classCombo, driverClasses);
125                 dialogChanged();
126             }
127         });
128         fillLabel(container);
129
130         gd = new GridData(GridData.BEGINNING, GridData.CENTER, false,false);
131         gd.horizontalAlignment = SWT.TOP;
132         gd.verticalAlignment = SWT.TOP;
133         label.setLayoutData(gd);
134         
135         Composite driverManagerTabContainer = container;
136         label = new Label(driverManagerTabContainer, SWT.NULL);
137         label.setText("&Driver class:");
138         driver_classCombo = new Combo(driverManagerTabContainer, SWT.NULL);
139         driver_classCombo.select(0);
140         gd = new GridData(GridData.FILL_HORIZONTAL);
141         gd.grabExcessHorizontalSpace = true;
142         driver_classCombo.setLayoutData(gd);
143         driver_classCombo.addModifyListener(new ModifyListener() {
144             public void modifyText(ModifyEvent e) {
145                 String JavaDoc[] connectionURLS = helper
146                         .getConnectionURLS(driver_classCombo.getText());
147                 fillHerUp(urlCombo, connectionURLS);
148                 dialogChanged();
149             }
150         });
151         fillLabel(driverManagerTabContainer);
152
153         label = new Label(driverManagerTabContainer, SWT.NULL);
154         label.setText("Connection &URL:");
155         urlCombo = new Combo(driverManagerTabContainer, SWT.NULL);
156         urlCombo.select(0);
157         gd = new GridData(GridData.FILL_HORIZONTAL);
158         gd.grabExcessHorizontalSpace = true;
159         urlCombo.setLayoutData(gd);
160         urlCombo.addModifyListener(listener);
161         fillLabel(driverManagerTabContainer);
162
163         label = new Label(driverManagerTabContainer, SWT.NULL);
164         label.setText("User&name:");
165         usernameText = new Text(driverManagerTabContainer, SWT.BORDER | SWT.SINGLE);
166         gd = new GridData(GridData.FILL_HORIZONTAL);
167         usernameText.setLayoutData(gd);
168         usernameText.addModifyListener(listener);
169         fillLabel(driverManagerTabContainer);
170
171         label = new Label(driverManagerTabContainer, SWT.NULL);
172         label.setText("&Password:");
173         passwordText = new Text(driverManagerTabContainer, SWT.BORDER | SWT.SINGLE);
174         gd = new GridData(GridData.FILL_HORIZONTAL);
175         passwordText.setLayoutData(gd);
176         passwordText.addModifyListener(listener);
177         fillLabel(driverManagerTabContainer);
178         
179         /*
180         label = new Label(driverManagerTabContainer, SWT.NULL);
181         label.setText("Data&source name:");
182         datasourceName = new Text(driverManagerTabContainer, SWT.BORDER | SWT.SINGLE);
183         gd = new GridData(GridData.FILL_HORIZONTAL);
184         datasourceName.setLayoutData(gd);
185         datasourceName.addModifyListener(listener);
186         fillLabel(driverManagerTabContainer);
187         
188         label = new Label(driverManagerTabContainer, SWT.NULL);
189         label.setText("JNDI URL:");
190         jndiURL = new Text(driverManagerTabContainer, SWT.BORDER | SWT.SINGLE);
191         gd = new GridData(GridData.FILL_HORIZONTAL);
192         jndiURL.setLayoutData(gd);
193         jndiURL.addModifyListener(listener);
194         fillLabel(driverManagerTabContainer);
195         
196         label = new Label(driverManagerTabContainer, SWT.NULL);
197         label.setText("JNDI Classname:");
198         jndiClassname = new Text(driverManagerTabContainer, SWT.BORDER | SWT.SINGLE);
199         gd = new GridData(GridData.FILL_HORIZONTAL);
200         jndiClassname.setLayoutData(gd);
201         jndiClassname.addModifyListener(listener);
202         fillLabel(driverManagerTabContainer);*/

203         
204         initialize();
205         dialogChanged();
206         
207         setControl(container);
208     }
209
210     /**
211      * @param urlCombo2
212      */

213     private void fillHerUp(Combo combo, String JavaDoc[] newContent) {
214
215         String JavaDoc original = combo.getText();
216         combo.removeAll();
217         for (int i = 0; i < newContent.length; i++) {
218             String JavaDoc name = newContent[i];
219             combo.add(name);
220         }
221         combo.setText(original);
222     }
223
224     /**
225      * @param container
226      */

227     private void fillLabel(Composite container) {
228         new Label(container, SWT.NULL);
229     }
230
231     /**
232      * Tests if the current workbench selection is a suitable container to use.
233      */

234
235     private void initialize() {
236         updateStatus(null);
237         /*if (selection != null && selection.isEmpty() == false
238                 && selection instanceof IStructuredSelection) {
239             IStructuredSelection ssel = (IStructuredSelection) selection;
240             if (ssel.size() > 1)
241                 return;
242             Object obj = ssel.getFirstElement();
243             if (obj instanceof IResource) {
244                 IContainer container;
245                 if (obj instanceof IContainer)
246                     container = (IContainer) obj;
247                 else
248                     container = ((IResource) obj).getParent();
249                 containerText.setText(container.getFullPath().toString());
250             }
251         }
252         fileText.setText("hibernate.cfg.xml");*/

253     }
254
255     /**
256      * Ensures that contents is ok.
257      */

258     private void dialogChanged() {
259         IResource container = ResourcesPlugin.getWorkspace().getRoot()
260                 .findMember(new Path(getContainerName()));
261         String JavaDoc fileName = getFileName();
262
263         if (getContainerName().length() == 0) {
264             updateStatus("File container must be specified");
265             return;
266         }
267         if (container == null
268                 || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
269             updateStatus("File container must exist");
270             return;
271         }
272         
273         if (!container.isAccessible()) {
274             updateStatus("Project must be writable");
275             return;
276         }
277                 
278         if (fileName.length() == 0) {
279             updateStatus("File name must be specified");
280             return;
281         }
282         if (fileName.replace('\\', '/').indexOf('/', 1) > 0) {
283             updateStatus("File name must be valid");
284             return;
285         }
286         if (!fileName.endsWith(".cfg.xml")) {
287             updateStatus("File extension must be \"cfg.xml\"");
288             return;
289         }
290
291         IFile file = ((IContainer) container).getFile(new Path(fileName));
292         if(file.exists()) {
293             updateStatus("File already exists");
294             return;
295         }
296         // TODO: check for driver class availability.
297
updateStatus(null);
298     }
299
300     private void updateStatus(String JavaDoc message) {
301         setErrorMessage(message);
302         setPageComplete(message == null && beenShown);
303     }
304
305     private String JavaDoc getContainerName() {
306         return containerText.getText();
307     }
308
309     private String JavaDoc getFileName() {
310         return fileText.getText();
311     }
312
313     /**
314      * @return
315      */

316     public String JavaDoc getSessionFactoryName() {
317         return nullIfEmpty(sessionFactoryNameText.getText());
318     }
319
320     /**
321      * @param text
322      * @return
323      */

324     private String JavaDoc nullIfEmpty(String JavaDoc text) {
325         if (text != null && text.trim().length() > 0) {
326             return text.trim();
327         }
328         return null;
329     }
330
331     /**
332      * @return
333      */

334     public String JavaDoc getDialect() {
335         return nullIfEmpty(helper.getDialectClass(dialectCombo.getText()));
336     }
337
338     /**
339      * @return
340      */

341     public String JavaDoc getDriver() {
342         return nullIfEmpty(driver_classCombo.getText());
343     }
344
345     /**
346      * @return
347      */

348     public String JavaDoc getConnectionURL() {
349         return nullIfEmpty(urlCombo.getText());
350     }
351
352     /**
353      * @return
354      */

355     public String JavaDoc getUsername() {
356         return nullIfEmpty(usernameText.getText());
357     }
358
359     /**
360      * @return
361      */

362     public String JavaDoc getPassword() {
363         return nullIfEmpty(passwordText.getText());
364     }
365
366     /* (non-Javadoc)
367      * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
368      */

369     public void setVisible(boolean visible) {
370         containerText.setText(fileCreation.getContainerFullPath().toPortableString());
371         fileText.setText(fileCreation.getFileName());
372         super.setVisible(visible);
373         if(visible) {
374             sessionFactoryNameText.setFocus();
375         }
376         beenShown = true;
377         dialogChanged();
378     }
379 }
Popular Tags