This project has retired. For details please refer to its Attic page.
CollectionRenderer xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.workbench.swing;
20  
21  import java.awt.Component;
22  import java.util.Collection;
23  import java.util.GregorianCalendar;
24  
25  import javax.swing.JTable;
26  import javax.swing.table.DefaultTableCellRenderer;
27  
28  import org.apache.chemistry.opencmis.commons.definitions.Choice;
29  import org.apache.chemistry.opencmis.workbench.ClientHelper;
30  
31  public class CollectionRenderer extends DefaultTableCellRenderer {
32      private static final long serialVersionUID = 1L;
33  
34      public CollectionRenderer() {
35          super();
36      }
37  
38      @Override
39      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
40              int row, int column) {
41          Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
42  
43          int height = (int) getPreferredSize().getHeight();
44          if (height > (getFontMetrics(getFont()).getHeight() + getInsets().bottom + getInsets().top)) {
45              if (table.getRowHeight(row) != height) {
46                  table.setRowHeight(row, height);
47              }
48          }
49  
50          return comp;
51      }
52  
53      @Override
54      protected void setValue(Object value) {
55          Collection<?> col = (Collection<?>) value;
56  
57          if ((col == null) || (col.isEmpty())) {
58              super.setValue("");
59              return;
60          }
61  
62          // build string
63          StringBuilder sb = new StringBuilder("<html>");
64          for (Object o : col) {
65              sb.append("<span>"); // workaround for a bug in Swing
66              if (o == null) {
67                  sb.append("<i>null</i>");
68              } else if (o instanceof GregorianCalendar) {
69                  sb.append(ClientHelper.getDateString((GregorianCalendar) o));
70              } else if (o instanceof Choice<?>) {
71                  sb.append(((Choice<?>) o).getValue());
72              } else {
73                  sb.append(o.toString());
74              }
75              sb.append("</span><br/>");
76          }
77          // sb.append("</html>");
78  
79          super.setValue(sb.toString());
80      }
81  }