一般來說,我想創建一個創建多個文件的Netbeans(7.4)文件嚮導。創建多個文件的Netbeans模板
在我的情況下,我希望它在加載fxml文件時創建JavaFX相關文件以使用約定而不是配置方法。 (我從亞當邊這個想法:http://www.youtube.com/watch?v=mawFd4h1Or4)
更具體:我要進入一個String定義了Basename
,並希望向導創建3個不同的文件,位於包在點擊右鍵 - >「新建「嚮導被用戶調用。
Basename.fxml
BasenameController.java
BasenameView.java
我試着寫我自己的嚮導快3天了,這是行不通的。我研究(薄)的NetBeans API,並仔細閱讀所有這些教程(及以上):
- https://blogs.oracle.com/geertjan/entry/multiple_files_from_a_single
- https://platform.netbeans.org/tutorials/nbm-filetemplates.html
- http://code-chimpanzee-at-large.blogspot.de/2012/09/netbeans-71-file-template-module-with.html
- https://platform.netbeans.org/tutorials/nbm-wizard.html
我嘗試更多的東西,然後我可以在這裏列舉,但這些都是一些令人高興的事情:
- 我從來沒有得到它與一個單一的
@TemplateRegistration
和多個content
條目。 - 所以我註冊了多個模板
@TemplateRegistrations
,每個文件我想創建一個模板。 - 這會創建多個文件,但只有第一個文件具有正確的屬性,例如$ {package}爲freemarker腳本設置。 (另外這種方式我結束了多個模板...但我可以忍受這一點)
- 然後我試着用其他屬性名稱並手動設置這些,但我無法弄清楚如何找到包嚮導被調用。
相關問題,它並沒有真正幫助我:
- how-to-create-several-files-based-on-netbeans-templates-into-different-packages
- how-to-make-netbeans-templates-that-creates-more-than-1-file
任何建議是非常值得歡迎的。提前致謝!
UPDATE:
我設法創建所需的文件。問題是你有使用稀疏記錄的命名約定。如果要正確創建Basename.java
文件,則必須命名模板文件:Basename.java
(這會使編譯器不高興,因爲模板文件不是有效的java類)或Basename.java.template
。如果您將其命名爲:Basename.java.templ
(並自行管理文件的正確命名),它將不會爲您設置${package}
變量。
所以現在我非常接近我想要的東西,但仍然存在正確註冊嚮導的問題。
目前我使用@TemplateRegistrations
來註冊3個模板(每個文件一個)。但由於「文件夾」屬性是強制性的,我現在在Netbeans的「新建」嚮導中有3個條目。當我調用其中一個時,它會根據需要創建所有3個文件,但我想在「新建」嚮導中只有1個條目。
@TemplateRegistrations({
@TemplateRegistration(content = "FXML.fxml.template", scriptEngine = "freemarker", folder = "ASpecial", displayName = "#TemplateName", iconBase = "de/ekrnrw/fxmlview/view.png", description = "newFXMLView.html"),
@TemplateRegistration(content = "View.java.template", scriptEngine = "freemarker", folder = "ASpecial"),
@TemplateRegistration(content = "Controller.java.template", scriptEngine = "freemarker", folder = "ASpecial")
})
我試圖用一個單一的@TemplateRegistration
與多個內容項目,但這並不與本教程的代碼工作。似乎只有fxml文件被處理爲freemarker,並且這兩個java文件只是模板文件的普通副本。
@TemplateRegistration(content = {"FXML.fxml.template","View.java.template","Controller.java.template"}, scriptEngine = "freemarker", folder = "ASpecial", displayName = "#TemplateName", iconBase = "de/ekrnrw/fxmlview/view.png", description = "newFXMLView.html")
我將此問題發佈到NetBeans郵件列表here。這裏是相關的代碼片段:
package de.ekrnrw.fxmlview;
import java.awt.Component;
...
import org.openide.util.NbBundle.Messages;
@TemplateRegistrations({
@TemplateRegistration(content = "FXML.fxml.template", scriptEngine = "freemarker", folder = "ASpecial", displayName = "#TemplateName", iconBase = "de/ekrnrw/fxmlview/view.png", description = "newFXMLView.html"),
@TemplateRegistration(content = "View.java.template", scriptEngine = "freemarker", folder = "ASpecial"),
@TemplateRegistration(content = "Controller.java.template", scriptEngine = "freemarker", folder = "ASpecial")
})
// Would like to use this, but i can't make it work.
//@TemplateRegistration(content = {"FXML.fxml.template","View.java.template","Controller.java.template"}, scriptEngine = "freemarker", folder = "ASpecial", displayName = "#TemplateName", iconBase = "de/ekrnrw/fxmlview/view.png", description = "newFXMLView.html")
@Messages("TemplateName=FXMLView4")
public final class NewFXMLViewWizardIterator implements WizardDescriptor.InstantiatingIterator<WizardDescriptor> {
private int index;
private WizardDescriptor wizard;
private List<WizardDescriptor.Panel<WizardDescriptor>> panels;
private List<WizardDescriptor.Panel<WizardDescriptor>> getPanels() {
if (panels == null) {
panels = new ArrayList<>();
// Change to default new file panel and add our panel at bottom
Project p = Templates.getProject(wizard);
SourceGroup[] groups = ProjectUtils.getSources(p).getSourceGroups(Sources.TYPE_GENERIC);
// SimpleTargetChooser is the default new file panel
WizardDescriptor.Panel<WizardDescriptor> advNewFilePanel = Templates.buildSimpleTargetChooser(p, groups).create();
panels.add(advNewFilePanel);
String[] steps = createSteps();
for (int i = 0; i < panels.size(); i++) {
Component c = panels.get(i).getComponent();
if (steps[i] == null) {
// Default step name to component name of panel. Mainly
// useful for getting the name of the target chooser to
// appear in the list of steps.
steps[i] = c.getName();
}
if (c instanceof JComponent) { // assume Swing components
JComponent jc = (JComponent) c;
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, i);
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, steps);
jc.putClientProperty(WizardDescriptor.PROP_AUTO_WIZARD_STYLE, true);
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DISPLAYED, true);
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_NUMBERED, true);
}
}
}
return panels;
}
@Override
public Set<?> instantiate() throws IOException {
//Get the folder:
FileObject dir = Templates.getTargetFolder(wizard);
DataFolder df = DataFolder.findFolder(dir);
//Get TargetName from File Wizard
String targetName = Templates.getTargetName(wizard);
// Read Title from wizard
String viewName = targetName + "View";
String controllerName = targetName + "Controller";
// FreeMarker Template will get its variables from HashMap.
// HashMap key is the variable name.
Map args = new HashMap();
args.put("controllerName", controllerName);
args.put("viewName", viewName);
//Get Templates
FileObject[] templates = Templates.getTemplate(wizard).getParent().getChildren();
// Create all Files
Set<DataObject> createdObjects = new HashSet<>();
for (FileObject fileObject : templates) {
DataObject dTemplate = DataObject.find(fileObject);
String templateName = dTemplate.getName();
// Sepcial treatment for the .fxml file.
if (templateName.equals("FXML")) {
templateName = "";
}
// Create file
createdObjects.add(dTemplate.createFromTemplate(df, targetName + templateName , args));
}
return createdObjects;
}
...
}
您是否設法爲每個模板只顯示一個條目? – Groben