/hudson-core/src/main/java/hudson/views/ListViewColumn.java

http://github.com/hudson/hudson · Java · 173 lines · 75 code · 13 blank · 85 comment · 7 complexity · bba13924cb38e4d8f6d83e506bb724ad MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Martin Eigenbrodt
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.views;
  25. import hudson.DescriptorExtensionList;
  26. import hudson.Extension;
  27. import hudson.ExtensionPoint;
  28. import hudson.model.Describable;
  29. import hudson.model.Descriptor;
  30. import hudson.model.Descriptor.FormException;
  31. import hudson.model.Hudson;
  32. import hudson.model.Item;
  33. import hudson.model.ItemGroup;
  34. import hudson.model.ListView;
  35. import hudson.model.View;
  36. import hudson.util.DescriptorList;
  37. import org.kohsuke.stapler.export.Exported;
  38. import java.util.ArrayList;
  39. import java.util.Arrays;
  40. import java.util.List;
  41. import java.util.logging.Level;
  42. import java.util.logging.Logger;
  43. /**
  44. * Extension point for adding a column to a table rendering of {@link Item}s, such as {@link ListView}.
  45. *
  46. * <p>
  47. * This object must have the <tt>column.jelly</tt>. This view
  48. * is called for each cell of this column. The {@link Item} object
  49. * is passed in the "job" variable. The view should render
  50. * the &lt;td> tag.
  51. *
  52. * <p>
  53. * This object may have an additional <tt>columHeader.jelly</tt>. The default ColmnHeader
  54. * will render {@link #getColumnCaption()}.
  55. *
  56. * <p>
  57. * If you opt to {@linkplain ListViewColumnDescriptor#shownByDefault() be shown by default},
  58. * there also must be a default constructor, which is invoked to create a list view column in
  59. * the default configuration.
  60. *
  61. * <p>
  62. * Originally, this extension point was designed for {@link ListView}, but since then
  63. * it has grown to be applicable to other {@link View}s and {@link ItemGroup}s that render
  64. * a collection of {@link Item}s in a tabular format.
  65. *
  66. * @author Kohsuke Kawaguchi
  67. * @since 1.279
  68. * @see ListViewColumnDescriptor
  69. */
  70. public abstract class ListViewColumn implements ExtensionPoint, Describable<ListViewColumn> {
  71. /**
  72. * Returns the name of the column that explains what this column means
  73. *
  74. * @return
  75. * The convention is to use capitalization like "Foo Bar Zot".
  76. */
  77. @Exported
  78. public String getColumnCaption() {
  79. return getDescriptor().getDisplayName();
  80. }
  81. /**
  82. * Returns all the registered {@link ListViewColumn} descriptors.
  83. */
  84. public static DescriptorExtensionList<ListViewColumn, Descriptor<ListViewColumn>> all() {
  85. return Hudson.getInstance().<ListViewColumn, Descriptor<ListViewColumn>>getDescriptorList(ListViewColumn.class);
  86. }
  87. /**
  88. * All registered {@link ListViewColumn}s.
  89. * @deprecated as of 1.281
  90. * Use {@link #all()} for read access and {@link Extension} for registration.
  91. */
  92. public static final DescriptorList<ListViewColumn> LIST = new DescriptorList<ListViewColumn>(ListViewColumn.class);
  93. /**
  94. * Whether this column will be shown by default.
  95. * The default implementation is true.
  96. *
  97. * @since 1.301
  98. * @deprecated as of 1.342.
  99. * Use {@link ListViewColumnDescriptor#shownByDefault()}
  100. */
  101. public boolean shownByDefault() {
  102. return true;
  103. }
  104. /**
  105. * For compatibility reason, this method may not return a {@link ListViewColumnDescriptor}
  106. * and instead return a plain {@link Descriptor} instance.
  107. */
  108. public Descriptor<ListViewColumn> getDescriptor() {
  109. return Hudson.getInstance().getDescriptorOrDie(getClass());
  110. }
  111. /**
  112. * Creates the list of {@link ListViewColumn}s to be used for newly created {@link ListView}s and their likes.
  113. * @since 1.391
  114. */
  115. public static List<ListViewColumn> createDefaultInitialColumnList() {
  116. // OK, set up default list of columns:
  117. // create all instances
  118. ArrayList<ListViewColumn> r = new ArrayList<ListViewColumn>();
  119. DescriptorExtensionList<ListViewColumn, Descriptor<ListViewColumn>> all = ListViewColumn.all();
  120. ArrayList<Descriptor<ListViewColumn>> left = new ArrayList<Descriptor<ListViewColumn>>(all);
  121. for (Class<? extends ListViewColumn> d: DEFAULT_COLUMNS) {
  122. Descriptor<ListViewColumn> des = all.find(d);
  123. if (des != null) {
  124. try {
  125. r.add(des.newInstance(null, null));
  126. left.remove(des);
  127. } catch (FormException e) {
  128. LOGGER.log(Level.WARNING, "Failed to instantiate "+des.clazz,e);
  129. }
  130. }
  131. }
  132. for (Descriptor<ListViewColumn> d : left)
  133. try {
  134. if (d instanceof ListViewColumnDescriptor) {
  135. ListViewColumnDescriptor ld = (ListViewColumnDescriptor) d;
  136. if (!ld.shownByDefault()) continue; // skip this
  137. }
  138. ListViewColumn lvc = d.newInstance(null, null);
  139. if (!lvc.shownByDefault()) continue; // skip this
  140. r.add(lvc);
  141. } catch (FormException e) {
  142. LOGGER.log(Level.WARNING, "Failed to instantiate "+d.clazz,e);
  143. }
  144. return r;
  145. }
  146. /**
  147. * Traditional column layout before the {@link ListViewColumn} becomes extensible.
  148. */
  149. private static final List<Class<? extends ListViewColumn>> DEFAULT_COLUMNS = Arrays.asList(
  150. StatusColumn.class,
  151. WeatherColumn.class,
  152. JobColumn.class,
  153. LastSuccessColumn.class,
  154. LastFailureColumn.class,
  155. LastDurationColumn.class,
  156. ConsoleColumn.class,
  157. BuildButtonColumn.class
  158. );
  159. private static final Logger LOGGER = Logger.getLogger(ListViewColumn.class.getName());
  160. }