TreeUIをつくってみたいのだがしんどいので。
2009/07/05
java
swing
Javaです。Swingです。
JTreeの描画部分をオーバライドしていろいろ描画できないかなぁと考えていましたら、JTreeってTreeUIの部分で描画してまして、単純にJTreeのpatintComponentsをオーバライドするだけではだめなのでした。
そこで、TreeUIなのですが、これってLookAndFeelを変更すると変わってしまいます。
それにTreeUIを実装するのってかなりしんどそうです。
Orz...
そこで、TreeUIをくるんで処理を紛れ込ませることにしました。
※エクリプスなら、SourceのGenerate Delegate Methods...で選択されたオブジェクトのデリゲートメソッドを生成してくれますね。
package a;
import javax.swing.plaf.TreeUI;
public class MyTree_2_UI extends TreeUI{
TreeUI treeUI;
public MyTree_2_UI(TreeUI treeUI){
this.treeUI = treeUI;
}
public java.awt.Rectangle getPathBounds(javax.swing.JTree a0,javax.swing.tree.TreePath a1){return this.treeUI.getPathBounds(a0,a1);}
public javax.swing.tree.TreePath getPathForRow(javax.swing.JTree a0,int a1){return this.treeUI.getPathForRow(a0,a1);}
public int getRowForPath(javax.swing.JTree a0,javax.swing.tree.TreePath a1){return this.treeUI.getRowForPath(a0,a1);}
public javax.swing.tree.TreePath getClosestPathForLocation(javax.swing.JTree a0,int a1,int a2){return this.treeUI.getClosestPathForLocation(a0,a1,a2);}
public boolean stopEditing(javax.swing.JTree a0){return this.treeUI.stopEditing(a0);}
public void cancelEditing(javax.swing.JTree a0){this.treeUI.cancelEditing(a0);}
public void startEditingAtPath(javax.swing.JTree a0,javax.swing.tree.TreePath a1){this.treeUI.startEditingAtPath(a0,a1);}
public javax.swing.tree.TreePath getEditingPath(javax.swing.JTree a0){return this.treeUI.getEditingPath(a0);}
public int getRowCount(javax.swing.JTree a0){return this.treeUI.getRowCount(a0);}
public boolean isEditing(javax.swing.JTree a0){return this.treeUI.isEditing(a0);}
public boolean contains(javax.swing.JComponent a0,int a1,int a2){return this.treeUI.contains(a0,a1,a2);}
public void update(java.awt.Graphics a0,javax.swing.JComponent a1){this.treeUI.update(a0,a1);}
public java.awt.Dimension getPreferredSize(javax.swing.JComponent a0){return this.treeUI.getPreferredSize(a0);}
public java.awt.Dimension getMinimumSize(javax.swing.JComponent a0){return this.treeUI.getMinimumSize(a0);}
public java.awt.Dimension getMaximumSize(javax.swing.JComponent a0){return this.treeUI.getMaximumSize(a0);}
public void paint(java.awt.Graphics a0,javax.swing.JComponent a1){this.treeUI.paint(a0,a1);}
public int getAccessibleChildrenCount(javax.swing.JComponent a0){return this.treeUI.getAccessibleChildrenCount(a0);}
public javax.accessibility.Accessible getAccessibleChild(javax.swing.JComponent a0,int a1){return this.treeUI.getAccessibleChild(a0,a1);}
public void uninstallUI(javax.swing.JComponent a0){this.treeUI.uninstallUI(a0);}
public void installUI(javax.swing.JComponent a0){this.treeUI.installUI(a0);}
public int hashCode(){return this.treeUI.hashCode();}
public boolean equals(java.lang.Object a0){return this.treeUI.equals(a0);}
public java.lang.String toString(){return this.treeUI.toString();}
}
import javax.swing.plaf.TreeUI;
public class MyTree_2_UI extends TreeUI{
TreeUI treeUI;
public MyTree_2_UI(TreeUI treeUI){
this.treeUI = treeUI;
}
public java.awt.Rectangle getPathBounds(javax.swing.JTree a0,javax.swing.tree.TreePath a1){return this.treeUI.getPathBounds(a0,a1);}
public javax.swing.tree.TreePath getPathForRow(javax.swing.JTree a0,int a1){return this.treeUI.getPathForRow(a0,a1);}
public int getRowForPath(javax.swing.JTree a0,javax.swing.tree.TreePath a1){return this.treeUI.getRowForPath(a0,a1);}
public javax.swing.tree.TreePath getClosestPathForLocation(javax.swing.JTree a0,int a1,int a2){return this.treeUI.getClosestPathForLocation(a0,a1,a2);}
public boolean stopEditing(javax.swing.JTree a0){return this.treeUI.stopEditing(a0);}
public void cancelEditing(javax.swing.JTree a0){this.treeUI.cancelEditing(a0);}
public void startEditingAtPath(javax.swing.JTree a0,javax.swing.tree.TreePath a1){this.treeUI.startEditingAtPath(a0,a1);}
public javax.swing.tree.TreePath getEditingPath(javax.swing.JTree a0){return this.treeUI.getEditingPath(a0);}
public int getRowCount(javax.swing.JTree a0){return this.treeUI.getRowCount(a0);}
public boolean isEditing(javax.swing.JTree a0){return this.treeUI.isEditing(a0);}
public boolean contains(javax.swing.JComponent a0,int a1,int a2){return this.treeUI.contains(a0,a1,a2);}
public void update(java.awt.Graphics a0,javax.swing.JComponent a1){this.treeUI.update(a0,a1);}
public java.awt.Dimension getPreferredSize(javax.swing.JComponent a0){return this.treeUI.getPreferredSize(a0);}
public java.awt.Dimension getMinimumSize(javax.swing.JComponent a0){return this.treeUI.getMinimumSize(a0);}
public java.awt.Dimension getMaximumSize(javax.swing.JComponent a0){return this.treeUI.getMaximumSize(a0);}
public void paint(java.awt.Graphics a0,javax.swing.JComponent a1){this.treeUI.paint(a0,a1);}
public int getAccessibleChildrenCount(javax.swing.JComponent a0){return this.treeUI.getAccessibleChildrenCount(a0);}
public javax.accessibility.Accessible getAccessibleChild(javax.swing.JComponent a0,int a1){return this.treeUI.getAccessibleChild(a0,a1);}
public void uninstallUI(javax.swing.JComponent a0){this.treeUI.uninstallUI(a0);}
public void installUI(javax.swing.JComponent a0){this.treeUI.installUI(a0);}
public int hashCode(){return this.treeUI.hashCode();}
public boolean equals(java.lang.Object a0){return this.treeUI.equals(a0);}
public java.lang.String toString(){return this.treeUI.toString();}
}
ついでにコード。
package a;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import javax.swing.plaf.TreeUI;
public class Test_print_TreeUI_override_method_implementation {
public static void main(String[] args) {
a();
}
static void a() {
Method[] methods = TreeUI.class.getMethods();
for (Method method : methods) {
if (Modifier.isFinal(method.getModifiers()))
continue;
print(method, "treeUI");
}
}
static void print(Method m, String o) {
final char SP = ' ';
StringBuilder b = new StringBuilder();
b.append(Modifier.toString(m.getModifiers() & Modifier.PUBLIC)).append(
SP);
b.append(m.getReturnType().getCanonicalName()).append(SP);
b.append(m.getName()).append('(');
b.append(param(m));
b.append(')').append(throwsException(m));
b.append('{');
{
if (!m.getReturnType().getSimpleName().equals("void")) {
b.append("return ");
}
b.append("this.").append(o).append('.').append(m.getName()).append(
"(");
int cnt = m.getParameterTypes().length;
for (int i = 0; i < cnt; i++) {
b.append('a').append(i).append(',');
}
if(0 < cnt)b.deleteCharAt(b.length() - 1);
b.append(");");
}
b.append('}');
System.out.println(b);
}
static StringBuilder param(Method m) {
final char SP = ' ';
StringBuilder b = new StringBuilder();
int pos = 0;
for (Class<?> classes : m.getParameterTypes()) {
b.append(classes.getCanonicalName()).append(SP).append('a').append(
pos++).append(',');
}
if (0 < b.length())
b.deleteCharAt(b.length() - 1);
return b;
}
static StringBuilder throwsException(Method m) {
StringBuilder b = new StringBuilder();
if (!(0 < m.getExceptionTypes().length))
return b;
b.append("throws ");
final char SP = ' ';
for (Class<?> classes : m.getExceptionTypes()) {
b.append(classes.getCanonicalName()).append(SP).append(',');
}
b.deleteCharAt(b.length() - 1);
return b;
}
}
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import javax.swing.plaf.TreeUI;
public class Test_print_TreeUI_override_method_implementation {
public static void main(String[] args) {
a();
}
static void a() {
Method[] methods = TreeUI.class.getMethods();
for (Method method : methods) {
if (Modifier.isFinal(method.getModifiers()))
continue;
print(method, "treeUI");
}
}
static void print(Method m, String o) {
final char SP = ' ';
StringBuilder b = new StringBuilder();
b.append(Modifier.toString(m.getModifiers() & Modifier.PUBLIC)).append(
SP);
b.append(m.getReturnType().getCanonicalName()).append(SP);
b.append(m.getName()).append('(');
b.append(param(m));
b.append(')').append(throwsException(m));
b.append('{');
{
if (!m.getReturnType().getSimpleName().equals("void")) {
b.append("return ");
}
b.append("this.").append(o).append('.').append(m.getName()).append(
"(");
int cnt = m.getParameterTypes().length;
for (int i = 0; i < cnt; i++) {
b.append('a').append(i).append(',');
}
if(0 < cnt)b.deleteCharAt(b.length() - 1);
b.append(");");
}
b.append('}');
System.out.println(b);
}
static StringBuilder param(Method m) {
final char SP = ' ';
StringBuilder b = new StringBuilder();
int pos = 0;
for (Class<?> classes : m.getParameterTypes()) {
b.append(classes.getCanonicalName()).append(SP).append('a').append(
pos++).append(',');
}
if (0 < b.length())
b.deleteCharAt(b.length() - 1);
return b;
}
static StringBuilder throwsException(Method m) {
StringBuilder b = new StringBuilder();
if (!(0 < m.getExceptionTypes().length))
return b;
b.append("throws ");
final char SP = ' ';
for (Class<?> classes : m.getExceptionTypes()) {
b.append(classes.getCanonicalName()).append(SP).append(',');
}
b.deleteCharAt(b.length() - 1);
return b;
}
}
: