/trunk/fest/fest-swing/src/main/java/org/fest/swing/format/JTabbedPaneFormatter.java

http://fest.googlecode.com/ · Java · 81 lines · 40 code · 11 blank · 30 comment · 7 complexity · de9b09b527358f841d58904c080402d4 MD5 · raw file

  1. /*
  2. * Created on Dec 24, 2007
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. * in compliance with the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License
  10. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. * or implied. See the License for the specific language governing permissions and limitations under
  12. * the License.
  13. *
  14. * Copyright @2007-2009 the original author or authors.
  15. */
  16. package org.fest.swing.format;
  17. import java.awt.Component;
  18. import javax.swing.JTabbedPane;
  19. import org.fest.util.Arrays;
  20. import static java.lang.String.valueOf;
  21. import static org.fest.util.Strings.*;
  22. /**
  23. * Understands a formatter for <code>{@link JTabbedPane}</code>s.
  24. *
  25. * @author Alex Ruiz
  26. */
  27. public class JTabbedPaneFormatter extends ComponentFormatterTemplate {
  28. private static final String NO_SELECTION = "<No selection>";
  29. /**
  30. * Returns the <code>String</code> representation of the given <code>{@link Component}</code>, which should be a
  31. * <code>{@link JTabbedPane}</code> (or subclass.)
  32. * @param c the given <code>Component</code>.
  33. * @return the <code>String</code> representation of the given <code>JTabbedPane</code>.
  34. */
  35. protected String doFormat(Component c) {
  36. JTabbedPane tabbedPane = (JTabbedPane)c;
  37. return concat(
  38. tabbedPane.getClass().getName(), "[",
  39. "name=", quote(tabbedPane.getName()), ", ",
  40. "selectedTabIndex=", valueOf(tabbedPane.getSelectedIndex()), ", ",
  41. "selectedTabTitle=", selectedTab(tabbedPane), ", ",
  42. "tabCount=", valueOf(tabbedPane.getTabCount()), ", ",
  43. "tabTitles=", Arrays.format(tabTitles(tabbedPane)), ", ",
  44. "enabled=", valueOf(tabbedPane.isEnabled()), ", ",
  45. "visible=", valueOf(tabbedPane.isVisible()), ", ",
  46. "showing=", valueOf(tabbedPane.isShowing()),
  47. "]"
  48. );
  49. }
  50. private String selectedTab(JTabbedPane tabbedPane) {
  51. if (tabbedPane.getTabCount() == 0) return NO_SELECTION;
  52. int index = tabbedPane.getSelectedIndex();
  53. if (index == -1) return NO_SELECTION;
  54. return quote(tabbedPane.getTitleAt(index));
  55. }
  56. private String[] tabTitles(JTabbedPane tabbedPane) {
  57. int count = tabbedPane.getTabCount();
  58. if (count == 0) return new String[0];
  59. String[] titles = new String[count];
  60. for (int i = 0; i < count; i++) titles[i] = tabbedPane.getTitleAt(i);
  61. return titles;
  62. }
  63. /**
  64. * Indicates that this formatter supports <code>{@link JTabbedPane}</code> only.
  65. * @return <code>JTabbedPane.class</code>.
  66. */
  67. public Class<? extends Component> targetType() {
  68. return JTabbedPane.class;
  69. }
  70. }