/* * Insert_Buffer_Properties.bsh - a Beanshell macro * for the jEdit text editor that provides a gui for * inserting Buffer Local properties for the current buffer * into the current buffer. If the buffer's mode as a line * comment defined, or comment start and end properties then * the inserted properties will be commented out. * * Copyright (C) 2002, 2003 Ollie Rutherfurd * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: Insert_Buffer_Properties.bsh 63 2003-11-13 23:39:31Z oliver $ */ import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.util.Hashtable; import java.util.StringTokenizer; import java.util.Vector; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.border.EmptyBorder; import org.gjt.sp.jedit.gui.JCheckBoxList; BufferLocalPropertiesDialog(View view){ buffer = view.getTextArea().getBuffer(); mode = buffer.getMode().name; props = new Hashtable(); props.put("mode",""); props.put("indentSize","int"); props.put("tabSize","int"); props.put("noTabs","bool"); props.put("indentOnTab","bool"); props.put("indentOnEnter","bool"); props.put("wrap","str"); props.put("maxLineLen","int"); props.put("folding","str"); props.put("collapseFolds","int"); props.put("lineSeparator",""); props.put("gzipped","bool"); props.put("trailingEOL","bool"); props.put("deepIndent","bool"); _checked = jEdit.getProperty("macro.insert-buffer-properties." + mode,""); tokens = new StringTokenizer(_checked,","); checkedProps = new Hashtable(); while(tokens.hasMoreTokens()) { property = tokens.nextToken(); checkedProps.put(property,"checked"); } dialog = new JDialog(view,"Insert Buffer Local Properties",true); content = new JPanel(new BorderLayout()); content.setBorder(new EmptyBorder(10,10,10,10)); dialog.setContentPane(content); content.add(new JLabel("Properties:"), BorderLayout.NORTH); _entries = new Vector(); names = props.keys(); while(names.hasMoreElements()){ name = names.nextElement(); checked = checkedProps.get(name); entry = new JCheckBoxList.Entry(checked != null,name); _entries.addElement(entry); } entries = new JCheckBoxList.Entry[_entries.size()]; _entries.copyInto(entries); checkBox = new JCheckBoxList(entries); checkBox.addKeyListener(this); content.add(new JScrollPane(checkBox), BorderLayout.CENTER); buttons = new JPanel(); buttons.setBorder(new EmptyBorder(12,50,0,50)); buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS)); buttons.add(Box.createGlue()); insert = new JButton("Insert"); cancel = new JButton("Cancel"); insert.addActionListener(this); cancel.addActionListener(this); dialog.getRootPane().setDefaultButton(insert); buttons.add(insert); buttons.add(Box.createHorizontalStrut(6)); buttons.add(cancel); buttons.add(Box.createGlue()); content.add(buttons,BorderLayout.SOUTH); void actionPerformed(ActionEvent evt){ if(evt.getSource() == cancel) dialog.dispose(); else this.insertProperties(); } keyPressed(KeyEvent evt){ if(evt.getKeyCode() == KeyEvent.VK_ESCAPE) dialog.dispose(); else if(evt.getKeyCode() == KeyEvent.VK_ENTER) this.insertProperties(); } keyReleased(KeyEvent evt){;} keyTyped(KeyEvent evt){;} insertProperties(){ if(buffer.isReadOnly()) { Macros.error(view,"Buffer is Read-Only"); dialog.dispose(); } checkNextTime = new StringBuffer(); buff = new StringBuffer(); names = checkBox.getCheckedValues(); for(i=0; i < names.length; i++) { if(i > 0) checkNextTime.append(","); checkNextTime.append(name); name = names[i]; type = props.get(name); if(name.equals("mode")) { value = mode; } else if(name.equals("lineSeparator")) { value = buffer.getProperty(name); if(value.equals("\n")) value = "\\n"; else if(value.equals("\r")) value = "\\r"; else value = "\\r\\n"; } else if(type.equals("bool")) { value = buffer.getProperty(name); if(value == null) value = "false"; else if(value.equals("")) value = "false"; else if(value.equals("0")) value = "false"; else if(value.equals("false")) value = value; // no-op else value = "true"; } else if(type.equals("int")) { value = buffer.getProperty(name); if(value == null) value = ""; } else // str { value = buffer.getProperty(name); if(value == null) value = ""; } buff.append(":"); buff.append(name).append("=").append(value); } jEdit.setProperty("macro.insert-buffer-properties." + mode, checkNextTime.toString()); if(buff.length() > 0) buff.append(":"); properties = buff.toString(); // try to comment out the properties first using a lineComment // and if that's not defined, look for comment start and end // properties -- use context senstive properties caret = view.getTextArea().getCaretPosition(); comment = buffer.getContextSensitiveProperty(caret,"lineComment"); if(comment != null && comment.length() > 0) properties = comment + " " + properties; else { commentStart = buffer.getContextSensitiveProperty(caret,"commentStart"); commentEnd = buffer.getContextSensitiveProperty(caret,"commentEnd"); if(commentStart != null && commentEnd != null) properties = commentStart + " " + properties + " " + commentEnd; } buffer.insert(caret,properties); line = view.getTextArea().getCaretLine(); if(line >= 10 && line < (buffer.getLineCount()-10)) Macros.message(view, "Note: Buffer Local properties must in the first or last 10 lines of a buffer to be recognized by jEdit."); dialog.dispose(); } dialog.pack(); dialog.setSize(250,350); dialog.setLocationRelativeTo(view); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); } BufferLocalPropertiesDialog(view); /* Macro index data (in DocBook format) Insert_Buffer_Properties.bsh GUI for inserting Buffer Local properties into the current Buffer. A macro that provides a GUI for inserting Buffer Local properties for the current buffer into the current buffer. If the buffer's mode as a line comment defined, or comment start and end properties defined, the inserted properties will be commented out. */ // :wrap=none:collapseFolds=0:noTabs=false:lineSeparator=\n:maxLineLen=80:mode=beanshell:indentSize=8:deepIndent=false:folding=none: