<commit_before>/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2013 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of any organization.
 * #L%
 */

package imagej.script.editor;

import imagej.command.CommandService;
import imagej.io.IOService;
import imagej.platform.PlatformService;
import imagej.script.ScriptLanguage;
import imagej.script.ScriptService;
import imagej.script.editor.command.ChooseFontSize;
import imagej.script.editor.command.ChooseTabSize;
import imagej.script.editor.command.GitGrep;
import imagej.script.editor.command.KillScript;
import imagej.util.AppUtils;
import imagej.util.Prefs;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipException;

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.Position;

import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.modes.JavaScriptTokenMaker;
import org.fife.ui.rsyntaxtextarea.modes.JavaTokenMaker;
import org.scijava.Context;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.util.FileUtils;

/**
 * TODO
 * 
 * @author <NAME>
 */
public class TextEditor extends JFrame implements ActionListener,
	       ChangeListener {

	public static final String TAB_SIZE_PREFS = "script.editor.TabSize";
	public static final String FONT_SIZE_PREFS = "script.editor.FontSize";
	public static final String LINE_WRAP_PREFS = "script.editor.WrapLines";
	public static final String TABS_EMULATED_PREFS = "script.editor.TabsEmulated";
	public static final String WINDOW_HEIGHT = "script.editor.height";
	public static final String WINDOW_WIDTH = "script.editor.width";
	public static final int DEFAULT_TAB_SIZE = 4;
	public static final int DEFAULT_WINDOW_WIDTH = 800;
	public static final int DEFAULT_WINDOW_HEIGHT = 600;

	static {
		try {
			AbstractTokenMakerFactory factory = (AbstractTokenMakerFactory)TokenMakerFactory.getDefaultInstance();
			// TODO: these should go to upstream RSyntaxTextArea
			factory.putMapping("text/matlab", MatlabTokenMaker.class.getName());
			factory.putMapping("text/ij-macro", ImageJMacroTokenMaker.class.getName());
			factory.putMapping("text/ecmascript", JavaScriptTokenMaker.class.getName());
			factory.putMapping("text/beanshell", JavaTokenMaker.class.getName());
		} catch (ClassCastException t) {
			// ignore for now, but log it to stderr
			t.printStackTrace();
		}
	}

	protected JTabbedPane tabbed;
	protected JMenuItem newFile, open, save, saveas, compileAndRun, compile, close,
		  undo, redo, cut, copy, paste, find, replace, selectAll,
		  kill, gotoLine,
		  makeJar, makeJarWithSource, removeUnusedImports,
		  sortImports, removeTrailingWhitespace, findNext, findPrevious,
		  openHelp, addImport, clearScreen, nextError, previousError,
		  openHelpWithoutFrames, nextTab, previousTab,
		  runSelection, extractSourceJar, toggleBookmark,
		  listBookmarks, openSourceForClass, newPlugin,
		  openSourceForMenuItem,
		  openMacroFunctions, decreaseFontSize, increaseFontSize,
		  chooseFontSize, chooseTabSize, gitGrep, openInGitweb,
		  replaceTabsWithSpaces, replaceSpacesWithTabs, toggleWhiteSpaceLabeling,
		  zapGremlins, savePreferences;
	protected RecentFilesMenuItem openRecent;
	protected JMenu gitMenu, tabsMenu, fontSizeMenu, tabSizeMenu, toolsMenu, runMenu,
		  whiteSpaceMenu;
	protected int tabsMenuTabsStart;
	protected Set<JMenuItem> tabsMenuItems;
	protected FindAndReplaceDialog findDialog;
	protected JCheckBoxMenuItem autoSave, showDeprecation, wrapLines, tabsEmulated;
	protected JTextArea errorScreen = new JTextArea();

	protected final String templateFolder = "templates/";

	protected int compileStartOffset;
	protected Position compileStartPosition;
	protected ErrorHandler errorHandler;

	@Parameter
	protected LogService log;
	@Parameter
	protected PlatformService platformService;
	@Parameter
	protected IOService ioService;
	@Parameter
	protected CommandService commandService;
	@Parameter
	protected ScriptService scriptService;

	protected Map<ScriptLanguage, JRadioButtonMenuItem> languageMenuItems;
	protected JRadioButtonMenuItem noneLanguageItem;<commit_msg>TextEditor: also inject Context and ModuleService<commit_after>/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2013 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of any organization.
 * #L%
 */

package imagej.script.editor;

import imagej.command.CommandService;
import imagej.io.IOService;
import imagej.module.ModuleService;
import imagej.platform.PlatformService;
import imagej.script.ScriptLanguage;
import imagej.script.ScriptService;
import imagej.script.editor.command.ChooseFontSize;
import imagej.script.editor.command.ChooseTabSize;
import imagej.script.editor.command.GitGrep;
import imagej.script.editor.command.KillScript;
import imagej.util.AppUtils;
import imagej.util.Prefs;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipException;

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.Position;

import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.modes.JavaScriptTokenMaker;
import org.fife.ui.rsyntaxtextarea.modes.JavaTokenMaker;
import org.scijava.Context;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.util.FileUtils;

/**
 * TODO
 * 
 * @author <NAME>
 */
public class TextEditor extends JFrame implements ActionListener,
	       ChangeListener {

	public static final String TAB_SIZE_PREFS = "script.editor.TabSize";
	public static final String FONT_SIZE_PREFS = "script.editor.FontSize";
	public static final String LINE_WRAP_PREFS = "script.editor.WrapLines";
	public static final String TABS_EMULATED_PREFS = "script.editor.TabsEmulated";
	public static final String WINDOW_HEIGHT = "script.editor.height";
	public static final String WINDOW_WIDTH = "script.editor.width";
	public static final int DEFAULT_TAB_SIZE = 4;
	public static final int DEFAULT_WINDOW_WIDTH = 800;
	public static final int DEFAULT_WINDOW_HEIGHT = 600;

	static {
		try {
			AbstractTokenMakerFactory factory = (AbstractTokenMakerFactory)TokenMakerFactory.getDefaultInstance();
			// TODO: these should go to upstream RSyntaxTextArea
			factory.putMapping("text/matlab", MatlabTokenMaker.class.getName());
			factory.putMapping("text/ij-macro", ImageJMacroTokenMaker.class.getName());
			factory.putMapping("text/ecmascript", JavaScriptTokenMaker.class.getName());
			factory.putMapping("text/beanshell", JavaTokenMaker.class.getName());
		} catch (ClassCastException t) {
			// ignore for now, but log it to stderr
			t.printStackTrace();
		}
	}

	protected JTabbedPane tabbed;
	protected JMenuItem newFile, open, save, saveas, compileAndRun, compile, close,
		  undo, redo, cut, copy, paste, find, replace, selectAll,
		  kill, gotoLine,
		  makeJar, makeJarWithSource, removeUnusedImports,
		  sortImports, removeTrailingWhitespace, findNext, findPrevious,
		  openHelp, addImport, clearScreen, nextError, previousError,
		  openHelpWithoutFrames, nextTab, previousTab,
		  runSelection, extractSourceJar, toggleBookmark,
		  listBookmarks, openSourceForClass, newPlugin,
		  openSourceForMenuItem,
		  openMacroFunctions, decreaseFontSize, increaseFontSize,
		  chooseFontSize, chooseTabSize, gitGrep, openInGitweb,
		  replaceTabsWithSpaces, replaceSpacesWithTabs, toggleWhiteSpaceLabeling,
		  zapGremlins, savePreferences;
	protected RecentFilesMenuItem openRecent;
	protected JMenu gitMenu, tabsMenu, fontSizeMenu, tabSizeMenu, toolsMenu, runMenu,
		  whiteSpaceMenu;
	protected int tabsMenuTabsStart;
	protected Set<JMenuItem> tabsMenuItems;
	protected FindAndReplaceDialog findDialog;
	protected JCheckBoxMenuItem autoSave, showDeprecation, wrapLines, tabsEmulated;
	protected JTextArea errorScreen = new JTextArea();

	protected final String templateFolder = "templates/";

	protected int compileStartOffset;
	protected Position compileStartPosition;
	protected ErrorHandler errorHandler;

	@Parameter
	protected Context context;
	@Parameter
	protected LogService log;
	@Parameter
	protected ModuleService moduleService;
	@Parameter
	protected PlatformService platformService;
	@Parameter
	protected IOService ioService;
	@Parameter
	protected CommandService commandService;
	@Parameter
	protected ScriptService scriptService;

	protected Map<ScriptLanguage, JRadioButtonMenuItem> languageMenuItems;
	protected JRadioButtonMenuItem noneLanguageItem;
{'fraction_non_alphanumeric': 0.07837701612903226, 'fraction_numerical': 0.0021421370967741934, 'mean_word_length': 5.703125, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '984344', 'n_tokens_mistral': 4717, 'n_tokens_neox': 4403, 'n_words': 1324}