Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] MBS changes to have a project conversion support in context view menu..

Hi,

 

      Here I attached a patch that contains the code/changes needed to have a context view support for project conversions. It displays a list of project converters available for the selected project in a dialog box. If there are no converters are available, it displays a message saying that there are no converters available. If the converters are available and user selects a particular converter from the list and clicks ‘Ok’ button, then it converts the Project.

 

 

Thanks & Regards,

Sunil

Index: plugin.properties
===================================================================
RCS file: /home/tools/org.eclipse.cdt-build/org.eclipse.cdt.managedbuilder.ui/plugin.properties,v
retrieving revision 1.25
diff -u -r1.25 plugin.properties
--- plugin.properties	20 Jun 2005 20:52:06 -0000	1.25
+++ plugin.properties	19 Sep 2005 21:24:13 -0000
@@ -26,3 +26,6 @@
 
 #The Resource Property page
 MngResourceProp.name=C/C++ Build
+
+#The Project Converter page
+ConvertTargetAction.label=Convert To...
Index: plugin.xml
===================================================================
RCS file: /home/tools/org.eclipse.cdt-build/org.eclipse.cdt.managedbuilder.ui/plugin.xml,v
retrieving revision 1.58
diff -u -r1.58 plugin.xml
--- plugin.xml	2 Jun 2005 11:31:40 -0000	1.58
+++ plugin.xml	19 Sep 2005 21:24:13 -0000
@@ -121,5 +121,25 @@
             id="org.eclipse.cdt.managedbuilder.ui.preferences.BuildPreferences">
       </page>
    </extension>
-  
+   
+   <!-- Action for Project Converter in context menu -->
+  <extension
+         point="org.eclipse.ui.popupMenus">
+      <objectContribution
+            objectClass="org.eclipse.core.resources.IProject"
+            adaptable="true"
+            id="org.eclipse.cdt.managedbuilder.ui.popupMenu.ConvertTargetContribution">
+         <action
+               label="%ConvertTargetAction.label"
+               class="org.eclipse.cdt.managedbuilder.ui.actions.ConvertTargetAction"
+               menubarPath="convertGroup"
+               enablesFor="1"
+               id="org.eclipse.cdt.managedbuilder.ui.ConvertTargetAction">
+         </action>         
+         <filter
+               name="projectNature"
+               value="org.eclipse.cdt.managedbuilder.core.managedBuildNature">
+         </filter>
+      </objectContribution>
+   </extension>   
 </plugin>
Index: src/org/eclipse/cdt/managedbuilder/internal/ui/PluginResources.properties
===================================================================
RCS file: /home/tools/org.eclipse.cdt-build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/internal/ui/PluginResources.properties,v
retrieving revision 1.37
diff -u -r1.37 PluginResources.properties
--- src/org/eclipse/cdt/managedbuilder/internal/ui/PluginResources.properties	22 Jul 2005 15:03:48 -0000	1.37
+++ src/org/eclipse/cdt/managedbuilder/internal/ui/PluginResources.properties	19 Sep 2005 21:24:13 -0000
@@ -327,3 +327,17 @@
 showAdvanced = &Advanced >>
 hideAdvanced = << &Advanced
 NewFolderDialog.folderNameEmpty = Folder name must be specified
+
+# Project Conversion Dialog messages
+ProjectConvert.confirmdialog.title=Confirm Project Conversion
+ProjectConvert.confirmdialog.message=The selected project {0} will be converted. Do you want to proceed ?
+
+ProjectConvert.conversionErrordialog.title=Project Conversion Error
+ProjectConvert.conversionErrordialog.message=Error has occured during the conversion of the project {0} .
+
+ProjectConvert.noConverterErrordialog.title=Project Conversion Error
+ProjectConvert.noConverterErrordialog.message=There are no converters available to convert the project {0} .
+
+ProjectConvert.title=Project Converters for {0}
+ProjectConvert.convertersList=Converters List
+			        
Index: src/org/eclipse/cdt/managedbuilder/ui/actions/ConvertTargetAction.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-build/org.eclipse.cdt.managedbuilder.ui/src/org/eclipse/cdt/managedbuilder/ui/actions/ConvertTargetAction.java,v
retrieving revision 1.1
diff -u -r1.1 ConvertTargetAction.java
--- src/org/eclipse/cdt/managedbuilder/ui/actions/ConvertTargetAction.java	10 Jun 2005 15:22:01 -0000	1.1
+++ src/org/eclipse/cdt/managedbuilder/ui/actions/ConvertTargetAction.java	19 Sep 2005 21:24:13 -0000
@@ -10,10 +10,6 @@
  *******************************************************************************/
 package org.eclipse.cdt.managedbuilder.ui.actions;
 
-import java.util.ArrayList;
-import java.util.List;
-
-import org.eclipse.cdt.managedbuilder.core.IConvertManagedBuildObject;
 import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
 import org.eclipse.cdt.managedbuilder.core.IManagedProject;
 import org.eclipse.cdt.managedbuilder.core.IProjectType;
@@ -22,11 +18,6 @@
 import org.eclipse.cdt.managedbuilder.internal.ui.ManagedBuilderUIPlugin;
 
 import org.eclipse.core.resources.IProject;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IConfigurationElement;
-import org.eclipse.core.runtime.IExtension;
-import org.eclipse.core.runtime.IExtensionPoint;
-import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.ISelection;
@@ -40,93 +31,29 @@
 	extends ActionDelegate
 	implements IObjectActionDelegate {
 
-	private String  converterId = null;
-	private String	fromId = null;
-	private String	toId = null;
-	private IConvertManagedBuildObject  convertBuildObject = null;
 	private IProject selectedProject = null;
 	
+	public static final String PREFIX = "ProjectConvert";	//$NON-NLS-1$
+	public static final String PROJECT_CONVERTER_DIALOG = PREFIX + ".title";	//$NON-NLS-1$
+	
 	public static void initStartup() {
 		return;
 	}
 	
-	public void initConvertAction(IAction action) {
-		convertBuildObject = null;
-		String id = action.getId();
-		try {
-			
-			// Get the Converter Extension Point
-			IExtensionPoint extensionPoint = Platform.getExtensionRegistry()
-					.getExtensionPoint("org.eclipse.cdt.managedbuilder.core",	//$NON-NLS-1$
-							"projectConverter");	//$NON-NLS-1$
-			if (extensionPoint != null) {
-				// Get the Converter Extensions
-				IExtension[] extensions = extensionPoint.getExtensions();
-				List list = new ArrayList(extensions.length);
-				for (int i = 0; i < extensions.length; i++) {
-					
-					// Get the configuration elements for each extension
-					IConfigurationElement[] configElements = extensions[i]
-							.getConfigurationElements();
-
-					for (int j = 0; j < configElements.length; j++) {
-						IConfigurationElement element = configElements[j];
-
-						if (element.getName().equals("converter")) {	//$NON-NLS-1$
-							// Get the converter 'id'
-							String tmpConverterID = element.getAttribute("id"); //$NON-NLS-1$
-							// If the converter 'id' and action 'id' are same.
-							if (id.equals(tmpConverterID)) {
-
-								convertBuildObject = (IConvertManagedBuildObject) element
-									.createExecutableExtension("class"); //$NON-NLS-1$
-								fromId = element.getAttribute("fromId"); //$NON-NLS-1$
-								toId = element.getAttribute("toId"); //$NON-NLS-1$
-								return;
-							}
-						}
-					}
-				}
-			}
-		} catch (CoreException e) {
-		}
-	}
-	
 	public void selectionChanged(IAction action, ISelection selection) {
-		initConvertAction(action);
 		if (selection instanceof IStructuredSelection) {
 			IStructuredSelection sel = (IStructuredSelection) selection;
 			Object obj = sel.getFirstElement();
 			if (obj instanceof IProject) {
-				 IProject project = (IProject)obj;
-				 
+				 IProject project = (IProject)obj;				 
 				 // Save the selected project.
 				 setSelectedProject(project);
-				 
-				 // If the project does not have managed build nature then disable the action.
-				 try {
-					if(!project.hasNature("org.eclipse.cdt.managedbuilder.core.managedBuildNature")) {	//$NON-NLS-1$
-						 action.setEnabled(false);
-						 return;
-					 }
-				} catch (CoreException e) {
-//					 e.printStackTrace();
-				}
-					 
-				// Get the projectType of the project.
-				IProjectType projectType = getProjectType(project);
-
-				// Check whether the Converter can convert the selected project.
-				 if( isProjectConvertable(projectType))
-					 action.setEnabled(true);
-				 else
-					 action.setEnabled(false);
 			} else {
-				action.setEnabled(false);
+				 setSelectedProject(null);
 			}
 		}
 	}
-	
+
 	private IProjectType getProjectType(IProject project) {
 		IProjectType projectType = null;
 
@@ -134,76 +61,48 @@
 		IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
 		if (info != null) {
 			IManagedProject managedProject = info.getManagedProject();
-			projectType = managedProject.getProjectType();
+			if ( managedProject != null )
+				projectType = managedProject.getProjectType();
 		}
 		return projectType;
 	}
 	
-	
-	private boolean isProjectConvertable(IProjectType projectType) {
+
+	public void run(IAction action) {
+		Shell shell = ManagedBuilderUIPlugin.getDefault().getShell();
 		
-		IProjectType  tmpProjectType = projectType;
+		// Check whether the converters available for the selected project
+		// If there are no converters display error dialog otherwise display converters list
 		
-//		Check whether the converter can convert the given projectType
-		if(fromId == null)
-			return false;
-
-		while( tmpProjectType != null) {
-			String id = tmpProjectType.getId();
-			
-			if (fromId.equals(id))
-				return true;
-			else
-				tmpProjectType = tmpProjectType.getSuperClass();
+		if( ManagedBuildManager.hasTargetConversionElements(getProjectType(getSelectedProject())) == true ) {			
+			handleConvertTargetAction();
+		} else {
+			MessageDialog.openError(shell,ManagedBuilderUIMessages.getResourceString("ProjectConvert.noConverterErrordialog.title"), //$NON-NLS-1$
+			        ManagedBuilderUIMessages.getFormattedString("ProjectConvert.noConverterErrordialog.message", new String[] {getSelectedProject().getName()}) );  //$NON-NLS-1$
 		}
-		return false;
 	}
 
-
-	
-	public void run(IAction action) {
-		if( convertBuildObject != null) {
-//			 Get the confirmation from user before converting the selected project
-			Shell shell = ManagedBuilderUIPlugin.getDefault().getShell();
-			boolean shouldConvert = MessageDialog.openQuestion(shell,
-			        ManagedBuilderUIMessages.getResourceString("ProjectConvert.confirmdialog.title"), //$NON-NLS-1$
-			        ManagedBuilderUIMessages.getFormattedString("ProjectConvert.confirmdialog.message",  //$NON-NLS-1$
-			                new String[] {getSelectedProject().getName()}));
-			
-			if (shouldConvert) {
-				convertBuildObject.convert( getProjectType(getSelectedProject()), getFromId(), getToId(), true);
+	private void handleConvertTargetAction() {
+		Shell shell = ManagedBuilderUIPlugin.getDefault().getShell();
+		
+		String title = ManagedBuilderUIMessages.getFormattedString(PROJECT_CONVERTER_DIALOG, new String(getSelectedProject().getName())); 
+		ConvertTargetDialog dialog = new ConvertTargetDialog(shell, getSelectedProject(), title);
+		if ( dialog.open() == ConvertTargetDialog.OK ) {
+			if ( ConvertTargetDialog.isConversionSuccessful() == false) {									
+				MessageDialog.openError(
+								shell,
+								ManagedBuilderUIMessages
+										.getResourceString("ProjectConvert.conversionErrordialog.title"), //$NON-NLS-1$
+								ManagedBuilderUIMessages
+										.getFormattedString(
+												"ProjectConvert.conversionErrordialog.message", new String[] { getSelectedProject().getName() })); //$NON-NLS-1$
 			}
 		}
-		
+		return;
 	}
-
-	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
-		// TODO Auto-generated method stub
 		
-	}
-
-	private IConvertManagedBuildObject getConvertBuildObject() {
-		return convertBuildObject;
-	}
-
-	private void setConvertBuildObject(IConvertManagedBuildObject convertBuildObject) {
-		this.convertBuildObject = convertBuildObject;
-	}
-
-	private String getConverterId() {
-		return converterId;
-	}
-
-	private void setConverterId(String converterId) {
-		this.converterId = converterId;
-	}
-
-	private String getFromId() {
-		return fromId;
-	}
-
-	private void setFromId(String fromId) {
-		this.fromId = fromId;
+	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
+		// TODO Auto-generated method stub	
 	}
 
 	/**
@@ -219,18 +118,4 @@
 	private void setSelectedProject(IProject selectedProject) {
 		this.selectedProject = selectedProject;
 	}
-
-	/**
-	 * @return Returns the toId.
-	 */
-	private String getToId() {
-		return toId;
-	}
-
-	/**
-	 * @param toId The toId to set.
-	 */
-	private void setToId(String toId) {
-		this.toId = toId;
-	}
 }
Index: src/org/eclipse/cdt/managedbuilder/ui/actions/ConvertTargetDialog.java
===================================================================
RCS file: src/org/eclipse/cdt/managedbuilder/ui/actions/ConvertTargetDialog.java
diff -N src/org/eclipse/cdt/managedbuilder/ui/actions/ConvertTargetDialog.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/cdt/managedbuilder/ui/actions/ConvertTargetDialog.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,225 @@
+/*******************************************************************************
+ * Copyright (c) 2005 Intel Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Intel Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.managedbuilder.ui.actions;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.eclipse.cdt.managedbuilder.core.IConvertManagedBuildObject;
+import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
+import org.eclipse.cdt.managedbuilder.core.IManagedProject;
+import org.eclipse.cdt.managedbuilder.core.IProjectType;
+import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
+import org.eclipse.cdt.managedbuilder.internal.ui.ManagedBuilderUIMessages;
+import org.eclipse.cdt.internal.ui.dialogs.StatusDialog;
+import org.eclipse.cdt.internal.ui.dialogs.StatusInfo;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.DisposeEvent;
+import org.eclipse.swt.events.DisposeListener;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.List;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Shell;
+
+public class ConvertTargetDialog extends StatusDialog {
+
+	final private String title;
+	protected List convertersList;
+	private IProject project;
+	private Map conversionElements;
+	private IConfigurationElement selectedConversionElement;
+	private static boolean isConversionSuccessful = false;
+	
+	public static final String PREFIX = "ProjectConvert";	//$NON-NLS-1$
+	public static final String CONVERTERS_LIST = PREFIX +".convertersList";	//$NON-NLS-1$
+	
+	
+	/**
+	 * @param parentShell
+	 * @param project	  
+	 * @param title The title of the dialog
+	 */
+	protected ConvertTargetDialog(Shell parentShell, IProject project, String title) {
+		super(parentShell);
+		this.title = title;
+		setProject(project);
+		
+		conversionElements = ManagedBuildManager.getConversionElements(getProjectType());
+		
+		setShellStyle(getShellStyle()|SWT.RESIZE);		
+	}
+	
+	protected void buttonPressed(int buttonId) {
+		if (buttonId == IDialogConstants.OK_ID) {
+		
+			handleConverterSelection();
+			IConvertManagedBuildObject convertBuildObject = null;
+			try {
+				convertBuildObject = (IConvertManagedBuildObject) getSelectedConversionElement()
+						.createExecutableExtension("class");	//$NON-NLS-1$
+			} catch (CoreException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+			if (convertBuildObject != null) {
+				String fromId = getSelectedConversionElement().getAttribute(
+						"fromId");	//$NON-NLS-1$
+				String toId = getSelectedConversionElement().getAttribute(
+						"toId");	//$NON-NLS-1$
+				
+				IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(getProject());
+				if (info != null) {
+					IManagedProject managedProject = info.getManagedProject();
+					if (managedProject != null) {
+						if (convertBuildObject.convert(managedProject, fromId,
+								toId, true) == null) {
+							setConversionSuccessful(false);
+						} else {
+							setConversionSuccessful(true);
+						}
+					} else {
+						setConversionSuccessful(false);
+					}
+				} else {
+					setConversionSuccessful(false);
+				}				
+			} else {
+				setConversionSuccessful(false);
+			}
+		} 
+		super.buttonPressed(buttonId);
+	}
+
+	
+	protected void configureShell(Shell shell) {
+		super.configureShell(shell);
+		if (title != null)
+			shell.setText(title);
+	}
+
+	protected Control createDialogArea(Composite parent) {
+		
+		Composite comp = new Composite(parent, SWT.NULL);
+		comp.setFont(parent.getFont());
+		comp.setLayout(new GridLayout(1, true));
+		comp.setLayoutData(new GridData(GridData.FILL_BOTH));
+		
+	// Create the converters list group area
+		final Group convertersListGroup = new Group(comp, SWT.NONE);
+		convertersListGroup.setFont(parent.getFont());
+		convertersListGroup.setText(ManagedBuilderUIMessages.getResourceString(CONVERTERS_LIST));
+		convertersListGroup.setLayout(new GridLayout(1, false));
+		convertersListGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
+	
+		// Create the current config List
+		convertersList = new List(convertersListGroup, SWT.SINGLE|SWT.V_SCROLL|SWT.H_SCROLL|SWT.BORDER);
+		convertersList.setFont(convertersListGroup.getFont());
+		GridData data = new GridData(GridData.FILL_BOTH);
+		convertersList.setLayoutData(data);
+		convertersList.addDisposeListener(new DisposeListener() {
+			public void widgetDisposed(DisposeEvent event) {
+				convertersList = null;
+			}
+		});
+		convertersList.addListener (SWT.Selection, new Listener () {
+			public void handleEvent(Event e) {
+				validateState();
+			}			
+		});
+		Object [] objs = getConversionElements().keySet().toArray();
+		String [] names = new String[objs.length];
+		for (int i = 0; i < objs.length; i++) {
+			Object object = objs[i];
+			names[i] = (String)object;			
+		}
+	    convertersList.setItems(names);
+	    validateState();
+		return comp;
+	}
+	
+	private void handleConverterSelection() {
+		// Determine which configuration was selected
+		int selectionIndex = convertersList.getSelectionIndex();
+
+		String selectedConverterName = convertersList
+				.getItem(selectionIndex);
+
+		IConfigurationElement selectedElement = (IConfigurationElement) getConversionElements()
+				.get(selectedConverterName);
+		setSelectedConversionElement(selectedElement);
+		return;
+	}
+	
+	private void validateState() {
+		StatusInfo status= new StatusInfo();
+		if ( convertersList.getSelectionIndex() == -1 ) {
+			// No error, just disable 'Ok' button
+			status.setError("");	//$NON-NLS-1$
+		}
+		updateStatus(status);
+		return;
+	}
+	
+	private Map getConversionElements() {
+		if (conversionElements == null) {
+			conversionElements = new HashMap(); 
+		}
+		return conversionElements;
+	}
+
+	private IProjectType getProjectType() {
+		IProjectType projectType = null;
+
+		// Get the projectType from project.
+		IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(getProject());
+		if (info != null) {
+			IManagedProject managedProject = info.getManagedProject();
+			if ( managedProject != null) {
+				projectType = managedProject.getProjectType();
+			}
+		}
+		return projectType;
+	}
+	
+	public IProject getProject() {
+		return project;
+	}
+
+	public void setProject(IProject project) {
+		this.project = project;
+	}
+
+	public IConfigurationElement getSelectedConversionElement() {
+		return selectedConversionElement;
+	}
+
+	public void setSelectedConversionElement(
+			IConfigurationElement selectedConversionElement) {
+		this.selectedConversionElement = selectedConversionElement;
+	}
+
+	public static boolean isConversionSuccessful() {
+		return isConversionSuccessful;
+	}
+
+	public void setConversionSuccessful(boolean isConversionSuccessful) {
+		ConvertTargetDialog.isConversionSuccessful = isConversionSuccessful;
+	}
+}
+

Back to the top