/xml/relaxng/src/org/intellij/plugins/relaxNG/convert/AdvancedDtdOptions.java

https://bitbucket.org/nbargnesi/idea · Java · 258 lines · 200 code · 38 blank · 20 comment · 38 complexity · bc49813e1cc0820530e75df309c7abf3 MD5 · raw file

  1. /*
  2. * Copyright 2007 Sascha Weinreuter
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. package org.intellij.plugins.relaxNG.convert;
  17. import com.intellij.icons.AllIcons;
  18. import com.intellij.openapi.actionSystem.*;
  19. import com.intellij.openapi.project.Project;
  20. import com.intellij.openapi.vfs.VirtualFile;
  21. import com.intellij.psi.PsiElement;
  22. import com.intellij.psi.PsiFile;
  23. import com.intellij.psi.PsiManager;
  24. import com.intellij.psi.PsiRecursiveElementVisitor;
  25. import com.intellij.psi.xml.XmlAttributeDecl;
  26. import com.intellij.psi.xml.XmlElementDecl;
  27. import org.jetbrains.annotations.NonNls;
  28. import javax.swing.*;
  29. import javax.swing.table.AbstractTableModel;
  30. import java.awt.event.ItemEvent;
  31. import java.awt.event.ItemListener;
  32. import java.util.*;
  33. /**
  34. * Created by IntelliJ IDEA.
  35. * User: sweinreuter
  36. * Date: 18.11.2007
  37. */
  38. public class AdvancedDtdOptions implements AdvancedOptions {
  39. @NonNls
  40. private static final String COLON_REPLACEMENT = "colon-replacement";
  41. @NonNls
  42. private static final String ELEMENT_DEFINE = "element-define";
  43. @NonNls
  44. private static final String ATTLIST_DEFINE = "attlist-define";
  45. @NonNls
  46. private static final String INLINE_ATTLIST = "inline-attlist";
  47. @NonNls
  48. private static final String ANY_NAME = "any-name";
  49. @NonNls
  50. private static final String STRICT_ANY = "strict-any";
  51. @NonNls
  52. private static final String ANNOTATION_PREFIX = "annotation-prefix";
  53. @NonNls
  54. private static final String GENERATE_START = "generate-start";
  55. @NonNls
  56. private static final String XMLNS = "xmlns";
  57. private JComponent myRoot;
  58. private JCheckBox myInlineAttlistCheckBox;
  59. private JTextField myColonReplacement;
  60. private JTextField myElementDefine;
  61. private JTextField myAttlistDefine;
  62. private JTextField myAnyName;
  63. private JCheckBox myStrictAnyCheckBox;
  64. private JTextField myAnnotationPrefix;
  65. private JCheckBox myGenerateStartCheckBox;
  66. private JTextField myDefaultNS;
  67. private JTable myNamespaceMap;
  68. private JPanel myToolbar;
  69. public AdvancedDtdOptions() {
  70. myInlineAttlistCheckBox.addItemListener(new ItemListener() {
  71. public void itemStateChanged(ItemEvent e) {
  72. if (e.getStateChange() == ItemEvent.SELECTED) {
  73. myAttlistDefine.setEnabled(false);
  74. } else {
  75. myAttlistDefine.setEnabled(true);
  76. }
  77. }
  78. });
  79. myNamespaceMap.setModel(new NamespaceMapModel());
  80. myNamespaceMap.getColumnModel().getColumn(0).setMaxWidth((int)(new JLabel("Prefix").getPreferredSize().width * 1.2));
  81. final DefaultActionGroup group = new DefaultActionGroup();
  82. group.add(new AnAction(null, "Remove Entry", AllIcons.General.Remove) {
  83. public void update(AnActionEvent e) {
  84. if (myNamespaceMap.getModel().getRowCount() == 0 || myNamespaceMap.getSelectedRow() == -1) {
  85. e.getPresentation().setEnabled(false);
  86. } else {
  87. e.getPresentation().setEnabled(true);
  88. }
  89. }
  90. public void actionPerformed(AnActionEvent e) {
  91. ((NamespaceMapModel)myNamespaceMap.getModel()).removeRow(myNamespaceMap.getSelectedRow());
  92. }
  93. });
  94. final ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, group, false);
  95. myToolbar.add(toolbar.getComponent());
  96. }
  97. public JComponent getRoot() {
  98. return myRoot;
  99. }
  100. public Map<String, ?> getOptions() {
  101. final HashMap<String, Object> map = new LinkedHashMap<String, Object>();
  102. map.put(INLINE_ATTLIST, myInlineAttlistCheckBox.isSelected());
  103. setText(map, COLON_REPLACEMENT, myColonReplacement);
  104. setText(map, ELEMENT_DEFINE, myElementDefine);
  105. setText(map, ATTLIST_DEFINE, myAttlistDefine);
  106. setText(map, ANY_NAME, myAnyName);
  107. if (myStrictAnyCheckBox.isSelected()) {
  108. map.put(STRICT_ANY, Boolean.TRUE);
  109. }
  110. setText(map, ANNOTATION_PREFIX, myAnnotationPrefix);
  111. map.put(GENERATE_START, myGenerateStartCheckBox.isSelected());
  112. if (myDefaultNS.getText().trim().length() > 0) {
  113. map.put(XMLNS, myDefaultNS.getText() );
  114. }
  115. final List<String[]> data = ((NamespaceMapModel)myNamespaceMap.getModel()).getData();
  116. for (String[] parts : data) {
  117. map.put(XMLNS + ":" + parts[0], parts[1]);
  118. }
  119. return map;
  120. }
  121. private static void setText(HashMap<String, Object> map, String option, JTextField field) {
  122. final String colonReplacement = field.getText();
  123. if (colonReplacement != null && colonReplacement.trim().length() > 0) {
  124. map.put(option, colonReplacement);
  125. }
  126. }
  127. public void setOptions(Map<String, ?> inputOptions) {
  128. if (inputOptions.containsKey(COLON_REPLACEMENT)) {
  129. myColonReplacement.setText((String)inputOptions.get(COLON_REPLACEMENT));
  130. }
  131. myInlineAttlistCheckBox.setSelected(inputOptions.get(INLINE_ATTLIST) == Boolean.TRUE);
  132. if (inputOptions.containsKey(ELEMENT_DEFINE)) {
  133. myElementDefine.setText((String)inputOptions.get(ELEMENT_DEFINE));
  134. }
  135. if (inputOptions.containsKey(ATTLIST_DEFINE)) {
  136. myAttlistDefine.setText((String)inputOptions.get(ATTLIST_DEFINE));
  137. }
  138. if (inputOptions.containsKey(ANY_NAME)) {
  139. myAnyName.setText((String)inputOptions.get(ANY_NAME));
  140. }
  141. myStrictAnyCheckBox.setSelected(inputOptions.get(STRICT_ANY) == Boolean.TRUE);
  142. if (inputOptions.containsKey(ANNOTATION_PREFIX)) {
  143. myAnnotationPrefix.setText((String)inputOptions.get(ANNOTATION_PREFIX));
  144. }
  145. myGenerateStartCheckBox.setSelected(inputOptions.get(GENERATE_START) == Boolean.TRUE);
  146. if (inputOptions.containsKey(XMLNS)) {
  147. myDefaultNS.setText((String)inputOptions.get(XMLNS));
  148. }
  149. final NamespaceMapModel model = (NamespaceMapModel)myNamespaceMap.getModel();
  150. final Set<String> set = inputOptions.keySet();
  151. final String prefix = XMLNS + ":";
  152. for (String s : set) {
  153. if (s.startsWith(prefix)) {
  154. model.addMapping(s.substring(prefix.length()), (String)inputOptions.get(s));
  155. }
  156. }
  157. }
  158. public static Map<String, ?> prepareNamespaceMap(Project project, VirtualFile firstFile) {
  159. final PsiFile file = PsiManager.getInstance(project).findFile(firstFile);
  160. if (file == null) {
  161. return Collections.emptyMap();
  162. }
  163. final HashMap<String, Object> map = new LinkedHashMap<String, Object>();
  164. file.accept(new PsiRecursiveElementVisitor() {
  165. public void visitElement(PsiElement element) {
  166. if (element instanceof XmlElementDecl) {
  167. final String s = ((XmlElementDecl)element).getName();
  168. if (s != null) {
  169. final String[] parts = s.split(":");
  170. if (parts.length > 1) {
  171. map.put(XMLNS + ":" + parts[0], null);
  172. }
  173. }
  174. } else if (element instanceof XmlAttributeDecl) {
  175. final String s = ((XmlAttributeDecl)element).getName();
  176. if (s != null) {
  177. final String[] parts = s.split(":");
  178. if (parts.length > 1) {
  179. map.put(XMLNS + ":" + parts[0], null);
  180. }
  181. }
  182. }
  183. super.visitElement(element);
  184. }
  185. });
  186. return map;
  187. }
  188. private static class NamespaceMapModel extends AbstractTableModel {
  189. private final List<String[]> myList = new ArrayList<String[]>();
  190. public String getColumnName(int column) {
  191. return column == 0 ? "Prefix" : "URI";
  192. }
  193. public int getRowCount() {
  194. return myList.size();
  195. }
  196. public int getColumnCount() {
  197. return 2;
  198. }
  199. public boolean isCellEditable(int rowIndex, int columnIndex) {
  200. return columnIndex == 1;
  201. }
  202. public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
  203. myList.get(rowIndex)[columnIndex] = (String)aValue;
  204. }
  205. public Object getValueAt(int rowIndex, int columnIndex) {
  206. return myList.get(rowIndex)[columnIndex];
  207. }
  208. public void addMapping(String prefix, String uri) {
  209. myList.add(new String[]{ prefix, uri });
  210. fireTableRowsInserted(myList.size() - 1, myList.size() - 1);
  211. }
  212. public void removeRow(int row) {
  213. myList.remove(row);
  214. fireTableRowsDeleted(row - 1, row - 1);
  215. }
  216. public List<String[]> getData() {
  217. return myList;
  218. }
  219. }
  220. }