View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.client.ui.dist.commands;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
21  import org.argeo.slc.client.ui.dist.DistPlugin;
22  import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
23  import org.argeo.slc.repo.JavaRepoManager;
24  import org.eclipse.core.commands.AbstractHandler;
25  import org.eclipse.core.commands.ExecutionEvent;
26  import org.eclipse.core.commands.ExecutionException;
27  import org.eclipse.jface.dialogs.Dialog;
28  import org.eclipse.jface.dialogs.InputDialog;
29  import org.eclipse.jface.resource.ImageDescriptor;
30  import org.eclipse.ui.handlers.HandlerUtil;
31  
32  /** Create a new empty workspace in the default local java repository */
33  public class CreateLocalJavaWorkspace extends AbstractHandler {
34  	private static final Log log = LogFactory
35  			.getLog(CreateLocalJavaWorkspace.class);
36  
37  	// Exposes commands meta-info
38  	public final static String ID = DistPlugin.PLUGIN_ID
39  			+ ".createLocalJavaWorkspace";
40  	public final static String DEFAULT_LABEL = "Create local Java workspace...";
41  	public final static ImageDescriptor DEFAULT_ICON = DistPlugin
42  			.getImageDescriptor("icons/addItem.gif");
43  
44  	// Parameters
45  	public final static String PARAM_WORKSPACE_PREFIX = "workspacePrefix";
46  
47  	/* DEPENDENCY INJECTION */
48  	private JavaRepoManager javaRepoManager;
49  
50  	public Object execute(ExecutionEvent event) throws ExecutionException {
51  
52  		String prefix = event.getParameter(PARAM_WORKSPACE_PREFIX);
53  		// TODO : add an input validator
54  		InputDialog inputDialog = new InputDialog(HandlerUtil
55  				.getActiveWorkbenchWindow(event).getShell(), "Workspace name?",
56  				"Choose a name for the workspace to create",
57  				prefix == null ? "" : prefix + "-", null);
58  		int result = inputDialog.open();
59  
60  		String enteredName = inputDialog.getValue();
61  		final String legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXZY0123456789_";
62  		char[] arr = enteredName.toUpperCase().toCharArray();
63  		int count = 0;
64  		for (int i = 0; i < arr.length; i++) {
65  			if (legalChars.indexOf(arr[i]) == -1)
66  				count = count + 7;
67  			else
68  				count++;
69  		}
70  
71  		if (count > 60) {
72  			ErrorFeedback.show("Workspace name '" + enteredName
73  					+ "' is too long or contains"
74  					+ " too many special characters such as '.' or '-'.");
75  			return null;
76  		}
77  
78  		String workspaceName = enteredName;
79  		// Canceled by user
80  		if (result == Dialog.CANCEL || workspaceName == null
81  				|| "".equals(workspaceName.trim()))
82  			return null;
83  
84  		// FIXME will throw an exception if this workspace name is already used.
85  		javaRepoManager.createWorkspace(workspaceName);
86  
87  		CommandHelpers.callCommand(RefreshDistributionsView.ID);
88  		if (log.isTraceEnabled())
89  			log.trace("WORKSPACE " + workspaceName + " CREATED");
90  
91  		return null;
92  	}
93  
94  	/* DEPENDENCY INJECTION */
95  	public void setJavaRepoManager(JavaRepoManager javaRepoManager) {
96  		this.javaRepoManager = javaRepoManager;
97  	}
98  }