package com.jvms.i18neditor;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.border.Border;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoManager;

import com.jvms.i18neditor.swing.EmptyAction;

public class ResourceField extends JTextArea implements Comparable<ResourceField> {
	private static final long serialVersionUID = 2034814490878477055L;
	
	private final Resource resource;
	private final UndoManager undoManager;
	
	public ResourceField(Resource resource) {
		super();
		this.undoManager = new UndoManager();
		this.resource = resource;
		setup();
	}
	
	public void updateValue(String key) {
		setText(resource.getTranslations().get(key));
		undoManager.discardAllEdits();
	}
	
	public Resource getResource() {
		return resource;
	}
	
	private void setup() {
		Border border = BorderFactory.createLineBorder(Color.GRAY);
		setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(4,4,4,4)));
		setAlignmentX(LEFT_ALIGNMENT);
		setLineWrap(true);
		setWrapStyleWord(true);
		setRows(10);
		
		// Remove multiline support
		getActionMap().put(getInputMap().get(KeyStroke.getKeyStroke("ENTER")), new EmptyAction());
		
		// Add undo support
		getDocument().addUndoableEditListener(new EditListener());
		getActionMap().put("undo", new UndoAction());
		getInputMap().put(KeyStroke.getKeyStroke('Z', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "undo");
	}
	
	@SuppressWarnings("serial")
	private class UndoAction extends AbstractAction {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (undoManager.canUndo()) {
				undoManager.undo();
			}
		}
	}
	
	private class EditListener implements UndoableEditListener {
		@Override
		public void undoableEditHappened(UndoableEditEvent e) {
			undoManager.addEdit(e.getEdit());
		}
	}
	
	@Override
	public int compareTo(ResourceField o) {
		return getResource().getLocale().getDisplayName().compareTo(o.getResource().getLocale().getDisplayName());
	}
}

@@ -293,162 +293,24 @@
 ing.
-event.UndoableEditEvent;%0Aimport javax.swing.event.UndoableEditListener;%0Aimport javax.swing.undo.UndoManager;%0A%0Aimport com.jvms.i18neditor.swing.EmptyAction
+undo.UndoManager
 ;%0A%0Ap
@@ -1145,134 +1145,8 @@
 %0A%09%09%0A
-%09%09// Remove multiline support%0A%09%09getActionMap().put(getInputMap().get(KeyStroke.getKeyStroke(%22ENTER%22)), new EmptyAction());%0A%09%09%0A
 %09%09//
@@ -1207,25 +1207,44 @@
 ner(
-new EditListener(
+e -%3E undoManager.addEdit(e.getEdit()
 ));%0A
@@ -1621,186 +1621,8 @@
 %7D%0A%09%0A
-%09private class EditListener implements UndoableEditListener %7B%0A%09%09@Override%0A%09%09public void undoableEditHappened(UndoableEditEvent e) %7B%0A%09%09%09undoManager.addEdit(e.getEdit());%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09@Ov

Allow multiline as translation value.