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 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 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 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 [] 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 [] 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 203 204 initialize(); 205 dialogChanged(); 206 207 setControl(container); 208 } 209 210 213 private void fillHerUp(Combo combo, String [] newContent) { 214 215 String original = combo.getText(); 216 combo.removeAll(); 217 for (int i = 0; i < newContent.length; i++) { 218 String name = newContent[i]; 219 combo.add(name); 220 } 221 combo.setText(original); 222 } 223 224 227 private void fillLabel(Composite container) { 228 new Label(container, SWT.NULL); 229 } 230 231 234 235 private void initialize() { 236 updateStatus(null); 237 253 } 254 255 258 private void dialogChanged() { 259 IResource container = ResourcesPlugin.getWorkspace().getRoot() 260 .findMember(new Path(getContainerName())); 261 String 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 updateStatus(null); 298 } 299 300 private void updateStatus(String message) { 301 setErrorMessage(message); 302 setPageComplete(message == null && beenShown); 303 } 304 305 private String getContainerName() { 306 return containerText.getText(); 307 } 308 309 private String getFileName() { 310 return fileText.getText(); 311 } 312 313 316 public String getSessionFactoryName() { 317 return nullIfEmpty(sessionFactoryNameText.getText()); 318 } 319 320 324 private String nullIfEmpty(String text) { 325 if (text != null && text.trim().length() > 0) { 326 return text.trim(); 327 } 328 return null; 329 } 330 331 334 public String getDialect() { 335 return nullIfEmpty(helper.getDialectClass(dialectCombo.getText())); 336 } 337 338 341 public String getDriver() { 342 return nullIfEmpty(driver_classCombo.getText()); 343 } 344 345 348 public String getConnectionURL() { 349 return nullIfEmpty(urlCombo.getText()); 350 } 351 352 355 public String getUsername() { 356 return nullIfEmpty(usernameText.getText()); 357 } 358 359 362 public String getPassword() { 363 return nullIfEmpty(passwordText.getText()); 364 } 365 366 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 |