carbidesdk/com.nokia.carbide.cpp.sdk.examples/src/com/nokia/carbide/cpp/sdk/examples/actions/NewProjectAction.java
author timkelly
Fri, 27 Mar 2009 10:37:25 -0500
changeset 13 a515b8873c8c
parent 2 d760517a8095
permissions -rw-r--r--
Update to describe how SFO CDK works
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
cawthron
parents:
diff changeset
     3
* All rights reserved.
cawthron
parents:
diff changeset
     4
* This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
* which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
*
cawthron
parents:
diff changeset
     9
* Initial Contributors:
cawthron
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
*
cawthron
parents:
diff changeset
    12
* Contributors:
cawthron
parents:
diff changeset
    13
*
cawthron
parents:
diff changeset
    14
* Description: 
cawthron
parents:
diff changeset
    15
*
cawthron
parents:
diff changeset
    16
*/
cawthron
parents:
diff changeset
    17
cawthron
parents:
diff changeset
    18
package com.nokia.carbide.cpp.sdk.examples.actions;
cawthron
parents:
diff changeset
    19
cawthron
parents:
diff changeset
    20
import com.nokia.carbide.cpp.sdk.examples.jobs.NewProjectJob;
cawthron
parents:
diff changeset
    21
cawthron
parents:
diff changeset
    22
import org.eclipse.core.resources.*;
cawthron
parents:
diff changeset
    23
import org.eclipse.core.runtime.IStatus;
cawthron
parents:
diff changeset
    24
import org.eclipse.jface.action.IAction;
cawthron
parents:
diff changeset
    25
import org.eclipse.jface.dialogs.*;
cawthron
parents:
diff changeset
    26
import org.eclipse.jface.viewers.ISelection;
cawthron
parents:
diff changeset
    27
import org.eclipse.ui.IWorkbenchWindow;
cawthron
parents:
diff changeset
    28
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
cawthron
parents:
diff changeset
    29
cawthron
parents:
diff changeset
    30
/**
cawthron
parents:
diff changeset
    31
 * This action is invoked when the user chooses our menu item.
cawthron
parents:
diff changeset
    32
 * The action is responsible for prompting the user for a
cawthron
parents:
diff changeset
    33
 * valid new project name.
cawthron
parents:
diff changeset
    34
 * The project creation is performed in {@link NewProjectJob}
cawthron
parents:
diff changeset
    35
 */
cawthron
parents:
diff changeset
    36
public class NewProjectAction implements IWorkbenchWindowActionDelegate {
cawthron
parents:
diff changeset
    37
	private IWorkbenchWindow window;
cawthron
parents:
diff changeset
    38
	/**
cawthron
parents:
diff changeset
    39
	 * The constructor.
cawthron
parents:
diff changeset
    40
	 */
cawthron
parents:
diff changeset
    41
	public NewProjectAction() {
cawthron
parents:
diff changeset
    42
	}
cawthron
parents:
diff changeset
    43
cawthron
parents:
diff changeset
    44
	/**
cawthron
parents:
diff changeset
    45
	 * The action has been activated. The argument of the
cawthron
parents:
diff changeset
    46
	 * method represents the 'real' action sitting
cawthron
parents:
diff changeset
    47
	 * in the workbench UI.
cawthron
parents:
diff changeset
    48
	 * @see IWorkbenchWindowActionDelegate#run
cawthron
parents:
diff changeset
    49
	 */
cawthron
parents:
diff changeset
    50
	public void run(IAction action) {
cawthron
parents:
diff changeset
    51
		
cawthron
parents:
diff changeset
    52
		// Validates that the project name is legal and doesn't
cawthron
parents:
diff changeset
    53
		// refer to an existing project.
cawthron
parents:
diff changeset
    54
		IInputValidator validator = new IInputValidator() {
cawthron
parents:
diff changeset
    55
			public String isValid(String newText) {
cawthron
parents:
diff changeset
    56
				String result = null;
cawthron
parents:
diff changeset
    57
				IWorkspace workspace = ResourcesPlugin.getWorkspace();
cawthron
parents:
diff changeset
    58
				IStatus status = workspace.validateName(newText, IResource.PROJECT);
cawthron
parents:
diff changeset
    59
				if (status.isOK()) {
cawthron
parents:
diff changeset
    60
					IResource existingProject = workspace.getRoot().findMember(newText);
cawthron
parents:
diff changeset
    61
					if (existingProject != null) {
cawthron
parents:
diff changeset
    62
						result = "A project with that name already exists.";
cawthron
parents:
diff changeset
    63
					}
cawthron
parents:
diff changeset
    64
				} else {
cawthron
parents:
diff changeset
    65
					result = status.getMessage();
cawthron
parents:
diff changeset
    66
				}
cawthron
parents:
diff changeset
    67
				return result;
cawthron
parents:
diff changeset
    68
			}
cawthron
parents:
diff changeset
    69
		};
cawthron
parents:
diff changeset
    70
		
cawthron
parents:
diff changeset
    71
		InputDialog dlg = new InputDialog(window.getShell(),
cawthron
parents:
diff changeset
    72
				"Create New Project", "Please enter a name for the new project",
cawthron
parents:
diff changeset
    73
				null, validator);
cawthron
parents:
diff changeset
    74
		if (dlg.open() == Dialog.OK) {
cawthron
parents:
diff changeset
    75
			// Do all the work in a WorkspaceJob
cawthron
parents:
diff changeset
    76
			NewProjectJob job = new NewProjectJob(dlg.getValue());
cawthron
parents:
diff changeset
    77
			job.schedule();
cawthron
parents:
diff changeset
    78
		}
cawthron
parents:
diff changeset
    79
	}
cawthron
parents:
diff changeset
    80
cawthron
parents:
diff changeset
    81
	/**
cawthron
parents:
diff changeset
    82
	 * Selection in the workbench has been changed. We 
cawthron
parents:
diff changeset
    83
	 * can change the state of the 'real' action here
cawthron
parents:
diff changeset
    84
	 * if we want, but this can only happen after 
cawthron
parents:
diff changeset
    85
	 * the delegate has been created.
cawthron
parents:
diff changeset
    86
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
cawthron
parents:
diff changeset
    87
	 */
cawthron
parents:
diff changeset
    88
	public void selectionChanged(IAction action, ISelection selection) {
cawthron
parents:
diff changeset
    89
	}
cawthron
parents:
diff changeset
    90
cawthron
parents:
diff changeset
    91
	/**
cawthron
parents:
diff changeset
    92
	 * We can use this method to dispose of any system
cawthron
parents:
diff changeset
    93
	 * resources we previously allocated.
cawthron
parents:
diff changeset
    94
	 * @see IWorkbenchWindowActionDelegate#dispose
cawthron
parents:
diff changeset
    95
	 */
cawthron
parents:
diff changeset
    96
	public void dispose() {
cawthron
parents:
diff changeset
    97
	}
cawthron
parents:
diff changeset
    98
cawthron
parents:
diff changeset
    99
	/**
cawthron
parents:
diff changeset
   100
	 * We will cache window object in order to
cawthron
parents:
diff changeset
   101
	 * be able to provide parent shell for the message dialog.
cawthron
parents:
diff changeset
   102
	 * @see IWorkbenchWindowActionDelegate#init
cawthron
parents:
diff changeset
   103
	 */
cawthron
parents:
diff changeset
   104
	public void init(IWorkbenchWindow window) {
cawthron
parents:
diff changeset
   105
		this.window = window;
cawthron
parents:
diff changeset
   106
	}
cawthron
parents:
diff changeset
   107
}