001 /* 002 * Created on Dec 23, 2007 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 @2007-2010 the original author or authors. 015 */ 016 package org.fest.swing.format; 017 018 import static java.lang.String.valueOf; 019 import static javax.swing.tree.TreeSelectionModel.*; 020 import static org.fest.util.Strings.concat; 021 import static org.fest.util.Strings.quote; 022 023 import java.awt.Component; 024 025 import javax.swing.JTree; 026 import javax.swing.tree.TreePath; 027 import javax.swing.tree.TreeSelectionModel; 028 029 import org.fest.util.Arrays; 030 031 /** 032 * Understands a formatter for <code>{@link JTree}</code>s. 033 * 034 * @author Alex Ruiz 035 */ 036 public class JTreeFormatter extends ComponentFormatterTemplate { 037 038 private static final String[] EMPTY = new String[0]; 039 040 private static final IntEnum SELECTION_MODES = new IntEnum(); 041 static { 042 SELECTION_MODES.put(SINGLE_TREE_SELECTION, "SINGLE_TREE_SELECTION") 043 .put(CONTIGUOUS_TREE_SELECTION, "CONTIGUOUS_TREE_SELECTION") 044 .put(DISCONTIGUOUS_TREE_SELECTION, "DISCONTIGUOUS_TREE_SELECTION"); 045 } 046 047 /** 048 * Returns the <code>String</code> representation of the given <code>{@link Component}</code>, which should be a 049 * <code>{@link JTree}</code> (or subclass.) 050 * @param c the given <code>Component</code>. 051 * @return the <code>String</code> representation of the given <code>JTree</code>. 052 */ 053 protected String doFormat(Component c) { 054 JTree tree = (JTree)c; 055 return concat( 056 tree.getClass().getName(), "[", 057 "name=", quote(tree.getName()), ", ", 058 "selectionCount=", valueOf(tree.getSelectionCount()), ", ", 059 "selectionPaths=", Arrays.format(selectionPaths(tree)), ", ", 060 "selectionMode=", selectionMode(tree), ", ", 061 "enabled=", valueOf(tree.isEnabled()), ", ", 062 "visible=", valueOf(tree.isVisible()), ", ", 063 "showing=", valueOf(tree.isShowing()), 064 "]" 065 ); 066 } 067 068 private String[] selectionPaths(JTree tree) { 069 TreePath[] paths = tree.getSelectionPaths(); 070 if (paths == null) return EMPTY; 071 int count = paths.length; 072 if (count == 0) return EMPTY; 073 String[] pathArray = new String[count]; 074 for (int i = 0; i < count; i++) { 075 TreePath path = paths[i]; 076 pathArray[i] = path != null ? path.toString() : null; 077 } 078 return pathArray; 079 } 080 081 private String selectionMode(JTree tree) { 082 TreeSelectionModel model = tree.getSelectionModel(); 083 return SELECTION_MODES.get(model.getSelectionMode()); 084 } 085 086 /** 087 * Indicates that this formatter supports <code>{@link JTree}</code> only. 088 * @return <code>JTree.class</code>. 089 */ 090 public Class<? extends Component> targetType() { 091 return JTree.class; 092 } 093 }