KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.hibernate.eclipse.console.wizards;
2
3 import org.eclipse.core.resources.IFile;
4 import org.eclipse.core.resources.IFolder;
5 import org.eclipse.core.resources.IProject;
6 import org.eclipse.core.resources.IResource;
7 import org.eclipse.core.resources.ResourcesPlugin;
8 import org.eclipse.core.runtime.CoreException;
9 import org.eclipse.core.runtime.IPath;
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.core.runtime.Path;
12 import org.eclipse.jdt.core.JavaConventions;
13 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ComboDialogField;
14 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
15 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
16 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
17 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField;
18 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
19 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
20 import org.eclipse.jface.dialogs.IDialogPage;
21 import org.eclipse.jface.dialogs.IMessageProvider;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.wizard.WizardPage;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Label;
31 import org.hibernate.console.ConsoleConfiguration;
32 import org.hibernate.console.KnownConfigurations;
33 import org.hibernate.console.node.ConfigurationNode;
34 import org.hibernate.eclipse.console.utils.DialogSelectionHelper;
35
36 /**
37  * The "New" wizard page allows setting the container for the new file as well
38  * as the file name. The page will only accept file name without the extension
39  * OR with the extension that matches the expected one (mpe).
40  */

41
42 public class BasicGeneratorSettingsPage extends WizardPage {
43     private ComboDialogField consoleConfigurationName;
44
45     private ISelection selection;
46
47     private SelectionButtonDialogField reverseengineer;
48
49     private SelectionButtonDialogField generatecfgfile;
50
51     private SelectionButtonDialogField generatejava;
52     private SelectionButtonDialogField enableEJB3annotations;
53     
54     private SelectionButtonDialogField generatemappings;
55
56     private SelectionButtonDialogField generatedocs;
57     
58     private StringButtonDialogField outputdir;
59     
60     private StringButtonDialogField reverseEngineeringSettings;
61     
62     private StringDialogField packageName;
63
64     private SelectionButtonDialogField preferRawCompositeIds;
65
66     private SelectionButtonDialogField useOwnTemplates;
67     private StringButtonDialogField templatedir;
68     
69     //private Package
70

71     /**
72      * Constructor for SampleNewWizardPage.
73      *
74      * @param pageName
75      */

76     public BasicGeneratorSettingsPage(ISelection selection) {
77         super("wizardPage");
78         setTitle("Basic settings for artifact generation");
79         setDescription("This wizard allows you to generate artifacts (configuration, mapping & source code files)");
80         this.selection = selection;
81     }
82
83     
84     /**
85      * @see IDialogPage#createControl(Composite)
86      */

87     public void createControl(Composite parent) {
88         Composite container = new Composite(parent, SWT.NULL);
89         GridLayout layout = new GridLayout();
90         
91         container.setLayout(layout);
92         layout.numColumns = 3;
93         layout.verticalSpacing = 9;
94         
95         consoleConfigurationName = new ComboDialogField(SWT.READ_ONLY);
96         consoleConfigurationName.setLabelText("Console &configuration:");
97         ConsoleConfiguration[] cfg = KnownConfigurations.getInstance().getConfigurations();
98         String JavaDoc[] names = new String JavaDoc[cfg.length];
99         for (int i = 0; i < cfg.length; i++) {
100             ConsoleConfiguration configuration = cfg[i];
101             names[i] = configuration.getName();
102         }
103         consoleConfigurationName.setItems(names);
104         
105         IDialogFieldListener fieldlistener = new IDialogFieldListener() {
106             public void dialogFieldChanged(DialogField field) {
107                 dialogChanged();
108             }
109         };
110         
111         consoleConfigurationName.setDialogFieldListener(fieldlistener);
112         
113         outputdir = new StringButtonDialogField(new IStringButtonAdapter() {
114             public void changeControlPressed(DialogField field) {
115                 IPath[] paths = DialogSelectionHelper.chooseFileEntries(getShell(), getOutputDirectory(), new IPath[0], "Select output directory", "Choose directory in which the generated files will be stored", new String JavaDoc[] {"cfg.xml"}, false, true, false);
116                 if(paths!=null && paths.length==1) {
117                     outputdir.setText(((paths[0]).toOSString()));
118                 }
119             }
120         });
121         outputdir.setDialogFieldListener(fieldlistener);
122         outputdir.setLabelText("Output &directory:");
123         outputdir.setButtonLabel("&Browse...");
124         
125         templatedir = new StringButtonDialogField(new IStringButtonAdapter() {
126             public void changeControlPressed(DialogField field) {
127                 IPath[] paths = DialogSelectionHelper.chooseFileEntries(getShell(), getTemplateDirectory(), new IPath[0], "Select output directory", "Choose directory in which the generated files will be stored", new String JavaDoc[0], false, true, false);
128                 if(paths!=null && paths.length==1) {
129                     templatedir.setText(((paths[0]).toOSString()));
130                 }
131             }
132         });
133         templatedir.setDialogFieldListener(fieldlistener);
134         templatedir.setLabelText("Template &directory:");
135         templatedir.setButtonLabel("&Browse...");
136         
137         packageName = new StringDialogField();
138         packageName.setDialogFieldListener(fieldlistener);
139         packageName.setLabelText("&Package:");
140         
141         reverseEngineeringSettings= new StringButtonDialogField(new IStringButtonAdapter() {
142             public void changeControlPressed(DialogField field) {
143                 IPath[] paths = DialogSelectionHelper.chooseFileEntries(getShell(), getTemplateDirectory(), new IPath[0], "Select reverse engineering settings file", "Choose file from which settings for the reverse engineering will be read", new String JavaDoc[] {"reveng.xml"}, false, false, true);
144                 if(paths!=null && paths.length==1) {
145                     reverseEngineeringSettings.setText(((paths[0]).toOSString()));
146                 }
147             }
148         });
149         reverseEngineeringSettings.setDialogFieldListener(fieldlistener);
150         reverseEngineeringSettings.setLabelText("Override &xml:");
151         reverseEngineeringSettings.setButtonLabel("&Browse...");
152         
153         reverseengineer = new SelectionButtonDialogField(SWT.CHECK);
154         reverseengineer.setLabelText("Reverse engineer from JDBC Connection");
155         generatejava = new SelectionButtonDialogField(SWT.CHECK);
156         generatejava.setLabelText("Generate domain code (.java)");
157         
158         enableEJB3annotations = new SelectionButtonDialogField(SWT.CHECK);
159         enableEJB3annotations.setLabelText("EJB3/JSR-220 annotations (experimental!)");
160         
161         generatejava.attachDialogField(enableEJB3annotations);
162         
163         useOwnTemplates = new SelectionButtonDialogField(SWT.CHECK);
164         useOwnTemplates.setDialogFieldListener(fieldlistener);
165         useOwnTemplates.setLabelText("Use custom templates");
166         
167         preferRawCompositeIds = new SelectionButtonDialogField(SWT.CHECK);
168         preferRawCompositeIds.setLabelText("Generate 'raw' composite ids");
169                 
170         generatemappings = new SelectionButtonDialogField(SWT.CHECK);
171         generatemappings.setLabelText("Generate mappings (hbm.xml)");
172         
173         generatedocs = new SelectionButtonDialogField(SWT.CHECK);
174         generatedocs.setLabelText("Generate schema html-documentation");
175         
176         generatecfgfile = new SelectionButtonDialogField(SWT.CHECK);
177         generatecfgfile.setLabelText("Generate hibernate configuration (hibernate.cfg.xml)");
178         
179         useOwnTemplates.attachDialogField(templatedir);
180         reverseengineer.attachDialogFields(new DialogField[] { packageName, preferRawCompositeIds, reverseEngineeringSettings });
181        
182         consoleConfigurationName.doFillIntoGrid(container, 3);
183         Control[] controls = outputdir.doFillIntoGrid(container, 3);
184         // Hack to tell the text field to stretch!
185
((GridData)controls[1].getLayoutData()).grabExcessHorizontalSpace=true;
186         reverseengineer.doFillIntoGrid(container, 3);
187         packageName.doFillIntoGrid(container, 3);
188         reverseEngineeringSettings.doFillIntoGrid(container, 3);
189         fillLabel(container);
190         preferRawCompositeIds.doFillIntoGrid(container, 2);
191         generatejava.doFillIntoGrid(container, 3);
192         fillLabel(container);
193         enableEJB3annotations.doFillIntoGrid(container, 2);
194         generatemappings.doFillIntoGrid(container, 3);
195         generatecfgfile.doFillIntoGrid(container, 3);
196         generatedocs.doFillIntoGrid(container, 3);
197         useOwnTemplates.doFillIntoGrid(container, 3);
198         controls = templatedir.doFillIntoGrid(container, 3);
199         // Hack to tell the text field to stretch!
200
((GridData)controls[1].getLayoutData()).grabExcessHorizontalSpace=true;
201         
202
203         initialize();
204         dialogChanged();
205         setControl(container);
206     }
207
208     private void fillLabel(Composite container) {
209         new Label(container, SWT.NULL);
210     }
211     /**
212      * Tests if the current workbench selection is a suitable container to use.
213      */

214
215     private void initialize() {
216         if (selection != null && selection.isEmpty() == false
217                 && selection instanceof IStructuredSelection) {
218             IStructuredSelection ssel = (IStructuredSelection) selection;
219             if (ssel.size() > 1)
220                 return;
221             Object JavaDoc obj = ssel.getFirstElement();
222             if (obj instanceof ConfigurationNode) {
223                 consoleConfigurationName.setText(((ConfigurationNode)obj).getConsoleConfiguration().getName());
224             } else if(consoleConfigurationName.getItems().length==1) {
225                 consoleConfigurationName.setText(consoleConfigurationName.getItems()[0]);
226             }
227         }
228         
229     }
230
231
232
233     /**
234      * Ensures that both text fields are set.
235      */

236
237     private void dialogChanged() {
238
239         if(packageName.isEnabled() && getOutputPackage().length()>0) {
240             IStatus val= JavaConventions.validatePackageName(getOutputPackage());
241             if (val.getSeverity() == IStatus.ERROR || val.getSeverity() == IStatus.WARNING) {
242                 updateStatus(val.getMessage());
243                 return;
244             }
245         }
246         if (getConfigurationName().length() == 0) {
247             updateStatus("Console configuration must be specified");
248             return;
249         }
250         
251         String JavaDoc msg = checkDirectory(getOutputDirectory(), "output directory");
252         
253         if (msg!=null) {
254             updateStatus(msg);
255             return;
256         }
257         
258         if(useOwnTemplates.isSelected()) {
259             msg = checkDirectory(getTemplateDirectory(), "template directory");
260             if (msg!=null) {
261                 updateStatus(msg);
262                 return;
263             } else {
264                 IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(getTemplateDirectory());
265                 IResource[] files = new IFile[0];
266                 if(resource.getType() == IResource.FOLDER) {
267                     try {
268                         files = ((IFolder)resource).members();
269                     } catch (CoreException e) {
270                         // noop
271
}
272                 }
273                 
274                 boolean found = false;
275                 for (int i = 0; i < files.length; i++) {
276                     if(files[i].getType() == IResource.FILE && files[i].getName().endsWith(".vm")) {
277                         found = true;
278                         break;
279                     }
280                 }
281                 if(!found) {
282                     setMessage("No templates (*.vm) found in template directory", IMessageProvider.WARNING);
283                 } else {
284                     setMessage(null);
285                 }
286             }
287         } else {
288             setMessage(null);
289         }
290         
291         updateStatus(null);
292     }
293
294
295
296     protected String JavaDoc checkDirectory(IPath path, String JavaDoc name) {
297         IResource res= ResourcesPlugin.getWorkspace().getRoot().findMember(path);
298         if (res != null) {
299             int resType= res.getType();
300             if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
301                 IProject proj= res.getProject();
302                 if (!proj.isOpen()) {
303                     return "Project for " + name + " is closed";
304                 }
305             } else {
306                 return name + " has to be a folder or project";
307             }
308         } else {
309             return name + " does not exist";
310         }
311         return null;
312     }
313     
314     private void updateStatus(String JavaDoc message) {
315         setErrorMessage(message);
316         setPageComplete(message==null);
317     }
318     
319     public String JavaDoc getConfigurationName() {
320         return consoleConfigurationName.getText();
321     }
322
323     private Path pathOrNull(String JavaDoc p) {
324         if(p==null || p.trim().length()==0) {
325             return null;
326         } else {
327             return new Path(p);
328         }
329     }
330
331
332     /**
333      * @return
334      */

335     public boolean isReverseEngineerEnabled() {
336         return reverseengineer.isSelected();
337     }
338
339
340     /**
341      * @return
342      */

343     public boolean isGenerateJava() {
344         return generatejava.isSelected();
345     }
346
347
348     /**
349      * @return
350      */

351     public boolean isGenerateMappings() {
352         return generatemappings.isSelected();
353     }
354
355
356     /**
357      * @return
358      */

359     public boolean isGenerateCfg() {
360         return generatecfgfile.isSelected();
361     }
362
363
364     /**
365      * @return
366      */

367     public IPath getOutputDirectory() {
368         return pathOrNull(outputdir.getText());
369     }
370     
371     public IPath getTemplateDirectory() {
372         return pathOrNull(templatedir.getText());
373     }
374
375
376     /**
377      * @return
378      */

379     public String JavaDoc getOutputPackage() {
380           return packageName.getText();
381     }
382
383
384     /**
385      * @return
386      */

387     public boolean isPreferRawCompositeIds() {
388         return preferRawCompositeIds.isSelected();
389     }
390
391
392     /**
393      * @return
394      */

395     public boolean isEJB3Enabled() {
396         return enableEJB3annotations.isSelected();
397     }
398
399
400     public IPath getReverseEngineeringSettingsFile() {
401         return pathOrNull(reverseEngineeringSettings.getText());
402     }
403
404
405     public boolean isGenerateDoc() {
406         return generatedocs.isSelected();
407     }
408     
409 }
Popular Tags