001 /* 002 * Created on Apr 14, 2008 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 * 014 * Copyright @2008-2010 the original author or authors. 015 */ 016 package org.fest.swing.driver; 017 018 import static org.fest.swing.util.Strings.isDefaultToString; 019 020 import java.awt.Component; 021 022 import javax.swing.JTree; 023 import javax.swing.tree.TreeCellRenderer; 024 025 import org.fest.swing.annotation.RunsInCurrentThread; 026 import org.fest.swing.cell.JTreeCellReader; 027 028 /** 029 * Understands the default implementation of <code>{@link JTreeCellReader}</code>. 030 * 031 * @author Yvonne Wang 032 * @author Alex Ruiz 033 */ 034 public class BasicJTreeCellReader implements JTreeCellReader { 035 036 private final CellRendererReader rendererReader; 037 038 /** 039 * Creates a new </code>{@link BasicJTreeCellReader}</code> that uses a 040 * <code>{@link BasicCellRendererReader}</code> to read the value from the cell renderer component in a 041 * <code>JTree</code>. 042 */ 043 public BasicJTreeCellReader() { 044 this(new BasicCellRendererReader()); 045 } 046 047 /** 048 * Creates a new </code>{@link BasicJTreeCellReader}</code>. 049 * @param rendererReader knows how to read values from the cell renderer component in a 050 * <code>JTree</code>. 051 * @throws NullPointerException if <code>rendererReader</code> is <code>null</code>. 052 */ 053 public BasicJTreeCellReader(CellRendererReader rendererReader) { 054 if (rendererReader == null) 055 throw new NullPointerException("CellRendererReader should not be null"); 056 this.rendererReader = rendererReader; 057 } 058 059 /** 060 * Returns the internal value of a cell in a <code>{@link JTree}</code> as expected in a test. 061 * <p> 062 * <b>Note:</b> This method is <b>not</b> guaranteed to be executed in the event dispatch thread (EDT.) Clients are 063 * responsible for calling this method from the EDT. 064 * </p> 065 * @param tree the given <code>JTree</code>. 066 * @param modelValue the value of a cell, retrieved from the model. 067 * @return the internal value of a cell in a <code>JTree</code> as expected in a test. 068 */ 069 @RunsInCurrentThread 070 public String valueAt(JTree tree, Object modelValue) { 071 TreeCellRenderer r = tree.getCellRenderer(); 072 Component c = r.getTreeCellRendererComponent(tree, modelValue, false, false, false, 0, false); 073 String value = (c != null) ? rendererReader.valueFrom(c) : null; 074 if (value != null) return value; 075 value= tree.convertValueToText(modelValue, false, false, false, 0, false); 076 if (isDefaultToString(value)) return null; 077 return value; 078 } 079 }