Hallo Zussammen!
Ich will für ein JTextField ein neues Model machen. Man übergibt ein Objekt, welches einen String als Attribut hat, mit entsprechenden gettern/settern und den Namen des Attributs. Nun soll dieses Objekt verwendet werden, um die Eingaben des Textfelds zu speichern. Dies funktioniert leider noch nicht ganz, es wird nur ein Zeichen angezeigt und die Reihenfolge der Eingaben stimmt auch nicht.
Vielleicht sieht jemand von euch den Überlegungsfehler...
Hier mein Code:
Ich will für ein JTextField ein neues Model machen. Man übergibt ein Objekt, welches einen String als Attribut hat, mit entsprechenden gettern/settern und den Namen des Attributs. Nun soll dieses Objekt verwendet werden, um die Eingaben des Textfelds zu speichern. Dies funktioniert leider noch nicht ganz, es wird nur ein Zeichen angezeigt und die Reihenfolge der Eingaben stimmt auch nicht.
Vielleicht sieht jemand von euch den Überlegungsfehler...
Hier mein Code:
Code:
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Vector;
import javax.swing.text.*;
import javax.swing.undo.*;
public class ContentAdapter
implements javax.swing.text.AbstractDocument.Content
{
class MyPosition
implements Position
{
public int getOffset()
{
return position;
}
private int position;
public MyPosition(int position)
{
this.position = position;
}
}
class InsertUndo extends AbstractUndoableEdit
{
public void undo()
throws CannotUndoException
{
super.undo();
try
{
string = getString(offset, length);
remove(offset, length);
}
catch(BadLocationException bl)
{
throw new CannotUndoException();
}
}
public void redo()
throws CannotRedoException
{
super.redo();
try
{
insertString(offset, string);
string = null;
}
catch(BadLocationException bl)
{
throw new CannotRedoException();
}
}
protected int offset;
protected int length;
protected String string;
protected InsertUndo(int offset, int length)
{
this.offset = offset;
this.length = length;
}
}
class RemoveUndo extends AbstractUndoableEdit
{
public void undo()
throws CannotUndoException
{
super.undo();
try
{
insertString(offset, string);
string = null;
}
catch(BadLocationException bl)
{
throw new CannotUndoException();
}
}
public void redo()
throws CannotRedoException
{
super.redo();
try
{
string = getString(offset, length);
remove(offset, length);
}
catch(BadLocationException bl)
{
throw new CannotRedoException();
}
}
protected int offset;
protected int length;
protected String string;
protected Vector posRefs;
protected RemoveUndo(int offset, String string)
{
this.offset = offset;
this.string = string;
length = string.length();
}
}
public ContentAdapter(Object anObject, String aPropertyName)
throws IllegalParameterException
{
model = null;
getter = null;
setter = null;
String getterName = "get" + aPropertyName;
try
{
getter = anObject.getClass().getMethod(getterName, null);
if(!java.lang.String.class.isAssignableFrom(getter.getReturnType()))
throw new IllegalParameterException("Methode " + getterName + " in " + anObject.getClass() + " liefert keinen String");
}
catch(SecurityException e)
{
e.printStackTrace();
throw new IllegalParameterException("Kein Zugriff auf " + getterName + " in " + anObject.getClass());
}
catch(NoSuchMethodException e)
{
e.printStackTrace();
throw new IllegalParameterException("Methode " + getterName + " nicht in " + anObject.getClass() + " vorhanden");
}
String setterName = "set" + aPropertyName;
try
{
Class parameters[] = {
java.lang.String.class
};
setter = anObject.getClass().getMethod(setterName, parameters);
}
catch(SecurityException e)
{
e.printStackTrace();
throw new IllegalParameterException("Kein Zugriff auf " + setterName + " in " + anObject.getClass());
}
catch(NoSuchMethodException e)
{
e.printStackTrace();
throw new IllegalParameterException("Methode " + setterName + " nicht in " + anObject.getClass() + " vorhanden");
}
model = anObject;
}
public String get()
{
try
{
String ret = (String)getter.invoke(model, null);
return ret == null ? "" : ret;
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(IllegalAccessException e)
{
e.printStackTrace();
}
catch(InvocationTargetException e)
{
e.printStackTrace();
}
return "";
}
public void set(String newString)
{
try
{
setter.invoke(model, new Object[] {
newString != null ? newString : ""
});
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch(IllegalAccessException e)
{
e.printStackTrace();
}
catch(InvocationTargetException e)
{
e.printStackTrace();
}
}
public int length()
{
return get().length();
}
public String getString(int aWhere, int aLen)
throws BadLocationException
{
return get().substring(aWhere, aWhere + aLen);
}
public Position createPosition(int anOffset)
throws BadLocationException
{
return new MyPosition(anOffset);
}
public void getChars(int aWhere, int aLen, Segment aTxt)
throws BadLocationException
{
int end = aWhere + aLen;
if(aWhere < 0 || end < 0)
throw new BadLocationException("Invalid location", -1);
if(end > length() || aWhere > length())
throw new BadLocationException("Invalid location", length() + 1);
try
{
aTxt.array = get().substring(aWhere, aWhere + aLen).toCharArray();
aTxt.offset = aWhere;
aTxt.count = aLen;
System.out.println(getClass().getName() + ": " + aTxt.count);
System.out.print(getClass().getName() + ": ");
for(int i = 0; i < aTxt.count; i++)
System.out.print(aTxt.array[i]);
System.out.println();
System.out.println(getClass().getName() + ": " + get());
}
catch(Exception e)
{
e.printStackTrace();
}
}
public UndoableEdit remove(int offset, int length)
throws BadLocationException
{
set((new StringBuffer(get())).replace(offset, offset + length, "").toString());
return new RemoveUndo(offset, get().substring(offset, offset + length));
}
public UndoableEdit insertString(int aWhere, String aStr)
throws BadLocationException
{
set((new StringBuffer(get())).replace(aWhere, aWhere, aStr).toString());
return new InsertUndo(aWhere, aStr.length());
}
private Object model;
private Method getter;
private Method setter;
}
Code:
import javax.swing.text.PlainDocument;
public class MyDocument extends PlainDocument
{
public MyDocument(Object anObject, String aPropertyName)
throws IllegalParameterException
{
super(new ContentAdapter(anObject, aPropertyName));
}
}
Code:
import java.awt.Container;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TestTextfieldModel extends JFrame
{
public TestTextfieldModel()
{
data = null;
data = "\n";
}
public String getData()
{
return data;
}
public void setData(String aData)
{
data = aData;
}
public static void main(String args[])
{
TestTextfieldModel textfieldModel = new TestTextfieldModel();
JTextField textField = new JTextField();
javax.swing.text.Document document = null;
try
{
document = new MyDocument(textfieldModel, "Data");
}
catch(IllegalParameterException e)
{
e.printStackTrace();
}
int where = 1;
String a = "an";
String b = "Sdro";
System.out.println("Test: " + b.substring(0, where) + a + b.substring(where));
textField.setDocument(document);
textfieldModel.getContentPane().add(textField);
textfieldModel.pack();
textfieldModel.setDefaultCloseOperation(3);
textfieldModel.setVisible(true);
}
private String data;
}