<commit_before>package ar.app.display;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;

import ar.Aggregates;
import ar.Aggregator;
import ar.Glyphset;
import ar.Renderer;
import ar.Transfer;
import ar.aggregates.FlatAggregates;
import ar.app.components.LabeledItem;
import ar.util.Util;

/**Host a panel, add to it a draw-on overlay and enhance-region capability.**/ 
public class EnhanceHost extends ARComponent.Aggregating {
	private static final long serialVersionUID = -6449887730981205865L;
	
	private static final Color SHOW_ENHANCED  = new Color(100,149,237);
	
	private SubsetDisplay hosted;
	private SelectionOverlay overlay;
	private EnhancedOverlay enhanced = new EnhancedOverlay();
	private boolean enhanceEnabled;

	private boolean redoRefAggregates = true;
	
	/**Host the given component in the overlay.**/
	public EnhanceHost(SubsetDisplay hosted) {	
		this.hosted = hosted;
		this.overlay = new SelectionOverlay(this);
		this.add(overlay);
		this.add(enhanced);
		this.add(hosted);
		
		overlay.setVisible(false);
		enhanced.setVisible(false);
		
		hosted.addAggregatesChangedListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {forceNewRefAggregates();}
		});
	}
	
	public void layout() {
		Rectangle b = this.getBounds();
		hosted.setBounds(b);
		enhanced.setBounds(b);
		overlay.setBounds(b);
	}

	/**Is the overlay currently visible?**/
	public boolean showOverlay() {return overlay.isVisible();}
	
	/**Set the overlay visibility state.**/
	public void showOverlay(boolean show) {overlay.setVisible(show);}

	/**Force a refresh of the ref aggregates and trigger a repaint.**/
	public void forceNewRefAggregates() {
		redoRefAggregates = true;
		this.repaint();
	}
	
	/**Get the subset of aggregates that corresponds to the currently selected region.
	 * @return null if no selection; otherwise a subset of aggregates.
	 */
	public <A> Aggregates<?> subset() {
		if (overlay.selected == null) {return null;}
		
		Aggregates<A> aggs = (Aggregates<A>) hosted.aggregates();
		AffineTransform rt = hosted.renderTransform();
		Area selection = overlay.selectedArea().createTransformedArea(rt);
		Rectangle bounds = selection.getBounds();
		
		int lowX = bounds.x;
		int lowY = bounds.y;
		int highX = (int) bounds.getMaxX();
		int highY = (int) bounds.getMaxY();
		
		int width = highX-lowX;
		int height = highY-lowY;
		
		Aggregates<A> subset= new FlatAggregates<>(0, 0, width, height, aggs.defaultValue());
		for (int x=0; x<width; x++) {
			for (int y=0; y<height; y++) {
				if (selection.contains(x+lowX, y+lowY)) {
					A val = aggs.get(x+lowX,y+lowY);
					subset.set(x, y, val);
				}
			}
		}
		return subset;

	}
	
	public void enableEnhance(boolean enable) {
		this.enhanceEnabled = enable;
		redoRefAggregates  = true;
		enhanced.setVisible(enable);
		this.repaint();
		
	}
	
	public void paint(Graphics g) {
		if (redoRefAggregates) {
			if (enhanceEnabled) {
				Aggregates<?> subset = subset();
				hosted.refAggregates(subset);
			} else {
				hosted.refAggregates(null);
			}
			redoRefAggregates = false;
		}
		super.paint(g);
		
		AffineTransform vt = hosted.viewTransformRef;
		if (enhanceEnabled && vt != null) {
			Graphics2D g2 = (Graphics2D) g.create();
			g2.setColor(SHOW_ENHANCED);
			g2.setStroke(new BasicStroke(1f));
			g2.draw(overlay.selectedArea().createTransformedArea(vt));
		}
	}

	public Transfer<?, ?> transfer() {return hosted.transfer();}
	public void transfer(Transfer<?, ?> t) {hosted.transfer(t);}
	public Aggregates<?> aggregates() {return hosted.aggregates();}
	public void aggregates(Aggregates<?> aggregates, AffineTransform renderTransform) {<commit_msg>Fixed null pointer exception when rendering in enhance mode with no selected region.<commit_after>package ar.app.display;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;

import ar.Aggregates;
import ar.Aggregator;
import ar.Glyphset;
import ar.Renderer;
import ar.Transfer;
import ar.aggregates.FlatAggregates;
import ar.app.components.LabeledItem;
import ar.util.Util;

/**Host a panel, add to it a draw-on overlay and enhance-region capability.**/ 
public class EnhanceHost extends ARComponent.Aggregating {
	private static final long serialVersionUID = -6449887730981205865L;
	
	private static final Color SHOW_ENHANCED  = new Color(100,149,237);
	
	private SubsetDisplay hosted;
	private SelectionOverlay overlay;
	private EnhancedOverlay enhanced = new EnhancedOverlay();
	private boolean enhanceEnabled;

	private boolean redoRefAggregates = true;
	
	/**Host the given component in the overlay.**/
	public EnhanceHost(SubsetDisplay hosted) {	
		this.hosted = hosted;
		this.overlay = new SelectionOverlay(this);
		this.add(overlay);
		this.add(enhanced);
		this.add(hosted);
		
		overlay.setVisible(false);
		enhanced.setVisible(false);
		
		hosted.addAggregatesChangedListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {forceNewRefAggregates();}
		});
	}
	
	public void layout() {
		Rectangle b = this.getBounds();
		hosted.setBounds(b);
		enhanced.setBounds(b);
		overlay.setBounds(b);
	}

	/**Is the overlay currently visible?**/
	public boolean showOverlay() {return overlay.isVisible();}
	
	/**Set the overlay visibility state.**/
	public void showOverlay(boolean show) {overlay.setVisible(show);}

	/**Force a refresh of the ref aggregates and trigger a repaint.**/
	public void forceNewRefAggregates() {
		redoRefAggregates = true;
		this.repaint();
	}
	
	/**Get the subset of aggregates that corresponds to the currently selected region.
	 * @return null if no selection; otherwise a subset of aggregates.
	 */
	public <A> Aggregates<?> subset() {
		if (overlay.selected == null) {return null;}
		
		Aggregates<A> aggs = (Aggregates<A>) hosted.aggregates();
		AffineTransform rt = hosted.renderTransform();
		Area selection = overlay.selectedArea().createTransformedArea(rt);
		Rectangle bounds = selection.getBounds();
		
		int lowX = bounds.x;
		int lowY = bounds.y;
		int highX = (int) bounds.getMaxX();
		int highY = (int) bounds.getMaxY();
		
		int width = highX-lowX;
		int height = highY-lowY;
		
		Aggregates<A> subset= new FlatAggregates<>(0, 0, width, height, aggs.defaultValue());
		for (int x=0; x<width; x++) {
			for (int y=0; y<height; y++) {
				if (selection.contains(x+lowX, y+lowY)) {
					A val = aggs.get(x+lowX,y+lowY);
					subset.set(x, y, val);
				}
			}
		}
		return subset;

	}
	
	public void enableEnhance(boolean enable) {
		this.enhanceEnabled = enable;
		redoRefAggregates  = true;
		enhanced.setVisible(enable);
		this.repaint();
		
	}
	
	public void paint(Graphics g) {
		if (redoRefAggregates) {
			if (enhanceEnabled) {
				Aggregates<?> subset = subset();
				hosted.refAggregates(subset);
			} else {
				hosted.refAggregates(null);
			}
			redoRefAggregates = false;
		}
		super.paint(g);
		
		AffineTransform vt = hosted.viewTransformRef;
		if (enhanceEnabled && vt != null && overlay.selectedArea() != null) {
			Graphics2D g2 = (Graphics2D) g.create();
			g2.setColor(SHOW_ENHANCED);
			g2.setStroke(new BasicStroke(1f));
			g2.draw(overlay.selectedArea().createTransformedArea(vt));
		}
	}

	public Transfer<?, ?> transfer() {return hosted.transfer();}
	public void transfer(Transfer<?, ?> t) {hosted.transfer(t);}
	public Aggregates<?> aggregates() {return hosted.aggregates();}
	public void aggregates(Aggregates<?> aggregates, AffineTransform renderTransform) {
{'fraction_non_alphanumeric': 0.1225518268239606, 'fraction_numerical': 0.009620891077768869, 'mean_word_length': 4.615434083601286, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, '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': '5390974', 'n_tokens_mistral': 3116, 'n_tokens_neox': 2824, 'n_words': 692}