public void resetFrom(TemplateImpl another) {
    removeAllParsed();
    toParseSegments = another.toParseSegments;

    myKey = another.getKey();
    myString = another.myString;
    myTemplateText = another.myTemplateText;
    myGroupName = another.myGroupName;
    myId = another.myId;
    myDescription = another.myDescription;
    myShortcutChar = another.myShortcutChar;
    isToReformat = another.isToReformat;
    isToShortenLongNames = another.isToShortenLongNames;
    myIsInline = another.myIsInline;
    myTemplateContext = another.myTemplateContext.createCopy();
    isDeactivated = another.isDeactivated;
    for (Property property : Property.values()) {
      boolean value = another.getValue(property);
      if (value != Template.getDefaultValue(property)) {
        setValue(property, value);
      }
    }
    for (Variable variable : another.myVariables) {
      addVariable(
          variable.getName(),
          variable.getExpressionString(),
          variable.getDefaultValueString(),
          variable.isAlwaysStopAt());
    }
  }
Ejemplo n.º 2
0
 protected Template readExternal(Element element, final String templateName) {
   TemplateImpl template =
       new TemplateImpl(
           element.getAttributeValue("name"), element.getAttributeValue("value"), templateName);
   template.setDescription(element.getAttributeValue("description"));
   template.setToReformat(Boolean.valueOf(element.getAttributeValue("toReformat")));
   template.setToShortenLongNames(Boolean.valueOf(element.getAttributeValue("toShortenFQNames")));
   TemplateContext context = template.getTemplateContext();
   for (Object o : element.getChildren("variable")) {
     Element e = (Element) o;
     template.addVariable(
         e.getAttributeValue("name"),
         e.getAttributeValue("expression"),
         e.getAttributeValue("defaultValue"),
         Boolean.valueOf(e.getAttributeValue("alwaysStopAt")));
   }
   Element contextElement = element.getChild("context");
   if (contextElement != null) {
     try {
       context.readExternal(contextElement);
     } catch (InvalidDataException e) {
       e.printStackTrace();
     }
   }
   return template;
 }
Ejemplo n.º 3
0
 /**
  * Set template variable values
  *
  * @param template The template
  * @param values These values will be set to variable v0, v1, ... , vn. "n" is "values.size() - 1"
  */
 public static void setTemplateVariableValues(TemplateImpl template, List<String> values) {
   for (int i = 0; i < values.size(); i++) {
     String value = values.get(i);
     if (!value.startsWith("\"") || !value.endsWith("\"")) {
       value = String.format("\"%s\"", value);
     }
     template.addVariable("v" + i, value, value, true);
   }
 }
Ejemplo n.º 4
0
  /**
   * In the specified range of editor, replace all variables with defaultValue, and let user change
   * the value.
   *
   * @param editor
   * @param range
   * @param variable
   * @param defaultValue
   */
  public static void runTemplate(
      Editor editor, TextRange range, String variable, String defaultValue) {
    String text = editor.getDocument().getText(range);
    editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());

    if (!defaultValue.matches("\".*\"")) {
      defaultValue = '"' + defaultValue + '"';
    }

    TemplateImpl template = createTemplate(text);
    template.addVariable(variable, defaultValue, defaultValue, true);
    TemplateManager.getInstance(editor.getProject()).startTemplate(editor, "", template);
  }
Ejemplo n.º 5
0
  private static void runOrApplyFileTemplate(
      Project project,
      VirtualFile file,
      String templateName,
      Properties properties,
      Properties conditions,
      boolean interactive)
      throws IOException {
    FileTemplateManager manager = FileTemplateManager.getInstance();
    FileTemplate fileTemplate = manager.getJ2eeTemplate(templateName);
    Properties allProperties = manager.getDefaultProperties(project);
    if (!interactive) {
      allProperties.putAll(properties);
    }
    allProperties.putAll(conditions);
    String text = fileTemplate.getText(allProperties);
    Pattern pattern = Pattern.compile("\\$\\{(.*)\\}");
    Matcher matcher = pattern.matcher(text);
    StringBuffer builder = new StringBuffer();
    while (matcher.find()) {
      matcher.appendReplacement(builder, "\\$" + matcher.group(1).toUpperCase() + "\\$");
    }
    matcher.appendTail(builder);
    text = builder.toString();

    TemplateImpl template =
        (TemplateImpl) TemplateManager.getInstance(project).createTemplate("", "", text);
    for (int i = 0; i < template.getSegmentsCount(); i++) {
      if (i == template.getEndSegmentNumber()) continue;
      String name = template.getSegmentName(i);
      String value = "\"" + properties.getProperty(name, "") + "\"";
      template.addVariable(name, value, value, true);
    }

    if (interactive) {
      OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file);
      Editor editor = FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
      editor.getDocument().setText("");
      TemplateManager.getInstance(project).startTemplate(editor, template);
    } else {
      VfsUtil.saveText(file, template.getTemplateText());
    }
  }