java - How to use relative cell highlighting in SwingX? -
i want highlight cells in org.jdesktop.swingx.jxtreetable relative value inside in swingx showcase under 'highlighting (extended)':
there code provided in showcase can't work, doesn't highlight anything. can't comprehend highlighter gets bind values (maybe provided code not complete). there no documentation available.
here code showcase:
this code:
treeframe.java
private void initialize( profilethread profilethread ) { try { uimanager.setlookandfeel( uimanager.getsystemlookandfeelclassname() ); } catch ( exception e ) { e.printstacktrace(); } setbounds( 0, 0, 800, 600 ); setdefaultcloseoperation( jframe.exit_on_close ); settitle( "profilelog" ); noroottreetablemodel model = new noroottreetablemodel( profilethread.getprofilethreadelements(), ); jxtreetable treetable = new jxtreetable( model ); mattepainter painter = new mattepainter( paintutils.setalpha( color.red, 125 ) ); highlighter = new relativepainterhighlighter( painter ); highlighter.sethighlightpredicate( highlightpredicate.always ); treetable.addhighlighter( highlighter ); jscrollpane treeview = new jscrollpane( treetable ); getcontentpane().add( treeview ); setvisible( true ); } public void setcurrentduration( long duration ) { durationrelativizer relativizer = createdurationrelativizer( duration ); highlighter.setrelativizer( relativizer ); } /** * creates , returns relativizer given intermediate value. * */ private durationrelativizer createdurationrelativizer( long duration ) { return new durationrelativizer( 0, true, 15000, duration ); } public static class durationrelativizer extends numberrelativizer { public durationrelativizer( int column, boolean spreadcolumns, number max, number current ) { super( column, spreadcolumns, max, current ); } @override protected number getnumber( componentadapter adapter ) { if ( !( adapter.getvalue( getvaluecolumn() ) instanceof profilethreadelement ) ) { return null; } return ( (profilethreadelement) adapter.getvalue( getvaluecolumn() ) ).getduration(); } }
i took relevant pieces of code example , changed them requirements. relativepainterhighlighter class same in showcase.
this profilethreadelement class:
profilethreadelement.java
import java.util.arraylist; import java.util.date; import java.util.enumeration; import java.util.iterator; import java.util.list; import javax.swing.tree.treenode; public class profilethreadelement implements treenode { private date start; private date end; private long duration; private string text; private string result; private profilethreadelement parent; private list<profilethreadelement> children; /** * instantiates new profile thread element. * * @param timestamp timestamp * @param text text */ public profilethreadelement( date timestamp, string text ) { super(); this.start = timestamp; this.text = text; this.children = new arraylist<profilethreadelement>(); } /** * close. * * @param timestamp timestamp * @param duration duration * @param result result */ public void close( date timestamp, long duration, string result ) { this.end = timestamp; this.duration = duration; this.result = result; } /** * adds children. * * @param child child */ public void addchildren( profilethreadelement child ) { // if ( children == null ) // { // children = new arraylist<profilethreadelement>(); // } children.add( child ); child.setparent( ); } /** * @param parent parent set */ void setparent( profilethreadelement parent ) { this.parent = parent; } /** * checks if open. * * @return true, if open */ public boolean isopen() { return end == null; } /** * @return start */ public date getstart() { return start; } /** * @return end */ public date getend() { return end; } /** * @return duration */ public long getduration() { return duration; } /** * @return text */ public string gettext() { return text; } /** * @return result */ public string getresult() { return result; } /** * @return parent */ public profilethreadelement getparent() { return parent; } /** * @return children */ public list<profilethreadelement> getchildren() { return children; } /* * (non-javadoc) * * @see javax.swing.tree.treenode#children() */ @override public enumeration<profilethreadelement> children() { return new enumeration<profilethreadelement>() { iterator<profilethreadelement> iterator = children.iterator(); @override public boolean hasmoreelements() { return iterator.hasnext(); } @override public profilethreadelement nextelement() { return iterator.next(); } }; } /* * (non-javadoc) * * @see javax.swing.tree.treenode#getallowschildren() */ @override public boolean getallowschildren() { return !children.isempty(); } /* * (non-javadoc) * * @see javax.swing.tree.treenode#getchildat(int) */ @override public treenode getchildat( int childindex ) { return children.get( childindex ); } /* * (non-javadoc) * * @see javax.swing.tree.treenode#getchildcount() */ @override public int getchildcount() { return children.size(); } /* * (non-javadoc) * * @see javax.swing.tree.treenode#getindex(javax.swing.tree.treenode) */ @override public int getindex( treenode node ) { return children.indexof( node ); } /* * (non-javadoc) * * @see javax.swing.tree.treenode#isleaf() */ @override public boolean isleaf() { return children.isempty(); } @override public string tostring() { stringbuffer sb = new stringbuffer(); sb.append( duration ); sb.append( " ms " ); sb.append( text ); return sb.tostring(); } }
and tree table model:
noroottreetablemodel.java
public class noroottreetablemodel extends abstracttreetablemodel { private final static string[] column_names = { "duration (ms)", "function call", "class" }; private list<profilethreadelement> profilethreadelements; private treeframe frame; public noroottreetablemodel( list<profilethreadelement> profilethreadelements, treeframe frame ) { super( new object() ); this.profilethreadelements = profilethreadelements; this.frame = frame; } @override public int getcolumncount() { return column_names.length; } @override public string getcolumnname( int column ) { return column_names[column]; } @override public boolean iscelleditable( object node, int column ) { return false; } @override public object getvalueat( object node, int column ) { if ( node instanceof profilethreadelement ) { matcher matcher; pattern pattern; profilethreadelement element = (profilethreadelement) node; switch ( column ) { case 0: // duration frame.setcurrentduration( element.getduration() ); return element.getduration(); case 1: // function pattern = pattern.compile( "\\.[a-z][a-za-z]+\\(.*\\)" ); matcher = pattern.matcher( element.gettext() ); if ( matcher.find() ) return matcher.group().substring( 1 ); else return ""; case 2: // class pattern = pattern.compile( ".+?(?=\\.[a-z][a-za-z]+\\(.*\\))" ); matcher = pattern.matcher( element.gettext() ); if ( matcher.find() ) return matcher.group().replace( "class ", "" ); else return ""; } } return null; } @override public int getchildcount( object parent ) { if ( parent instanceof profilethreadelement ) { profilethreadelement element = (profilethreadelement) parent; return element.getchildren().size(); } return profilethreadelements.size(); } @override public object getchild( object parent, int index ) { if ( parent instanceof profilethreadelement ) { profilethreadelement element = (profilethreadelement) parent; return element.getchildren().get( index ); } return profilethreadelements.get( index ); } @override public int getindexofchild( object parent, object child ) { profilethreadelement element = (profilethreadelement) parent; return element.getchildren().indexof( (profilethreadelement) child ); } }
i think biggest problem have no clue call treeframe.setcurrentduration(...) method. missing in showcase demo. has got relative highlighting work?
any appreciated, in advance!
ps: if missed or if need further information, please tell me!
i've cut out didn't need , noticed dohighlight(..) never called. reason canhighlight(..) returned false because relativizer null.
Comments
Post a Comment