Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 59c6f2b4d0d676e82e227ebdc5d3bd99e28a8d28 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package org.eclipse.jst.jsf.ui.internal.classpath;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.ArchiveFile;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.JSFLibrary;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.JSFLibraryRegistryFactory;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.JSFVersion;
import org.eclipse.jst.jsf.ui.internal.JSFUiPlugin;
import org.eclipse.jst.jsf.ui.internal.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class JSFLibraryEditControl extends Composite implements ModifyListener, SelectionListener {
	private Text txtName;
	private Label lblName;
	private CCombo cboVersions;
	private Label lblVersions;
	private Button chkDeploy;
	private Button chkImpl;
	private Composite btnBar;
	private Button btnAdd;
	private Button btnRemove;
	private TableViewer jars;
	
	private boolean initing = false;

	private JSFLibrary workingCopyLibrary;
	
	private String validationMsg;
	private Set _listeners;
	private int _isNew = -1;//use isNew() method.. not this variable directly

	public JSFLibraryEditControl(JSFLibrary workingCopyLibrary, Composite parent){
		super(parent, SWT.NONE);
		this.workingCopyLibrary = workingCopyLibrary;
		_listeners = new HashSet(1);
		createControl(parent);
	}
	
	public void addValidationListener(JSFLibraryValidationListener listener){
		removeValidationListener(listener);
		_listeners.add(listener);
	}
	
	public void removeValidationListener(JSFLibraryValidationListener listener){
		_listeners.remove(listener);
	}
	
	public void createControl(Composite parent) {
		initing = true;

//		this = new Composite(parent, SWT.NONE);
		this.setLayout(new GridLayout(2, false));
		this.setLayoutData(new GridData(GridData.FILL_BOTH));

		lblName = new Label(this, SWT.NONE);
		lblName.setText(Messages.JSFLibraryWizard_LibraryName);
		lblName.setLayoutData(new GridData(GridData.BEGINNING));

		txtName = new Text(this, SWT.BORDER);
		txtName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		txtName.addModifyListener(this);

		lblVersions = new Label(this, SWT.NONE);
		lblVersions.setText(Messages.JSFLibraryWizard_VersionSupported);

		cboVersions = new CCombo(this, SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER);
		cboVersions.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		cboVersions.addModifyListener(this);

		Group jarsComp = new Group(this, SWT.NONE);
		jarsComp.setText(Messages.JSFLibraryWizard_LibraryJars);
		GridLayout gl1 = new GridLayout(2, false);
		jarsComp.setLayout(gl1);
		GridData gd2 = new GridData(GridData.FILL_HORIZONTAL
				| GridData.FILL_VERTICAL);
		gd2.horizontalSpan = 2;
		jarsComp.setLayoutData(gd2);

		createJarsViewer(jarsComp);

		createButtons(jarsComp);

		chkImpl = new Button(this, SWT.CHECK);
		chkImpl.setText(Messages.JSFLibraryWizard_IsJSFImplementation);
		GridData gd3 = new GridData();
		gd3.horizontalSpan = 2;
		chkImpl.setLayoutData(gd3);
		chkImpl.addSelectionListener(this);

		chkDeploy = new Button(this, SWT.CHECK);
		chkDeploy.setText(Messages.JSFLibraryWizard_DeployJars);
		GridData gd4 = new GridData();
		gd4.horizontalSpan = 2;
		chkDeploy.setLayoutData(gd4);
//		chkDeploy.addSelectionListener(this);
		chkDeploy.setVisible(false);

		loadVersions();

		if (!isNew()) {
			txtName.setText(workingCopyLibrary.getName());
			cboVersions.setText(workingCopyLibrary.getJSFVersion().getName());
			chkDeploy.setSelection(workingCopyLibrary.isDeployed());
			chkImpl.setSelection(workingCopyLibrary.isImplementation());
		}
		jars.setInput(workingCopyLibrary);

		initing = false;

		txtName.setFocus();
	}

	private void createJarsViewer(Group jarsComp) {
		jars = new TableViewer(jarsComp, SWT.BORDER | SWT.MULTI);
		jars.setContentProvider(new IStructuredContentProvider() {
			public Object[] getElements(Object inputElement) {
				if (inputElement instanceof JSFLibrary)
					return ((JSFLibrary) inputElement).getArchiveFiles()
							.toArray();
				return new Object[0];
			}

			public void dispose() {
                // do nothing
			}

			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {
				// won't happen
			}
		});
		jars.setLabelProvider(new ILabelProvider() {
			private Image jarImg = null;
			public Image getImage(Object element) {
				if (jarImg == null){
					ImageDescriptor desc = JSFUiPlugin.getImageDescriptor("obj16/jar_obj.gif"); //$NON-NLS-1$
					jarImg = desc.createImage();
				}
				return jarImg;
			}

			public String getText(Object element) {
				StringBuffer labelBuf = new StringBuffer();
				if (element instanceof ArchiveFile) {
					ArchiveFile archive = (ArchiveFile) element;
					labelBuf.append(archive.getName());	
					if (!archive.exists())
						labelBuf.append("[missing]"); //$NON-NLS-1$
					labelBuf.append(" - ").append(archive.getPath()); //$NON-NLS-1$
				}
				return labelBuf.toString();
			}

			public void addListener(ILabelProviderListener listener) {
                // no listeners supported
			}

			public void dispose() {
				if (jarImg != null)
					jarImg.dispose();
			}

			public boolean isLabelProperty(Object element, String property) {
				return false;
			}

			public void removeListener(ILabelProviderListener listener) {
                // no listeners supported
			}
		});

		jars.addSelectionChangedListener(new ISelectionChangedListener(){
			public void selectionChanged(SelectionChangedEvent event){
				updateButtons();
			}
		});
		
		GridData gd = new GridData(GridData.FILL_BOTH);
//		gd.widthHint = convertWidthInCharsToPixels(30);
//		gd.heightHint = convertHeightInCharsToPixels(10);
		jars.getControl().setLayoutData(gd);
	}

	private void updateButtons() {
		btnRemove.setEnabled(!((StructuredSelection)jars.getSelection()).isEmpty());
		// getButton(IDialogConstants.OK_ID).setEnabled(modified);
	}

	private void createButtons(Composite c) {
		btnBar = new Composite(c, SWT.NONE);
		GridLayout gl = new GridLayout(1, false);
		gl.marginHeight = 0;
		gl.marginTop = 0;
		gl.marginWidth = 0;
		btnBar.setLayout(gl);
		btnBar.setLayoutData(new GridData(GridData.END));

		btnAdd = new Button(btnBar, SWT.NONE);
		btnAdd.setText(Messages.JSFLibraryWizard_Add);
		btnAdd.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL
				| GridData.VERTICAL_ALIGN_BEGINNING));
		btnAdd.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				String cur = null;
				String[] chosenJars = openExtJarFileDialog(cur);
				if (chosenJars != null) {
					for (int i = 0; i < chosenJars.length; i++) {
						String jar = chosenJars[i];
						if (!workingCopyLibrary.containsArchiveFile(jar)) {
							ArchiveFile archive = JSFLibraryRegistryFactory.eINSTANCE
								.createArchiveFile();
							archive.setSourceLocation(jar);
							archive.setRelativeDestLocation("WEB-INF/lib"); //$NON-NLS-1$
							workingCopyLibrary.getArchiveFiles().add(archive);
						}
					}
					jars.refresh();
					validate();
				}
			}
		});

		btnRemove = new Button(btnBar, SWT.NONE);
		btnRemove.setEnabled(false);
		btnRemove.setText(Messages.JSFLibraryWizard_Remove);
		btnRemove.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL
				| GridData.VERTICAL_ALIGN_BEGINNING));
		btnRemove.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (jars.getSelection() instanceof StructuredSelection){
					StructuredSelection objs = (StructuredSelection)jars.getSelection();
					if (objs != null){
						Iterator it = objs.iterator();
						 while (it.hasNext()){
							 Object obj = it.next();
							 ArchiveFile jar = (ArchiveFile)obj;
							 workingCopyLibrary.getArchiveFiles().remove(jar);							 
						 }
					}
					jars.refresh();
					validate();
				}
			}
		});
	}

	private void loadVersions() {
		cboVersions.removeAll();
		Iterator it = JSFVersion.VALUES.iterator();
		while (it.hasNext()) {
			JSFVersion ver = (JSFVersion) it.next();
			cboVersions.add(ver.getName());
		}
	}

	private String[] openExtJarFileDialog(String existing) {
		String title = Messages.JSFLibraryWizard_ExtJarFileDialogTitle;

		FileDialog dialog = new FileDialog(getShell(),
				existing == null ? SWT.MULTI : SWT.SINGLE);
		dialog.setText(title);
		dialog.setFilterExtensions(new String[] { "*.jar;*.zip" }); //$NON-NLS-1$
		// FIXME: remember and use last path chosen??
		String filterPath = ResourcesPlugin.getWorkspace().getRoot()
				.getFullPath().toString();
		dialog.setFilterPath(filterPath);
		// if (existing != null) {
		// dialog.setFileName(existing.getPath().lastSegment());
		// }

		String res = dialog.open();
		if (res == null) {
			return null;
		}
		String[] fileNames = dialog.getFileNames();
		String[] elems = new String[fileNames.length];
		IPath file = new Path(res);
		IPath apath = file.removeLastSegments(1);
		for (int i = 0; i < fileNames.length; i++) {
			elems[i] = apath.append(fileNames[i]).toString();
		}
		return elems;
	}	

	public void modifyText(ModifyEvent e) {
		validate();
		updateButtons();	
	}

	private void fireValidateEvent(final JSFLibraryValidationEvent jSFLibraryValidationEvent) {
		new Runnable(){
			public void run() {
				for (Iterator it=_listeners.iterator();it.hasNext();){
					JSFLibraryValidationListener listener = (JSFLibraryValidationListener)it.next();
					listener.notifyValidation(jSFLibraryValidationEvent);
				}
			}
		}.run();
	}


	public void widgetSelected(SelectionEvent e) {
		validate();
		updateButtons();
	}

	public void widgetDefaultSelected(SelectionEvent e) {
        // no handling for default selection
	}

	private void validate() {
		if (initing)
			return;
		validationMsg = null;
//		setPageComplete(true);
		if (!validateName() || !validateJars() || !validateVersion()) {
//			setPageComplete(false);
		}
		fireValidateEvent(new JSFLibraryValidationEvent(validationMsg));
	}
	
	private boolean validateJars() {
		if (workingCopyLibrary.getArchiveFiles().isEmpty()) {
			validationMsg = Messages.JSFLibraryWizard_ValidateNoJars;
			return false;
		}
		return true;
	}
	
	private boolean validateVersion() {
		//FIXME: why isn't selection indesx correct???
	//	if (cboVersions.getSelectionIndex() < 0) {
	//		setErrorMessage("Choose the maximum JSF version supported if known.");
	//		return false;
	//	}
		return true;
	}
	
	private boolean validateName() {
		if (txtName.getText() == null
				|| txtName.getText().trim().equals("")) { //$NON-NLS-1$
			validationMsg = Messages.JSFLibraryWizard_ValidateNoLibraryName;
			return false;
		}
		String aName = txtName.getText().trim();
		if (isNew() || (!isNew() && !getCurrentLibraryName().equals(aName))) {
			if (isNameInRegistry(JSFCorePlugin.getDefault()
					.getJSFLibraryRegistry().getAllJSFLibraries(), aName)) {
				validationMsg = Messages.JSFLibraryWizard_ValidateExistingLibraryName;
				return false;
			}
		}
		return true;
	}
	
	private boolean isNew() {
		if (_isNew == -1){
			_isNew = workingCopyLibrary.getName() == null ? 1 : 0;
		}
		return _isNew == 1;
	}

	private String getCurrentLibraryName() {
		return workingCopyLibrary.getName();		
	}

	private boolean isNameInRegistry(Collection c, String name) {
		Iterator it = c.iterator();
		while (it.hasNext()) {
			JSFLibrary lib = (JSFLibrary) it.next();
			if (lib.getName().equals(name)) {
				return true;
			}
		}
		return false;
	}

	public String getJSFLibraryName() {		
		return txtName.getText().trim();
	}


	public boolean getIsDeployed() {		
		return chkDeploy.getSelection();
	}


	public boolean getIsImplementation() {
		return chkImpl.getSelection();
	}

	public JSFVersion getJSFVersion() {
		if (cboVersions.getSelectionIndex() >= 0) {
			JSFVersion ver = (JSFVersion) JSFVersion.VALUES.get(cboVersions
					.getSelectionIndex());
			return ver;
		}
		return JSFVersion.UNKNOWN_LITERAL;
	}

	public void setImplOnly(boolean implsOnly) {
		if (implsOnly){
			chkImpl.setSelection(true);
			chkImpl.setEnabled(false);
		}
	}
	
	public void setNonImplOnly(boolean nonImplsOnly) {
		if (nonImplsOnly){
			chkImpl.setSelection(false);
			chkImpl.setEnabled(false);
		}
	}
}

Back to the top