PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/macros/Properties/Insert_Buffer_Properties.bsh

#
Unknown | 250 lines | 223 code | 27 blank | 0 comment | 0 complexity | 35f660d9307e4696566e262525c8b2af 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. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. * $Id: Insert_Buffer_Properties.bsh 4988 2004-03-08 04:29:12Z spestov $
  26. */
  27. import java.awt.BorderLayout;
  28. import java.awt.event.ActionEvent;
  29. import java.util.Hashtable;
  30. import java.util.StringTokenizer;
  31. import java.util.Vector;
  32. import javax.swing.Box;
  33. import javax.swing.BoxLayout;
  34. import javax.swing.JButton;
  35. import javax.swing.JDialog;
  36. import javax.swing.JLabel;
  37. import javax.swing.JPanel;
  38. import javax.swing.JScrollPane;
  39. import javax.swing.border.EmptyBorder;
  40. import org.gjt.sp.jedit.gui.JCheckBoxList;
  41. BufferLocalPropertiesDialog(View view){
  42. this.view = view;
  43. buffer = view.getTextArea().getBuffer();
  44. mode = buffer.getMode().name;
  45. props = new Hashtable();
  46. props.put("mode","");
  47. props.put("indentSize","int");
  48. props.put("tabSize","int");
  49. props.put("noTabs","bool");
  50. props.put("indentOnTab","bool");
  51. props.put("indentOnEnter","bool");
  52. props.put("wrap","str");
  53. props.put("maxLineLen","int");
  54. props.put("folding","str");
  55. props.put("collapseFolds","int");
  56. props.put("lineSeparator","");
  57. props.put("gzipped","bool");
  58. props.put("trailingEOL","bool");
  59. props.put("deepIndent","bool");
  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. if(buffer.isReadOnly())
  117. {
  118. Macros.error(view,"Buffer is read-only");
  119. dialog.dispose();
  120. }
  121. checkNextTime = new StringBuffer();
  122. buff = new StringBuffer();
  123. names = checkBox.getCheckedValues();
  124. for(i=0; i < names.length; i++)
  125. {
  126. if(i > 0)
  127. checkNextTime.append(",");
  128. checkNextTime.append(name);
  129. name = names[i];
  130. type = props.get(name);
  131. if(name.equals("mode"))
  132. {
  133. value = mode;
  134. }
  135. else if(name.equals("lineSeparator"))
  136. {
  137. value = buffer.getProperty(name);
  138. if(value.equals("\n"))
  139. value = "\\n";
  140. else if(value.equals("\r"))
  141. value = "\\r";
  142. else
  143. value = "\\r\\n";
  144. }
  145. else if(type.equals("bool"))
  146. {
  147. value = buffer.getProperty(name);
  148. if(value == null)
  149. value = "false";
  150. else if(value.equals(""))
  151. value = "false";
  152. else if(value.equals("0"))
  153. value = "false";
  154. else if(value.equals("false"))
  155. value = value; // no-op
  156. else
  157. value = "true";
  158. }
  159. else if(type.equals("int"))
  160. {
  161. value = buffer.getProperty(name);
  162. if(value == null)
  163. value = "";
  164. }
  165. else // str
  166. {
  167. value = buffer.getProperty(name);
  168. if(value == null)
  169. value = "";
  170. }
  171. buff.append(":");
  172. buff.append(name).append("=").append(value);
  173. }
  174. jEdit.setProperty("macro.insert-buffer-properties." + mode,
  175. checkNextTime.toString());
  176. if(buff.length() > 0)
  177. buff.append(":");
  178. properties = buff.toString();
  179. // try to comment out the properties first using a lineComment
  180. // and if that's not defined, look for comment start and end
  181. // properties -- use context senstive properties
  182. caret = view.getTextArea().getCaretPosition();
  183. comment = buffer.getContextSensitiveProperty(caret,"lineComment");
  184. if(comment != null && comment.length() > 0)
  185. properties = comment + " " + properties;
  186. else
  187. {
  188. commentStart = buffer.getContextSensitiveProperty(caret,"commentStart");
  189. commentEnd = buffer.getContextSensitiveProperty(caret,"commentEnd");
  190. if(commentStart != null && commentEnd != null)
  191. properties = commentStart + " " + properties + " " + commentEnd;
  192. }
  193. buffer.insert(caret,properties);
  194. line = view.getTextArea().getCaretLine();
  195. if(line >= 10 && line < (buffer.getLineCount()-10))
  196. Macros.message(view, "Note: Buffer Local properties must in the first or last 10 lines of a buffer to be recognized by jEdit.");
  197. dialog.dispose();
  198. }
  199. dialog.pack();
  200. dialog.setSize(250,350);
  201. dialog.setLocationRelativeTo(view);
  202. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  203. dialog.setVisible(true);
  204. }
  205. if(buffer.isReadOnly())
  206. Macros.error(view, "Buffer is read-only.");
  207. else
  208. BufferLocalPropertiesDialog(view);
  209. /*
  210. Macro index data (in DocBook format)
  211. <listitem>
  212. <para><filename>Insert_Buffer_Properties.bsh</filename></para>
  213. <abstract><para>
  214. Inserts buffer-local properties into the current buffer.
  215. </para></abstract>
  216. <para>
  217. If the buffer's
  218. mode has a line comment defined, or comment start and end
  219. defined, the inserted properties will be commented out.
  220. </para>
  221. </listitem>
  222. */
  223. // :wrap=none:collapseFolds=0:noTabs=false:lineSeparator=\n:maxLineLen=80:mode=beanshell:indentSize=8:deepIndent=false:folding=none: