<commit_before>package org.workcraft.gui.edit.tools;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.workcraft.dom.Component;
import org.workcraft.dom.DisplayName;
import org.workcraft.dom.visual.VisualComponent;
import org.workcraft.framework.exceptions.DuplicateIDException;
import org.workcraft.framework.exceptions.InvalidComponentException;
import org.workcraft.framework.exceptions.VisualModelConstructionException;
import org.workcraft.framework.plugins.PluginManager;
import org.workcraft.gui.events.GraphEditorMouseEvent;

public class ComponentCreationTool extends AbstractTool {
	protected Class<?> componentClass;

	public ComponentCreationTool (Class<? extends Component> componentClass) {
		this.componentClass = componentClass;
	}

	public String getIconPath() {
		return null;
	}

	public String getName() {
		DisplayName name = componentClass.getAnnotation(DisplayName.class);
		if (name == null)
			return "Create " + componentClass.getSimpleName();
		else
			return "Create " + name.value();
	}

	public void mousePressed(GraphEditorMouseEvent e) {
		Constructor<?> ctor;
		try {
			ctor = componentClass.getConstructor();
			Component comp = (Component)ctor.newInstance();
			VisualComponent vComp = (VisualComponent)PluginManager.createVisualComponent(comp, e.getModel().getRoot());

			vComp.setX(e.getX());
			vComp.setY(e.getY());

			e.getEditor().getModel().getMathModel().addComponent(comp);
			e.getEditor().getModel().getRoot().add(vComp);
			e.getEditor().getModel().fireModelStructureChanged();

		} catch (SecurityException e1) {
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			e1.printStackTrace();
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();<commit_msg>ComponentCreationTool now snaps the new component's position to grid.<commit_after>package org.workcraft.gui.edit.tools;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.workcraft.dom.Component;
import org.workcraft.dom.DisplayName;
import org.workcraft.dom.visual.VisualComponent;
import org.workcraft.framework.exceptions.DuplicateIDException;
import org.workcraft.framework.exceptions.InvalidComponentException;
import org.workcraft.framework.exceptions.VisualModelConstructionException;
import org.workcraft.framework.plugins.PluginManager;
import org.workcraft.gui.events.GraphEditorMouseEvent;

public class ComponentCreationTool extends AbstractTool {
	protected Class<?> componentClass;

	public ComponentCreationTool (Class<? extends Component> componentClass) {
		this.componentClass = componentClass;
	}

	public String getIconPath() {
		return null;
	}

	public String getName() {
		DisplayName name = componentClass.getAnnotation(DisplayName.class);
		if (name == null)
			return "Create " + componentClass.getSimpleName();
		else
			return "Create " + name.value();
	}

	public void mousePressed(GraphEditorMouseEvent e) {
		Constructor<?> ctor;
		try {
			ctor = componentClass.getConstructor();
			Component comp = (Component)ctor.newInstance();
			VisualComponent vComp = (VisualComponent)PluginManager.createVisualComponent(comp, e.getModel().getRoot());

			Point2D pos = e.getPosition();
			e.getEditor().snap(pos);
			vComp.setX(pos.getX());
			vComp.setY(pos.getY());

			e.getEditor().getModel().getMathModel().addComponent(comp);
			e.getEditor().getModel().getRoot().add(vComp);
			e.getEditor().getModel().fireModelStructureChanged();

		} catch (SecurityException e1) {
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			e1.printStackTrace();
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
{'fraction_non_alphanumeric': 0.11826418693371483, 'fraction_numerical': 0.00619933237958989, 'mean_word_length': 5.627172195892575, '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': '6197135', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1217, 'n_words': 229}