update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / application / options / CodeStyleHtmlPanel.java
blobb9a1e96c95cb297951ab5adf0198ef691188777a
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.application.options;
18 import com.intellij.ide.highlighter.XmlHighlighterFactory;
19 import com.intellij.openapi.application.ApplicationBundle;
20 import com.intellij.openapi.editor.colors.EditorColorsScheme;
21 import com.intellij.openapi.editor.highlighter.EditorHighlighter;
22 import com.intellij.openapi.fileTypes.FileType;
23 import com.intellij.openapi.fileTypes.StdFileTypes;
24 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
25 import com.intellij.openapi.util.Comparing;
26 import com.intellij.openapi.util.text.StringUtil;
27 import com.intellij.psi.PsiFile;
28 import com.intellij.psi.codeStyle.CodeStyleSettings;
29 import com.intellij.util.ArrayUtil;
30 import com.intellij.util.Icons;
31 import org.jetbrains.annotations.NotNull;
33 import javax.swing.*;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.util.ArrayList;
37 import java.util.Arrays;
39 public class CodeStyleHtmlPanel extends CodeStyleAbstractPanel {
41 private JTextField myKeepBlankLines;
42 private JComboBox myWrapAttributes;
43 private JCheckBox myAlignAttributes;
44 private JCheckBox myKeepWhiteSpaces;
46 private JPanel myPanel;
47 private JPanel myPreviewPanel;
49 private JCheckBox mySpacesAroundEquality;
50 private JCheckBox mySpacesAroundTagName;
51 private JCheckBox myAlignText;
52 private TextFieldWithBrowseButton myInsertNewLineTagNames;
53 private TextFieldWithBrowseButton myRemoveNewLineTagNames;
54 private TextFieldWithBrowseButton myDoNotAlignChildrenTagNames;
55 private TextFieldWithBrowseButton myKeepWhiteSpacesTagNames;
56 private TextFieldWithBrowseButton myInlineElementsTagNames;
57 private JTextField myDoNotAlignChildrenMinSize;
58 private JCheckBox myShouldKeepBlankLines;
59 private JCheckBox mySpaceInEmptyTag;
60 private JCheckBox myWrapText;
61 private JCheckBox myShouldKeepLineBreaksInText;
62 private TextFieldWithBrowseButton myDontBreakIfInlineContent;
64 public CodeStyleHtmlPanel(CodeStyleSettings settings) {
65 super(settings);
66 installPreviewPanel(myPreviewPanel);
68 fillWrappingCombo(myWrapAttributes);
70 customizeField(ApplicationBundle.message("title.insert.new.line.before.tags"), myInsertNewLineTagNames);
71 customizeField(ApplicationBundle.message("title.remove.line.breaks.before.tags"), myRemoveNewLineTagNames);
72 customizeField(ApplicationBundle.message("title.do.not.indent.children.of"), myDoNotAlignChildrenTagNames);
73 customizeField(ApplicationBundle.message("title.inline.elements"), myInlineElementsTagNames);
74 customizeField(ApplicationBundle.message("title.keep.whitespaces.inside"), myKeepWhiteSpacesTagNames);
75 customizeField(ApplicationBundle.message("title.dont.wrap.if.inline.content"), myDontBreakIfInlineContent);
77 myInsertNewLineTagNames.getTextField().setColumns(5);
78 myRemoveNewLineTagNames.getTextField().setColumns(5);
79 myDoNotAlignChildrenTagNames.getTextField().setColumns(5);
80 myKeepWhiteSpacesTagNames.getTextField().setColumns(5);
81 myInlineElementsTagNames.getTextField().setColumns(5);
82 myDontBreakIfInlineContent.getTextField().setColumns(5);
85 addPanelToWatch(myPanel);
88 protected EditorHighlighter createHighlighter(final EditorColorsScheme scheme) {
89 return XmlHighlighterFactory.createXMLHighlighter(scheme);
92 private static void customizeField(final String title, final TextFieldWithBrowseButton uiField) {
93 uiField.getTextField().setEditable(false);
94 uiField.setButtonIcon(Icons.OPEN_EDIT_DIALOG_ICON);
95 uiField.addActionListener(new ActionListener() {
96 public void actionPerformed(ActionEvent e) {
97 final TagListDialog tagListDialog = new TagListDialog(title);
98 tagListDialog.setData(createCollectionOn(uiField.getText()));
99 tagListDialog.show();
100 if (tagListDialog.isOK()) {
101 uiField.setText(createStringOn(tagListDialog.getData()));
105 private String createStringOn(final ArrayList<String> data) {
106 return StringUtil.join(ArrayUtil.toStringArray(data), ",");
109 private ArrayList<String> createCollectionOn(final String data) {
110 if (data == null) {
111 return new ArrayList<String>();
113 return new ArrayList<String>(Arrays.asList(data.split(",")));
119 protected int getRightMargin() {
120 return 60;
123 public void apply(CodeStyleSettings settings) {
124 settings.HTML_KEEP_BLANK_LINES = getIntValue(myKeepBlankLines);
125 settings.HTML_ATTRIBUTE_WRAP = ourWrappings[myWrapAttributes.getSelectedIndex()];
126 settings.HTML_TEXT_WRAP = myWrapText.isSelected() ? CodeStyleSettings.WRAP_AS_NEEDED : CodeStyleSettings.DO_NOT_WRAP;
127 settings.HTML_SPACE_INSIDE_EMPTY_TAG = mySpaceInEmptyTag.isSelected();
128 settings.HTML_ALIGN_ATTRIBUTES = myAlignAttributes.isSelected();
129 settings.HTML_ALIGN_TEXT = myAlignText.isSelected();
130 settings.HTML_KEEP_WHITESPACES = myKeepWhiteSpaces.isSelected();
131 settings.HTML_SPACE_AROUND_EQUALITY_IN_ATTRINUTE = mySpacesAroundEquality.isSelected();
132 settings.HTML_SPACE_AFTER_TAG_NAME = mySpacesAroundTagName.isSelected();
134 settings.HTML_ELEMENTS_TO_INSERT_NEW_LINE_BEFORE = myInsertNewLineTagNames.getText();
135 settings.HTML_ELEMENTS_TO_REMOVE_NEW_LINE_BEFORE = myRemoveNewLineTagNames.getText();
136 settings.HTML_DO_NOT_INDENT_CHILDREN_OF = myDoNotAlignChildrenTagNames.getText();
137 settings.HTML_DO_NOT_ALIGN_CHILDREN_OF_MIN_LINES = getIntValue(myDoNotAlignChildrenMinSize);
138 settings.HTML_INLINE_ELEMENTS = myInlineElementsTagNames.getText();
139 settings.HTML_DONT_ADD_BREAKS_IF_INLINE_CONTENT = myDontBreakIfInlineContent.getText();
140 settings.HTML_KEEP_WHITESPACES_INSIDE = myKeepWhiteSpacesTagNames.getText();
141 settings.HTML_KEEP_LINE_BREAKS = myShouldKeepBlankLines.isSelected();
142 settings.HTML_KEEP_LINE_BREAKS_IN_TEXT = myShouldKeepLineBreaksInText.isSelected();
145 private static int getIntValue(JTextField keepBlankLines) {
146 try {
147 return Integer.parseInt(keepBlankLines.getText());
149 catch (NumberFormatException e) {
150 return 0;
154 protected void resetImpl(final CodeStyleSettings settings) {
155 myKeepBlankLines.setText(String.valueOf(settings.HTML_KEEP_BLANK_LINES));
156 myWrapAttributes.setSelectedIndex(getIndexForWrapping(settings.HTML_ATTRIBUTE_WRAP));
157 myWrapText.setSelected(settings.HTML_TEXT_WRAP != CodeStyleSettings.DO_NOT_WRAP);
158 mySpaceInEmptyTag.setSelected(settings.HTML_SPACE_INSIDE_EMPTY_TAG);
159 myAlignAttributes.setSelected(settings.HTML_ALIGN_ATTRIBUTES);
160 myAlignText.setSelected(settings.HTML_ALIGN_TEXT);
161 myKeepWhiteSpaces.setSelected(settings.HTML_KEEP_WHITESPACES);
162 mySpacesAroundTagName.setSelected(settings.HTML_SPACE_AFTER_TAG_NAME);
163 mySpacesAroundEquality.setSelected(settings.HTML_SPACE_AROUND_EQUALITY_IN_ATTRINUTE);
164 myShouldKeepBlankLines.setSelected(settings.HTML_KEEP_LINE_BREAKS);
165 myShouldKeepLineBreaksInText.setSelected(settings.HTML_KEEP_LINE_BREAKS_IN_TEXT);
167 myInsertNewLineTagNames.setText(settings.HTML_ELEMENTS_TO_INSERT_NEW_LINE_BEFORE);
168 myRemoveNewLineTagNames.setText(settings.HTML_ELEMENTS_TO_REMOVE_NEW_LINE_BEFORE);
169 myDoNotAlignChildrenTagNames.setText(settings.HTML_DO_NOT_INDENT_CHILDREN_OF);
170 myDoNotAlignChildrenMinSize.setText(String.valueOf(settings.HTML_DO_NOT_ALIGN_CHILDREN_OF_MIN_LINES));
171 myInlineElementsTagNames.setText(settings.HTML_INLINE_ELEMENTS);
172 myDontBreakIfInlineContent.setText(settings.HTML_DONT_ADD_BREAKS_IF_INLINE_CONTENT);
173 myKeepWhiteSpacesTagNames.setText(settings.HTML_KEEP_WHITESPACES_INSIDE);
176 public boolean isModified(CodeStyleSettings settings) {
177 if (settings.HTML_KEEP_BLANK_LINES != getIntValue(myKeepBlankLines)) {
178 return true;
180 if (settings.HTML_ATTRIBUTE_WRAP != ourWrappings[myWrapAttributes.getSelectedIndex()]) {
181 return true;
184 if ((settings.HTML_TEXT_WRAP == CodeStyleSettings.WRAP_AS_NEEDED) != myWrapText.isSelected()) {
185 return true;
188 if (settings.HTML_SPACE_INSIDE_EMPTY_TAG != mySpaceInEmptyTag.isSelected()) {
189 return true;
192 if (settings.HTML_ALIGN_ATTRIBUTES != myAlignAttributes.isSelected()) {
193 return true;
196 if (settings.HTML_ALIGN_TEXT != myAlignText.isSelected()) {
197 return true;
200 if (settings.HTML_KEEP_WHITESPACES != myKeepWhiteSpaces.isSelected()) {
201 return true;
204 if (settings.HTML_SPACE_AROUND_EQUALITY_IN_ATTRINUTE != mySpacesAroundEquality.isSelected()) {
205 return true;
208 if (settings.HTML_SPACE_AFTER_TAG_NAME != mySpacesAroundTagName.isSelected()) {
209 return true;
212 if (!Comparing.equal(settings.HTML_ELEMENTS_TO_INSERT_NEW_LINE_BEFORE, myInsertNewLineTagNames.getText().trim())) {
213 return true;
216 if (!Comparing.equal(settings.HTML_ELEMENTS_TO_REMOVE_NEW_LINE_BEFORE, myRemoveNewLineTagNames.getText().trim())) {
217 return true;
220 if (!Comparing.equal(settings.HTML_DO_NOT_INDENT_CHILDREN_OF, myDoNotAlignChildrenTagNames.getText().trim())) {
221 return true;
224 if (settings.HTML_DO_NOT_ALIGN_CHILDREN_OF_MIN_LINES != getIntValue(myDoNotAlignChildrenMinSize)) {
225 return true;
228 if (!Comparing.equal(settings.HTML_INLINE_ELEMENTS, myInlineElementsTagNames.getText().trim())) return true;
229 if (!Comparing.equal(settings.HTML_DONT_ADD_BREAKS_IF_INLINE_CONTENT, myDontBreakIfInlineContent.getText().trim())) return true;
231 if (!Comparing.equal(settings.HTML_KEEP_WHITESPACES_INSIDE, myKeepWhiteSpacesTagNames.getText().trim())) {
232 return true;
235 if (myShouldKeepBlankLines.isSelected() != settings.HTML_KEEP_LINE_BREAKS) {
236 return true;
239 if (myShouldKeepLineBreaksInText.isSelected() != settings.HTML_KEEP_LINE_BREAKS_IN_TEXT) {
240 return true;
243 return false;
246 public JComponent getPanel() {
247 return myPanel;
250 protected String getPreviewText() {
251 return readFromFile(this.getClass(), "preview.html.template");
255 @NotNull
256 protected FileType getFileType() {
257 return StdFileTypes.HTML;
260 protected void prepareForReformat(final PsiFile psiFile) {
261 //psiFile.putUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY, LanguageLevel.HIGHEST);