Skip to main content
summaryrefslogtreecommitdiffstats
blob: 245b05ef447f43c8b0d5b4cc368978f2dd06530a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*******************************************************************************
 * Copyright (c) 2003, 2006 IBM 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:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Created on Mar 30, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.eclipse.jst.j2ee.internal.actions;

import java.text.MessageFormat;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jem.util.logger.proxy.Logger;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jst.j2ee.internal.deploy.DeployerRegistry;
import org.eclipse.jst.j2ee.internal.deploy.J2EEDeployOperation;
import org.eclipse.jst.j2ee.internal.dialogs.Messages;
import org.eclipse.jst.j2ee.internal.dialogs.RuntimeSelectionDialog;
import org.eclipse.jst.j2ee.internal.plugin.J2EEUIMessages;
import org.eclipse.jst.j2ee.internal.plugin.J2EEUIPlugin;
import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.frameworks.internal.WTPResourceHandler;
import org.eclipse.wst.common.frameworks.internal.plugin.WTPCommonPlugin;
import org.eclipse.wst.server.core.IRuntime;

/**
 * @author cbridgha
 * 
 * To change the template for this generated type comment go to Window - Preferences - Java - Code
 * Generation - Code and Comments
 */
public class J2EEDeployAction extends BaseAction {
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.j2ee.internal.internal.ui.actions.BaseAction#primRun(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	protected void primRun(Shell shell) {

		if (checkEnabled(shell)) {
			final IStructuredSelection deploySelection = selection;
			Job deployJob = new Job(Messages.J2EEDeployAction_Deploy_) {
				@Override
				protected IStatus run(IProgressMonitor monitor) {
					IStatus result = null;
					J2EEDeployOperation op = new J2EEDeployOperation(deploySelection.toArray());
					try {
						result = op.execute(monitor, null);
					} catch (Exception e) {
						result = new Status(IStatus.ERROR, WTPCommonPlugin.PLUGIN_ID, IStatus.ERROR, WTPResourceHandler.getString("27"), e); //$NON-NLS-1$
						Logger.getLogger().logError(e);
					} finally {
						
					}
					return result;
				}
			};
			
			
			try {
				deployJob.setUser(true);
				deployJob.schedule();
			} catch (Exception e) {
				//Ignore
			}
			
		}

	}

	/*
	 *  
	 */
	public J2EEDeployAction() {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
	 *      org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IAction action, ISelection aSelection) {
		super.selectionChanged(action, aSelection);
		action.setEnabled(true);
	}

	
	public boolean checkEnabled(Shell shell) {

		try {
			DeployerRegistry reg = DeployerRegistry.instance();

			List components = DeployerRegistry.getSelectedModules(selection.toArray());
			for (int i = 0; i < components.size(); i++) {
				IVirtualComponent component = (IVirtualComponent) components.get(i);
				IProject proj = component.getProject();
				if (proj == null) {
					displayMessageDialog(J2EEUIMessages.getResourceString("DEPLOY_PROJECT_NOT_FOUND") , shell); //$NON-NLS-1$
					return false;
				}
				
				IRuntime runtime = J2EEProjectUtilities.getServerRuntime(proj);
				if (runtime == null) {
					String message = MessageFormat.format(J2EEUIMessages.getResourceString("DEPLOY_RUNTIME_NOT_FOUND"), new Object []{proj.getName()}); //$NON-NLS-1$
					RuntimeSelectionDialog selectionDialog = new RuntimeSelectionDialog(shell, 
							J2EEUIMessages.getResourceString("DEPLOY_DIALOG_TITLE"),  //$NON-NLS-1$
 								null /* default image */, 
 								message, 
 								MessageDialog.ERROR, 
 								new String[] { IDialogConstants.OK_LABEL }, 0, proj) ;
					selectionDialog.open();
					runtime = J2EEProjectUtilities.getServerRuntime(proj);
					if (runtime == null)
						return false;
				}
				List visitors = reg.getDeployModuleExtensions(proj, runtime);
				if (visitors.isEmpty()) {
					displayMessageDialog(MessageFormat.format(J2EEUIMessages.getResourceString("DEPLOY_PROJECT_NOT_SUPPORTED"), new Object []{proj.getName()}), shell); //$NON-NLS-1$
					return false;
				}
				
			}
			
			return true;
		} catch (CoreException e) {
			J2EEUIPlugin.logError(e);
		}
		return false;
	}
	
	private void displayMessageDialog(String message, Shell shell) {
		 String title = J2EEUIMessages.getResourceString("DEPLOY_DIALOG_TITLE"); //$NON-NLS-1$
		 MessageDialog dialog = new MessageDialog(shell, 
				 								title, 
				 								null /* default image */, 
				 								message, 
				 								MessageDialog.ERROR, 
				 								new String[] { IDialogConstants.OK_LABEL }, 0) ;
	     dialog.open();
	}

}

Back to the top