PageRenderTime 63ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 17ms app.codeStats 1ms

/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
  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
 30import java.awt.BorderLayout;
 31import java.awt.event.ActionEvent;
 32import java.util.Hashtable;
 33import java.util.StringTokenizer;
 34import java.util.Vector;
 35import javax.swing.Box;
 36import javax.swing.BoxLayout;
 37import javax.swing.JButton;
 38import javax.swing.JDialog;
 39import javax.swing.JLabel;
 40import javax.swing.JPanel;
 41import javax.swing.JScrollPane;
 42import javax.swing.border.EmptyBorder;
 43import org.gjt.sp.jedit.gui.JCheckBoxList;
 44
 45BufferLocalPropertiesDialog(View view){
 46
 47	this.view = view;
 48	buffer = view.getTextArea().getBuffer();
 49	mode = buffer.getMode().name;
 50
 51// removed non-valid BLPs and added missing ones
 52	props = new Hashtable();
 53	props.put("mode","");
 54	props.put("indentSize","int");
 55	props.put("tabSize","int");
 56	props.put("noTabs","bool");
 57	props.put("wrap","str");
 58	props.put("maxLineLen","int");
 59	props.put("folding","str");
 60	props.put("collapseFolds","int");
 61	props.put("deepIndent","bool");
 62	props.put("noWordSep","str");
 63	props.put("wordBreakChars","str");
 64
 65	_checked = jEdit.getProperty("macro.insert-buffer-properties." + mode,"");
 66	tokens = new StringTokenizer(_checked,",");
 67	checkedProps = new Hashtable();
 68	while(tokens.hasMoreTokens())
 69	{
 70		property = tokens.nextToken();
 71		checkedProps.put(property,"checked");
 72	}
 73
 74	dialog = new JDialog(view,"Insert Buffer Local Properties",true);
 75	content = new JPanel(new BorderLayout());
 76	content.setBorder(new EmptyBorder(10,10,10,10));
 77	dialog.setContentPane(content);
 78	content.add(new JLabel("Properties:"), BorderLayout.NORTH);
 79
 80	_entries = new Vector();
 81	names = props.keys();
 82	while(names.hasMoreElements()){
 83		name = names.nextElement();
 84		checked = checkedProps.get(name);
 85		entry = new JCheckBoxList.Entry(checked != null,name);
 86		_entries.addElement(entry);
 87	}
 88	entries = new JCheckBoxList.Entry[_entries.size()];
 89	_entries.copyInto(entries);
 90
 91	checkBox = new JCheckBoxList(entries);
 92	checkBox.addKeyListener(this);
 93	content.add(new JScrollPane(checkBox),
 94		BorderLayout.CENTER);
 95
 96	buttons = new JPanel();
 97	buttons.setBorder(new EmptyBorder(12,50,0,50));
 98	buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
 99	buttons.add(Box.createGlue());
100	insert = new JButton("Insert");
101	cancel = new JButton("Cancel");
102	insert.addActionListener(this);
103	cancel.addActionListener(this);
104	dialog.getRootPane().setDefaultButton(insert);
105	buttons.add(insert);
106	buttons.add(Box.createHorizontalStrut(6));
107	buttons.add(cancel);
108	buttons.add(Box.createGlue());
109	content.add(buttons,BorderLayout.SOUTH);
110
111	void actionPerformed(ActionEvent evt){
112		if(evt.getSource() == cancel)
113			dialog.dispose();
114		else
115			this.insertProperties();
116	}
117
118	keyPressed(KeyEvent evt){
119		if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
120			dialog.dispose();
121		else if(evt.getKeyCode() == KeyEvent.VK_ENTER)
122			this.insertProperties();
123	}
124	keyReleased(KeyEvent evt){;}
125	keyTyped(KeyEvent evt){;}
126
127	insertProperties(){
128// removed isReadOnly-Check because already done in the beginning of the script
129		checkNextTime = new StringBuffer();
130		buff = new StringBuffer();
131		names = checkBox.getCheckedValues();
132		for(i=0; i < names.length; i++)
133		{
134// moved the assignation in front of the checkNextTime-build,
135// because "name" is used and will have a wrong value otherwise
136			name = names[i];
137			type = props.get(name);
138
139			if(i > 0)
140				checkNextTime.append(',');
141			checkNextTime.append(name);
142
143// changed the value reading/building/escaping
144			if(name.equals("mode"))
145				value = mode;
146			else if(type.equals("bool"))
147				value = buffer.getBooleanProperty(name);
148			else if(type.equals("str"))
149			{
150				value = buffer.getStringProperty(name);
151				if(value == null)
152					value = "";
153				value = value.replaceAll("=","\\\\=").replaceAll(":","\\\\:").replaceAll("\n","\\\\n").replaceAll("\t","\\\\t");
154			}
155			else if(type.equals("int"))
156			{
157				value = buffer.getIntegerProperty(name,-1);
158				if(value == -1)
159					value = "";
160			}
161			else
162				value = "";
163			buff.append(':').append(name).append('=').append(value);
164		}
165
166		jEdit.setProperty("macro.insert-buffer-properties." + mode,
167			checkNextTime.toString());
168
169		if(buff.length() > 0)
170			buff.append(':');
171		properties = buff.toString();
172
173		// try to comment out the properties first using a lineComment
174		// and if that's not defined, look for comment start and end
175		// properties -- use context senstive properties
176		caret = view.getTextArea().getCaretPosition();
177		comment = buffer.getContextSensitiveProperty(caret,"lineComment");
178		if(comment != null && comment.length() > 0)
179			properties = comment + " " + properties;
180		else
181		{
182			commentStart = buffer.getContextSensitiveProperty(caret,"commentStart");
183			commentEnd = buffer.getContextSensitiveProperty(caret,"commentEnd");
184			if(commentStart != null && commentEnd != null)
185					properties = commentStart + " " + properties + " " + commentEnd;
186		}
187		buffer.insert(caret,properties);
188
189		line = view.getTextArea().getCaretLine();
190		if(line >= 10 && line < (buffer.getLineCount()-10))
191			Macros.message(view, "Note: Buffer Local properties must in the first or last 10 lines of a buffer to be recognized by jEdit.");
192
193		dialog.dispose();
194	}
195
196	dialog.pack();
197// cosmetic size-correction
198	dialog.setSize(250,290);
199	dialog.setLocationRelativeTo(view);
200	dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
201	dialog.setVisible(true);
202}
203
204if(buffer.isReadOnly())
205	Macros.error(view, "Buffer is read-only.");
206else
207	BufferLocalPropertiesDialog(view);
208
209/*
210	Macro index data (in DocBook format)
211
212<listitem>
213	<para><filename>Insert_Buffer_Properties.bsh</filename></para>
214	<abstract><para>
215	Inserts buffer-local properties into the current buffer.
216	</para></abstract>
217	<para>
218	If the buffer's 
219	mode has a line comment defined, or comment start and end
220	defined, the inserted properties will be commented out.
221	</para>
222</listitem>
223
224*/
225
226// :wrap=none:noTabs=false:collapseFolds=0:maxLineLen=80:mode=beanshell:indentSize=8:deepIndent=false:folding=none: