PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/macros/Properties/Insert_Buffer_Properties.bsh

#
Unknown | 226 lines | 201 code | 25 blank | 0 comment | 0 complexity | 5bd920df5c349a08c11df13279a89872 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * Insert_Buffer_Properties.bsh - a Beanshell macro
  3. * for the jEdit text editor that provides a gui for
  4. * inserting Buffer Local properties for the current buffer
  5. * into the current buffer. If the buffer's mode as a line
  6. * comment defined, or comment start and end properties then
  7. * the inserted properties will be commented out.
  8. *
  9. * Copyright (C) 2002, 2003 Ollie Rutherfurd <oliver@rutherfurd.net>
  10. *
  11. * BugFixed by Björn "Vampire" Kautler <Vampire0@gmx.net>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. *
  27. * $Id: Insert_Buffer_Properties.bsh 5265 2005-08-14 22:31:16Z spestov $
  28. */
  29. import java.awt.BorderLayout;
  30. import java.awt.event.ActionEvent;
  31. import java.util.Hashtable;
  32. import java.util.StringTokenizer;
  33. import java.util.Vector;
  34. import javax.swing.Box;
  35. import javax.swing.BoxLayout;
  36. import javax.swing.JButton;
  37. import javax.swing.JDialog;
  38. import javax.swing.JLabel;
  39. import javax.swing.JPanel;
  40. import javax.swing.JScrollPane;
  41. import javax.swing.border.EmptyBorder;
  42. import org.gjt.sp.jedit.gui.JCheckBoxList;
  43. BufferLocalPropertiesDialog(View view){
  44. this.view = view;
  45. buffer = view.getTextArea().getBuffer();
  46. mode = buffer.getMode().name;
  47. // removed non-valid BLPs and added missing ones
  48. props = new Hashtable();
  49. props.put("mode","");
  50. props.put("indentSize","int");
  51. props.put("tabSize","int");
  52. props.put("noTabs","bool");
  53. props.put("wrap","str");
  54. props.put("maxLineLen","int");
  55. props.put("folding","str");
  56. props.put("collapseFolds","int");
  57. props.put("deepIndent","bool");
  58. props.put("noWordSep","str");
  59. props.put("wordBreakChars","str");
  60. _checked = jEdit.getProperty("macro.insert-buffer-properties." + mode,"");
  61. tokens = new StringTokenizer(_checked,",");
  62. checkedProps = new Hashtable();
  63. while(tokens.hasMoreTokens())
  64. {
  65. property = tokens.nextToken();
  66. checkedProps.put(property,"checked");
  67. }
  68. dialog = new JDialog(view,"Insert Buffer Local Properties",true);
  69. content = new JPanel(new BorderLayout());
  70. content.setBorder(new EmptyBorder(10,10,10,10));
  71. dialog.setContentPane(content);
  72. content.add(new JLabel("Properties:"), BorderLayout.NORTH);
  73. _entries = new Vector();
  74. names = props.keys();
  75. while(names.hasMoreElements()){
  76. name = names.nextElement();
  77. checked = checkedProps.get(name);
  78. entry = new JCheckBoxList.Entry(checked != null,name);
  79. _entries.addElement(entry);
  80. }
  81. entries = new JCheckBoxList.Entry[_entries.size()];
  82. _entries.copyInto(entries);
  83. checkBox = new JCheckBoxList(entries);
  84. checkBox.addKeyListener(this);
  85. content.add(new JScrollPane(checkBox),
  86. BorderLayout.CENTER);
  87. buttons = new JPanel();
  88. buttons.setBorder(new EmptyBorder(12,50,0,50));
  89. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  90. buttons.add(Box.createGlue());
  91. insert = new JButton("Insert");
  92. cancel = new JButton("Cancel");
  93. insert.addActionListener(this);
  94. cancel.addActionListener(this);
  95. dialog.getRootPane().setDefaultButton(insert);
  96. buttons.add(insert);
  97. buttons.add(Box.createHorizontalStrut(6));
  98. buttons.add(cancel);
  99. buttons.add(Box.createGlue());
  100. content.add(buttons,BorderLayout.SOUTH);
  101. void actionPerformed(ActionEvent evt){
  102. if(evt.getSource() == cancel)
  103. dialog.dispose();
  104. else
  105. this.insertProperties();
  106. }
  107. keyPressed(KeyEvent evt){
  108. if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
  109. dialog.dispose();
  110. else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
  111. this.insertProperties();
  112. }
  113. keyReleased(KeyEvent evt){;}
  114. keyTyped(KeyEvent evt){;}
  115. insertProperties(){
  116. // removed isReadOnly-Check because already done in the beginning of the script
  117. checkNextTime = new StringBuffer();
  118. buff = new StringBuffer();
  119. names = checkBox.getCheckedValues();
  120. for(i=0; i < names.length; i++)
  121. {
  122. // moved the assignation in front of the checkNextTime-build,
  123. // because "name" is used and will have a wrong value otherwise
  124. name = names[i];
  125. type = props.get(name);
  126. if(i > 0)
  127. checkNextTime.append(',');
  128. checkNextTime.append(name);
  129. // changed the value reading/building/escaping
  130. if(name.equals("mode"))
  131. value = mode;
  132. else if(type.equals("bool"))
  133. value = buffer.getBooleanProperty(name);
  134. else if(type.equals("str"))
  135. {
  136. value = buffer.getStringProperty(name);
  137. if(value == null)
  138. value = "";
  139. value = value.replaceAll("=","\\\\=").replaceAll(":","\\\\:").replaceAll("\n","\\\\n").replaceAll("\t","\\\\t");
  140. }
  141. else if(type.equals("int"))
  142. {
  143. value = buffer.getIntegerProperty(name,-1);
  144. if(value == -1)
  145. value = "";
  146. }
  147. else
  148. value = "";
  149. buff.append(':').append(name).append('=').append(value);
  150. }
  151. jEdit.setProperty("macro.insert-buffer-properties." + mode,
  152. checkNextTime.toString());
  153. if(buff.length() > 0)
  154. buff.append(':');
  155. properties = buff.toString();
  156. // try to comment out the properties first using a lineComment
  157. // and if that's not defined, look for comment start and end
  158. // properties -- use context senstive properties
  159. caret = view.getTextArea().getCaretPosition();
  160. comment = buffer.getContextSensitiveProperty(caret,"lineComment");
  161. if(comment != null && comment.length() > 0)
  162. properties = comment + " " + properties;
  163. else
  164. {
  165. commentStart = buffer.getContextSensitiveProperty(caret,"commentStart");
  166. commentEnd = buffer.getContextSensitiveProperty(caret,"commentEnd");
  167. if(commentStart != null && commentEnd != null)
  168. properties = commentStart + " " + properties + " " + commentEnd;
  169. }
  170. buffer.insert(caret,properties);
  171. line = view.getTextArea().getCaretLine();
  172. if(line >= 10 && line < (buffer.getLineCount()-10))
  173. Macros.message(view, "Note: Buffer Local properties must in the first or last 10 lines of a buffer to be recognized by jEdit.");
  174. dialog.dispose();
  175. }
  176. dialog.pack();
  177. // cosmetic size-correction
  178. dialog.setSize(250,290);
  179. dialog.setLocationRelativeTo(view);
  180. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  181. dialog.setVisible(true);
  182. }
  183. if(buffer.isReadOnly())
  184. Macros.error(view, "Buffer is read-only.");
  185. else
  186. BufferLocalPropertiesDialog(view);
  187. /*
  188. Macro index data (in DocBook format)
  189. <listitem>
  190. <para><filename>Insert_Buffer_Properties.bsh</filename></para>
  191. <abstract><para>
  192. Inserts buffer-local properties into the current buffer.
  193. </para></abstract>
  194. <para>
  195. If the buffer's
  196. mode has a line comment defined, or comment start and end
  197. defined, the inserted properties will be commented out.
  198. </para>
  199. </listitem>
  200. */
  201. // :wrap=none:noTabs=false:collapseFolds=0:maxLineLen=80:mode=beanshell:indentSize=8:deepIndent=false:folding=none: