Skip to main content
summaryrefslogtreecommitdiffstats
blob: bff80288eee2eca753b7828d7fead89965bf682d (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*******************************************************************************
 * Copyright (c) 2009, 2012 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.jpa.ui.internal.persistence;

import java.util.Collections;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jpt.common.core.internal.utility.ProjectTools;
import org.eclipse.jpt.common.ui.internal.jface.ArchiveFileViewerFilter;
import org.eclipse.jpt.common.ui.internal.utility.swt.SWTTools;
import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel;
import org.eclipse.jpt.jpa.ui.internal.plugin.JptJpaUiPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.views.navigator.ResourceComparator;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.resources.IVirtualContainer;
import org.eclipse.wst.common.componentcore.resources.IVirtualFile;
import org.eclipse.wst.common.componentcore.resources.IVirtualFolder;
import org.eclipse.wst.common.componentcore.resources.IVirtualResource;

public class ArchiveFileSelectionDialog 
	extends ElementTreeSelectionDialog
{
	private final ModifiablePropertyValueModel<String> jarPathModel;
	
	private DeploymentPathCalculator pathCalculator;
	
	
	public ArchiveFileSelectionDialog(Shell parent) {
		this(parent, new SimpleDeploymentPathCalculator());
	}
	
	public ArchiveFileSelectionDialog(Shell parent, DeploymentPathCalculator pathCalculator) {
		super(parent, new WorkbenchLabelProvider(), new WorkbenchContentProvider());
		this.pathCalculator = pathCalculator;
		setComparator(new ResourceComparator(ResourceComparator.NAME));
		addFilter(new ArchiveFileViewerFilter());
		setValidator(new ArchiveFileSelectionValidator());
		this.jarPathModel = new SimplePropertyValueModel<String>();
	}
	
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		
		Label helpLabel = new Label(composite, SWT.WRAP);
		helpLabel.setText(JptUiPersistenceMessages.ArchiveFileSelectionDialog_jarPathHelpLabel);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.widthHint = 400;
		helpLabel.setLayoutData(gd);
		
		Composite subComposite = new Composite(composite, SWT.NONE);
		subComposite.setLayout(new GridLayout(2, false));
		subComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label jarPathLabel = new Label(subComposite, SWT.NONE);
		jarPathLabel.setFont(composite.getFont());
		jarPathLabel.setText(JptUiPersistenceMessages.ArchiveFileSelectionDialog_jarPathLabel);
		
		Text jarPathText = new Text(subComposite, SWT.BORDER);
		jarPathText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		SWTTools.bind(jarPathModel, jarPathText);
		
		return composite;
	}
	
	@Override
	protected TreeViewer doCreateTreeViewer(Composite parent, int style) {
		TreeViewer treeViewer = super.doCreateTreeViewer(parent, style);
		
		treeViewer.addSelectionChangedListener(
			new ISelectionChangedListener() {
				public void selectionChanged(SelectionChangedEvent event) {
					updateJarPathModel(event.getSelection());
				}
			});
		
		return treeViewer;
	}
	
	protected void updateJarPathModel(ISelection selection) {
		Object selectedObj = ((IStructuredSelection) selection).getFirstElement();
		if (selectedObj instanceof IFile) {
			this.jarPathModel.setValue(calculateDeployPath((IFile) selectedObj));
		}
		else {
			this.jarPathModel.setValue("");
		}
	}
	
	protected String calculateDeployPath(IFile archiveFile) {
		return this.pathCalculator.calculateDeploymentPath(archiveFile);
	}
	
	@Override
	protected void computeResult() {
		setResult(Collections.singletonList(this.jarPathModel.getValue()));
	}
	
	
	private static class ArchiveFileSelectionValidator 
		implements ISelectionStatusValidator 
	{
		public ArchiveFileSelectionValidator() {
			super();
		}
		
		
		public IStatus validate(Object[] selection) {
			int nSelected= selection.length;
			if (nSelected == 0 || (nSelected > 1)) {
				return JptJpaUiPlugin.instance().buildErrorStatus();
			}
			for (int i= 0; i < selection.length; i++) {
				Object curr= selection[i];
				if (curr instanceof IFile) {
					return JptJpaUiPlugin.instance().buildOKStatus();
				}
			}
			return JptJpaUiPlugin.instance().buildErrorStatus();
		}
	}	
	
	
	public static interface DeploymentPathCalculator
	{
		String calculateDeploymentPath(IFile file);
	}
	
	
	public static class SimpleDeploymentPathCalculator
		implements DeploymentPathCalculator
	{
		public String calculateDeploymentPath(IFile file) {
			return file.getName();
		}
	}
	
	
	public static class ModuleDeploymentPathCalculator
		extends SimpleDeploymentPathCalculator
	{
		@Override
		public String calculateDeploymentPath(IFile file) {
			// first look for virtual component that matches this file, returning
			// the path to that virtual component
			IVirtualComponent vComponent = ComponentCore.createComponent(file.getProject());
			if (vComponent != null) {
				IVirtualFolder vFolder = vComponent.getRootFolder();
				IVirtualFile vFile = findVirtualFile(vFolder, file);
				if (vFile != null) {
					return calculatePersistenceRootRelativePath(vFile);
				}
			}
			
			// then default to simple behavior
			return super.calculateDeploymentPath(file);
		}
		
		protected IVirtualFile findVirtualFile(IVirtualContainer vContainer, IFile realFile) {
			try { 
				for (IVirtualResource vResource : vContainer.members()) {
					if (vResource.getType() == IVirtualResource.FILE) {
						IVirtualFile vFile = (IVirtualFile) vResource;
						if (realFile.equals(vFile.getUnderlyingFile())) {
							return vFile;
						}
					}
					else {
						IVirtualFile vFile = findVirtualFile((IVirtualContainer) vResource, realFile);
						if (vFile != null) {
							return vFile;
						}
					}
				}
			}
			catch (CoreException ce) {
				JptJpaUiPlugin.instance().logError(ce);
			}
			
			return null;
		}
		
		protected String calculatePersistenceRootRelativePath(IVirtualFile vFile) {
			IProject project = vFile.getProject();
			IPath puRootPath = ProjectTools.getJarRuntimeRootPath(project);
			
			IPath path = vFile.getRuntimePath().makeRelativeTo(puRootPath);
			
			return path.toString();
		}
	}
}

Back to the top