use of org.jetbrains.java.generate.template.TemplateResource in project intellij-community by JetBrains.
the class EqualsHashCodeTemplatesManager method getTemplateCouples.
public Collection<Couple<TemplateResource>> getTemplateCouples() {
final LinkedHashMap<String, Couple<TemplateResource>> resources = new LinkedHashMap<>();
for (TemplateResource resource : getAllTemplates()) {
final String baseName = getTemplateBaseName(resource);
TemplateResource eq = toEqualsName(baseName).equals(resource.getFileName()) ? resource : null;
TemplateResource hc = toHashCodeName(baseName).equals(resource.getFileName()) ? resource : null;
final Couple<TemplateResource> couple = resources.get(baseName);
if (couple != null) {
resources.put(baseName, Couple.of(couple.first != null ? couple.first : eq, couple.second != null ? couple.second : hc));
} else {
resources.put(baseName, Couple.of(eq, hc));
}
}
return resources.values();
}
use of org.jetbrains.java.generate.template.TemplateResource in project intellij-community by JetBrains.
the class TemplatesPanel method apply.
@Override
public void apply() throws ConfigurationException {
super.apply();
myTemplatesManager.setTemplates(getItems());
final TemplateResource selection = getSelectedItem();
if (selection != null) {
myTemplatesManager.setDefaultTemplate(selection);
}
}
use of org.jetbrains.java.generate.template.TemplateResource in project intellij-community by JetBrains.
the class GenerateToStringActionHandlerImpl method doExecuteAction.
private static void doExecuteAction(@NotNull final Project project, @NotNull final PsiClass clazz, final Editor editor) {
if (!FileModificationService.getInstance().preparePsiElementsForWrite(clazz)) {
return;
}
logger.debug("+++ doExecuteAction - START +++");
if (logger.isDebugEnabled()) {
logger.debug("Current project " + project.getName());
}
final PsiElementClassMember[] dialogMembers = buildMembersToShow(clazz);
final MemberChooserHeaderPanel header = new MemberChooserHeaderPanel(clazz);
logger.debug("Displaying member chooser dialog");
final MemberChooser<PsiElementClassMember> chooser = new MemberChooser<PsiElementClassMember>(dialogMembers, true, true, project, PsiUtil.isLanguageLevel5OrHigher(clazz), header) {
@Nullable
@Override
protected String getHelpId() {
return "editing.altInsert.tostring";
}
};
//noinspection DialogTitleCapitalization
chooser.setTitle("Generate toString()");
chooser.setCopyJavadocVisible(false);
chooser.selectElements(getPreselection(clazz, dialogMembers));
header.setChooser(chooser);
chooser.show();
if (DialogWrapper.OK_EXIT_CODE == chooser.getExitCode()) {
Collection<PsiMember> selectedMembers = GenerationUtil.convertClassMembersToPsiMembers(chooser.getSelectedElements());
final TemplateResource template = header.getSelectedTemplate();
ToStringTemplatesManager.getInstance().setDefaultTemplate(template);
if (template.isValidTemplate()) {
final GenerateToStringWorker worker = new GenerateToStringWorker(clazz, editor, chooser.isInsertOverrideAnnotation());
// decide what to do if the method already exists
ConflictResolutionPolicy resolutionPolicy = worker.exitsMethodDialog(template);
WriteAction.run(() -> {
try {
worker.execute(selectedMembers, template, resolutionPolicy);
} catch (Exception e) {
GenerationUtil.handleException(project, e);
}
});
} else {
HintManager.getInstance().showErrorHint(editor, "toString() template '" + template.getFileName() + "' is invalid");
}
}
logger.debug("+++ doExecuteAction - END +++");
}
use of org.jetbrains.java.generate.template.TemplateResource in project intellij-community by JetBrains.
the class EqualsHashCodeTemplatesManager method getDefaultTemplate.
private TemplateResource getDefaultTemplate(String selfSuffix, String oppositeSuffix, TemplateResource defaultTemplate) {
final String fileName = defaultTemplate.getFileName();
if (fileName.endsWith(selfSuffix)) {
return defaultTemplate;
}
final String equalsTemplateName = StringUtil.trimEnd(fileName, oppositeSuffix) + selfSuffix;
for (TemplateResource resource : getAllTemplates()) {
if (equalsTemplateName.equals(resource.getFileName())) {
return resource;
}
}
assert false : selfSuffix + " template for " + fileName + " not found";
return null;
}
use of org.jetbrains.java.generate.template.TemplateResource in project intellij-community by JetBrains.
the class GenerateGetterSetterHandlerBase method getHeaderPanel.
protected static JComponent getHeaderPanel(final Project project, final TemplatesManager templatesManager, final String templatesTitle) {
final JPanel panel = new JPanel(new BorderLayout());
final JLabel templateChooserLabel = new JLabel(templatesTitle);
panel.add(templateChooserLabel, BorderLayout.WEST);
final ComboBox comboBox = new ComboBox();
templateChooserLabel.setLabelFor(comboBox);
comboBox.setRenderer(new ListCellRendererWrapper<TemplateResource>() {
@Override
public void customize(JList list, TemplateResource value, int index, boolean selected, boolean hasFocus) {
setText(value.getName());
}
});
final ComponentWithBrowseButton<ComboBox> comboBoxWithBrowseButton = new ComponentWithBrowseButton<>(comboBox, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final TemplatesPanel ui = new TemplatesPanel(project, templatesManager) {
@Override
protected boolean onMultipleFields() {
return false;
}
@Nls
@Override
public String getDisplayName() {
return StringUtil.capitalizeWords(UIUtil.removeMnemonic(StringUtil.trimEnd(templatesTitle, ":")), true);
}
};
ui.setHint("Visibility is applied according to File | Settings | Editor | Code Style | Java | Code Generation");
ui.selectNodeInTree(templatesManager.getDefaultTemplate());
if (ShowSettingsUtil.getInstance().editConfigurable(panel, ui)) {
setComboboxModel(templatesManager, comboBox);
}
}
});
setComboboxModel(templatesManager, comboBox);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(@NotNull final ActionEvent M) {
templatesManager.setDefaultTemplate((TemplateResource) comboBox.getSelectedItem());
}
});
panel.add(comboBoxWithBrowseButton, BorderLayout.CENTER);
return panel;
}
Aggregations