Преглед изворни кода

Genmodel Wizard: preselect generator based on project nature

holger.willebrandt@gmail.com пре 13 година
родитељ
комит
2698f4ae78

+ 30 - 0
plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/CoreGenerator.java

@@ -0,0 +1,30 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU 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:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.generator.genmodel.ui.wizard;
+
+/**
+ * @author holger willebrandt - Initial contribution and API
+ */
+public enum CoreGenerator {
+
+	Java("yakindu::java"), C("yakindu::c"), Cpp("yakindu::cpp");
+
+	private String id;
+
+	private CoreGenerator(String id) {
+		this.id = id;
+	}
+
+	public String getId() {
+		return id;
+	}
+
+}

+ 13 - 0
plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/GeneratorType.java

@@ -1,5 +1,18 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU 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:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
 package org.yakindu.sct.generator.genmodel.ui.wizard;
 
+/**
+ * @author holger willebrandt - Initial contribution and API
+ */
 public enum GeneratorType {
 	Java, Xpand, Xtend
 }

+ 35 - 0
plugins/org.yakindu.sct.generator.genmodel.ui/src/org/yakindu/sct/generator/genmodel/ui/wizard/SGenWizardPage2.java

@@ -13,8 +13,12 @@ package org.yakindu.sct.generator.genmodel.ui.wizard;
 import java.io.IOException;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.TreeMap;
 
 import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IResourceVisitor;
 import org.eclipse.core.resources.ResourcesPlugin;
@@ -64,6 +68,15 @@ import com.google.common.collect.Lists;
 public class SGenWizardPage2 extends WizardPage {
 
 	protected static final String STATECHART_FILE_EXTENSION = "sct";
+	static Map<String, CoreGenerator> natureDefaultGenerators = new TreeMap<String, CoreGenerator>();
+	static {
+		natureDefaultGenerators.put("org.eclipse.cdt.core.cnature",
+				CoreGenerator.C);
+		natureDefaultGenerators.put("org.eclipse.cdt.core.ccnature",
+				CoreGenerator.Cpp);
+		natureDefaultGenerators.put("org.eclipse.jdt.core.javanature",
+				CoreGenerator.Java);
+	}
 	private ComboViewer generatorCombo;
 	protected CheckboxTreeViewer stateChartTree;
 	private final SGenWizardPage1 fileSelectionPage;
@@ -252,10 +265,32 @@ public class SGenWizardPage2 extends WizardPage {
 			} catch (CoreException e) {
 				// input will be empty
 			}
+			preselectGenerator(folder);
 			checkComplete();
 		}
 	}
 
+	private void preselectGenerator(IFolder folder) {
+		String generatorId = CoreGenerator.Java.getId();
+		IProject project = folder.getProject();
+		try {
+			for (Entry<String, CoreGenerator> entry : natureDefaultGenerators
+					.entrySet()) {
+				if (project.getNature(entry.getKey()) != null) {
+					generatorId = entry.getValue().getId();
+				}
+			}
+		} catch (CoreException e) {
+			// nothing will be pre-selected
+		}
+		GeneratorDescriptor descriptor = generatorId != null ? GeneratorExtensions
+				.getGeneratorDescriptorForId(generatorId) : null;
+		if (descriptor != null) {
+			generatorCombo.setSelection(new StructuredSelection(
+					new Object[] { descriptor }), true);
+		}
+	}
+
 	private void applyPreselection(TreeNode treeNode) {
 		if (treeNode.isPreselected())
 			stateChartTree.setChecked(treeNode, true);