[jboss-cvs] jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch ...

Max Rydahl Andersen mandersen at jboss.com
Tue Feb 20 08:21:50 EST 2007


  User: mandersen
  Date: 07/02/20 08:21:50

  Modified:    hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch         
                        CodeGenerationSettings.java
                        ICodeGenerationLaunchConstants.java
  Added:       hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch         
                        ConsoleConfigurationMappingsTab.java
                        ConsoleConfigurationTab.java
                        ConsoleConfigurationOptionsTab.java
                        IConsoleConfigurationLaunchConstants.java
                        ConsoleConfigurationTabGroup.java
                        ConsoleConfigurationLaunchDelegate.java
                        ConsoleConfigurationMainTab.java
  Log:
  HBX-889  sort console configuration lists in ui
  HBX-637  Provide ability to import/export console configs
  HBX-863 Hibernate Code Generation toolbar group is appearing in all Eclipse perspectives.
  
  Revision  Changes    Path
  1.12      +1 -1      jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenerationSettings.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CodeGenerationSettings.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenerationSettings.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -b -r1.11 -r1.12
  --- CodeGenerationSettings.java	4 Sep 2006 05:10:51 -0000	1.11
  +++ CodeGenerationSettings.java	20 Feb 2007 13:21:50 -0000	1.12
  @@ -103,7 +103,7 @@
   		
   		consoleConfigurationName = new ComboDialogField(SWT.READ_ONLY);
   		consoleConfigurationName.setLabelText("Console &configuration:");
  -		ConsoleConfiguration[] cfg = KnownConfigurations.getInstance().getConfigurations();
  +		ConsoleConfiguration[] cfg = KnownConfigurations.getInstance().getConfigurationsSortedByName();
   		String[] names = new String[cfg.length];
   		for (int i = 0; i < cfg.length; i++) {
   			ConsoleConfiguration configuration = cfg[i];
  
  
  
  1.2       +4 -3      jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ICodeGenerationLaunchConstants.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ICodeGenerationLaunchConstants.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ICodeGenerationLaunchConstants.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- ICodeGenerationLaunchConstants.java	10 Aug 2006 07:01:51 -0000	1.1
  +++ ICodeGenerationLaunchConstants.java	20 Feb 2007 13:21:50 -0000	1.2
  @@ -2,8 +2,9 @@
   
   public interface ICodeGenerationLaunchConstants {
   
  -	int ERR_UNSPECIFIED_CONSOLE_CONFIGURATION = 1000;
  -	int ERR_CONSOLE_CONFIGURATION_NOTFOUND = 1001;
  -	int ERR_OUTPUT_PATH_NOTFOUND = 1002;
  +	static final String CONSOLE_CONFIGURATION_LAUNCH_TYPE_ID = "org.hibernate.eclipse.launch.ConsoleConfigurationLaunchConfigurationType";
  +	static final int ERR_UNSPECIFIED_CONSOLE_CONFIGURATION = 1000;
  +	static final int ERR_CONSOLE_CONFIGURATION_NOTFOUND = 1001;
  +	static final int ERR_OUTPUT_PATH_NOTFOUND = 1002;
   
   }
  
  
  
  1.1      date: 2007/02/20 13:21:50;  author: mandersen;  state: Exp;jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ConsoleConfigurationMappingsTab.java
  
  Index: ConsoleConfigurationMappingsTab.java
  ===================================================================
  package org.hibernate.eclipse.launch;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Iterator;
  import java.util.List;
  
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.core.runtime.IPath;
  import org.eclipse.core.runtime.Path;
  import org.eclipse.debug.core.ILaunchConfiguration;
  import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.graphics.Image;
  import org.eclipse.swt.layout.GridData;
  import org.eclipse.swt.widgets.Composite;
  import org.eclipse.swt.widgets.Table;
  import org.eclipse.swt.widgets.TableItem;
  import org.hibernate.console.ImageConstants;
  import org.hibernate.eclipse.console.HibernateConsolePlugin;
  import org.hibernate.eclipse.console.utils.DialogSelectionHelper;
  import org.hibernate.eclipse.console.utils.EclipseImages;
  import org.hibernate.eclipse.console.wizards.UpDownListComposite;
  
  public class ConsoleConfigurationMappingsTab extends ConsoleConfigurationTab {
  
  	private UpDownListComposite mappingFilesViewer;
  	
  	public void createControl(Composite parent) {
  		Composite composite = buildMappingFileTable(parent);
  		setControl( composite );
  	}
  
  	private UpDownListComposite buildMappingFileTable(Composite parent) {
  		mappingFilesViewer = new UpDownListComposite(parent, SWT.NONE, "Additonal mapping files (not listed in cfg.xml)") {
  			protected Object[] handleAdd(int idx) {
  				TableItem[] items = getTable().getItems();
  				IPath[] exclude = new IPath[items.length];
  				
  				for (int i = 0; i < items.length; i++) {
  					TableItem item = items[i];
  					exclude[i] = (IPath) item.getData();			
  				}
  				
  				return DialogSelectionHelper.chooseFileEntries(getShell(), null, exclude, "Add hbm.xml file", "Add a Hibernate Mapping file", new String[] { "hbm.xml" }, true, false, true);
  			}
  
  			protected void listChanged() {
  				updateLaunchConfigurationDialog();
  			}
  		};
  		
  		GridData gd;
  		gd = new GridData(GridData.FILL_BOTH);
  		
  		gd.horizontalSpan = 3;
  		gd.verticalSpan = 1;
  		
  		mappingFilesViewer.setLayoutData( gd );
  		return mappingFilesViewer;
  	}
  
  	public String getName() {
  		return "Mappings";
  	}
  
  	private IPath[] getMappings() {
  		Table table = mappingFilesViewer.getTable();
  		TableItem[] items = table.getItems();
  		IPath[] str = new IPath[items.length];
  		for (int i = 0; i < items.length; i++) {
  			TableItem item = items[i];
  			IPath path = (IPath) item.getData();
  			str[i] = path;			
  		}
  		return str;
  	}
  
  	public void initializeFrom(ILaunchConfiguration configuration) {
  		try {
  			mappingFilesViewer.clear();
  			List mappings = configuration.getAttribute( IConsoleConfigurationLaunchConstants.FILE_MAPPINGS, Collections.EMPTY_LIST );
  			IPath[] mapA = new IPath[mappings.size()];
  			int i=0;
  			for (Iterator iter = mappings.iterator(); iter.hasNext();) {
  				String file = (String) iter.next();
  				mapA[i++] = Path.fromPortableString( file );
  			}
  			mappingFilesViewer.add(mapA, false);
  		}
  		catch (CoreException e) {
  			HibernateConsolePlugin.getDefault().log( e );
  		}		
  	}
  
  	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
  		IPath[] mappings = getMappings();
  		List l = new ArrayList();
  		for (int i = 0; i < mappings.length; i++) {
  			IPath path = mappings[i];
  			l.add(path.toPortableString());
  		}
  		configuration.setAttribute( IConsoleConfigurationLaunchConstants.FILE_MAPPINGS, l );
  	}
  
  	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
  		
  	}
  
  	public Image getImage() {
  		return EclipseImages.getImage( ImageConstants.MAPPEDCLASS );
  	}
  	
  }
  
  
  
  1.1      date: 2007/02/20 13:21:50;  author: mandersen;  state: Exp;jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ConsoleConfigurationTab.java
  
  Index: ConsoleConfigurationTab.java
  ===================================================================
  package org.hibernate.eclipse.launch;
  
  import org.eclipse.debug.core.ILaunchConfiguration;
  import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.events.ModifyEvent;
  import org.eclipse.swt.events.ModifyListener;
  import org.eclipse.swt.events.SelectionEvent;
  import org.eclipse.swt.events.SelectionListener;
  import org.eclipse.swt.graphics.Font;
  import org.eclipse.swt.layout.GridData;
  import org.eclipse.swt.layout.GridLayout;
  import org.eclipse.swt.widgets.Button;
  import org.eclipse.swt.widgets.Composite;
  import org.eclipse.swt.widgets.Group;
  import org.eclipse.swt.widgets.Text;
  import org.hibernate.util.StringHelper;
  
  abstract public class ConsoleConfigurationTab extends AbstractLaunchConfigurationTab {
  
  	protected class ChangeListener implements ModifyListener, SelectionListener {
  		
  		public void modifyText(ModifyEvent e) {
  			updateLaunchConfigurationDialog();
  		}
  		
  		public void widgetDefaultSelected(SelectionEvent e) {/*do nothing*/}
  		
  		public void widgetSelected(SelectionEvent e) {
  			// can use e.getSource() to handle button selection
  			updateLaunchConfigurationDialog();			
  		}
  	}
  
  	ChangeListener changeListener = new ChangeListener();
  	private ILaunchConfiguration currentLaunchConfig;
  	
  	protected Button createBrowseButton(Group group, SelectionListener selectionListener) {
  		Button button = createPushButton(group, "Browse...", null); 
  		button.addSelectionListener(selectionListener);
  		return button;
  	}
  
  	protected Group createGroup(Composite parent, String title) {
  		Font font = parent.getFont();
  		Group group= new Group(parent, SWT.NONE);
  		group.setText(title); 
  		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
  		group.setLayoutData(gd);
  		GridLayout layout = new GridLayout();
  		layout.numColumns = 2;
  		group.setLayout(layout);
  		group.setFont(font);
  		return group;
  	}
  
  	protected ChangeListener getChangeListener() {
  		return changeListener;
  	}
  
  	protected Text createBrowseEditor(Composite parent, Group group) {
  		Text text = new Text(group, SWT.SINGLE | SWT.BORDER);
  		Font font=parent.getFont();
  		GridData gd;				
  		gd = new GridData(GridData.FILL_HORIZONTAL);
  		text.setLayoutData(gd);
  		text.setFont(font);
  		text.addModifyListener(getChangeListener());		
  		return text;
  	}
  
  	protected String nonEmptyTrimOrNull(Text t) {
  		return nonEmptyTrimOrNull( t.getText() );
  	}
  
  	String nonEmptyTrimOrNull(String str) {
  		if(StringHelper.isEmpty( str )) {
  			return null;
  		} else {
  			return str.trim();
  		}
  	}	
  	
  		
  }
  
  
  
  1.1      date: 2007/02/20 13:21:50;  author: mandersen;  state: Exp;jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ConsoleConfigurationOptionsTab.java
  
  Index: ConsoleConfigurationOptionsTab.java
  ===================================================================
  package org.hibernate.eclipse.launch;
  
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.debug.core.ILaunchConfiguration;
  import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
  import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
  import org.eclipse.debug.ui.DebugUITools;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.events.SelectionAdapter;
  import org.eclipse.swt.events.SelectionEvent;
  import org.eclipse.swt.graphics.Font;
  import org.eclipse.swt.graphics.Image;
  import org.eclipse.swt.layout.GridLayout;
  import org.eclipse.swt.widgets.Composite;
  import org.eclipse.swt.widgets.Group;
  import org.eclipse.swt.widgets.Text;
  import org.hibernate.cfg.NamingStrategy;
  import org.hibernate.eclipse.console.HibernateConsolePlugin;
  import org.hibernate.eclipse.console.utils.DialogSelectionHelper;
  import org.xml.sax.EntityResolver;
  
  public class ConsoleConfigurationOptionsTab extends ConsoleConfigurationTab {
  
  	
  	private Text entityResolverClassNameText;
  	private Text namingStrategyClassNameText;
  	
  	public void createControl(Composite parent) {
  		Font font = parent.getFont();
  		Composite comp = new Composite(parent, SWT.NONE);
  		setControl(comp);
  		GridLayout layout = new GridLayout();
  		layout.verticalSpacing = 0;
  		comp.setLayout(layout);
  		comp.setFont(font);
  	
  		createNamingStrategyClassNameEditor( comp );
  		createEntityResolverClassNameEditor( comp );
  		
  	}
  	
  	private void createNamingStrategyClassNameEditor(Composite parent) {
  		Group group = createGroup( parent, "Naming strategy:" );
  		namingStrategyClassNameText = createBrowseEditor( parent, group);
  		createBrowseButton( group, new SelectionAdapter() {
  			public void widgetSelected(SelectionEvent e) {
  				handleNamingStrategyBrowse();
  			}
  		});	 		
  	}
  
  	private void createEntityResolverClassNameEditor(Composite parent) {
  		Group group = createGroup( parent, "Entity resolver:" );
  		entityResolverClassNameText = createBrowseEditor( parent, group);
  		createBrowseButton( group, new SelectionAdapter() {
  			public void widgetSelected(SelectionEvent e) {
  				handleEntityResolverBrowse();
  			}
  		});	 		
  	}
  
  
  	public String getName() {
  		return "Options";
  	}
  
  	public Image getImage() {
  		return DebugUITools.getImage(IInternalDebugUIConstants.IMG_OBJS_COMMON_TAB);
  	}
  	
  	public void initializeFrom(ILaunchConfiguration configuration) {
  		try {
  			namingStrategyClassNameText.setText( configuration.getAttribute( IConsoleConfigurationLaunchConstants.NAMING_STRATEGY, "" ) );
  			entityResolverClassNameText.setText( configuration.getAttribute( IConsoleConfigurationLaunchConstants.ENTITY_RESOLVER, "" ) );
  		}
  		catch (CoreException e) {
  			HibernateConsolePlugin.getDefault().log(e);
  		}			
  	}
  
  	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
  		configuration.setAttribute( IConsoleConfigurationLaunchConstants.NAMING_STRATEGY, nonEmptyTrimOrNull( namingStrategyClassNameText ) );
  		configuration.setAttribute( IConsoleConfigurationLaunchConstants.ENTITY_RESOLVER, nonEmptyTrimOrNull( entityResolverClassNameText ) );		
  	}
  
  	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
  		
  	}
  
  	
  	private void handleEntityResolverBrowse() {
  		String string = DialogSelectionHelper.chooseImplementation(EntityResolver.class.getName(), entityResolverClassNameText.getText(), "Select entity resolver class", getShell());
  		if(string!=null) {
  			entityResolverClassNameText.setText(string);
  		}
  	}
  	
  	private void handleNamingStrategyBrowse() {
  		String string = DialogSelectionHelper.chooseImplementation(NamingStrategy.class.getName(), namingStrategyClassNameText.getText(), "Select naming strategy class", getShell());
  		if(string!=null) {
  			namingStrategyClassNameText.setText(string);
  		}		
  	}
  
  }
  
  
  
  1.1      date: 2007/02/20 13:21:50;  author: mandersen;  state: Exp;jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/IConsoleConfigurationLaunchConstants.java
  
  Index: IConsoleConfigurationLaunchConstants.java
  ===================================================================
  package org.hibernate.eclipse.launch;
  
  import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
  
  public interface IConsoleConfigurationLaunchConstants {
  
  	public static final String ID = "org.hibernate.eclipse.launch";
  	public static final String NAMING_STRATEGY = ID + "NAMING_STRATEGY";
  	public static final String ENTITY_RESOLVER = ID + "ENTITY_RESOLVER";
  	public static final String PROPERTY_FILE = ID + ".PROPERTY_FILE";
  	public static final String CFG_XML_FILE = ID + ".CFG_XML_FILE";
  	public static final String PERSISTENCE_UNIT_NAME = ID + ".PERSISTENCE_UNIT_NAME";
  	public static final String CONFIGURATION_FACTORY = ID + ".CONFIGURATION_FACTORY";
  	public static final String FILE_MAPPINGS = ID + ".FILE_MAPPINGS";
  	public static final String PROJECT_NAME = IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME;
  
  }
  
  
  
  1.1      date: 2007/02/20 13:21:50;  author: mandersen;  state: Exp;jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ConsoleConfigurationTabGroup.java
  
  Index: ConsoleConfigurationTabGroup.java
  ===================================================================
  package org.hibernate.eclipse.launch;
  
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.debug.core.ILaunchConfiguration;
  import org.eclipse.debug.ui.AbstractLaunchConfigurationTabGroup;
  import org.eclipse.debug.ui.CommonTab;
  import org.eclipse.debug.ui.ILaunchConfigurationDialog;
  import org.eclipse.debug.ui.ILaunchConfigurationTab;
  import org.eclipse.jdt.debug.ui.launchConfigurations.JavaClasspathTab;
  import org.eclipse.jdt.internal.debug.ui.classpath.ClasspathModel;
  import org.eclipse.jdt.internal.debug.ui.classpath.IClasspathEntry;
  import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
  import org.eclipse.jdt.launching.JavaRuntime;
  import org.hibernate.eclipse.console.HibernateConsolePlugin;
  
  public class ConsoleConfigurationTabGroup extends
  		AbstractLaunchConfigurationTabGroup {
  
  	public ConsoleConfigurationTabGroup() {
  
  	}
  
  	
  	public void createTabs(ILaunchConfigurationDialog dialog, String mode) {
  		ILaunchConfigurationTab[] tabs = new ILaunchConfigurationTab[] {
  				new ConsoleConfigurationMainTab(),
  				new ConsoleConfigurationOptionsTab(),
  				new JavaClasspathTab() {
  				
  					public boolean isShowBootpath() {
  						return false;
  					}
  				
  					public boolean isValid(ILaunchConfiguration launchConfig) {						
  						if(super.isValid( launchConfig )) {
  							setErrorMessage( null );
  							setMessage( null );
  							IRuntimeClasspathEntry[] entries;
  							try {
  								entries = JavaRuntime.computeUnresolvedRuntimeClasspath(launchConfig);
  								for (int i = 0; i < entries.length; i++) {
  									IRuntimeClasspathEntry entry = entries[i];
  									if(entry.getClasspathProperty()==IRuntimeClasspathEntry.USER_CLASSES) {
  										return true;
  									}
  								}
  								
  							}
  							catch (CoreException e) {
  								HibernateConsolePlugin.getDefault().log( e );
  							}
  							setErrorMessage( "Classpath must be set or restored to default" );
  							return false;													
  						} 
  						return false;						
  					}
  					
  					public void initializeFrom(ILaunchConfiguration configuration) {
  						
  						super.initializeFrom( configuration );
  					}
  					
  					public boolean canSave() {
  						return super.canSave();
  					}
  				},
  				new ConsoleConfigurationMappingsTab(),
  				new CommonTab(),
  				
  			};
  			
  			this.setTabs(tabs);		
  	}
  
  }
  
  
  
  1.1      date: 2007/02/20 13:21:50;  author: mandersen;  state: Exp;jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ConsoleConfigurationLaunchDelegate.java
  
  Index: ConsoleConfigurationLaunchDelegate.java
  ===================================================================
  package org.hibernate.eclipse.launch;
  
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.core.runtime.IProgressMonitor;
  import org.eclipse.debug.core.ILaunch;
  import org.eclipse.debug.core.ILaunchConfiguration;
  import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
  
  public class ConsoleConfigurationLaunchDelegate extends LaunchConfigurationDelegate {
  
  	public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
  		throw new IllegalStateException("Direct launch not supported");
  	}
  
  
  }
  
  
  
  1.1      date: 2007/02/20 13:21:50;  author: mandersen;  state: Exp;jbosside/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ConsoleConfigurationMainTab.java
  
  Index: ConsoleConfigurationMainTab.java
  ===================================================================
  /**
   * 
   */
  package org.hibernate.eclipse.launch;
  
  import org.eclipse.core.resources.IFile;
  import org.eclipse.core.resources.IResource;
  import org.eclipse.core.resources.ResourcesPlugin;
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.core.runtime.IPath;
  import org.eclipse.core.runtime.Path;
  import org.eclipse.debug.core.ILaunchConfiguration;
  import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
  import org.eclipse.jdt.core.IJavaProject;
  import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.events.SelectionAdapter;
  import org.eclipse.swt.events.SelectionEvent;
  import org.eclipse.swt.graphics.Font;
  import org.eclipse.swt.graphics.Image;
  import org.eclipse.swt.layout.GridData;
  import org.eclipse.swt.layout.GridLayout;
  import org.eclipse.swt.layout.RowLayout;
  import org.eclipse.swt.widgets.Button;
  import org.eclipse.swt.widgets.Composite;
  import org.eclipse.swt.widgets.Group;
  import org.eclipse.swt.widgets.Text;
  import org.hibernate.console.ImageConstants;
  import org.hibernate.console.preferences.ConsoleConfigurationPreferences.ConfigurationMode;
  import org.hibernate.eclipse.console.HibernateConsolePlugin;
  import org.hibernate.eclipse.console.utils.DialogSelectionHelper;
  import org.hibernate.eclipse.console.utils.EclipseImages;
  import org.hibernate.eclipse.console.utils.ProjectUtils;
  import org.hibernate.util.StringHelper;
  
  public class ConsoleConfigurationMainTab extends ConsoleConfigurationTab {
  	
  	
  	
  	private Button coreMode;
  	private Button jpaMode;
  	private Button annotationsMode;
  	private Button confbutton;
  	
  	private Text propertyFileText;
  	private Text configurationFileText;
  	private Text projectNameText;
  	private Text persistenceUnitNameText;
  	
  	public String getName() {
  		return "Main";
  	}
  
  	public void createControl(Composite parent) {
  		Font font = parent.getFont();
  		Composite comp = new Composite(parent, SWT.NONE);
  		setControl(comp);
  		GridLayout layout = new GridLayout();
  		layout.verticalSpacing = 0;
  		comp.setLayout(layout);
  		comp.setFont(font);
  		
  		createConfigurationMode( comp );
  		
  		createProjectEditor( comp );
  		
  		createPropertyFileEditor(comp);
  		createConfigurationFileEditor(comp);
  		
  		createPersistenceUnitEditor( comp );
  			
  		
  	}
  
  	private void createConfigurationMode(Composite container) {
  		Group group = createGroup( container, "Type:");
  		group.setLayout( new RowLayout( SWT.HORIZONTAL ) );
  		coreMode = new Button(group, SWT.RADIO);
  		coreMode.setText("Core");
  		coreMode.addSelectionListener( getChangeListener() );
  		coreMode.setSelection( true );
  		annotationsMode = new Button(group, SWT.RADIO);
  		annotationsMode.setText("Annotations (jdk 1.5+)");
  		annotationsMode.addSelectionListener( getChangeListener() );
  		jpaMode = new Button(group, SWT.RADIO);
  		jpaMode.setText("JPA (jdk 1.5+)");
  		jpaMode.addSelectionListener( getChangeListener() );
  		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
  		gd.horizontalSpan = 2;
  		group.setLayoutData( gd );
  	}
  	
  	protected void createProjectEditor(Composite parent) {
  		Group group = createGroup( parent, "Project:" );
  		projectNameText = createBrowseEditor( parent, group);
  		createBrowseButton( group, new SelectionAdapter() {
  			public void widgetSelected(SelectionEvent e) {
  				handleProjectBrowse();
  			}
  		} );
  	}
  	
  	private void createPropertyFileEditor(Composite parent) {
  		Group group = createGroup( parent, "Property file:" );
  		propertyFileText = createBrowseEditor( parent, group);
  		createBrowseButton( group, new SelectionAdapter() {
  			public void widgetSelected(SelectionEvent e) {
  				handlePropertyFileBrowse();
  			}
  		} );		
  	}
  	
  
  	private void createConfigurationFileEditor(Composite parent) {
  		Group group = createGroup( parent, "Configuration file:" );
  		configurationFileText = createBrowseEditor( parent, group);
  		confbutton = createBrowseButton( group, new SelectionAdapter() {
  			public void widgetSelected(SelectionEvent e) {
  				handleConfigurationFileBrowse();
  			}
  		});
  	}
  	
  
  	private void createPersistenceUnitEditor(Composite parent) {
  		Group group = createGroup( parent, "Persistence unit:" );
  		persistenceUnitNameText = new Text(group, SWT.BORDER | SWT.SINGLE);
  		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
  		persistenceUnitNameText.setFont( parent.getFont() );
  		persistenceUnitNameText.setLayoutData(gd);
  		persistenceUnitNameText.addModifyListener(getChangeListener());
  	}
  
  	
  	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
  		
  		configuration.setAttribute( IConsoleConfigurationLaunchConstants.CONFIGURATION_FACTORY, getConfigurationMode().toString());
  		configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, nonEmptyTrimOrNull( projectNameText ));
  		configuration.setAttribute(IConsoleConfigurationLaunchConstants.PROPERTY_FILE, nonEmptyTrimOrNull(propertyFileText));
  		configuration.setAttribute(IConsoleConfigurationLaunchConstants.CFG_XML_FILE, nonEmptyTrimOrNull(configurationFileText));		
  		configuration.setAttribute( IConsoleConfigurationLaunchConstants.PERSISTENCE_UNIT_NAME, nonEmptyTrimOrNull(persistenceUnitNameText));
  		
  	}
  
  	public void initializeFrom(ILaunchConfiguration configuration) {
  		
  		try {
  			ConfigurationMode cm = ConfigurationMode.parse(configuration.getAttribute( IConsoleConfigurationLaunchConstants.CONFIGURATION_FACTORY, "" ));
  			coreMode.setSelection( cm.equals( ConfigurationMode.CORE ) );
  			annotationsMode.setSelection( cm.equals( ConfigurationMode.ANNOTATIONS ) );
  			jpaMode.setSelection( cm.equals( ConfigurationMode.JPA ) );
  			
  			projectNameText.setText( configuration.getAttribute( IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "" ) );
  			propertyFileText.setText( configuration.getAttribute( IConsoleConfigurationLaunchConstants.PROPERTY_FILE, "" ) );
  			configurationFileText.setText( configuration.getAttribute( IConsoleConfigurationLaunchConstants.CFG_XML_FILE, "" ));
  			persistenceUnitNameText.setText( configuration.getAttribute( IConsoleConfigurationLaunchConstants.PERSISTENCE_UNIT_NAME, "" ));
  		}
  		catch (CoreException e) {
  			HibernateConsolePlugin.getDefault().log( e );
  		}
  		
  	}
  	
  	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {		
  		
  	}
  	
  	private void handlePropertyFileBrowse() {
  		IPath[] paths = org.hibernate.eclipse.console.utils.xpl.DialogSelectionHelper.chooseFileEntries(getShell(),  getPropertyFilePath(), new IPath[0], "Select property file", "Choose file to use as hibernate.properties", new String[] {"properties"}, false, false, true);
  		if(paths!=null && paths.length==1) {
  			propertyFileText.setText( (paths[0]).toOSString() );
  		}
  	}
  	
  	private void handleProjectBrowse() {
  		IJavaProject paths = DialogSelectionHelper.chooseJavaProject( getShell(), ProjectUtils.findJavaProject( propertyFileText.getText() ), "Select java project", "The (optional) java project is used to determine the default classpath" );
  		if(paths!=null) {
  			projectNameText.setText( paths.getProject().getName() );
  		} else {
  			projectNameText.setText("");
  		}
  	}
  	
  	
  	private void handleConfigurationFileBrowse() {
  		IPath[] paths = DialogSelectionHelper.chooseFileEntries(getShell(),  getConfigurationFilePath(), new IPath[0], "Select hibernate.cfg.xml file", "Choose file to use as hibernate.cfg.xml", new String[] {"cfg.xml"}, false, false, true);
  		if(paths!=null && paths.length==1) {
  			configurationFileText.setText( (paths[0]).toOSString() );
  		}
  	}
  	
  
  	private Path pathOrNull(String p) {
  		if(p==null || p.trim().length()==0) {
  			return null;
  		} else {
  			return new Path(p);
  		}
  	}
  
  	public Path getConfigurationFilePath() {
  		return pathOrNull(configurationFileText.getText() );
  	}
  
  	public Path getPropertyFilePath() {
  		String p = propertyFileText.getText();		
  		return pathOrNull(p);
  	}
  		
  	public boolean isValid(ILaunchConfiguration launchConfig) {
  		setErrorMessage( null );
  		setMessage( null );
  		String propertyFilename = propertyFileText.getText();
  		String configurationFilename = configurationFileText.getText();
  		
  		configurationFileText.setEnabled( /* TODO !configurationFileWillBeCreated && */ !getConfigurationMode().equals( ConfigurationMode.JPA ) );
  		confbutton.setEnabled( !getConfigurationMode().equals( ConfigurationMode.JPA ) );
  		
  		persistenceUnitNameText.setEnabled( getConfigurationMode().equals( ConfigurationMode.JPA) );
  		
  		if(getProjectName()!=null && StringHelper.isNotEmpty(getProjectName().trim())) {
  			IJavaProject findJavaProject = ProjectUtils.findJavaProject( getProjectName() );
  			if(findJavaProject==null || !findJavaProject.exists()) {
  				setErrorMessage("The Java project " + getProjectName() + " does not exist.");
  				return false;
  			}
  		}
  		
  		/* TODO: warn about implicit behavior of loading /hibernate.cfg.xml, /hibernate.properties and /META-INF/persistence.xml
  		 * if (propertyFilename.length() == 0 && configurationFilename.trim().length() == 0) {
  			setErrorMessage("Property or Configuration file must be specified");
  			return;
  		} */
  		
  		if (propertyFilename.length() > 0) {
  			IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(propertyFilename);
  			String msg = checkForFile("Property file", resource);
  			if(msg!=null) {
  				setErrorMessage(msg);
  				return false;
  			}
  		}
  		
  		if (/*!configurationFileWillBeCreated &&*/ configurationFilename.length() > 0) {
  			IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(configurationFilename);
  			String msg = checkForFile("Configuration file",resource);
  			if(msg!=null) {
  				setErrorMessage(msg);
  				return false;
  			}
  		} 
  		
  		/*if((useProjectClassPath() && StringHelper.isEmpty( getProjectName() )) && classPathViewer.getTable().getItemCount()==0) {
  			setErrorMessage( "Need to specify a project or setup a classpath" );
  			return;
  		}
  		
  		if((!useProjectClassPath() && classPathViewer.getTable().getItemCount()==0)) {
  			setErrorMessage( "Need to specify a classpath when not using a project classpath" );
  			return;
  		} TODO*/
  				
  		return true;
  	}
  	
  	private ConfigurationMode getConfigurationMode() {
  		if(annotationsMode.getSelection()) {
  			return ConfigurationMode.ANNOTATIONS;
  		} else if(jpaMode.getSelection()) {
  			return ConfigurationMode.JPA;
  		} else {
  			return ConfigurationMode.CORE;
  		}
  	}
  
  	String getProjectName() {
  		return projectNameText.getText();
  	}
  
  	private String checkForFile(String msgPrefix, IResource resource) {
  		if(resource!=null) {
  			if(resource instanceof IFile) {
  				return null;
  			} else {
  				return msgPrefix + " is not a file";
  			}				
  		} else {
  			return msgPrefix + " does not exist";
  		}
  	}
  
  	public Image getImage() {
  		return EclipseImages.getImage(ImageConstants.MINI_HIBERNATE);
  	}
  
  	
  }
  
  



More information about the jboss-cvs-commits mailing list