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;
17  
18  import javax.jcr.Node;
19  import javax.jcr.RepositoryException;
20  
21  import org.argeo.slc.BasicNameVersion;
22  import org.argeo.slc.NameVersion;
23  import org.argeo.slc.SlcException;
24  import org.argeo.slc.SlcNames;
25  import org.argeo.slc.SlcTypes;
26  import org.argeo.slc.deploy.ModulesManager;
27  import org.eclipse.core.runtime.IProgressMonitor;
28  import org.eclipse.core.runtime.IStatus;
29  import org.eclipse.core.runtime.Status;
30  import org.eclipse.core.runtime.jobs.Job;
31  import org.eclipse.jface.resource.ImageDescriptor;
32  import org.eclipse.swt.graphics.Image;
33  import org.eclipse.ui.plugin.AbstractUIPlugin;
34  import org.osgi.framework.BundleContext;
35  
36  /** The activator class controls the plug-in life cycle */
37  public class ClientUiPlugin extends AbstractUIPlugin implements SlcNames {
38  	public static final String ID = "org.argeo.slc.client.ui";
39  	private static ClientUiPlugin plugin;
40  
41  	public void start(BundleContext context) throws Exception {
42  		super.start(context);
43  		plugin = this;
44  	}
45  
46  	public void stop(BundleContext context) throws Exception {
47  		plugin = null;
48  		super.stop(context);
49  	}
50  
51  	public static ClientUiPlugin getDefault() {
52  		return plugin;
53  	}
54  
55  	/** Creates the image */
56  	public static Image img(String path) {
57  		return getImageDescriptor(path).createImage();
58  	}
59  
60  	public static ImageDescriptor getImageDescriptor(String path) {
61  		return imageDescriptorFromPlugin(ID, path);
62  	}
63  
64  	/** Start execution module if it was stopped and vice-versa */
65  	public static void startStopExecutionModule(
66  			final ModulesManager modulesManager, Node node) {
67  		try {
68  			if (!node.isNodeType(SlcTypes.SLC_EXECUTION_MODULE))
69  				throw new SlcException(node + " is not an execution module");
70  
71  			String name = node.getProperty(SLC_NAME).getString();
72  			String version = node.getProperty(SLC_VERSION).getString();
73  			final NameVersion nameVersion = new BasicNameVersion(name, version);
74  			Boolean started = node.getProperty(SLC_STARTED).getBoolean();
75  
76  			Job job;
77  			if (started) {
78  				job = new Job("Stop " + nameVersion) {
79  					protected IStatus run(IProgressMonitor monitor) {
80  						monitor.beginTask("Stop " + nameVersion, 1);
81  						modulesManager.stop(nameVersion);
82  						monitor.worked(1);
83  						return Status.OK_STATUS;
84  					}
85  
86  					protected void canceling() {
87  						getThread().interrupt();
88  						super.canceling();
89  					}
90  				};
91  			} else {
92  				job = new Job("Start " + nameVersion) {
93  					protected IStatus run(IProgressMonitor monitor) {
94  						monitor.beginTask("Start " + nameVersion, 1);
95  						modulesManager.start(nameVersion);
96  						monitor.worked(1);
97  						return Status.OK_STATUS;
98  					}
99  
100 					protected void canceling() {
101 						getThread().interrupt();
102 						super.canceling();
103 					}
104 				};
105 			}
106 			job.setUser(true);
107 			job.schedule();
108 		} catch (RepositoryException e) {
109 			throw new SlcException("Cannot start " + node, e);
110 		}
111 
112 	}
113 }