Search in sources :

Example 1 with TemplateResource

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();
}
Also used : Couple(com.intellij.openapi.util.Couple) TemplateResource(org.jetbrains.java.generate.template.TemplateResource) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with TemplateResource

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);
    }
}
Also used : TemplateResource(org.jetbrains.java.generate.template.TemplateResource)

Example 3 with TemplateResource

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 +++");
}
Also used : ConflictResolutionPolicy(org.jetbrains.java.generate.config.ConflictResolutionPolicy) MemberChooser(com.intellij.ide.util.MemberChooser) PsiElementClassMember(com.intellij.codeInsight.generation.PsiElementClassMember) TemplateResource(org.jetbrains.java.generate.template.TemplateResource) ConfigurationException(com.intellij.openapi.options.ConfigurationException)

Example 4 with TemplateResource

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;
}
Also used : TemplateResource(org.jetbrains.java.generate.template.TemplateResource)

Example 5 with TemplateResource

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;
}
Also used : ComboBox(com.intellij.openapi.ui.ComboBox) ActionEvent(java.awt.event.ActionEvent) TemplatesPanel(org.jetbrains.java.generate.view.TemplatesPanel) ActionListener(java.awt.event.ActionListener) Nls(org.jetbrains.annotations.Nls) ComponentWithBrowseButton(com.intellij.openapi.ui.ComponentWithBrowseButton) TemplateResource(org.jetbrains.java.generate.template.TemplateResource)

Aggregations

TemplateResource (org.jetbrains.java.generate.template.TemplateResource)5 PsiElementClassMember (com.intellij.codeInsight.generation.PsiElementClassMember)1 MemberChooser (com.intellij.ide.util.MemberChooser)1 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1 ComboBox (com.intellij.openapi.ui.ComboBox)1 ComponentWithBrowseButton (com.intellij.openapi.ui.ComponentWithBrowseButton)1 Couple (com.intellij.openapi.util.Couple)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 LinkedHashMap (java.util.LinkedHashMap)1 Nls (org.jetbrains.annotations.Nls)1 ConflictResolutionPolicy (org.jetbrains.java.generate.config.ConflictResolutionPolicy)1 TemplatesPanel (org.jetbrains.java.generate.view.TemplatesPanel)1