[overlord-commits] Overlord SVN: r212 - in cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb: actions and 2 other directories.

overlord-commits at lists.jboss.org overlord-commits at lists.jboss.org
Fri Aug 1 07:06:31 EDT 2008


Author: objectiser
Date: 2008-08-01 07:06:31 -0400 (Fri, 01 Aug 2008)
New Revision: 212

Added:
   cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/dialogs/
   cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/dialogs/GenerateDialog.java
Modified:
   cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/actions/GenerateAction.java
   cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/eclipse/Activator.java
Log:
Initial implementation of generation dialog.

Modified: cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/actions/GenerateAction.java
===================================================================
--- cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/actions/GenerateAction.java	2008-07-31 16:34:40 UTC (rev 211)
+++ cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/actions/GenerateAction.java	2008-08-01 11:06:31 UTC (rev 212)
@@ -41,6 +41,7 @@
 import org.scribble.model.change.*;
 import org.w3c.dom.Node;
 
+import org.jboss.tools.overlord.jbossesb.dialogs.*;
 import org.jboss.tools.overlord.jbossesb.model.*;
 
 /**
@@ -64,7 +65,20 @@
 			IResource res=(IResource)sel.getFirstElement();
 			
 			if (res instanceof IFile) {
-				generate((IFile)res);
+				
+				if (true) {
+					GenerateDialog dialog=
+						new GenerateDialog(m_targetPart.getSite().getShell(),
+									(IFile)res);
+					
+					try {
+						dialog.open();
+					} catch(Throwable e) {
+						e.printStackTrace();
+					}
+				} else {
+					generate((IFile)res);
+				}
 			}
 		}
 	}

Added: cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/dialogs/GenerateDialog.java
===================================================================
--- cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/dialogs/GenerateDialog.java	                        (rev 0)
+++ cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/dialogs/GenerateDialog.java	2008-08-01 11:06:31 UTC (rev 212)
@@ -0,0 +1,473 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ */
+package org.jboss.tools.overlord.jbossesb.dialogs;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.*;
+import org.eclipse.swt.layout.*;
+import org.eclipse.swt.widgets.*;
+import org.jboss.tools.overlord.jbossesb.model.DefaultESBLanguageModel;
+import org.jboss.tools.overlord.jbossesb.model.ESBLanguageModel;
+import org.scribble.conversation.model.ConversationModel;
+import org.scribble.extensions.RegistryFactory;
+import org.scribble.model.DefaultModelListener;
+import org.scribble.model.Model;
+import org.scribble.model.ModelReference;
+import org.scribble.model.ModelRepository;
+import org.scribble.model.Role;
+import org.scribble.model.change.ModelGenerator;
+import org.eclipse.jdt.core.*;
+import org.eclipse.core.resources.*;
+import org.eclipse.core.runtime.*;
+
+/**
+ * This class provides the dialog for generating JBossESB
+ * service artefacts.
+ */
+public class GenerateDialog extends org.eclipse.jface.dialogs.Dialog {
+
+	private static final String JAVA_SOURCE_LOCATION = "src/java";
+
+	private static final String CLASSPATH_FILENAME = ".classpath";
+
+	private static final String OUTPUT_LOCATION = "classes";
+	/**
+	 * This is the constructor for the generate dialog.
+	 * 
+	 * @param shell The shell
+	 */
+	public GenerateDialog(Shell shell, IFile file) {
+		super(shell);
+		
+		m_file = file;
+		
+		initialize(m_file);
+	}
+	
+	/**
+	 * This method initializes the conversation model associated
+	 * with the supplied file resource.
+	 * 
+	 * @param res The file
+	 */
+	protected void initialize(IFile res) {
+		ModelReference ref=
+			org.scribble.osgi.model.OSGIModelRepository.createReference(res);
+		
+		if (ref != null) {
+			ModelRepository mrep=(ModelRepository)
+					RegistryFactory.getRegistry().getExtension(
+						ModelRepository.class, null);
+
+			if (mrep != null) {
+				Model model=mrep.getModel(ref,
+								new DefaultModelListener());
+				
+				if (model instanceof ConversationModel) {
+					m_conversationModel = (ConversationModel)model;
+					
+					m_roles = m_conversationModel.getRoles();
+				}
+			}
+		}
+		
+		if (m_conversationModel == null) {
+			error("Unable to load model used to generate " +
+					"the ESB artefacts", null);
+		}
+	}
+	
+	/**
+	 * This method creates the dialog details.
+	 * 
+	 * @param parent The parent control
+	 * @return The control containing the dialog components
+	 */
+	protected Control createDialogArea(Composite parent) {
+		
+		Composite composite=(Composite)super.createDialogArea(parent);
+		
+		GridLayout layout = new GridLayout();
+		layout.numColumns = 1;
+		composite.setLayout(layout);		
+	
+		GridData gd=null;
+
+		Group group=new Group(composite, SWT.H_SCROLL|SWT.V_SCROLL);
+		
+		gd=new GridData();
+		gd.horizontalAlignment = SWT.FILL;
+		gd.horizontalSpan = 1;
+		gd.widthHint = 500;
+		gd.grabExcessHorizontalSpace = true;
+		group.setLayoutData(gd);
+		
+		layout = new GridLayout();
+		layout.numColumns = 2;
+		group.setLayout(layout);
+
+		// Labels
+		Label label=new Label(group, SWT.NONE);
+		label.setText("Service Role");
+		
+		gd = new GridData();
+		gd.horizontalSpan = 1;
+		gd.widthHint = 150;
+		label.setLayoutData(gd);
+
+		label = new Label(group, SWT.NONE);
+		label.setText("Project Name");
+		
+		gd = new GridData();
+		gd.horizontalSpan = 1;
+		gd.widthHint = 150;
+		label.setLayoutData(gd);
+
+		if (m_conversationModel != null) {
+
+			for (int i=0; i < m_roles.size(); i++) {
+				
+				Button button=new Button(group, SWT.CHECK);
+				button.setText(m_roles.get(i).getName());
+				button.setSelection(true);
+				
+				gd = new GridData();
+				gd.horizontalSpan = 1;
+				gd.widthHint = 150;
+				button.setLayoutData(gd);
+				
+				m_roleButtons.add(button);
+				
+				button.addSelectionListener(new SelectionListener() {
+					public void widgetDefaultSelected(SelectionEvent e) {
+						widgetSelected(e);
+					}
+
+					public void widgetSelected(SelectionEvent e) {
+						checkStatus();
+					}
+				});
+				
+				Text projectName=new Text(group, SWT.NONE);
+				projectName.setText(m_roles.get(i).getName());
+				
+				gd = new GridData();
+				gd.horizontalSpan = 1;
+				gd.widthHint = 300;
+				projectName.setLayoutData(gd);
+				
+				m_projectNames.add(projectName);
+
+				projectName.addModifyListener(new ModifyListener() {					
+					public void modifyText(ModifyEvent e) {
+						checkStatus();
+					}
+				});
+				
+			}
+		}
+
+		Button button=new Button(group, SWT.NONE);
+		button.setText("Check All");
+		
+		gd = new GridData();
+		gd.horizontalSpan = 1;
+		gd.widthHint = 150;
+		button.setLayoutData(gd);
+		
+		button.addSelectionListener(new SelectionListener() {
+
+			public void widgetDefaultSelected(SelectionEvent e) {
+				widgetSelected(e);	
+			}
+			
+			public void widgetSelected(SelectionEvent e) {
+				for (int i=0; i < m_roleButtons.size(); i++) {
+					m_roleButtons.get(i).setSelection(true);
+				}
+				checkStatus();
+			}			
+		});
+
+		button=new Button(group, SWT.NONE);
+		button.setText("Clear All");
+		
+		gd = new GridData();
+		gd.horizontalSpan = 1;
+		gd.widthHint = 150;
+		button.setLayoutData(gd);
+		
+		button.addSelectionListener(new SelectionListener() {
+
+			public void widgetDefaultSelected(SelectionEvent e) {
+				widgetSelected(e);	
+			}
+			
+			public void widgetSelected(SelectionEvent e) {
+				for (int i=0; i < m_roleButtons.size(); i++) {
+					m_roleButtons.get(i).setSelection(false);
+				}
+				checkStatus();
+			}			
+		});
+		
+		return(composite);
+	}
+
+	@Override
+	protected Control createButtonBar(Composite parent) {
+		Control ret=super.createButtonBar(parent);
+		
+		checkStatus();
+		
+		return(ret);
+	}
+	
+	protected void checkStatus() {
+		int selected=0;
+		boolean f_error=false;
+		
+		for (int i=0; i < m_roleButtons.size(); i++) {
+			if (m_roleButtons.get(i).getSelection()) {
+				selected++;
+				
+				m_projectNames.get(i).setEnabled(true);
+				
+				// Check project name
+				String projectName=m_projectNames.get(i).getText();
+				
+				if (isProjectNameValid(projectName) == false) {
+					f_error = true;
+					
+					m_projectNames.get(i).setBackground(
+							Display.getCurrent().getSystemColor(SWT.COLOR_RED));
+				} else {
+					m_projectNames.get(i).setBackground(
+							Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
+				}
+			} else {
+				m_projectNames.get(i).setEnabled(false);
+				m_projectNames.get(i).setBackground(
+						Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
+			}
+		}
+		
+		if (f_error || selected == 0) {
+			getButton(IDialogConstants.OK_ID).setEnabled(false);
+		} else {
+			getButton(IDialogConstants.OK_ID).setEnabled(true);
+		}
+	}
+	
+	protected boolean isProjectNameValid(String name) {
+		boolean ret=true;
+		
+		if (name == null || name.trim().length() == 0) {
+			ret = false;
+		} else if (m_file.getWorkspace().getRoot().getProject(name).exists()) {
+			ret = false;
+		} else {
+			for (int i=0; ret && i < name.length(); i++) {
+				if (i == 0) {
+					ret = Character.isJavaIdentifierStart(name.charAt(i));
+				} else {
+					ret = Character.isJavaIdentifierPart(name.charAt(i));
+				}
+			}
+		}
+		
+		return(ret);
+	}
+	
+	/**
+	 * The ok button has been pressed.
+	 */
+	public void okPressed() {
+		
+		try {
+			for (int i=0; i < m_roles.size(); i++) {
+				
+				if (m_roleButtons.get(i).getSelection()) {
+					generateRole(m_roles.get(i),
+							m_projectNames.get(i).getText());
+				}
+			}
+			
+			super.okPressed();
+		} catch(Exception e) {
+			error("Failed to generate ESB artefacts", e);
+		}
+	}
+		
+	protected void generateRole(Role role, String projectName)
+						throws Exception {
+		
+		if (logger.isLoggable(Level.FINEST)) {
+			logger.finest("Generate role '"+role+"' for: "+
+						m_conversationModel);
+		}
+		
+		// Project to role
+		org.scribble.projector.Projector projector=
+			(org.scribble.projector.Projector)
+			RegistryFactory.getRegistry().getExtension(
+					org.scribble.projector.Projector.class, null);
+
+		if (projector != null) {
+			Model localModel=projector.project(m_conversationModel,
+					role, new DefaultModelListener());
+			
+			if (localModel instanceof ConversationModel &&
+					((ConversationModel)localModel).getConversation() != null) {
+				ConversationModel lcm=(ConversationModel)localModel;
+				
+				ModelReference targetRef=
+					new ModelReference(ESBLanguageModel.JBOSSESB_NOTATION);
+				
+				DefaultESBLanguageModel target=
+					new DefaultESBLanguageModel(targetRef);
+				
+				ModelGenerator generator=(ModelGenerator)
+					RegistryFactory.getRegistry().getExtension(
+							ModelGenerator.class, null);
+				
+				if (generator != null) {
+					generator.generate(targetRef, role, target,
+								lcm);
+					
+					generateRoleProject(projectName,
+								target.getESBConfiguration(),
+								lcm);
+				} else {
+					logger.severe("Unable to find model generator");
+				}
+			}
+		}
+	}
+	
+	protected void generateRoleProject(String projectName,
+			org.w3c.dom.Element esbConfig, ConversationModel localcm)
+							throws Exception {
+		
+		// Create project
+		IProject project=m_file.getWorkspace().getRoot().getProject(projectName);
+		project.create(new org.eclipse.core.runtime.NullProgressMonitor());
+
+		IJavaProject jproj=JavaCore.create(project);
+		
+		project.open(new org.eclipse.core.runtime.NullProgressMonitor());
+		
+		IProjectDescription description = jproj.getProject().getDescription();
+		String[] prevNatures= description.getNatureIds();
+		String[] newNatures= new String[prevNatures.length + 1];
+		System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
+		newNatures[prevNatures.length]= JavaCore.NATURE_ID;
+		description.setNatureIds(newNatures);
+		
+		jproj.getProject().setDescription(description,
+				new org.eclipse.core.runtime.NullProgressMonitor());
+		
+		IPath outputLocation=jproj.getPath().append(new Path(OUTPUT_LOCATION));
+		IPath classpathLocation=jproj.getPath().append(new Path(CLASSPATH_FILENAME));
+		IPath sourceLocation=jproj.getPath().append(new Path(JAVA_SOURCE_LOCATION));
+		
+		// create and set the output and source paths first
+		IFolder outputFolder= jproj.getProject().getWorkspace().getRoot().getFolder(outputLocation);
+		createParentFolder(outputFolder);
+		outputFolder.create(true, true, new org.eclipse.core.runtime.NullProgressMonitor());
+		outputFolder.setDerived(true);		
+		
+		IFolder sourceFolder= jproj.getProject().getWorkspace().getRoot().getFolder(sourceLocation);
+		createParentFolder(sourceFolder);
+		sourceFolder.create(true, true, new org.eclipse.core.runtime.NullProgressMonitor());
+		sourceFolder.setDerived(true);		
+		
+		IFile file=jproj.getProject().getWorkspace().getRoot().getFile(classpathLocation);
+		file.create(null, true, new org.eclipse.core.runtime.NullProgressMonitor());
+		file.setDerived(true);
+		
+		// Write empty contents, to overcome 'Resource '..' is not local' exception
+		String classpath="<classpath/>";
+		file.setContents(new java.io.ByteArrayInputStream(classpath.getBytes()), true,
+				false, new org.eclipse.core.runtime.NullProgressMonitor());
+			
+		IClasspathEntry[] classpaths=new IClasspathEntry[2];
+		
+		classpaths[0] = JavaCore.newContainerEntry(
+				new Path("org.eclipse.jdt.launching.JRE_CONTAINER"));
+					
+		classpaths[1] = JavaCore.newSourceEntry(sourceLocation);
+					
+		jproj.setRawClasspath(classpaths, outputLocation,
+				new org.eclipse.core.runtime.NullProgressMonitor());
+	}
+
+	/**
+	 * This method checks whether the parent folder exists,
+	 * and if not attempts to create it.
+	 * 
+	 * @param res The current resource
+	 */
+	protected void createParentFolder(IResource res) {
+		
+		if (res.getParent() instanceof IFolder) {
+			IFolder parent=(IFolder)res.getParent();
+			
+			if (parent.exists() == false) {
+				createParentFolder(parent);
+
+				try {
+					parent.create(true, true,
+							new org.eclipse.core.runtime.NullProgressMonitor());
+				} catch(Exception e) {
+					e.printStackTrace();
+				}
+			}
+		}
+	}
+
+	/**
+	 * This method is used to report an error.
+	 * 
+	 * @param mesg The error message
+	 * @param ex The exception 
+	 */
+	public void error(String mesg, Exception ex) {
+		
+		org.jboss.tools.overlord.jbossesb.eclipse.Activator.logError(mesg, ex);
+		
+		MessageBox mbox=new MessageBox(getShell(),
+				SWT.ICON_ERROR|SWT.OK);
+		mbox.setMessage(mesg);
+		mbox.open();
+	}
+
+	private static Logger logger = Logger.getLogger("org.jboss.tools.overlord.jbossesb.dialogs");
+	
+	private IFile m_file=null;
+	private ConversationModel m_conversationModel=null;
+	private java.util.List<Role> m_roles=null;
+	private java.util.List<Button> m_roleButtons=new java.util.Vector<Button>();
+	private java.util.List<Text> m_projectNames=new java.util.Vector<Text>();
+}

Modified: cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/eclipse/Activator.java
===================================================================
--- cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/eclipse/Activator.java	2008-07-31 16:34:40 UTC (rev 211)
+++ cdl/trunk/tools/plugins/org.jboss.tools.overlord.jbossesb/src/java/org/jboss/tools/overlord/jbossesb/eclipse/Activator.java	2008-08-01 11:06:31 UTC (rev 212)
@@ -19,6 +19,9 @@
 
 import org.eclipse.ui.plugin.AbstractUIPlugin;
 import org.osgi.framework.BundleContext;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import java.util.logging.Logger;
 
 /**
  * The activator class controls the plug-in life cycle
@@ -66,4 +69,24 @@
 		return plugin;
 	}
 
+	/**
+	 * This method logs an error against the plugin.
+	 * 
+	 * @param mesg The error message
+	 * @param t The optional exception
+	 */
+	public static void logError(String mesg, Throwable t) {
+		
+		if (getDefault() != null) {
+			Status status=new Status(IStatus.ERROR,
+					PLUGIN_ID, 0, mesg, t);
+			
+			getDefault().getLog().log(status);
+		}
+		
+		logger.severe("LOG ERROR: "+mesg+
+				(t == null ? "" : ": "+t));
+	}
+	
+	private static Logger logger = Logger.getLogger("org.pi4soa.service.generation.eclipse");
 }




More information about the overlord-commits mailing list