Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 256731 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/pde/internal/ds/ui/wizards/DSNewWizard.java (-5 / +166 lines)
Lines 14-28 Link Here
14
14
15
import java.lang.reflect.InvocationTargetException;
15
import java.lang.reflect.InvocationTargetException;
16
16
17
import org.eclipse.core.resources.IFolder;
18
import org.eclipse.core.resources.IProject;
19
import org.eclipse.core.resources.IResource;
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.NullProgressMonitor;
22
import org.eclipse.jdt.core.ICompilationUnit;
23
import org.eclipse.jdt.core.IJavaProject;
24
import org.eclipse.jdt.core.IType;
25
import org.eclipse.jdt.core.JavaCore;
26
import org.eclipse.jdt.core.JavaModelException;
27
import org.eclipse.jdt.internal.core.JavaElement;
28
import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
29
import org.eclipse.jface.dialogs.IDialogSettings;
17
import org.eclipse.jface.operation.IRunnableWithProgress;
30
import org.eclipse.jface.operation.IRunnableWithProgress;
18
import org.eclipse.jface.viewers.IStructuredSelection;
31
import org.eclipse.jface.viewers.IStructuredSelection;
32
import org.eclipse.jface.viewers.StructuredSelection;
19
import org.eclipse.jface.wizard.Wizard;
33
import org.eclipse.jface.wizard.Wizard;
20
import org.eclipse.pde.internal.ds.ui.Activator;
34
import org.eclipse.pde.internal.ds.ui.Activator;
21
import org.eclipse.pde.internal.ds.ui.Messages;
35
import org.eclipse.pde.internal.ds.ui.Messages;
36
import org.eclipse.pde.internal.ds.ui.parts.HandyPromptDialog;
37
import org.eclipse.pde.internal.ui.PDEPlugin;
22
import org.eclipse.ui.INewWizard;
38
import org.eclipse.ui.INewWizard;
23
import org.eclipse.ui.IWorkbench;
39
import org.eclipse.ui.IWorkbench;
24
40
25
public class DSNewWizard extends Wizard implements INewWizard {
41
public class DSNewWizard extends Wizard implements INewWizard {
42
	private static final String F_DefaultOSGIFolder_key = "default_folder"; //$NON-NLS-1$
43
	private static final String F_createOSGIFolder_key = "create_folder"; //$NON-NLS-1$
44
	private static final String F_OSGI_FOLDER_NAME = "OSGI-INF"; //$NON-NLS-1$
26
	protected DSFileWizardPage fMainPage;
45
	protected DSFileWizardPage fMainPage;
27
46
28
	public DSNewWizard() {
47
	public DSNewWizard() {
Lines 32-59 Link Here
32
	/*
51
	/*
33
	 * (non-Javadoc)
52
	 * (non-Javadoc)
34
	 * 
53
	 * 
35
	 * @see org.eclipse.ui.wizards.newresource.BasicNewFileResourceWizard#addPages()
54
	 * @see
55
	 * org.eclipse.ui.wizards.newresource.BasicNewFileResourceWizard#addPages()
36
	 */
56
	 */
37
	public void addPages() {
57
	public void addPages() {
38
		 addPage(fMainPage);
58
		addPage(fMainPage);
39
	}
59
	}
40
	
60
41
	public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
61
	public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
42
		setWindowTitle(Messages.DSNewWizard_title);
62
		setWindowTitle(Messages.DSNewWizard_title);
63
		setDialogSettings(PDEPlugin.getDefault().getDialogSettings());
64
		boolean value = false;
65
		if (this.getDialogSettings().get(F_DefaultOSGIFolder_key) == null) {
66
			value = showDefaultFolderPromptyDialog();
67
			if (value) {
68
				currentSelection = getOSGIFolderSelection(currentSelection);
69
			}
70
		} else {
71
			if (this.getDialogSettings().getBoolean(F_DefaultOSGIFolder_key)) {
72
				currentSelection = getOSGIFolderSelection(currentSelection);
73
			}
74
		}
75
43
		fMainPage = new DSFileWizardPage(currentSelection);
76
		fMainPage = new DSFileWizardPage(currentSelection);
44
	}
77
	}
45
78
79
	private boolean showDefaultFolderPromptyDialog() {
80
		boolean value;
81
		HandyPromptDialog d = new HandyPromptDialog(this.getShell(),
82
				Messages.DSNewWizard_defaultOSGIFolder_message);
83
		d.setTitle(Messages.DSNewWizard_defaultOSGIFolder_title);
84
		int checkboxId = d.addCheckbox(Messages.DSNewWizard_RememberMyDecision,
85
				false);
86
		d.open();
87
		value = d.getReturnCode() == HandyPromptDialog.OK;
88
		if (d.isCheckboxSelected(checkboxId)) {
89
			this.getDialogSettings().put(F_DefaultOSGIFolder_key, value);
90
		}
91
		return value;
92
	}
93
94
	private IStructuredSelection getOSGIFolderSelection(
95
			IStructuredSelection currentSelection) {
96
		if (currentSelection != null) {
97
			Object element = currentSelection.getFirstElement();
98
			if (element != null) {
99
				IProject project = getProject(element);
100
				if (project != null) {
101
					IResource findMember = project.findMember(F_OSGI_FOLDER_NAME);
102
					if (findMember != null) {
103
						if (findMember instanceof IFolder) {
104
							currentSelection = new StructuredSelection(
105
									findMember);
106
						}
107
					} else {
108
						currentSelection = getNewFolderPath(currentSelection,
109
								project);
110
					}
111
				}
112
			}
113
114
		}
115
		return currentSelection;
116
	}
117
118
	private IStructuredSelection getNewFolderPath(
119
			IStructuredSelection currentSelection, IProject project) {
120
		if (this.getDialogSettings().get(F_createOSGIFolder_key) == null) {
121
			HandyPromptDialog d = new HandyPromptDialog(this.getShell(),
122
					Messages.DSNewWizard_createOSGIFolder_message);
123
			d.setTitle(Messages.DSNewWizard_createOSGIFolder_title);
124
			int checkboxId = d.addCheckbox(
125
					Messages.DSNewWizard_RememberMyDecision, false);
126
			d.open();
127
			boolean value = d.getReturnCode() == HandyPromptDialog.OK;
128
			if (value) {
129
				IFolder folder = project.getFolder(F_OSGI_FOLDER_NAME);
130
				try {
131
					folder.create(false, true, new NullProgressMonitor());
132
					currentSelection = new StructuredSelection(folder);
133
				} catch (CoreException e) {
134
				}
135
			}
136
			if (d.isCheckboxSelected(checkboxId)) {
137
				this.getDialogSettings().put(F_createOSGIFolder_key, value);
138
			}
139
		} else {
140
			if (this.getDialogSettings().getBoolean(F_createOSGIFolder_key)) {
141
				IFolder folder = project.getFolder(F_OSGI_FOLDER_NAME);
142
				try {
143
					folder.create(false, true, new NullProgressMonitor());
144
					currentSelection = new StructuredSelection(folder);
145
				} catch (CoreException e) {
146
				}
147
			}
148
		}
149
		return currentSelection;
150
	}
151
152
	private IProject getProject(Object element) {
153
		IProject project = null;
154
		if (element instanceof IResource) {
155
			project = ((IResource) element).getProject();
156
		} else if (element instanceof JavaElement) {
157
			project = ((JavaElement) element).getJavaProject().getProject();
158
		} else if (element instanceof ClassPathContainer) {
159
			project = ((ClassPathContainer) element).getJavaProject()
160
					.getProject();
161
		}
162
		return project;
163
	}
164
46
	/*
165
	/*
47
	 * (non-Javadoc)
166
	 * (non-Javadoc)
48
	 * 
167
	 * 
49
	 * @see org.eclipse.ui.wizards.newresource.BasicNewFileResourceWizard#performFinish()
168
	 * @see
169
	 * org.eclipse.ui.wizards.newresource.BasicNewFileResourceWizard#performFinish
170
	 * ()
50
	 */
171
	 */
51
	public boolean performFinish() {
172
	public boolean performFinish() {
52
		try {
173
		try {
53
174
175
			String implementationClassValue = fMainPage
176
					.getDSImplementationClassValue();
177
			if (fMainPage.isGenerateMethodsButtonSelected()) {
178
				createMethods(implementationClassValue);
179
			}
180
181
			IDialogSettings settings = getDialogSettings();
182
			if (settings != null) {
183
				fMainPage.saveSettings(settings);
184
			}
54
			IRunnableWithProgress op = new DSCreationOperation(fMainPage
185
			IRunnableWithProgress op = new DSCreationOperation(fMainPage
55
					.createNewFile(), fMainPage.getDSComponentNameValue(),
186
					.createNewFile(), fMainPage.getDSComponentNameValue(),
56
					fMainPage.getDSImplementationClassValue());
187
					implementationClassValue);
57
188
58
			getContainer().run(false, true, op);
189
			getContainer().run(false, true, op);
59
		} catch (InvocationTargetException e) {
190
		} catch (InvocationTargetException e) {
Lines 61-67 Link Here
61
			return false;
192
			return false;
62
		} catch (InterruptedException e) {
193
		} catch (InterruptedException e) {
63
			return false;
194
			return false;
195
196
		} catch (JavaModelException e) {
197
			return false;
64
		}
198
		}
65
		return true;
199
		return true;
66
	}
200
	}
201
202
	private void createMethods(String implementationClassValue)
203
			throws JavaModelException {
204
205
		IJavaProject proj = JavaCore.create(fMainPage.getProject());
206
		IType saved_type = proj.findType(implementationClassValue);
207
		ICompilationUnit workingCU = saved_type.getCompilationUnit()
208
				.getWorkingCopy(new NullProgressMonitor());
209
210
		// create import
211
		String importString = "org.osgi.service.component.ComponentContext";//$NON-NLS-1$
212
		workingCU.createImport(importString, null, new NullProgressMonitor());
213
214
		// create methods
215
		IType workingType = workingCU.getType(saved_type.getElementName());
216
		String activateMethodString = "protected void activate(ComponentContext context) {\n" //$NON-NLS-1$
217
				+ "	// TODO Auto-generated method stub \n}";//$NON-NLS-1$
218
		String deactivateMethodString = "protected void deactivate(ComponentContext context) {\n" //$NON-NLS-1$
219
				+ "	// TODO Auto-generated method stub \n}";//$NON-NLS-1$
220
		workingType.createMethod(activateMethodString, null, false,
221
				new NullProgressMonitor());
222
		workingType.createMethod(deactivateMethodString, null, false,
223
				new NullProgressMonitor());
224
225
		workingCU.commitWorkingCopy(false, new NullProgressMonitor());
226
	}
227
67
}
228
}
(-)src/org/eclipse/pde/internal/ds/ui/wizards/DSFileWizardPage.java (-13 / +116 lines)
Lines 20-25 Link Here
20
import org.eclipse.core.runtime.Status;
20
import org.eclipse.core.runtime.Status;
21
import org.eclipse.jdt.core.IJavaElement;
21
import org.eclipse.jdt.core.IJavaElement;
22
import org.eclipse.jdt.core.IJavaProject;
22
import org.eclipse.jdt.core.IJavaProject;
23
import org.eclipse.jdt.core.IMethod;
23
import org.eclipse.jdt.core.IType;
24
import org.eclipse.jdt.core.IType;
24
import org.eclipse.jdt.core.JavaCore;
25
import org.eclipse.jdt.core.JavaCore;
25
import org.eclipse.jdt.core.search.SearchEngine;
26
import org.eclipse.jdt.core.search.SearchEngine;
Lines 27-32 Link Here
27
import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
28
import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
28
import org.eclipse.jdt.ui.IJavaElementSearchConstants;
29
import org.eclipse.jdt.ui.IJavaElementSearchConstants;
29
import org.eclipse.jdt.ui.JavaUI;
30
import org.eclipse.jdt.ui.JavaUI;
31
import org.eclipse.jface.dialogs.IDialogSettings;
30
import org.eclipse.jface.viewers.IStructuredSelection;
32
import org.eclipse.jface.viewers.IStructuredSelection;
31
import org.eclipse.jface.window.Window;
33
import org.eclipse.jface.window.Window;
32
import org.eclipse.jface.wizard.WizardDialog;
34
import org.eclipse.jface.wizard.WizardDialog;
Lines 35-40 Link Here
35
import org.eclipse.pde.internal.ds.ui.Activator;
37
import org.eclipse.pde.internal.ds.ui.Activator;
36
import org.eclipse.pde.internal.ds.ui.Messages;
38
import org.eclipse.pde.internal.ds.ui.Messages;
37
import org.eclipse.pde.internal.ds.ui.SWTUtil;
39
import org.eclipse.pde.internal.ds.ui.SWTUtil;
40
import org.eclipse.pde.internal.ui.PDEPlugin;
38
import org.eclipse.pde.internal.ui.util.PDEModelUtility;
41
import org.eclipse.pde.internal.ui.util.PDEModelUtility;
39
import org.eclipse.swt.SWT;
42
import org.eclipse.swt.SWT;
40
import org.eclipse.swt.events.ModifyEvent;
43
import org.eclipse.swt.events.ModifyEvent;
Lines 64-70 Link Here
64
67
65
	private static final String F_FILE_EXTENSION = "xml"; //$NON-NLS-1$
68
	private static final String F_FILE_EXTENSION = "xml"; //$NON-NLS-1$
66
69
67
	private static final String F_COMPONENT_NAME = "component.xml"; //$NON-NLS-1$
70
	private static final String F_DEFAULT_COMPONENT_NAME = "component.xml"; //$NON-NLS-1$
71
72
	private static final String F_COMPONENT_NAME = "component"; //$NON-NLS-1$
73
74
	private static final String F_GENERATE_METHODS = "generate_methods"; //$NON-NLS-1$
68
75
69
	private Group fGroup;
76
	private Group fGroup;
70
77
Lines 75-82 Link Here
75
	private Hyperlink fDSImplementationClassHyperlink;
82
	private Hyperlink fDSImplementationClassHyperlink;
76
	private Button fDSImplementationClassButton;
83
	private Button fDSImplementationClassButton;
77
84
85
	private Button fGenerateMethodsButton;
86
78
	private IStructuredSelection fSelection;
87
	private IStructuredSelection fSelection;
79
88
89
	private IProject project;
90
80
	public DSFileWizardPage(IStructuredSelection selection) {
91
	public DSFileWizardPage(IStructuredSelection selection) {
81
		super(F_PAGE_NAME, selection);
92
		super(F_PAGE_NAME, selection);
82
		this.fSelection = selection;
93
		this.fSelection = selection;
Lines 86-107 Link Here
86
	/*
97
	/*
87
	 * (non-Javadoc)
98
	 * (non-Javadoc)
88
	 * 
99
	 * 
89
	 * @see org.eclipse.pde.internal.ui.wizards.cheatsheet.CheatSheetFileWizardPage#initialize()
100
	 * @see
101
	 * org.eclipse.pde.internal.ui.wizards.cheatsheet.CheatSheetFileWizardPage
102
	 * #initialize()
90
	 */
103
	 */
91
	protected void initialize() {
104
	protected void initialize() {
92
		setTitle(Messages.DSFileWizardPage_title);
105
		setTitle(Messages.DSFileWizardPage_title);
93
		setDescription(Messages.DSFileWizardPage_description);
106
		setDescription(Messages.DSFileWizardPage_description);
94
		// Force the file extension to be 'xml'
107
		// Force the file extension to be 'xml'
95
		setFileExtension(F_FILE_EXTENSION);
108
		setFileExtension(F_FILE_EXTENSION);
96
		setFileName(F_COMPONENT_NAME);
97
	}
109
	}
98
110
99
	private void setComponentName() {
111
	private void setComponentName() {
100
		Object element = fSelection.getFirstElement();
112
		if (fSelection != null) {
101
		if (element != null) {
113
			Object element = fSelection.getFirstElement();
102
			IProject project = getProject(element);
114
			if (element != null) {
103
			if (project != null)
115
				IProject project = getProject(element);
104
				setComponentNameText(project);
116
				if (project != null)
117
					setComponentNameText(project);
118
			}
105
		}
119
		}
106
	}
120
	}
107
121
Lines 115-120 Link Here
115
			project = ((ClassPathContainer) element).getJavaProject()
129
			project = ((ClassPathContainer) element).getJavaProject()
116
					.getProject();
130
					.getProject();
117
		}
131
		}
132
		this.project = project;
133
		return project;
134
	}
135
136
	public IProject getProject() {
118
		return project;
137
		return project;
119
	}
138
	}
120
139
Lines 134-139 Link Here
134
	}
153
	}
135
154
136
	protected void createAdvancedControls(Composite parent) {
155
	protected void createAdvancedControls(Composite parent) {
156
		IDialogSettings settings = getDialogSettings();
157
		if (settings != null) {
158
			String component = settings.get(F_COMPONENT_NAME);
159
			if (component != null && !component.equals("")) {
160
				setFileName(component);
161
			} else {
162
				setFileName(F_DEFAULT_COMPONENT_NAME);
163
			}
164
		} else {
165
			setFileName(F_DEFAULT_COMPONENT_NAME);
166
		}
137
167
138
		// Controls Group
168
		// Controls Group
139
		fGroup = new Group(parent, SWT.NONE);
169
		fGroup = new Group(parent, SWT.NONE);
Lines 153-162 Link Here
153
		fDSComponentNameText.setText(""); //$NON-NLS-1$
183
		fDSComponentNameText.setText(""); //$NON-NLS-1$
154
		fDSComponentNameText.addModifyListener(new ModifyListener() {
184
		fDSComponentNameText.addModifyListener(new ModifyListener() {
155
			public void modifyText(ModifyEvent e) {
185
			public void modifyText(ModifyEvent e) {
156
				setPageComplete(checkPageComplete());
186
				setPageComplete(isPageComplete());
157
			}
187
			}
158
		});
188
		});
159
		setComponentName();
160
189
161
		fDSImplementationClassHyperlink = new Hyperlink(fGroup, SWT.NONE);
190
		fDSImplementationClassHyperlink = new Hyperlink(fGroup, SWT.NONE);
162
		fDSImplementationClassHyperlink
191
		fDSImplementationClassHyperlink
Lines 236-242 Link Here
236
		fDSImplementationClassText.setText(""); //$NON-NLS-1$
265
		fDSImplementationClassText.setText(""); //$NON-NLS-1$
237
		fDSImplementationClassText.addModifyListener(new ModifyListener() {
266
		fDSImplementationClassText.addModifyListener(new ModifyListener() {
238
			public void modifyText(ModifyEvent e) {
267
			public void modifyText(ModifyEvent e) {
239
				setPageComplete(checkPageComplete());
268
				setPageComplete(isPageComplete());
240
			}
269
			}
241
		});
270
		});
242
271
Lines 277-282 Link Here
277
			}
306
			}
278
		});
307
		});
279
308
309
		fGenerateMethodsButton = new Button(fGroup, SWT.CHECK);
310
		fGenerateMethodsButton.setText(Messages.DSFileWizardPage_createMethods);
311
		fGenerateMethodsButton.setEnabled(false);
312
		fGenerateMethodsButton.setSelection(false);
313
314
		setComponentName();
315
		validatePage();
280
	}
316
	}
281
317
282
	public String getDSComponentNameValue() {
318
	public String getDSComponentNameValue() {
Lines 298-304 Link Here
298
	/*
334
	/*
299
	 * (non-Javadoc)
335
	 * (non-Javadoc)
300
	 * 
336
	 * 
301
	 * @see org.eclipse.ui.dialogs.WizardNewFileCreationPage#validateLinkedResource()
337
	 * @see
338
	 * org.eclipse.ui.dialogs.WizardNewFileCreationPage#validateLinkedResource()
302
	 */
339
	 */
303
	protected IStatus validateLinkedResource() {
340
	protected IStatus validateLinkedResource() {
304
		return new Status(IStatus.OK, Activator.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
341
		return new Status(IStatus.OK, Activator.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
Lines 309-315 Link Here
309
	}
346
	}
310
347
311
	public boolean isPageComplete() {
348
	public boolean isPageComplete() {
312
		return checkPageComplete();
349
		updateCreateMethodsButton();
350
		return checkPageComplete() & validatePage();
313
	}
351
	}
314
352
353
	public void saveSettings(IDialogSettings settings) {
354
		settings.put(F_COMPONENT_NAME, getFileName());
355
		if (fGenerateMethodsButton.isEnabled()) {
356
			settings.put(F_GENERATE_METHODS, fGenerateMethodsButton
357
					.getSelection());
358
		}
359
360
	}
361
362
	protected boolean validatePage() {
363
		if (fDSImplementationClassText != null) {
364
			IStatus status = PDEPlugin.getWorkspace().validateName(
365
					fDSImplementationClassText.getText(), IResource.FILE);
366
			if (!status.isOK()) {
367
				setErrorMessage(status.getMessage());
368
				return false;
369
			}
370
		}
371
		return super.validatePage();
372
	}
373
374
	private void updateCreateMethodsButton() {
375
		boolean wasDisabled = !fGenerateMethodsButton.isEnabled();
376
		try {
377
			IType type = JavaCore.create(this.getProject()).findType(
378
					this.getDSImplementationClassValue());
379
			if (type != null) {
380
				IMethod methodActivate = type.getMethod("activate", //$NON-NLS-1$
381
						new String[] { "QComponentContext;" }); //$NON-NLS-1$
382
				IMethod methodDeactivate = type.getMethod("deactivate", //$NON-NLS-1$
383
						new String[] { "QComponentContext;" }); //$NON-NLS-1$
384
				if (type.findMethods(methodActivate) == null
385
						&& type.findMethods(methodDeactivate) == null) {
386
					if (wasDisabled) {
387
						enableAndToggleGenerateButton();
388
					}
389
					return;
390
				}
391
			}
392
		} catch (Exception e) {
393
		}
394
		fGenerateMethodsButton.setEnabled(false);
395
		fGenerateMethodsButton.setSelection(false);
396
397
	}
398
399
	private void enableAndToggleGenerateButton() {
400
		fGenerateMethodsButton.setEnabled(true);
401
		IDialogSettings settings = getDialogSettings();
402
		if (settings != null) {
403
			if (settings.get(F_GENERATE_METHODS) != null) {
404
				fGenerateMethodsButton.setSelection(settings
405
						.getBoolean(F_GENERATE_METHODS));
406
407
			} else {
408
				fGenerateMethodsButton.setSelection(true);
409
			}
410
		} else {
411
			fGenerateMethodsButton.setSelection(true);
412
		}
413
	}
414
415
	public boolean isGenerateMethodsButtonSelected() {
416
		return fGenerateMethodsButton.getSelection();
417
	}
315
}
418
}
(-)src/org/eclipse/pde/internal/ds/ui/Messages.java (+7 lines)
Lines 40-47 Link Here
40
	public static String DSFileWizardPage_browse;
40
	public static String DSFileWizardPage_browse;
41
	public static String DSFileWizardPage_selectType;
41
	public static String DSFileWizardPage_selectType;
42
	public static String DSFileWizardPage_not_OSGI_folder;
42
	public static String DSFileWizardPage_not_OSGI_folder;
43
	public static String DSFileWizardPage_createMethods;
43
	
44
	
44
	public static String DSNewWizard_title;
45
	public static String DSNewWizard_title;
46
	public static String DSNewWizard_createOSGIFolder_title;
47
	public static String DSNewWizard_createOSGIFolder_message;
48
	public static String DSNewWizard_defaultOSGIFolder_title;
49
	public static String DSNewWizard_defaultOSGIFolder_message;
50
	public static String DSNewWizard_RememberMyDecision;
45
	
51
	
46
	public static String DSImplementationDetails_title;
52
	public static String DSImplementationDetails_title;
47
	public static String DSImplementationDetails_description;
53
	public static String DSImplementationDetails_description;
Lines 165-170 Link Here
165
	
171
	
166
	public static String DSService_title;
172
	public static String DSService_title;
167
173
174
168
	
175
	
169
	
176
	
170
177
(-)src/org/eclipse/pde/internal/ds/ui/messages.properties (+6 lines)
Lines 34-41 Link Here
34
DSFileWizardPage_browse=Browse...
34
DSFileWizardPage_browse=Browse...
35
DSFileWizardPage_selectType= Select Type
35
DSFileWizardPage_selectType= Select Type
36
DSFileWizardPage_not_OSGI_folder=By convention, component files should be located in OSGI-INF/ folder
36
DSFileWizardPage_not_OSGI_folder=By convention, component files should be located in OSGI-INF/ folder
37
DSFileWizardPage_createMethods=Generate &lifecycle methods
37
38
38
DSNewWizard_title=New Component Definition
39
DSNewWizard_title=New Component Definition
40
DSNewWizard_createOSGIFolder_title=Create OSGI-INF Folder
41
DSNewWizard_createOSGIFolder_message=Do you want to create the OSGI-INF folder? (recomended)
42
DSNewWizard_defaultOSGIFolder_title=OSGI-INF Folder
43
DSNewWizard_defaultOSGIFolder_message=Make OSGI-INF as default folder? (recomended)
44
DSNewWizard_RememberMyDecision=Remember my decision
39
45
40
DSImplementationDetails_title=Definition
46
DSImplementationDetails_title=Definition
41
DSImplementationDetails_description=Specify the service's implementation class:
47
DSImplementationDetails_description=Specify the service's implementation class:
(-)src/org/eclipse/pde/internal/ds/ui/parts/Messages.java (+52 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2001, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.ds.ui.parts;
12
13
import java.util.MissingResourceException;
14
import java.util.ResourceBundle;
15
16
/**
17
 * Messages.java
18
 */
19
public class Messages extends Object {
20
	//
21
	// Static Fields
22
	//
23
24
	private static final String BUNDLE_NAME = "org.eclipse.pde.internal.ds.ui.parts.messages"; //$NON-NLS-1$
25
	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(Messages.BUNDLE_NAME);
26
27
	//
28
	// Static Methods
29
	//
30
31
	public static String getString(String key) {
32
		if (key == null) throw new IllegalArgumentException();
33
		String value;
34
35
		try {
36
			value = Messages.RESOURCE_BUNDLE.getString(key);
37
			value = value.trim();
38
		} catch (MissingResourceException exception) {
39
			value = '!' + key + '!';
40
		}
41
42
		return value;
43
	}
44
45
	//
46
	// Constructors
47
	//
48
49
	private Messages() {
50
		super();
51
	}
52
}
(-)src/org/eclipse/pde/internal/ds/ui/parts/IHandyDialog.java (+55 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2001, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.ds.ui.parts;
12
13
import org.eclipse.jface.dialogs.IDialogConstants;
14
15
public interface IHandyDialog {
16
	// Values returned by open()
17
	public static final int CANCEL_ID = IDialogConstants.CANCEL_ID;
18
	public static final int HELP_ID = IDialogConstants.HELP_ID;
19
	public static final int NO_ID = IDialogConstants.NO_ID;
20
	public static final int OK_ID = IDialogConstants.OK_ID;
21
	public static final int YES_ID = IDialogConstants.YES_ID;
22
23
	/**
24
	 * @see org.eclipse.jface.window.Window.open()
25
	 */
26
	public int open();
27
28
	/**
29
	 * Set the context ID to be used when the user clicks the Help button.
30
	 * This ID should be defined somewhere in a <code>contexts.xml</code> file,
31
	 * which has been registered using the <code>org.eclipse.help.contexts</code>
32
	 * extension-point. Setting a context ID has the side effect of causing a
33
	 * Help button to be displayed.
34
	 *
35
	 * @param helpContextId  The help context ID.
36
	 */
37
	public void setHelpContextId(String helpContextId);
38
39
	/**
40
	 * Set the size of the dialog.  This is only a hint, since dialogs must
41
	 * respect the dialog font size.  The most you can expect is that the
42
	 * dialog will be no smaller than the width and height specified.
43
	 *
44
	 * @param width  The width of the dialog.
45
	 * @param height The height of the dialog.
46
	 */
47
	public void setSize(int width, int height);
48
49
	/**
50
	 * Set the title of the dialog.
51
	 *
52
	 * @param title  The title of the dialog.
53
	 */
54
	public void setTitle(String title);
55
}
(-)src/org/eclipse/pde/internal/ds/ui/parts/IHandyPromptDialog.java (+38 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2001, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.ds.ui.parts;
12
13
import org.eclipse.swt.graphics.Image;
14
15
public interface IHandyPromptDialog extends IHandyDialog {
16
	public int addCheckbox(String text, boolean selected);
17
	public int addRadioButton(String text, boolean selected);
18
	public void defaultButtonIsCancel();
19
	public void defaultButtonIsNo();
20
	public void defaultButtonIsYes();
21
	public int getSelectedRadioButtonId();
22
	public boolean isCheckboxSelected(int id);
23
	public void removeAllCheckboxes();
24
	public void removeAllRadioButtons();
25
	public void setCancelButtonText(String cancelButtonText);
26
	public void setCheckboxGroupText(String checkboxGroupText);
27
	public void setCheckboxSelected(int id, boolean selected);
28
	public void setHelpContextId(String helpContextId);
29
	public void setImage(Image image);
30
	public void setNoButtonText(String negativeButtonLabel);
31
	public void setRadioButtonGroupText(String radioButtonGroupText);
32
	public void setRadioButtonSelected(int id, boolean selected);
33
	public void setYesButtonText(String positiveButtonLabel);
34
	public void useErrorIcon();
35
	public void useInformationIcon();
36
	public void useQuestionIcon();
37
	public void useWarningIcon();
38
}
(-)src/org/eclipse/pde/internal/ds/ui/parts/UiUtility.java (+291 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2001, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.ds.ui.parts;
12
13
import java.net.URL;
14
import java.text.MessageFormat;
15
16
import org.eclipse.core.resources.IFile;
17
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.Status;
19
import org.eclipse.jface.dialogs.ErrorDialog;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.SWTError;
22
import org.eclipse.swt.dnd.Clipboard;
23
import org.eclipse.swt.dnd.TextTransfer;
24
import org.eclipse.swt.dnd.Transfer;
25
import org.eclipse.swt.graphics.Color;
26
import org.eclipse.swt.graphics.Device;
27
import org.eclipse.swt.widgets.Composite;
28
import org.eclipse.swt.widgets.Control;
29
import org.eclipse.swt.widgets.Display;
30
import org.eclipse.swt.widgets.MessageBox;
31
import org.eclipse.swt.widgets.Shell;
32
import org.eclipse.ui.IEditorInput;
33
import org.eclipse.ui.IViewPart;
34
import org.eclipse.ui.IWorkbench;
35
import org.eclipse.ui.IWorkbenchPage;
36
import org.eclipse.ui.IWorkbenchWindow;
37
import org.eclipse.ui.PartInitException;
38
import org.eclipse.ui.PlatformUI;
39
import org.eclipse.ui.browser.IWebBrowser;
40
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
41
import org.eclipse.ui.help.IWorkbenchHelpSystem;
42
import org.eclipse.ui.part.FileEditorInput;
43
44
/**
45
 * UiUtility.java
46
 */
47
public class UiUtility extends Object {
48
	//
49
	// Static Fields
50
	//
51
52
	// Externalized Strings
53
	private static final String FAILED_TO_OPEN_URL_KEY = "UiUtility.FailedToOpenUrl";  //$NON-NLS-1$
54
	private static final String ERROR_KEY = "UiUtility.Error";  //$NON-NLS-1$
55
	private static final String WEB_BROWSER_CAN_NOT_BE_FOUND = "UiUtility.WebBrowserCanNotBeFound";  //$NON-NLS-1$
56
57
	// Singleton
58
	private static final UiUtility INSTANCE = new UiUtility();
59
60
	//
61
	// Static Methods
62
	//
63
64
	public static UiUtility getInstance() {
65
		return UiUtility.INSTANCE;
66
	}
67
68
	//
69
	// Constructors
70
	//
71
72
	private UiUtility() {
73
		super();
74
	}
75
76
	//
77
	// Instance Methods
78
	//
79
80
	public void copyToClipboard(String text) {
81
		Display display = Display.getCurrent();
82
		Clipboard clipboard = new Clipboard(display);
83
		TextTransfer textTransfer = TextTransfer.getInstance();
84
85
		Object[] data = new Object[] {
86
			text
87
		};
88
89
		Transfer[] transfers = new Transfer[] {
90
			textTransfer
91
		};
92
93
		try {
94
			clipboard.setContents(data, transfers);
95
		} catch (SWTError error) {
96
			// Only when another application is accessing the system clipboard.
97
			// OK to ignore.
98
		} finally {
99
			clipboard.dispose();
100
		}
101
	}
102
103
	public void copyToClipboard(String[] items) {
104
		int size = items.length;
105
		StringBuffer buffer = new StringBuffer(size * 20);
106
		String lineSeparator = System.getProperty("line.separator");  //$NON-NLS-1$
107
108
		for (int i = 0; i < size; i++) {
109
			String item = items [ i ];
110
			buffer.append(item);
111
			buffer.append(lineSeparator);
112
		}
113
114
		String text = buffer.toString();
115
		copyToClipboard(text);
116
	}
117
118
	public IViewPart findView(String id) {
119
		IWorkbench workbench = PlatformUI.getWorkbench();
120
		IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
121
		IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
122
		IViewPart view = workbenchPage.findView(id);
123
		return view;
124
	}
125
126
	private IWorkbenchPage getActiveWorkbenchPage() {
127
		IWorkbenchWindow[] windows = getWorkbenchWindows();
128
		IWorkbenchPage page = null;
129
		int i = 0;
130
131
		while (page == null && i < windows.length) {
132
			IWorkbenchWindow window = windows [ i ];
133
			page = window.getActivePage();
134
			i++;
135
		}
136
137
		return page;
138
	}
139
140
	public IWorkbenchWindow getActiveWorkbenchWindow() {
141
		IWorkbench workbench = getWorkbench();
142
		IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
143
		return window;
144
	}
145
146
	public Color getBackgroundColor() {
147
		Color color = getSystemColor(SWT.COLOR_LIST_BACKGROUND);
148
		return color;
149
	}
150
151
	private Control getControlWithFocus(Composite parent) {
152
		Control result = null;
153
		Control[] children = parent.getChildren();
154
		int length = children.length;
155
		int index = 0;
156
157
		while (result == null && index < length) {
158
			result = children [ index ];
159
			result = getControlWithFocus(result);
160
			index++;
161
		}
162
163
		return result;
164
	}
165
166
	public Control getControlWithFocus(Control parent) {
167
		boolean focus = parent.isFocusControl();
168
		Control result = focus == true ? parent : null;
169
170
		if (result == null && parent instanceof Composite) { // $codepro.audit.disable disallowInstanceof
171
			Composite composite = (Composite) parent;
172
			result = getControlWithFocus(composite);
173
		}
174
175
		return result;
176
	}
177
178
	public IWorkbenchHelpSystem getHelpSystem() {
179
		boolean valid = PlatformUI.isWorkbenchRunning();
180
		if (valid == false)
181
			return null;  // Early return.
182
		IWorkbench workbench = PlatformUI.getWorkbench();
183
		IWorkbenchHelpSystem helpSystem = workbench.getHelpSystem();
184
		return helpSystem;
185
	}
186
187
	public Shell getShell() {
188
		Display display = Display.getCurrent();
189
		Shell shell = display.getActiveShell();
190
		return shell;
191
	}
192
193
	public Color getSystemColor(int id) {
194
		Device device = Display.getDefault();
195
		Color color = device.getSystemColor(id);
196
		return color;
197
	}
198
199
	public String getTextFromClipboard() {
200
		Display display = Display.getCurrent();
201
		Clipboard clipboard = new Clipboard(display);
202
		TextTransfer textTransfer = TextTransfer.getInstance();
203
		String value = (String) clipboard.getContents(textTransfer);
204
		clipboard.dispose();
205
		return value;
206
	}
207
208
	public IWorkbench getWorkbench() {
209
		return PlatformUI.getWorkbench();
210
	}
211
212
	public IWorkbenchPage getWorkbenchPage() {
213
		IWorkbenchWindow window = getActiveWorkbenchWindow();
214
		IWorkbenchPage page = window.getActivePage();
215
		return page;
216
	}
217
218
	public IWorkbenchWindow[] getWorkbenchWindows() {
219
		IWorkbench workbench = getWorkbench();
220
		IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
221
		return windows;
222
	}
223
224
	public boolean isOnUserInterfaceThread() {
225
		Object display = Display.getCurrent();
226
		boolean result = display != null;
227
		return result;
228
	}
229
230
	public void openBrowser(String address) {
231
		IWorkbench workbench = getWorkbench();
232
		IWorkbenchBrowserSupport support = workbench.getBrowserSupport();
233
234
		try {
235
			IWebBrowser browser = support.getExternalBrowser();
236
			URL url = new URL(address);
237
			browser.openURL(url);
238
		} catch (Exception exception) {
239
			String message = openUnableToOpenBrowserErrorMessageDialog(address);
240
			throw new RuntimeException(message, exception);
241
		}
242
	}
243
244
	public void openEditor(IFile file, String editorId) throws PartInitException {
245
		IWorkbenchPage page = getActiveWorkbenchPage();
246
		IEditorInput editorInput = new FileEditorInput(file);
247
		page.openEditor(editorInput, editorId);
248
	}
249
250
	public void openErrorDialog(String id, String message, String reason, Throwable throwable) {
251
		Shell shell = getShell();
252
		String title = Messages.getString(UiUtility.ERROR_KEY);
253
		IStatus status = new Status(IStatus.ERROR, id, IStatus.ERROR, reason, throwable);
254
		ErrorDialog.openError(shell, title, message, status, IStatus.ERROR);
255
	}
256
257
	public void openMessageDialog(String title, String message, int style) {
258
		Shell shell = getShell();
259
		MessageBox messageBox = new MessageBox(shell, style);
260
		messageBox.setText(title);
261
		messageBox.setMessage(message);
262
		messageBox.open();
263
	}
264
265
	private String openUnableToOpenBrowserErrorMessageDialog(String url) {
266
		String title = Messages.getString(UiUtility.ERROR_KEY);
267
		String error = Messages.getString(UiUtility.WEB_BROWSER_CAN_NOT_BE_FOUND);
268
		String separator = System.getProperty("line.separator");  //$NON-NLS-1$
269
		String pattern = Messages.getString(UiUtility.FAILED_TO_OPEN_URL_KEY);
270
271
		Object[] values = new Object[] {
272
			url
273
		};
274
275
		String consequence = MessageFormat.format(pattern, values);
276
		int length = error.length() + separator.length() + consequence.length();
277
		StringBuffer buffer = new StringBuffer(length);
278
		buffer.append(error);
279
		buffer.append(separator);
280
		buffer.append(consequence);
281
		String message = buffer.toString();
282
		int style = SWT.OK | SWT.APPLICATION_MODAL | SWT.ICON_ERROR;
283
		openMessageDialog(title, message, style);
284
		return message;
285
	}
286
287
	public void setHelp(Control control, String contextId) {
288
		IWorkbenchHelpSystem helpSystem = getHelpSystem();
289
		helpSystem.setHelp(control, contextId);
290
	}
291
}
(-)src/org/eclipse/pde/internal/ds/ui/parts/HandyDialog.java (+197 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2001, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.ds.ui.parts;
12
13
import org.eclipse.jface.dialogs.Dialog;
14
import org.eclipse.swt.graphics.Image;
15
import org.eclipse.swt.graphics.Point;
16
import org.eclipse.swt.widgets.Button;
17
import org.eclipse.swt.widgets.Composite;
18
import org.eclipse.swt.widgets.Control;
19
import org.eclipse.swt.widgets.Display;
20
import org.eclipse.swt.widgets.Shell;
21
import org.eclipse.ui.help.IWorkbenchHelpSystem;
22
23
abstract class HandyDialog extends Dialog implements IHandyDialog {
24
	//
25
	// Static Fields
26
	//
27
28
	// Externalized Strings
29
	protected static final String HELP_KEY = "Common.Help";  //$NON-NLS-1$
30
31
	//
32
	// Instance Fields
33
	//
34
35
	private String title;
36
	private String helpContextId;
37
	private Point size;
38
39
	//
40
	// Constructors
41
	//
42
43
	protected HandyDialog(Shell parentShell) {
44
		super(parentShell);
45
	}
46
47
	//
48
	// Instance Methods
49
	//
50
51
	/**
52
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
53
	 */
54
	protected void configureShell(Shell shell) {
55
		super.configureShell(shell);
56
		configureShellTitle(shell);
57
	}
58
59
	private void configureShellTitle(Shell shell) {
60
		String title = getTitle();
61
		if (title == null)
62
			return;  // Early return.
63
		shell.setText(title);
64
	}
65
66
	protected abstract void createArea(Composite composite);
67
68
	/**
69
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
70
	 */
71
	protected final Control createDialogArea(Composite parent) {
72
		Composite composite = (Composite) super.createDialogArea(parent);
73
		hookHelpToComposite(composite);
74
		createArea(composite);
75
		postCreateArea(composite);
76
		Dialog.applyDialogFont(composite);
77
		return composite;
78
	}
79
80
	protected final Button createHelpButtonForButtonBar(Composite parent) {
81
		boolean required = isHelpButtonRequired();
82
		if (required == false) return null;
83
		String label = Messages.getString(HandyDialog.HELP_KEY);
84
		Button button = createButton(parent, IHandyDialog.HELP_ID, label, false);
85
		return button;
86
	}
87
88
	private Display getDisplay() {
89
		Shell shell = getShell();
90
		Display display = shell != null ? shell.getDisplay() : Display.getDefault();
91
		return display;
92
	}
93
94
	private String getHelpContextId() {
95
		return helpContextId;
96
	}
97
98
	private IWorkbenchHelpSystem getHelpSystem() {
99
		UiUtility utility = UiUtility.getInstance();
100
		IWorkbenchHelpSystem helpSystem = utility.getHelpSystem();
101
		return helpSystem;
102
	}
103
104
	/**
105
	 * @see org.eclipse.jface.window.Window#getInitialSize()
106
	 */
107
	protected Point getInitialSize() {
108
		Point initialSize = super.getInitialSize();
109
		Point result = initialSize;
110
		Point size = getSize();
111
112
		if (size != null) {
113
			if (size.x >= initialSize.x) {
114
				result.x = size.x;
115
			}
116
117
			if (size.y >= initialSize.y) {
118
				result.y = size.y;
119
			}
120
		}
121
122
		return result;
123
	}
124
125
	private Point getSize() {
126
		return size;
127
	}
128
129
	protected final Image getSystemImage(int id) {
130
		Display display = getDisplay();
131
		Image image = display.getSystemImage(id);
132
		return image;
133
	}
134
135
	private String getTitle() {
136
		return title;
137
	}
138
139
	protected final void helpPressed() {
140
		String id = getHelpContextId();
141
		IWorkbenchHelpSystem helpSystem = getHelpSystem();
142
		if (helpSystem == null)
143
			return;  // Early return.
144
		helpSystem.displayHelp(id);
145
	}
146
147
	private void hookHelpToComposite(Composite parent) {
148
		String id = getHelpContextId();
149
		if (id == null)
150
			return;  // Early return.
151
		IWorkbenchHelpSystem helpSystem = getHelpSystem();
152
		if (helpSystem == null)
153
			return;  // Early return.
154
		helpSystem.setHelp(parent, id);
155
	}
156
157
	protected final boolean isHelpButtonRequired() {
158
		IWorkbenchHelpSystem helpSystem = getHelpSystem();
159
		boolean valid = helpSystem != null;
160
		if (valid == false)
161
			return false;  // Early return.
162
		String id = getHelpContextId();
163
		if (id == null)
164
			return false;  // Early return.
165
		return true;
166
	}
167
168
	protected void postCreateArea(Composite composite) {
169
		// No-op
170
	}
171
172
	/**
173
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#setHelpContextId(java.lang.String)
174
	 */
175
	public void setHelpContextId(String helpContextId) {
176
		this.helpContextId = helpContextId;
177
	}
178
179
	/**
180
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyDialog#setSize(int, int)
181
	 */
182
	public void setSize(int width, int height) {
183
		Point point = new Point(width, height);
184
		setSize(point);
185
	}
186
187
	private void setSize(Point size) {
188
		this.size = size;
189
	}
190
191
	/**
192
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#setTitle(java.lang.String)
193
	 */
194
	public void setTitle(String title) {
195
		this.title = title;
196
	}
197
}
(-)src/org/eclipse/pde/internal/ds/ui/parts/messages.properties (+20 lines)
Added Link Here
1
# Common
2
Common.Help=&Help
3
4
# HandyPromptDialog.java
5
HandyPromptDialog.Prompt=Prompt
6
HandyPromptDialog.UnknownButtonId=Unknown button id {0}
7
8
# UiUtility.java
9
UiUtility.Error=Error
10
UiUtility.FailedToOpenUrl=Failed to open {0}
11
UiUtility.WebBrowserCanNotBeFound=A web browser could not be found!	
12
13
# Copyright (c) 2001, 2007 IBM Corporation and others.
14
# All rights reserved. This program and the accompanying materials
15
# are made available under the terms of the Eclipse Public License v1.0
16
# which accompanies this distribution, and is available at
17
# http://www.eclipse.org/legal/epl-v10.html
18
#
19
# Contributors:
20
#     IBM Corporation - initial API and implementation
(-)src/org/eclipse/pde/internal/ds/ui/parts/HandyPromptDialog.java (+627 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2001, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.ds.ui.parts;
12
13
import java.text.MessageFormat;
14
import java.util.ArrayList;
15
import java.util.Iterator;
16
import java.util.List;
17
18
import org.eclipse.jface.dialogs.IDialogConstants;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.graphics.Image;
21
import org.eclipse.swt.layout.GridData;
22
import org.eclipse.swt.layout.GridLayout;
23
import org.eclipse.swt.layout.RowLayout;
24
import org.eclipse.swt.widgets.Button;
25
import org.eclipse.swt.widgets.Composite;
26
import org.eclipse.swt.widgets.Group;
27
import org.eclipse.swt.widgets.Label;
28
import org.eclipse.swt.widgets.Shell;
29
30
/**
31
 * A handy Yes/No/Cancel dialog.
32
 */
33
public class HandyPromptDialog extends HandyDialog implements IHandyPromptDialog {
34
	//
35
	// Nested Classes
36
	//
37
38
	private static class ButtonDetails extends Object {
39
		private Button button;
40
		private boolean selected;
41
		private String text;
42
	}
43
44
	// Static Fields
45
	//
46
47
	// Externalized Strings
48
	private static final String PROMPT_KEY = "HandyPromptDialog.Prompt";  //$NON-NLS-1$
49
	private static final String UNKNOWN_BUTTON_ID_KEY = "HandyPromptDialog.UnknownButtonId";  //$NON-NLS-1$
50
51
	// Misc
52
	private static final int NO_DEFAULT_BUTTON_ID = -1;
53
54
	//
55
	// Instance Fields
56
	//
57
58
	private String cancelButtonText;
59
	private List/*<ButtonDetails>*/ checkboxButtons;
60
	private String checkboxGroupText;
61
	private int defaultButtonId;
62
	private Image image;
63
	private String noButtonText;
64
	private String prompt;
65
	private List/*<ButtonDetails>*/ radioButtons;
66
	private String radioButtonGroupText;
67
	private String yesButtonText;
68
69
	//
70
	// Constructors
71
	//
72
73
	public HandyPromptDialog(Shell parentShell, String prompt) {
74
		super(parentShell);
75
		setTitle(Messages.getString(HandyPromptDialog.PROMPT_KEY));
76
		setPrompt(prompt);
77
		initializeCheckboxButtons();
78
		initializeRadioButtons();
79
		initializePushButtons();
80
		setDefaultButtonId(HandyPromptDialog.NO_DEFAULT_BUTTON_ID);
81
	}
82
83
	//
84
	// Instance Methods
85
	//
86
87
	private int addButton(List/*<ButtonDetails>*/ list, String text, boolean selected) {
88
		synchronized (list) {
89
			int size = list.size();
90
			ButtonDetails details = new ButtonDetails();
91
			details.text = text;
92
			details.selected = selected;
93
			list.add(details);
94
			return size;
95
		}
96
	}
97
98
	/**
99
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#addCheckbox(java.lang.String, boolean)
100
	 */
101
	public int addCheckbox(String text, boolean selected) {
102
		List/*<ButtonDetails>*/ list = getCheckboxButtons();
103
		int id = addButton(list, text, selected);
104
		return id;
105
	}
106
107
	/**
108
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#addRadioButton(java.lang.String, boolean)
109
	 */
110
	public int addRadioButton(String text, boolean selected) {
111
		List/*<ButtonDetails>*/ list = getRadioButtons();
112
		int id = addButton(list, text, selected);
113
		return id;
114
	}
115
116
	/**
117
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
118
	 */
119
	protected void buttonPressed(int buttonId) {
120
		if (IHandyDialog.HELP_ID == buttonId) {
121
			helpPressed();
122
		} else {
123
			try {
124
				updateButtonSelections();
125
				setReturnCode(buttonId);
126
			} finally {
127
				close();
128
			}
129
		}
130
	}
131
132
	private void checkButtonId(List/*<ButtonDetails>*/ list, int id) {
133
		int size = list.size();
134
		if (id < size)
135
			return;  // Early return.
136
137
		String pattern = Messages.getString(HandyPromptDialog.UNKNOWN_BUTTON_ID_KEY);
138
		Object[] values = new Object[] {
139
			new Integer(id)
140
		};
141
		String message = MessageFormat.format(pattern, values);
142
		throw new IllegalArgumentException(message);
143
	}
144
145
	/**
146
	 * @see org.eclipse.soda.sat.plugin.ui.internal.HandyDialog#createArea(org.eclipse.swt.widgets.Composite)
147
	 */
148
	protected void createArea(Composite composite) {
149
		createPromptControls(composite);
150
	}
151
152
	private Button createButton(Composite parent, int id, String label) {
153
		int defaultButtonId = getDefaultButtonId();
154
		boolean defaultButton = defaultButtonId == id;
155
		Button button = createButton(parent, id, label, defaultButton);
156
		return button;
157
	}
158
159
	/**
160
	 * @see org.eclipse.jface.dialogs.Dialog#createButton(org.eclipse.swt.widgets.Composite, int, java.lang.String, boolean)
161
	 */
162
	protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
163
		Button button = super.createButton(parent, id, label, defaultButton);
164
165
		// Force the default button to take focus.  This fixes an apparent
166
		// bug in SWT where the default button does not always take focus.
167
		// Strangely enough, the default button seems to take focus when the
168
		// HandyPromptDialog has at least one radio button or checkbox.
169
170
		if (defaultButton == true) {
171
			button.setFocus();
172
		}
173
174
		return button;
175
	}
176
177
	private void createButtons(List/* <ButtonDetails> */list,
178
			Composite parent, boolean grabExcessSpace, String groupText,
179
			int style) {
180
		synchronized (list) {
181
			boolean empty = list.isEmpty();
182
			if (empty == true)
183
				return;  // Early return.
184
185
			Composite composite;
186
			RowLayout layout = new RowLayout(SWT.VERTICAL);
187
188
			if (groupText == null) {
189
				composite = new Composite(parent, SWT.NONE);
190
				layout.marginLeft = 0;
191
				layout.marginRight = 0;
192
				layout.marginTop = 0;
193
				layout.marginBottom = 0;
194
			} else {
195
				Group group = new Group(parent, SWT.NONE);
196
				group.setText(groupText);
197
				composite = group;
198
			}
199
200
			composite.setLayout(layout);
201
202
			int alignment = grabExcessSpace == true ? SWT.FILL : SWT.BEGINNING;
203
			Object data = new GridData(alignment, alignment, grabExcessSpace, grabExcessSpace);
204
			composite.setLayoutData(data);
205
206
			Iterator/*<ButtonDetails>*/ iterator = list.iterator();
207
			while (iterator.hasNext() == true) {
208
				ButtonDetails details = (ButtonDetails) iterator.next();
209
				Button button = new Button(composite, style);
210
				button.setText(details.text);
211
				button.setSelection(details.selected);
212
				details.button = button;
213
			}
214
		}
215
	}
216
217
	/**
218
	 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
219
	 */
220
	protected void createButtonsForButtonBar(Composite parent) {
221
		createYesButtonForButtonBar(parent);
222
		createNoButtonForButtonBar(parent);
223
		createCancelButtonForButtonBar(parent);
224
		createHelpButtonForButtonBar(parent);
225
	}
226
227
	private Button createCancelButtonForButtonBar(Composite parent) {
228
		String label = getCancelButtonText();
229
		if (label == null)
230
			return null;  // Early return.
231
		int id = IHandyDialog.CANCEL_ID;
232
		Button button = createButton(parent, id, label);
233
		return button;
234
	}
235
236
	private Button createNoButtonForButtonBar(Composite parent) {
237
		String label = getNoButtonText();
238
		if (label == null)
239
			return null;  // Early return.
240
		int id = IHandyDialog.NO_ID;
241
		Button button = createButton(parent, id, label);
242
		return button;
243
	}
244
245
	private void createPromptButtonControls(Composite parent) {
246
		int columns = 0;
247
248
		List/*<ButtonDetails>*/ checkboxButtons = getCheckboxButtons();
249
		boolean hasCheckboxButtons = checkboxButtons.isEmpty() == false;
250
251
		if (hasCheckboxButtons == true) {
252
			columns++;
253
		}
254
255
		List/*<ButtonDetails>*/ radioButtons = getRadioButtons();
256
		boolean hasRadioButtons = radioButtons.isEmpty() == false;
257
258
		if (hasRadioButtons == true) {
259
			columns++;
260
		}
261
262
		if (columns == 0)
263
			return;  // Early return.
264
265
		Composite composite = createPromptButtonControlsComposite(parent, columns);
266
267
		if (hasCheckboxButtons == true) {
268
			String groupText = getCheckboxGroupText();
269
			createButtons(checkboxButtons, composite, hasRadioButtons, groupText, SWT.CHECK);
270
		}
271
272
		if (hasRadioButtons == true) {
273
			String groupText = getRadioButtonGroupText();
274
			createButtons(radioButtons, composite, hasCheckboxButtons, groupText, SWT.RADIO);
275
		}
276
	}
277
278
	private Composite createPromptButtonControlsComposite(Composite parent, int columns) {
279
		GridLayout layout = new GridLayout(columns, false);
280
		layout.marginWidth = 0;
281
		layout.marginHeight = 0;
282
		layout.marginTop = 20;
283
		layout.marginHeight = 0;
284
285
		if (columns > 1) {
286
			layout.horizontalSpacing = 10;
287
		}
288
289
		Composite composite = new Composite(parent, SWT.NONE);
290
		composite.setLayout(layout);
291
292
		Object data = new GridData(SWT.FILL, SWT.FILL, true, false);
293
		composite.setLayoutData(data);
294
295
		return composite;
296
	}
297
298
	private void createPromptControls(Composite parent) {
299
		createPromptLabelControls(parent);
300
		createPromptButtonControls(parent);
301
	}
302
303
	private void createPromptLabelControls(Composite parent) {
304
		GridData data;
305
306
		Composite composite = new Composite(parent, SWT.NONE);
307
		GridLayout layout = new GridLayout(2, false);
308
		layout.marginWidth = 0;
309
		layout.marginHeight = 0;
310
		composite.setLayout(layout);
311
		data = new GridData(SWT.FILL, SWT.FILL, true, true);
312
		composite.setLayoutData(data);
313
314
		Label icon = new Label(composite, SWT.LEFT | SWT.NONE);
315
		Image image = getImage();
316
		icon.setImage(image);
317
318
		Label label = new Label(composite, SWT.WRAP);
319
		String prompt = getPrompt();
320
		label.setText(prompt);
321
		data = new GridData(SWT.FILL, SWT.CENTER, true, true);
322
		data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
323
		label.setLayoutData(data);
324
	}
325
326
	private Button createYesButtonForButtonBar(Composite parent) {
327
		String label = getYesButtonText();
328
		if (label == null)
329
			return null;  // Early return.
330
		int id = IHandyDialog.YES_ID;
331
		Button button = createButton(parent, id, label);
332
		return button;
333
	}
334
335
	/**
336
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#defaultButtonIsCancel()
337
	 */
338
	public void defaultButtonIsCancel() {
339
		setDefaultButtonId(IHandyDialog.CANCEL_ID);
340
	}
341
342
	/**
343
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#defaultButtonIsNo()
344
	 */
345
	public void defaultButtonIsNo() {
346
		setDefaultButtonId(IHandyDialog.NO_ID);
347
	}
348
349
	/**
350
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#defaultButtonIsYes()
351
	 */
352
	public void defaultButtonIsYes() {
353
		setDefaultButtonId(IHandyDialog.YES_ID);
354
	}
355
356
	private ButtonDetails getButtonDetails(List/*<ButtonDetails>*/ list, int id) {
357
		checkButtonId(list, id);
358
		ButtonDetails details = (ButtonDetails) list.get(id);
359
		return details;
360
	}
361
362
	private String getCancelButtonText() {
363
		return cancelButtonText;
364
	}
365
366
	private ButtonDetails getCheckboxButtonDetails(int id) {
367
		List/*<ButtonDetails>*/ list = getCheckboxButtons();
368
		ButtonDetails details = getButtonDetails(list, id);
369
		return details;
370
	}
371
372
	private List/*<ButtonDetails>*/ getCheckboxButtons() {
373
		return checkboxButtons;
374
	}
375
376
	private String getCheckboxGroupText() {
377
		return checkboxGroupText;
378
	}
379
380
	private int getDefaultButtonId() {
381
		synchronized (this) {
382
			if (defaultButtonId == -1) {
383
				int id = guessDefaultButtonId();
384
				setDefaultButtonId(id);
385
			}
386
		}
387
388
		return defaultButtonId;
389
	}
390
391
	private Image getImage() {
392
		synchronized (this) {
393
			if (image == null) {
394
				useInformationIcon();
395
			}
396
		}
397
		return image;
398
	}
399
400
	private String getNoButtonText() {
401
		return noButtonText;
402
	}
403
404
	private String getPrompt() {
405
		return prompt;
406
	}
407
408
	private ButtonDetails getRadioButtonDetails(int id) {
409
		List/*<ButtonDetails>*/ list = getRadioButtons();
410
		ButtonDetails details = getButtonDetails(list, id);
411
		return details;
412
	}
413
414
	private String getRadioButtonGroupText() {
415
		return radioButtonGroupText;
416
	}
417
418
	private List/*<ButtonDetails>*/ getRadioButtons() {
419
		return radioButtons;
420
	}
421
422
	/**
423
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#getSelectedRadioButtonId()
424
	 */
425
	public int getSelectedRadioButtonId() {
426
		List/*<ButtonDetails>*/ list = getRadioButtons();
427
		int size = list.size();
428
429
		for (int id = 0; id < size; id++) {
430
			ButtonDetails details = (ButtonDetails) list.get(id);
431
			if (details.selected == true)
432
				return id;  // Early return.
433
		}
434
435
		return -1;
436
	}
437
438
	private String getYesButtonText() {
439
		return yesButtonText;
440
	}
441
442
	private int guessDefaultButtonId() {
443
		Object text;
444
445
		text = getCancelButtonText();
446
		if (text != null) return IHandyDialog.CANCEL_ID;
447
448
		text = getNoButtonText();
449
		if (text != null) return IHandyDialog.NO_ID;
450
451
		text = getYesButtonText();
452
		if (text != null) return IHandyDialog.YES_ID;
453
454
		boolean helpButtonRequired = isHelpButtonRequired();
455
		if (helpButtonRequired == true) return IHandyDialog.HELP_ID;
456
457
		return HandyPromptDialog.NO_DEFAULT_BUTTON_ID;
458
	}
459
460
	private void initializeCheckboxButtons() {
461
		setCheckboxButtons(new ArrayList/*<ButtonDetails>*/(3));
462
	}
463
464
	private void initializePushButtons() {
465
		setYesButtonText(IDialogConstants.YES_LABEL);
466
		setNoButtonText(IDialogConstants.NO_LABEL);
467
		setCancelButtonText('&' + IDialogConstants.CANCEL_LABEL);
468
	}
469
470
	private void initializeRadioButtons() {
471
		setRadioButtons(new ArrayList/*<ButtonDetails>*/(3));
472
	}
473
474
	/**
475
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#isCheckboxSelected(int)
476
	 */
477
	public boolean isCheckboxSelected(int id) {
478
		ButtonDetails details = getCheckboxButtonDetails(id);
479
		return details.selected;
480
	}
481
482
	/**
483
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#removeAllCheckboxes()
484
	 */
485
	public void removeAllCheckboxes() {
486
		initializeCheckboxButtons();
487
	}
488
489
	/**
490
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#removeAllRadioButtons()
491
	 */
492
	public void removeAllRadioButtons() {
493
		initializeRadioButtons();
494
	}
495
496
	/**
497
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#setCancelButtonText(java.lang.String)
498
	 */
499
	public void setCancelButtonText(String cancelButtonText) {
500
		this.cancelButtonText = cancelButtonText;
501
	}
502
503
	private void setCheckboxButtons(List/*<ButtonDetails>*/ checkboxButtons) {
504
		this.checkboxButtons = checkboxButtons;
505
	}
506
507
	/**
508
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#setCheckboxGroupText(java.lang.String)
509
	 */
510
	public void setCheckboxGroupText(String checkboxGroupText) {
511
		this.checkboxGroupText = checkboxGroupText;
512
	}
513
514
	/**
515
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#setCheckboxSelected(int, boolean)
516
	 */
517
	public void setCheckboxSelected(int id, boolean selected) {
518
		ButtonDetails details = getCheckboxButtonDetails(id);
519
		details.selected = selected;
520
	}
521
522
	private void setDefaultButtonId(int defaultButtonId) {
523
		this.defaultButtonId = defaultButtonId;
524
	}
525
526
	/**
527
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#setImage(org.eclipse.swt.graphics.Image)
528
	 */
529
	public void setImage(Image image) {
530
		this.image = image;
531
	}
532
533
	private void setImage(int id) {
534
		Image image = getSystemImage(id);
535
		setImage(image);
536
	}
537
538
	/**
539
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#setNoButtonText(java.lang.String)
540
	 */
541
	public void setNoButtonText(String noButtonText) {
542
		this.noButtonText = noButtonText;
543
	}
544
545
	private void setPrompt(String prompt) {
546
		this.prompt = prompt;
547
	}
548
549
	/**
550
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#setRadioButtonGroupText(java.lang.String)
551
	 */
552
	public void setRadioButtonGroupText(String radioButtonGroupText) {
553
		this.radioButtonGroupText = radioButtonGroupText;
554
	}
555
556
	private void setRadioButtons(List/*<ButtonDetails>*/ radioButtons) {
557
		this.radioButtons = radioButtons;
558
	}
559
560
	/**
561
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#setRadioButtonSelected(int, boolean)
562
	 */
563
	public void setRadioButtonSelected(int id, boolean selected) {
564
		ButtonDetails details = getRadioButtonDetails(id);
565
		details.selected = selected;
566
	}
567
568
	/**
569
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#setYesButtonText(java.lang.String)
570
	 */
571
	public void setYesButtonText(String yesButtonText) {
572
		this.yesButtonText = yesButtonText;
573
	}
574
575
	private void updateButtonSelections() {
576
		updateCheckboxButtonSelections();
577
		updateRadioButtonSelections();
578
	}
579
580
	private void updateButtonSelections(List/*<ButtonDetails>*/ list) {
581
		synchronized (list) {
582
			Iterator/*<ButtonDetails>*/ iterator = list.iterator();
583
			while (iterator.hasNext() == true) {
584
				ButtonDetails details = (ButtonDetails) iterator.next();
585
				details.selected = details.button.getSelection();
586
			}
587
		}
588
	}
589
590
	private void updateCheckboxButtonSelections() {
591
		List/*<ButtonDetails>*/ list = getCheckboxButtons();
592
		updateButtonSelections(list);
593
	}
594
595
	private void updateRadioButtonSelections() {
596
		List/*<ButtonDetails>*/ list = getRadioButtons();
597
		updateButtonSelections(list);
598
	}
599
600
	/**
601
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#useErrorIcon()
602
	 */
603
	public void useErrorIcon() {
604
		setImage(SWT.ICON_ERROR);
605
	}
606
607
	/**
608
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#useInformationIcon()
609
	 */
610
	public void useInformationIcon() {
611
		setImage(SWT.ICON_INFORMATION);
612
	}
613
614
	/**
615
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#useQuestionIcon()
616
	 */
617
	public void useQuestionIcon() {
618
		setImage(SWT.ICON_QUESTION);
619
	}
620
621
	/**
622
	 * @see org.eclipse.soda.sat.plugin.ui.util.IHandyPromptDialog#useWarningIcon()
623
	 */
624
	public void useWarningIcon() {
625
		setImage(SWT.ICON_WARNING);
626
	}
627
}

Return to bug 256731