1
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
4
Part of the Processing project - http://processing.org
6
Copyright (c) 2006 Ben Fry and Casey Reas
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software Foundation,
20
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
package processing.app.tools;
25
import processing.app.*;
26
import processing.core.*;
32
* Tool for auto-formatting code that interfaces to
33
* <A HREF="http://jalopy.sourceforge.net/">Jalopy</A>. This is to replace
34
* the buggy code formatter found in previous releases.
36
public class AutoFormat {
40
public AutoFormat(Editor editor) {
46
String originalText = editor.textarea.getText();
47
int indentSize = Preferences.getInteger("editor.tabs.size");
51
String formattedText = null; //strOut.toString();
52
if (formattedText.equals(originalText)) {
53
editor.message("No changes necessary for Auto Format.");
56
// replace with new bootiful text
57
// selectionEnd hopefully at least in the neighborhood
58
editor.setText(formattedText, selectionEnd, selectionEnd);
59
editor.sketch.setModified(true);
62
// warn user if there are too many parens in either direction
64
editor.error("Warning: Too many " +
65
((paren < 0) ? "right" : "left") +
68
} else if (c_level != 0) { // check braces only if parens are ok
69
editor.error("Warning: Too many " +
70
((c_level < 0) ? "right" : "left") +
73
editor.message("Auto Format finished.");
80
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
83
private static class PluginImpl extends AbstractPlugin {
84
JEditStatusBar statusBar;
89
* Creates a new PluginImpl object.
93
super(new JEditAppender());
97
public Project getActiveProject()
99
if (this.project == null)
101
this.project = new JEditProject();
108
public FileFormat getFileFormat()
110
// there is a bug(?) in jEdit's text area whereas inserting text with
111
// DOS file format results in displaying EOF characters, so we always
112
// use UNIX format and let jEdit handle the specified file format upon
114
return FileFormat.UNIX;
118
public Frame getMainWindow()
120
return jEdit.getActiveView();
124
public StatusBar getStatusBar()
126
return this.statusBar;
130
public void afterEnd()
133
MessageView.getInstance().update();
138
* Formats the currently active buffer.
140
public void formatActive()
142
// only perform the action if the current Buffer contains
143
// a Java source file
144
//if (isJava(jEdit.getActiveView().getBuffer()))
146
performAction(Action.FORMAT_ACTIVE);
152
* Formats the currently open buffers.
154
public void formatOpen()
156
performAction(Action.FORMAT_OPEN);