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.cms.ui.workbench.internal.jcr.commands;
17  
18  import java.util.Arrays;
19  
20  import org.argeo.cms.ui.jcr.model.RepositoryElem;
21  import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
22  import org.argeo.cms.ui.workbench.jcr.JcrBrowserView;
23  import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
24  import org.argeo.eclipse.ui.dialogs.SingleValue;
25  import org.eclipse.core.commands.AbstractHandler;
26  import org.eclipse.core.commands.ExecutionEvent;
27  import org.eclipse.core.commands.ExecutionException;
28  import org.eclipse.jface.viewers.ISelection;
29  import org.eclipse.jface.viewers.IStructuredSelection;
30  import org.eclipse.ui.handlers.HandlerUtil;
31  
32  /** Create a new JCR workspace */
33  public class CreateWorkspace extends AbstractHandler {
34  
35  	public final static String ID = WorkbenchUiPlugin.PLUGIN_ID
36  			+ ".addFolderNode";
37  
38  	public Object execute(ExecutionEvent event) throws ExecutionException {
39  
40  		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
41  				.getActivePage().getSelection();
42  
43  		JcrBrowserView view = (JcrBrowserView) HandlerUtil
44  				.getActiveWorkbenchWindow(event).getActivePage()
45  				.findView(HandlerUtil.getActivePartId(event));
46  
47  		if (selection != null && !selection.isEmpty()
48  				&& selection instanceof IStructuredSelection) {
49  			Object obj = ((IStructuredSelection) selection).getFirstElement();
50  			if (!(obj instanceof RepositoryElem))
51  				return null;
52  
53  			RepositoryElem repositoryNode = (RepositoryElem) obj;
54  			String workspaceName = SingleValue.ask("Workspace name",
55  					"Enter workspace name");
56  			if (workspaceName != null) {
57  				if (Arrays.asList(repositoryNode.getAccessibleWorkspaceNames())
58  						.contains(workspaceName)) {
59  					ErrorFeedback.show("Workspace " + workspaceName
60  							+ " already exists.");
61  				} else {
62  					repositoryNode.createWorkspace(workspaceName);
63  					view.nodeAdded(repositoryNode);
64  				}
65  			}
66  		} else {
67  			ErrorFeedback.show("Cannot create workspace");
68  		}
69  		return null;
70  	}
71  }