本文整理汇总了Java中com.intellij.ide.fileTemplates.JavaTemplateUtil.setPackageNameAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java JavaTemplateUtil.setPackageNameAttribute方法的具体用法?Java JavaTemplateUtil.setPackageNameAttribute怎么用?Java JavaTemplateUtil.setPackageNameAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.ide.fileTemplates.JavaTemplateUtil
的用法示例。
在下文中一共展示了JavaTemplateUtil.setPackageNameAttribute方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setupCatchBlock
import com.intellij.ide.fileTemplates.JavaTemplateUtil; //导入方法依赖的package包/类
@Override
public void setupCatchBlock(String exceptionName, PsiElement context, PsiCatchSection catchSection) {
final FileTemplate catchBodyTemplate = FileTemplateManager.getInstance().getCodeTemplate(JavaTemplateUtil.TEMPLATE_CATCH_BODY);
LOG.assertTrue(catchBodyTemplate != null);
final Properties props = new Properties();
props.setProperty(FileTemplate.ATTRIBUTE_EXCEPTION, exceptionName);
if (context != null && context.isPhysical()) {
final PsiDirectory directory = context.getContainingFile().getContainingDirectory();
if (directory != null) {
JavaTemplateUtil.setPackageNameAttribute(props, directory);
}
}
final PsiCodeBlock codeBlockFromText;
try {
codeBlockFromText = PsiElementFactory.SERVICE.getInstance(myProject).createCodeBlockFromText("{\n" + catchBodyTemplate.getText(props) + "\n}", null);
}
catch (ProcessCanceledException ce) {
throw ce;
}
catch (Exception e) {
throw new IncorrectOperationException("Incorrect file template", e);
}
catchSection.getCatchBlock().replace(codeBlockFromText);
}
示例2: setupCatchBlock
import com.intellij.ide.fileTemplates.JavaTemplateUtil; //导入方法依赖的package包/类
@Override
public void setupCatchBlock(@NotNull String exceptionName, @NotNull PsiType exceptionType, PsiElement context, @NotNull PsiCatchSection catchSection) {
final FileTemplate catchBodyTemplate = FileTemplateManager.getInstance(catchSection.getProject()).getCodeTemplate(JavaTemplateUtil.TEMPLATE_CATCH_BODY);
LOG.assertTrue(catchBodyTemplate != null);
Properties props = FileTemplateManager.getInstance(myProject).getDefaultProperties();
props.setProperty(FileTemplate.ATTRIBUTE_EXCEPTION, exceptionName);
props.setProperty(FileTemplate.ATTRIBUTE_EXCEPTION_TYPE, exceptionType.getCanonicalText());
if (context != null && context.isPhysical()) {
final PsiDirectory directory = context.getContainingFile().getContainingDirectory();
if (directory != null) {
JavaTemplateUtil.setPackageNameAttribute(props, directory);
}
}
final PsiCodeBlock codeBlockFromText;
try {
codeBlockFromText = PsiElementFactory.SERVICE.getInstance(myProject).createCodeBlockFromText("{\n" + catchBodyTemplate.getText(props) + "\n}", null);
}
catch (ProcessCanceledException ce) {
throw ce;
}
catch (Exception e) {
throw new IncorrectOperationException("Incorrect file template", (Throwable)e);
}
catchSection.getCatchBlock().replace(codeBlockFromText);
}
示例3: setupCatchBlock
import com.intellij.ide.fileTemplates.JavaTemplateUtil; //导入方法依赖的package包/类
@Override
public void setupCatchBlock(String exceptionName, PsiElement context, PsiCatchSection catchSection)
{
final FileTemplate catchBodyTemplate = FileTemplateManager.getInstance(catchSection.getProject()).getCodeTemplate(JavaTemplateUtil.TEMPLATE_CATCH_BODY);
LOG.assertTrue(catchBodyTemplate != null);
final Properties props = new Properties();
props.setProperty(FileTemplate.ATTRIBUTE_EXCEPTION, exceptionName);
if(context != null && context.isPhysical())
{
final PsiDirectory directory = context.getContainingFile().getContainingDirectory();
if(directory != null)
{
JavaTemplateUtil.setPackageNameAttribute(props, directory);
}
}
final PsiCodeBlock codeBlockFromText;
try
{
codeBlockFromText = PsiElementFactory.SERVICE.getInstance(myProject).createCodeBlockFromText("{\n" + catchBodyTemplate.getText(props) + "\n}", null);
}
catch(ProcessCanceledException ce)
{
throw ce;
}
catch(Throwable e)
{
throw new IncorrectOperationException("Incorrect file template", e);
}
catchSection.getCatchBlock().replace(codeBlockFromText);
}
示例4: createFromTemplateInternal
import com.intellij.ide.fileTemplates.JavaTemplateUtil; //导入方法依赖的package包/类
protected static PsiFile createFromTemplateInternal( @NotNull PsiDirectory directory,
@NotNull String name,
@NotNull String fileName,
@NotNull String templateName,
@NonNls String... parameters )
throws IncorrectOperationException
{
// Load template
FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance();
FileTemplate template = fileTemplateManager.getJ2eeTemplate( templateName );
// Process template properties
Properties properties = new Properties( fileTemplateManager.getDefaultProperties() );
JavaTemplateUtil.setPackageNameAttribute( properties, directory );
properties.setProperty( NAME_TEMPLATE_PROPERTY, name );
// Add parameters
for( int i = 0; i < parameters.length; i += 2 )
{
properties.setProperty( parameters[ i ], parameters[ i + 1 ] );
}
// Create text from template with specified properties
String text;
try
{
text = template.getText( properties );
}
catch( Exception e )
{
String message = "Unable to load template for " +
fileTemplateManager.internalTemplateToSubject( templateName );
throw new RuntimeException( message, e );
}
// Serialized text to file
PsiManager psiManager = PsiManager.getInstance( directory.getProject() );
PsiFileFactory fileFactory = PsiFileFactory.getInstance( directory.getProject() );
PsiFile file = fileFactory.createFileFromText( fileName, text );
// Reformat the file according to project/default style
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance( psiManager );
codeStyleManager.reformat( file );
// Add newly created file to directory
return (PsiFile) directory.add( file );
}