embedded live templates: ui refactored

This commit is contained in:
Dmitry Avdeev
2015-09-18 12:26:05 +03:00
parent 1ab778ebcc
commit c7a5baa79f
10 changed files with 50 additions and 52 deletions
@@ -34,7 +34,7 @@ public class EmbeddedLiveTemplatesTest extends LightPlatformCodeInsightFixtureTe
myFixture.configureByText(PlainTextFileType.INSTANCE, "");
CustomFileTemplate template = new CustomFileTemplate("foo", "txt");
template.setText("Put caret here: #[[$END$]]# end of template");
template.setHasEmbeddedLiveTemplate(true);
template.setLiveTemplateEnabled(true);
myFixture.testAction(new TestAction(template));
VirtualFile[] files = FileEditorManager.getInstance(getProject()).getSelectedFiles();
myFixture.openFileInEditor(files[0]);
@@ -81,9 +81,9 @@ public interface FileTemplate extends Cloneable {
void setReformatCode(boolean reformat);
boolean isHasEmbeddedLiveTemplate();
boolean isLiveTemplateEnabled();
void setHasEmbeddedLiveTemplate(boolean hasEmbeddedLiveTemplate);
void setLiveTemplateEnabled(boolean value);
FileTemplate clone();
@@ -71,7 +71,7 @@ public abstract class CreateFromTemplateActionBase extends AnAction {
if (createdElement != null) {
elementCreated(dialog, createdElement);
view.selectElement(createdElement);
if (selectedTemplate.isHasEmbeddedLiveTemplate() && createdElement instanceof PsiFile) {
if (selectedTemplate.isLiveTemplateEnabled() && createdElement instanceof PsiFile) {
startLiveTemplate((PsiFile)createdElement);
}
}
@@ -164,6 +164,7 @@ public class AllFileTemplatesConfigurable implements SearchableConfigurable, Con
final FileTemplate newTemplate = new CustomFileTemplate(name, selected.getExtension());
newTemplate.setText(selected.getText());
newTemplate.setReformatCode(selected.isReformatCode());
newTemplate.setLiveTemplateEnabled(selected.isLiveTemplateEnabled());
myCurrentTab.addTemplate(newTemplate);
myModified = true;
myCurrentTab.selectTemplate(newTemplate);
@@ -96,6 +96,7 @@ public final class BundledFileTemplate extends FileTemplateBase {
public void revertToDefaults() {
setText(null);
setReformatCode(DEFAULT_REFORMAT_CODE_VALUE);
setLiveTemplateEnabled(false);
}
public boolean isTextModified() {
@@ -40,6 +40,7 @@ public class ExportableFileTemplateSettings extends FileTemplatesLoader implemen
static final String ELEMENT_TEMPLATE = "template";
static final String ATTRIBUTE_NAME = "name";
static final String ATTRIBUTE_REFORMAT = "reformat";
static final String ATTRIBUTE_LIVE_TEMPLATE = "live-template-enabled";
static final String ATTRIBUTE_ENABLED = "enabled";
private boolean myLoaded = false;
@@ -60,7 +61,7 @@ public class ExportableFileTemplateSettings extends FileTemplatesLoader implemen
Element templatesGroup = null;
for (FileTemplateBase template : manager.getAllTemplates(true)) {
// save only those settings that differ from defaults
boolean shouldSave = template.isReformatCode() != FileTemplateBase.DEFAULT_REFORMAT_CODE_VALUE;
boolean shouldSave = template.isReformatCode() != FileTemplateBase.DEFAULT_REFORMAT_CODE_VALUE || template.isLiveTemplateEnabled();
if (template instanceof BundledFileTemplate) {
shouldSave |= ((BundledFileTemplate)template).isEnabled() != FileTemplateBase.DEFAULT_ENABLED_VALUE;
}
@@ -70,6 +71,7 @@ public class ExportableFileTemplateSettings extends FileTemplatesLoader implemen
final Element templateElement = new Element(ELEMENT_TEMPLATE);
templateElement.setAttribute(ATTRIBUTE_NAME, template.getQualifiedName());
templateElement.setAttribute(ATTRIBUTE_REFORMAT, Boolean.toString(template.isReformatCode()));
templateElement.setAttribute(ATTRIBUTE_LIVE_TEMPLATE, Boolean.toString(template.isLiveTemplateEnabled()));
if (template instanceof BundledFileTemplate) {
templateElement.setAttribute(ATTRIBUTE_ENABLED, Boolean.toString(((BundledFileTemplate)template).isEnabled()));
}
@@ -108,8 +110,8 @@ public class ExportableFileTemplateSettings extends FileTemplatesLoader implemen
if (template == null) {
continue;
}
final boolean reformat = Boolean.TRUE.toString().equals(child.getAttributeValue(ATTRIBUTE_REFORMAT));
template.setReformatCode(reformat);
template.setReformatCode(Boolean.TRUE.toString().equals(child.getAttributeValue(ATTRIBUTE_REFORMAT)));
template.setLiveTemplateEnabled(Boolean.TRUE.toString().equals(child.getAttributeValue(ATTRIBUTE_LIVE_TEMPLATE)));
if (template instanceof BundledFileTemplate) {
final boolean enabled = Boolean.parseBoolean(child.getAttributeValue(ATTRIBUTE_ENABLED, "true"));
((BundledFileTemplate)template).setEnabled(enabled);
@@ -187,6 +187,7 @@ class FTManager {
final FileTemplateBase _template = addTemplate(template.getName(), template.getExtension());
_template.setText(template.getText());
_template.setReformatCode(template.isReformatCode());
_template.setLiveTemplateEnabled(template.isLiveTemplateEnabled());
}
}
@@ -39,7 +39,7 @@ public abstract class FileTemplateBase implements FileTemplate {
@Nullable
private String myText;
private boolean myShouldReformatCode = DEFAULT_REFORMAT_CODE_VALUE;
private boolean myHasEmbeddedLiveTemplate;
private boolean myLiveTemplateEnabled;
@Override
public final boolean isReformatCode() {
@@ -116,12 +116,12 @@ public abstract class FileTemplateBase implements FileTemplate {
}
@Override
public boolean isHasEmbeddedLiveTemplate() {
return myHasEmbeddedLiveTemplate;
public boolean isLiveTemplateEnabled() {
return myLiveTemplateEnabled;
}
@Override
public void setHasEmbeddedLiveTemplate(boolean hasEmbeddedLiveTemplate) {
myHasEmbeddedLiveTemplate = hasEmbeddedLiveTemplate;
public void setLiveTemplateEnabled(boolean value) {
myLiveTemplateEnabled = value;
}
}
@@ -46,19 +46,20 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.testFramework.LightVirtualFile;
import com.intellij.ui.HyperlinkLabel;
import com.intellij.ui.BrowserHyperlinkListener;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.SeparatorFactory;
import com.intellij.ui.components.panels.HorizontalLayout;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import com.intellij.xml.util.XmlStringUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -83,7 +84,6 @@ import java.util.List;
public class FileTemplateConfigurable implements Configurable, Configurable.NoScroll {
private static final Logger LOG = Logger.getInstance("#com.intellij.ide.fileTemplates.impl.FileTemplateConfigurable");
@NonNls private static final String EMPTY_HTML = "<html></html>";
@NonNls private static final String CONTENT_TYPE_PLAIN = "text/plain";
private JPanel myMainPanel;
private FileTemplate myTemplate;
@@ -92,6 +92,7 @@ public class FileTemplateConfigurable implements Configurable, Configurable.NoSc
private JTextField myNameField;
private JTextField myExtensionField;
private JCheckBox myAdjustBox;
private JCheckBox myLiveTemplateBox;
private JPanel myTopPanel;
private JEditorPane myDescriptionComponent;
private boolean myModified = false;
@@ -101,8 +102,7 @@ public class FileTemplateConfigurable implements Configurable, Configurable.NoSc
private final List<ChangeListener> myChangeListeners = ContainerUtil.createLockFreeCopyOnWriteList();
private Splitter mySplitter;
private final FileType myVelocityFileType = FileTypeManager.getInstance().getFileTypeByExtension("ft");
private JPanel myDescriptionPanel;
private float myProportion = 0.5f;
private float myProportion = 0.6f;
public FileTemplateConfigurable(Project project) {
myProject = project;
@@ -164,33 +164,31 @@ public class FileTemplateConfigurable implements Configurable, Configurable.NoSc
myExtensionField = new JTextField();
mySplitter = new Splitter(true, myProportion);
myAdjustBox = new JCheckBox(IdeBundle.message("checkbox.reformat.according.to.style"));
myLiveTemplateBox = new JCheckBox(IdeBundle.message("checkbox.enable.live.templates"));
myTemplateEditor = createEditor();
myDescriptionComponent = new JEditorPane(UIUtil.HTML_MIME, EMPTY_HTML);
myDescriptionComponent.setEditable(false);
myDescriptionComponent.addHyperlinkListener(new BrowserHyperlinkListener());
myTopPanel = new JPanel(new GridBagLayout());
myDescriptionPanel = new JPanel(new GridBagLayout());
myDescriptionPanel.add(SeparatorFactory.createSeparator(IdeBundle.message("label.description"), null),
new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
JPanel descriptionPanel = new JPanel(new GridBagLayout());
descriptionPanel.add(SeparatorFactory.createSeparator(IdeBundle.message("label.description"), null),
new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 2, 0), 0, 0));
myDescriptionPanel.add(ScrollPaneFactory.createScrollPane(myDescriptionComponent),
new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
descriptionPanel.add(ScrollPaneFactory.createScrollPane(myDescriptionComponent),
new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(2, 0, 0, 0), 0, 0));
myMainPanel.add(myTopPanel,
new GridBagConstraints(0, 0, 4, 1, 1.0, 0.0, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
//myMainPanel.add(myAdjustBox,
// new GridBagConstraints(0, 1, 4, 1, 0.0, 0.0, GridBagConstraints.WEST,
// GridBagConstraints.HORIZONTAL, new Insets(2, 0, 2, 0), 0, 0));
myMainPanel.add(mySplitter,
new GridBagConstraints(0, 2, 4, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
mySplitter.setSecondComponent(myDescriptionPanel);
mySplitter.setSecondComponent(descriptionPanel);
setShowInternalMessage(null);
myNameField.addFocusListener(new FocusAdapter() {
@@ -241,13 +239,9 @@ public class FileTemplateConfigurable implements Configurable, Configurable.NoSc
((EditorEx)editor).setHighlighter(createHighlighter());
JPanel topPanel = new JPanel(new BorderLayout());
HyperlinkLabel hyperlinkLabel = new HyperlinkLabel();
hyperlinkLabel.setHyperlinkText("", "Apache Velocity", " template language is used");
hyperlinkLabel.setHyperlinkTarget(
"http://velocity.apache.org/engine/devel/user-guide.html#Velocity_Template_Language_VTL:_An_Introduction");
JPanel southPanel = new JPanel(new BorderLayout());
southPanel.add(myAdjustBox, BorderLayout.WEST);
southPanel.add(hyperlinkLabel, BorderLayout.EAST);
JPanel southPanel = new JPanel(new HorizontalLayout(40));
southPanel.add(myAdjustBox);
southPanel.add(myLiveTemplateBox);
topPanel.add(southPanel, BorderLayout.SOUTH);
topPanel.add(editor.getComponent(), BorderLayout.CENTER);
@@ -263,10 +257,6 @@ public class FileTemplateConfigurable implements Configurable, Configurable.NoSc
return myNameField.getText();
}
public String getExtensionValue() {
return myExtensionField.getText();
}
private void onNameChanged() {
ChangeEvent event = new ChangeEvent(this);
for (ChangeListener changeListener : myChangeListeners) {
@@ -298,7 +288,7 @@ public class FileTemplateConfigurable implements Configurable, Configurable.NoSc
return true;
}
if (myTemplate != null) {
if (myTemplate.isReformatCode() != myAdjustBox.isSelected()) {
if (myTemplate.isReformatCode() != myAdjustBox.isSelected() || myTemplate.isLiveTemplateEnabled() != myLiveTemplateBox.isSelected()) {
return true;
}
}
@@ -322,6 +312,7 @@ public class FileTemplateConfigurable implements Configurable, Configurable.NoSc
myTemplate.setName(name);
myTemplate.setExtension(extension);
myTemplate.setReformatCode(myAdjustBox.isSelected());
myTemplate.setLiveTemplateEnabled(myLiveTemplateBox.isSelected());
}
myModified = false;
}
@@ -355,22 +346,23 @@ public class FileTemplateConfigurable implements Configurable, Configurable.NoSc
myFile = createFile(text, name);
myTemplateEditor = createEditor();
boolean adjust = (myTemplate != null) && myTemplate.isReformatCode();
myNameField.setText(name);
myExtensionField.setText(extension);
myAdjustBox.setSelected(adjust);
String desc = description.length() > 0 ? description : EMPTY_HTML;
myAdjustBox.setSelected(myTemplate != null && myTemplate.isReformatCode());
myLiveTemplateBox.setSelected(myTemplate != null && myTemplate.isLiveTemplateEnabled());
// [myakovlev] do not delete these stupid lines! Or you get Exception!
myDescriptionComponent.setContentType(CONTENT_TYPE_PLAIN);
myDescriptionComponent.setEditable(true);
myDescriptionComponent.setText(desc);
myDescriptionComponent.setContentType(UIUtil.HTML_MIME);
myDescriptionComponent.setText(desc);
int i = description.indexOf("<html>");
if (i > 0) {
description = description.substring(i);
}
description = XmlStringUtil.stripHtml(description);
description = description.replace("\n", "").replace("\r", "");
description = XmlStringUtil.stripHtml(description);
description = description + "<hr> <font face=\"verdana\" size=\"-1\"><a href='http://velocity.apache.org/engine/devel/user-guide.html#Velocity_Template_Language_VTL:_An_Introduction'>\n" +
"Apache Velocity</a> template language is used</font>";
myDescriptionComponent.setText(description);
myDescriptionComponent.setCaretPosition(0);
myDescriptionComponent.setEditable(false);
myDescriptionPanel.setVisible(StringUtil.isNotEmpty(description));
myNameField.setEditable((myTemplate != null) && (!myTemplate.isDefault()));
myExtensionField.setEditable((myTemplate != null) && (!myTemplate.isDefault()));
@@ -133,7 +133,8 @@ label.name=Name:
label.extension=Extension:
title.file.templates=File and Code Templates
title.edit.file.template=Edit File Template
checkbox.reformat.according.to.style=Reformat according to style
checkbox.reformat.according.to.style=&Reformat according to style
checkbox.enable.live.templates=Enable &Live Templates
label.description=Description
item.file.templates=File templates
tab.filetemplates.templates=Files