下面列出了javax.swing.tree.DefaultMutableTreeNode#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void findTreeChildValue(String findText,List<TreePath> treePathLst) {
JTree tree = getTree();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)tree.getModel().getRoot();
Enumeration e = root.depthFirstEnumeration();
treePathLst.clear();
curPos = 0;
while (e.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
if (node.isLeaf()) {
String str = node.toString();
if (str.substring(2).indexOf(findText) >= 0) {
tree.expandPath(new TreePath(node.getPath()));
TreePath tp = expandTreeNode(tree,node.getPath(), true);
treePathLst.add(tp);
}
}
}
if(!treePathLst.isEmpty()){
tree.setSelectionPath(treePathLst.get(0));
tree.scrollPathToVisible(treePathLst.get(0));
}
// return treePathLst;
}
private void insertReports( final TreeModel model,
final Object currentLevel,
final XulMenupopup popup ) throws XulException {
final int childCount = model.getChildCount( currentLevel );
for ( int i = 0; i < childCount; i += 1 ) {
final ReportDesignerView frame = context.getView();
final Object child = model.getChild( currentLevel, i );
if ( model.isLeaf( child ) ) {
final DefaultMutableTreeNode node = (DefaultMutableTreeNode) child;
final File file = new File( String.valueOf( node.getUserObject() ) );
final OpenSampleReportAction action = new OpenSampleReportAction( file, node.toString() );
action.setReportDesignerContext( context );
popup.addChild( frame.createMenuItem( action ) );
} else {
final XulMenupopup childPopup = frame.createPopupMenu( String.valueOf( child ), popup );
insertReports( model, child, childPopup );
}
}
}
/**
* Deletes the selected node from the tree and removes the corresponding entry from the report.
* @param evt
*/
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
selected = (DefaultMutableTreeNode) reportTree.getLastSelectedPathComponent();
if (selected == null) {
//Nothing is selected
return;
}
//If it's a host
if (selected.getPath().length == 2) {
String hostname = selected.toString();
int toDelete = this.getIndexOfHost(hostname);
if (toDelete == -1) {
return;
}
report.getReports().remove(toDelete);
} else if (selected.getPath().length == 3) { //If it's a rule
String host = selected.getParent().toString();
String rule = selected.toString();
int hostNum = this.getIndexOfHost(host);
int ruleNum = this.getIndexOfRule(hostNum, rule);
if (hostNum == -1 || ruleNum == -1) {
return;
}
report.getReports().get(hostNum).getMatchedRules().remove(ruleNum);
}
MutableTreeNode parent = (MutableTreeNode) selected.getParent();
int index = parent.getIndex(selected);
parent.remove(selected);
model.nodesWereRemoved(parent, new int[]{index}, new Object[]{selected});
reportTree.setModel(model);
}
private void initComponents(){
treeRoot = new DefaultMutableTreeNode(new File(ProfileGraph2D.LOG_FILEPATH));
treeModel = new DefaultTreeModel(treeRoot);
tree = new JTree(treeRoot){
@Override
public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
try{
File f = (File) node.getUserObject();
return f.getName();
}
catch(Exception e){
return node.toString();
}
}
};
tree .setModel(treeModel);
tree .expandRow(0);
tree .setShowsRootHandles(true);
btnOpenFiles = new JButton("Open File(s)");
actionButtons .add(btnOpenFiles);
btnDeleteFiles = new JButton("Delete File(s)");
actionButtons .add(btnDeleteFiles);
btnReloadFiles = new JButton("Refresh");
actionButtons .add(btnReloadFiles);
}
public String getFileName(final DefaultMutableTreeNode node) {
return node.toString();
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
Object o = ((DefaultMutableTreeNode) value).getUserObject();
if (o instanceof DataSet) {
setIcon(Styles.COMPUTER_ICON);
setToolTipText(null);
}
else if (o instanceof DataType) {
setIcon(DATATYPE_ICON);
setToolTipText(((DataType) o).getName());
}
else if (o instanceof Process) {
setIcon(PROCESS_ICON);
// Show the command line on Process nodes
// Truncate names longer than 100 characters
String commandLine = ((Process) o).getCommandLine();
if (commandLine.length() > 0) {
if (commandLine.length() > 100) {
commandLine = commandLine.substring(0, 50) + " ... "
+ commandLine.substring(commandLine.length() - 50);
}
setToolTipText(commandLine);
}
else {
setToolTipText(((Process) o).getName());
}
}
else if (o instanceof String) {
String s = (String) o;
if (s.equals(TreePanel.ROOT_NAME)) {
setIcon(Styles.REPORT_ICON);
setToolTipText(null);
}
else if (s.equals("TOP")) {
setIcon(DATASUBTYPE_ICON);
setToolTipText("Top Processes");
}
else if (s.equals("Process")) {
setIcon(DATASUBTYPE_ICON);
setToolTipText("Processes");
}
else if (s.equals("GC")) {
setIcon(DATASUBTYPE_ICON);
setToolTipText("Garbage Collection");
}
else {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) ((DefaultMutableTreeNode) value).getParent();
if (parent.getUserObject() instanceof DataSet) {
// sub data type
setIcon(DATASUBTYPE_ICON);
// set tool tip the same as the first child
setToolTipText(s);
}
else {
String p = parent.toString();
if (p.equals("TOP")) {
setIcon(DATATYPE_ICON);
}
if (p.equals("Process")) {
setIcon(DATATYPE_ICON);
}
else if (p.equals("GC")) {
setIcon(DATATYPE_ICON);
}
else {
setIcon(FIELD_ICON);
}
setToolTipText(null);
}
}
}
return this;
}
@RequiredUIAccess
@Override
public void customizeCellRenderer(final JTree tree,
final Object value,
final boolean selected,
final boolean expanded,
final boolean leaf,
final int row,
final boolean hasFocus) {
myRow = row;
myDurationWidth = -1;
final DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
final Object userObj = node.getUserObject();
if (userObj instanceof SMTRunnerNodeDescriptor) {
final SMTRunnerNodeDescriptor desc = (SMTRunnerNodeDescriptor)userObj;
final SMTestProxy testProxy = desc.getElement();
if (testProxy instanceof SMTestProxy.SMRootTestProxy) {
SMTestProxy.SMRootTestProxy rootTestProxy = (SMTestProxy.SMRootTestProxy) testProxy;
if (node.isLeaf()) {
TestsPresentationUtil.formatRootNodeWithoutChildren(rootTestProxy, this);
} else {
TestsPresentationUtil.formatRootNodeWithChildren(rootTestProxy, this);
}
if (myAdditionalRootFormatter != null) {
myAdditionalRootFormatter.format(rootTestProxy, this);
}
} else {
TestsPresentationUtil.formatTestProxy(testProxy, this);
}
if (TestConsoleProperties.SHOW_INLINE_STATISTICS.value(myConsoleProperties)) {
String durationString = testProxy.getDurationString(myConsoleProperties);
if (durationString != null) {
durationString = " " + durationString;
myDurationWidth = getFontMetrics(getFont()).stringWidth(durationString);
if (((TestTreeView)myTree).isExpandableHandlerVisibleForCurrentRow(myRow)) {
append(durationString);
}
}
}
//Done
return;
}
//strange node
final String text = node.toString();
//no icon
append(text != null ? text : SPACE_STRING, SimpleTextAttributes.GRAYED_ATTRIBUTES);
}
public String convert(TreePath object) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)object.getLastPathComponent();
return node.toString();
}