Skip to main content
summaryrefslogtreecommitdiffstats
blob: ed3268fe5bcb7a9af83bb5d0d5cc34521beda68c (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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*******************************************************************************
 * Copyright (c) 2006, 2012 Soyatec(http://www.soyatec.com) 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:
 *     Soyatec - initial API and implementation
 *     IBM Corporation - ongoing enhancements
 *     Sopot Cela - ongoing enhancements
 *     Lars Vogel - ongoing enhancements
 *******************************************************************************/
package org.eclipse.e4.internal.tools.wizards.project;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.e4.ui.model.application.MAddon;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.MApplicationFactory;
import org.eclipse.e4.ui.model.application.commands.MBindingContext;
import org.eclipse.e4.ui.model.application.commands.MBindingTable;
import org.eclipse.e4.ui.model.application.commands.MCommand;
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
import org.eclipse.e4.ui.model.application.commands.MHandler;
import org.eclipse.e4.ui.model.application.commands.MKeyBinding;
import org.eclipse.e4.ui.model.application.ui.advanced.MAdvancedFactory;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspectiveStack;
import org.eclipse.e4.ui.model.application.ui.basic.MBasicFactory;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainer;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar;
import org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow;
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
import org.eclipse.e4.ui.model.application.ui.menu.MHandledToolItem;
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.pde.core.build.IBuildEntry;
import org.eclipse.pde.core.plugin.IPluginBase;
import org.eclipse.pde.core.plugin.IPluginElement;
import org.eclipse.pde.core.plugin.IPluginExtension;
import org.eclipse.pde.core.plugin.IPluginImport;
import org.eclipse.pde.internal.core.ICoreConstants;
import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
import org.eclipse.pde.internal.core.bundle.WorkspaceBundlePluginModel;
import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase;
import org.eclipse.pde.internal.core.project.PDEProject;
import org.eclipse.pde.internal.core.util.CoreUtility;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.eclipse.pde.internal.ui.wizards.IProjectProvider;
import org.eclipse.pde.internal.ui.wizards.plugin.NewPluginProjectWizard;
import org.eclipse.pde.internal.ui.wizards.plugin.NewProjectCreationOperation;
import org.eclipse.pde.internal.ui.wizards.plugin.PluginFieldData;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.osgi.framework.Bundle;
import org.osgi.framework.Version;

/**
 * @author jin.liu (jin.liu@soyatec.com)
 */
public class E4NewProjectWizard extends NewPluginProjectWizard {

	private static final String MODEL_EDITOR_ID = "org.eclipse.e4.tools.emf.editor3x.e4wbm";
	private static final String APPLICATION_MODEL = "Application.e4xmi";
	private PluginFieldData fPluginData;
	private NewApplicationWizardPage fApplicationPage;
	private IProjectProvider fProjectProvider;
	private PluginContentPage fContentPage;

	public E4NewProjectWizard() {
		fPluginData = new PluginFieldData();
	}

	public void addPages() {
		fMainPage = new E4NewProjectWizardPage(
				"main", fPluginData, false, getSelection()); //$NON-NLS-1$
		fMainPage.setTitle(PDEUIMessages.NewProjectWizard_MainPage_title);
		fMainPage.setDescription(PDEUIMessages.NewProjectWizard_MainPage_desc);
		String pname = getDefaultValue(DEF_PROJECT_NAME);
		if (pname != null)
			fMainPage.setInitialProjectName(pname);
		addPage(fMainPage);

		fProjectProvider = new IProjectProvider() {
			public String getProjectName() {
				return fMainPage.getProjectName();
			}

			public IProject getProject() {
				return fMainPage.getProjectHandle();
			}

			public IPath getLocationPath() {
				return fMainPage.getLocationPath();
			}
		};

		fContentPage = new PluginContentPage(
				"page2", fProjectProvider, fMainPage, fPluginData); //$NON-NLS-1$

		fApplicationPage = new NewApplicationWizardPage(fProjectProvider, fPluginData);

		addPage(fContentPage);
		addPage(fApplicationPage);
	}

	@SuppressWarnings("restriction")
	public boolean performFinish() {
		try {
			fMainPage.updateData();
			fContentPage.updateData();
			IDialogSettings settings = getDialogSettings();
			if (settings != null) {
				fMainPage.saveSettings(settings);
				fContentPage.saveSettings(settings);
			}
			getContainer().run(
					false,
					true,
					new NewProjectCreationOperation(fPluginData,
							fProjectProvider, null) {
						private WorkspacePluginModelBase model;

						@Override
						protected void adjustManifests(
								IProgressMonitor monitor, IProject project,
								IPluginBase bundle) throws CoreException {
							super.adjustManifests(monitor, project, bundle);
							IPluginBase pluginBase = model.getPluginBase();
							String[] dependencyId = new String[] {
									"javax.inject",
									"org.eclipse.core.runtime",
									"org.eclipse.swt",
									"org.eclipse.core.databinding",
									"org.eclipse.core.databinding.beans",
									"org.eclipse.jface",
									"org.eclipse.jface.databinding",
									"org.eclipse.e4.ui.services",
									"org.eclipse.e4.ui.workbench",
									"org.eclipse.e4.core.services",
									"org.eclipse.e4.core.di",
									"org.eclipse.e4.ui.di",
									"org.eclipse.e4.core.contexts",
									"org.eclipse.e4.ui.workbench.swt",
									"org.eclipse.core.databinding.property",
									"org.eclipse.e4.ui.css.core",
									"org.w3c.css.sac",
									"org.eclipse.e4.core.commands",
									"org.eclipse.e4.ui.bindings" };
							for (String id : dependencyId) {
								Bundle dependency = Platform.getBundle(id);

								IPluginImport iimport = model
										.getPluginFactory().createImport();
								iimport.setId(id);
								if(dependency != null) {
    								Version version = dependency.getVersion();
    								String versionString = version.getMajor() + "."
    										+ version.getMinor() + "."
    										+ version.getMicro();
    								iimport.setVersion(versionString);
								}
								pluginBase.add(iimport);
							}
						}

						@Override
						protected void setPluginLibraries(
								WorkspacePluginModelBase model)
								throws CoreException {
							this.model = model;
							super.setPluginLibraries(model);
						}
					});

			IWorkingSet[] workingSets = fMainPage.getSelectedWorkingSets();
			if (workingSets.length > 0)
				getWorkbench().getWorkingSetManager().addToWorkingSets(
						fProjectProvider.getProject(), workingSets);

			this.createProductsExtension(fProjectProvider.getProject());

			this.createApplicationResources(fProjectProvider.getProject(),
					new NullProgressMonitor());
			// Add the product sources
			adjustBuildPropertiesFile(fProjectProvider.getProject());
			
			// Open the model editor
			openEditorForApplicationModel();
			
			return true;
		} catch (InvocationTargetException e) {
			PDEPlugin.logException(e);
		} catch (InterruptedException e) {
		} catch (CoreException e) {
			PDEPlugin.logException(e);
		}
		return false;
	}

	private void openEditorForApplicationModel() throws PartInitException {
		IFile file = fProjectProvider.getProject().getFile(
				APPLICATION_MODEL);
		if (file != null) {
			FileEditorInput input = new FileEditorInput(file);
			IWorkbenchWindow window = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow();
			IWorkbenchPage page = window.getActivePage();
			page.openEditor(input, MODEL_EDITOR_ID);
		}
	}
	
	private void adjustBuildPropertiesFile(IProject project) throws CoreException {
		IFile file = PDEProject.getBuildProperties(project);
		if (file.exists()) {
			WorkspaceBuildModel model = new WorkspaceBuildModel(file);
			IBuildEntry e = model.getBuild().getEntry(IBuildEntry.BIN_INCLUDES);
			
			e.addToken("plugin.xml");
			
			Map<String, String> map = fApplicationPage.getData();
			String cssEntry = map.get(NewApplicationWizardPage.APPLICATION_CSS_PROPERTY);
			if( cssEntry != null ) {
				e.addToken(cssEntry);	
			}
			
			model.save();
		}
	}

	/**
	 * create products extension detail
	 * 
	 * @param project
	 */
	@SuppressWarnings("restriction")
	public void createProductsExtension(IProject project) {
		Map<String, String> map = fApplicationPage.getData();
		if (map == null
				|| map.get(NewApplicationWizardPage.PRODUCT_NAME) == null)
			return;

		WorkspacePluginModelBase fmodel = new WorkspaceBundlePluginModel(
				project.getFile(ICoreConstants.BUNDLE_FILENAME_DESCRIPTOR),
				project.getFile(ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR));
		IPluginExtension extension = fmodel.getFactory().createExtension();
		try {
			String productName = map.get(NewApplicationWizardPage.PRODUCT_NAME);
			String applicationName = map
					.get(NewApplicationWizardPage.APPLICATION);

			String cssValue = map
					.get(NewApplicationWizardPage.APPLICATION_CSS_PROPERTY);
			if (cssValue != null) {
				cssValue = "platform:/plugin/" + productName + "/" + cssValue;
				map.put(NewApplicationWizardPage.APPLICATION_CSS_PROPERTY,
						cssValue);
			}

			extension.setPoint("org.eclipse.core.runtime.products");
			extension.setId("product");
			IPluginElement productElement = fmodel.getFactory().createElement(
					extension);

			productElement.setName("product");
			if (applicationName != null) {
				productElement.setAttribute("application", applicationName);
			} else {
				productElement.setAttribute("application",
						NewApplicationWizardPage.E4_APPLICATION);
			}
			productElement.setAttribute("name", productName);

			Set<Entry<String, String>> set = map.entrySet();
			if (set != null) {
				Iterator<Entry<String, String>> it = set.iterator();
				if (it != null) {
					while (it.hasNext()) {
						Entry<String, String> entry = it.next();
						String value = entry.getValue();
						if (value == null || value.trim().length() == 0) {
							continue;
						}

						if (entry.getKey().equals(
								NewApplicationWizardPage.PRODUCT_NAME)
								|| entry.getKey().equals(
										NewApplicationWizardPage.APPLICATION)
											||entry.getKey().equals(
													NewApplicationWizardPage.richSample)) {
							continue;
						}
						IPluginElement element = fmodel.getFactory()
								.createElement(productElement);
						element.setName("property");
						element.setAttribute("name", entry.getKey());
						element.setAttribute("value", value);
						productElement.add(element);
					}
				}
			}
			extension.add(productElement);
			fmodel.getPluginBase().add(extension);
			fmodel.save();

		} catch (CoreException e) {
			PDEPlugin.logException(e);
		}
	}

	/**
	 * create products extension detail
	 * 
	 * @param project
	 */
	@SuppressWarnings("restriction")
	public void createApplicationResources(IProject project,
			IProgressMonitor monitor) {
		Map<String, String> map = fApplicationPage.getData();
		boolean isMinimalist = !map.get(NewApplicationWizardPage.richSample).equalsIgnoreCase("TRUE");
		if (map == null
				|| map.get(NewApplicationWizardPage.PRODUCT_NAME) == null)
			return;

		// If the project has invalid characters, the plug-in name would replace them with underscores, product name does the same
		String pluginName = map.get(NewApplicationWizardPage.PRODUCT_NAME);

		// If there's no Activator created we create default package
		if (!fPluginData.doGenerateClass()) {
			String packageName = fPluginData.getId();
			IPath path = new Path(packageName.replace('.', '/'));
			if (fPluginData.getSourceFolderName().trim().length() > 0)
				path = new Path(fPluginData.getSourceFolderName()).append(path);
			
			try {
				CoreUtility.createFolder(project.getFolder(path));
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		IJavaProject javaProject = JavaCore.create(project);
		IPackageFragment fragment = null;

		try {
			for (IPackageFragment element : javaProject.getPackageFragments()) {
				if (element.getKind() == IPackageFragmentRoot.K_SOURCE) {
					fragment = element;
				}
			}
		} catch (JavaModelException e1) {
			e1.printStackTrace();
		}

		createApplicationModel(project, pluginName, fragment);

		String cssPath = map
				.get(NewApplicationWizardPage.APPLICATION_CSS_PROPERTY);
		if (cssPath != null && cssPath.trim().length() > 0) {
			IFile file = project.getFile(cssPath);

			try {
				prepareFolder(file.getParent(), monitor);

				URL corePath = ResourceLocator
						.getProjectTemplateFiles("css/default.css");
				file.create(corePath.openStream(), true, monitor);
			} catch (Exception e) {
				PDEPlugin.logException(e);
			}
		}

		// IFolder folder = project.getFolder("icons");
		// try {
		// folder.create(true, true, monitor);
		// Bundle bundle = Platform
		// .getBundle("org.eclipse.e4.tools.ui.designer");
		//
		// for (String fileName : new String[] { "sample.gif", "save_edit.gif"
		// }) {
		// URL sampleUrl = bundle.getEntry("resources/icons/" + fileName);
		// sampleUrl = FileLocator.resolve(sampleUrl);
		// InputStream inputStream = sampleUrl.openStream();
		// IFile file = folder.getFile(fileName);
		// file.create(inputStream, true, monitor);
		// }
		// } catch (Exception e) {
		// PDEPlugin.logException(e);
		// }

		String template_id = "common";
		Set<String> binaryExtentions = new HashSet<String>();
		binaryExtentions.add(".gif");
		binaryExtentions.add(".png");

		Map<String, String> keys = new HashMap<String, String>();
		keys.put("projectName", pluginName);
		String elementName = fragment.getElementName();
		keys.put("packageName", (elementName.equals("")?"": elementName + ".")+"handlers");
		keys.put("packageName2", (elementName.equals("")?"": elementName + ".")+"parts");
		keys.put("programArgs", "true".equalsIgnoreCase(map.get(NewApplicationWizardPage.CLEAR_PERSISTED_STATE))?"-clearPersistedState":"" );
		try {
			URL corePath = ResourceLocator.getProjectTemplateFiles(template_id);
			IRunnableWithProgress op = new TemplateOperation(corePath, project,
					keys, binaryExtentions,isMinimalist);
			getContainer().run(false, true, op);
		} catch (Exception e) {
			PDEPlugin.logException(e);
		}
		if (!isMinimalist)
		{
		try {
			URL corePath = ResourceLocator.getProjectTemplateFiles("src");
			IRunnableWithProgress op = new TemplateOperation(corePath,
					(IContainer) fragment.getResource(), keys, binaryExtentions, isMinimalist);
			getContainer().run(false, true, op);
		} catch (Exception e) {
			PDEPlugin.logException(e);
		}
		}
	}

	private void createApplicationModel(IProject project, String pluginName,
			IPackageFragment fragment) {
		Map<String, String> map = fApplicationPage.getData();
		boolean isMinimalist = !map.get(NewApplicationWizardPage.richSample).equalsIgnoreCase("TRUE");
		if (APPLICATION_MODEL != null && APPLICATION_MODEL.trim().length() > 0) {
			// Create a resource set
			//
			ResourceSet resourceSet = new ResourceSetImpl();

			// Get the URI of the model file.
			//
			URI fileURI = URI.createPlatformResourceURI(project.getName() + "/"
					+ APPLICATION_MODEL, true);

			// Create a resource for this file.
			//
			Resource resource = resourceSet.createResource(fileURI);

			MApplication application = MApplicationFactory.INSTANCE
					.createApplication();

			application.setElementId("org.eclipse.e4.ide.application");

			MAddon addon = MApplicationFactory.INSTANCE.createAddon();
			addon.setElementId("org.eclipse.e4.core.commands.service");
			addon.setContributionURI("bundleclass://org.eclipse.e4.core.commands/org.eclipse.e4.core.commands.CommandServiceAddon");
			application.getAddons().add(addon);

			addon = MApplicationFactory.INSTANCE.createAddon();
			addon.setElementId("org.eclipse.e4.ui.contexts.service");
			addon.setContributionURI("bundleclass://org.eclipse.e4.ui.services/org.eclipse.e4.ui.services.ContextServiceAddon");
			application.getAddons().add(addon);

			addon = MApplicationFactory.INSTANCE.createAddon();
			addon.setElementId("org.eclipse.e4.ui.bindings.service");
			addon.setContributionURI("bundleclass://org.eclipse.e4.ui.bindings/org.eclipse.e4.ui.bindings.BindingServiceAddon");
			application.getAddons().add(addon);

			addon = MApplicationFactory.INSTANCE.createAddon();
			addon.setElementId("org.eclipse.e4.ui.workbench.commands.model");
			addon.setContributionURI("bundleclass://org.eclipse.e4.ui.workbench/org.eclipse.e4.ui.internal.workbench.addons.CommandProcessingAddon");
			application.getAddons().add(addon);

			addon = MApplicationFactory.INSTANCE.createAddon();
			addon.setElementId("org.eclipse.e4.ui.workbench.contexts.model");
			addon.setContributionURI("bundleclass://org.eclipse.e4.ui.workbench/org.eclipse.e4.ui.internal.workbench.addons.ContextProcessingAddon");
			application.getAddons().add(addon);

			addon = MApplicationFactory.INSTANCE.createAddon();
			addon.setElementId("org.eclipse.e4.ui.workbench.bindings.model");
			addon.setContributionURI("bundleclass://org.eclipse.e4.ui.workbench.swt/org.eclipse.e4.ui.workbench.swt.util.BindingProcessingAddon");
			application.getAddons().add(addon);
			MTrimmedWindow mainWindow = MBasicFactory.INSTANCE
					.createTrimmedWindow();
			application.getChildren().add(mainWindow);
				mainWindow.setLabel(pluginName);
				mainWindow.setWidth(500);
				mainWindow.setHeight(400);
				resource.getContents().add((EObject) application);
				if (!isMinimalist){
					MBindingContext rootContext = MCommandsFactory.INSTANCE
							.createBindingContext();
					rootContext.setElementId("org.eclipse.ui.contexts.dialogAndWindow");
					rootContext.setName("In Dialog and Windows");

					MBindingContext childContext = MCommandsFactory.INSTANCE
							.createBindingContext();
					childContext.setElementId("org.eclipse.ui.contexts.window");
					childContext.setName("In Windows");
					rootContext.getChildren().add(childContext);

					childContext = MCommandsFactory.INSTANCE.createBindingContext();
					childContext.setElementId("org.eclipse.ui.contexts.dialog");
					childContext.setName("In Dialogs");
					rootContext.getChildren().add(childContext);

					application.getRootContext().add(rootContext);
					application.getBindingContexts().add(rootContext);



					// Create Quit command
					MCommand quitCommand = createCommand("org.eclipse.ui.file.exit",
							"quitCommand", "QuitHandler",
							"M1+Q", pluginName, fragment, application);

					MCommand openCommand = createCommand(pluginName + ".open",
							"openCommand", "OpenHandler",
							"M1+O", pluginName, fragment, application);

					MCommand saveCommand = createCommand("org.eclipse.ui.file.save",
							"saveCommand", "SaveHandler",
							"M1+S", pluginName, fragment, application);

					MCommand aboutCommand = createCommand(
							"org.eclipse.ui.help.aboutAction", "aboutCommand",
							"AboutHandler", "M1+A", pluginName, fragment,
							application);

					MMenu menu = MMenuFactory.INSTANCE.createMenu();
					mainWindow.setMainMenu(menu);
					menu.setElementId("menu:org.eclipse.ui.main.menu");

					MMenu fileMenuItem = MMenuFactory.INSTANCE.createMenu();
					menu.getChildren().add(fileMenuItem);
					fileMenuItem.setLabel("File");
					{
						MHandledMenuItem menuItemOpen = MMenuFactory.INSTANCE
								.createHandledMenuItem();
						fileMenuItem.getChildren().add(menuItemOpen);
						menuItemOpen.setLabel("Open");
						menuItemOpen.setIconURI("platform:/plugin/"
								+ pluginName + "/icons/sample.gif");
						menuItemOpen.setCommand(openCommand);

						MHandledMenuItem menuItemSave = MMenuFactory.INSTANCE
								.createHandledMenuItem();
						fileMenuItem.getChildren().add(menuItemSave);
						menuItemSave.setLabel("Save");
						menuItemSave.setIconURI("platform:/plugin/"
								+ pluginName + "/icons/save_edit.gif");
						menuItemSave.setCommand(saveCommand);

						MHandledMenuItem menuItemQuit = MMenuFactory.INSTANCE
								.createHandledMenuItem();
						fileMenuItem.getChildren().add(menuItemQuit);
						menuItemQuit.setLabel("Quit");
						menuItemQuit.setCommand(quitCommand);
					}
					MMenu helpMenuItem = MMenuFactory.INSTANCE.createMenu();
					menu.getChildren().add(helpMenuItem);
					helpMenuItem.setLabel("Help");
					{
						MHandledMenuItem menuItemAbout = MMenuFactory.INSTANCE
								.createHandledMenuItem();
						helpMenuItem.getChildren().add(menuItemAbout);
						menuItemAbout.setLabel("About");
						menuItemAbout.setCommand(aboutCommand);
					}

					// PerspectiveStack
					MPerspectiveStack perspectiveStack = MAdvancedFactory.INSTANCE
							.createPerspectiveStack();
					mainWindow.getChildren().add(perspectiveStack);

					MPerspective perspective = MAdvancedFactory.INSTANCE
							.createPerspective();
					perspectiveStack.getChildren().add(perspective);
					{
						// Part Container
						MPartSashContainer partSashContainer = MBasicFactory.INSTANCE
								.createPartSashContainer();
						perspective.getChildren().add(partSashContainer);

						MPartStack partStack = MBasicFactory.INSTANCE
								.createPartStack();
						partSashContainer.getChildren().add(partStack);

						MPart part = MBasicFactory.INSTANCE.createPart();
						partStack.getChildren().add(part);
						part.setLabel("Sample Part");
						part.setContributionURI("bundleclass://"+pluginName+"/"+fragment.getElementName()+".parts"+".SamplePart");

					}

					// WindowTrim
					MTrimBar trimBar = MBasicFactory.INSTANCE
							.createTrimBar();
					mainWindow.getTrimBars().add(trimBar);

					MToolBar toolBar = MMenuFactory.INSTANCE
							.createToolBar();
					toolBar.setElementId("toolbar:org.eclipse.ui.main.toolbar");
					trimBar.getChildren().add(toolBar);

					MHandledToolItem toolItemOpen = MMenuFactory.INSTANCE
							.createHandledToolItem();
					toolBar.getChildren().add(toolItemOpen);
					toolItemOpen.setIconURI("platform:/plugin/"
							+ pluginName + "/icons/sample.gif");
					toolItemOpen.setCommand(openCommand);

					MHandledToolItem toolItemSave = MMenuFactory.INSTANCE
							.createHandledToolItem();
					toolBar.getChildren().add(toolItemSave);
					toolItemSave.setIconURI("platform:/plugin/"
							+ pluginName + "/icons/save_edit.gif");
					toolItemSave.setCommand(saveCommand);
				}
			Map<Object, Object> options = new HashMap<Object, Object>();
			options.put(XMLResource.OPTION_ENCODING, "UTF-8");
			try {
				resource.save(options);
			} catch (IOException e) {
				PDEPlugin.logException(e);
		
			}
		}
	}

	private MCommand createCommand(String commandId, String name,
			String className,
			String keyBinding, String projectName, IPackageFragment fragment,
			MApplication application) {
		MCommand command = MCommandsFactory.INSTANCE.createCommand();
		command.setCommandName(name);
		command.setElementId(commandId);
		application.getCommands().add(command);
		{
			// Create Quit handler for command
			MHandler quitHandler = MCommandsFactory.INSTANCE.createHandler();
			quitHandler.setCommand(command);
			String elementName = fragment.getElementName();
			quitHandler.setContributionURI("bundleclass://" + projectName
					+ "/" + (elementName.equals("")?"": elementName + ".")+"handlers."
					+ className);
			application.getHandlers().add(quitHandler);

			MKeyBinding binding = MCommandsFactory.INSTANCE.createKeyBinding();
			binding.setKeySequence(keyBinding);
			binding.setCommand(command);
			List<MBindingTable> tables = application.getBindingTables();
			if (tables.size() == 0) {
				MBindingContext rootContext = null;
				if (application.getRootContext().size() > 0) {
					rootContext = application.getRootContext().get(0);
				} else {
					rootContext = MCommandsFactory.INSTANCE
							.createBindingContext();
					rootContext
							.setElementId("org.eclipse.ui.contexts.dialogAndWindow");
					rootContext.setName("In Dialog and Windows");
					application.getRootContext().add(rootContext);
				}
				MBindingTable table = MCommandsFactory.INSTANCE
						.createBindingTable();
				table.setBindingContext(rootContext);
				tables.add(table);
			}
			tables.get(0).getBindings().add(binding);
		}
		return command;
	}

	private void prepareFolder(IContainer container, IProgressMonitor monitor)
			throws CoreException {
		IContainer parent = container.getParent();
		if (parent instanceof IFolder) {
			prepareFolder((IFolder) parent, monitor);
		}
		if (!container.exists() && container instanceof IFolder) {
			IFolder folder = (IFolder) container;
			folder.create(true, true, monitor);
		}
	}

	public String getPluginId() {
		return fPluginData.getId();
	}

	public String getPluginVersion() {
		return fPluginData.getVersion();
	}
}

Back to the top