View Javadoc
1   package net.sourceforge.jsh3modtool.gui.imagetable;
2   
3   import java.awt.FontMetrics;
4   
5   /***
6    * An abstract cell for the image table.
7    * 
8    * @author redsolo
9    */
10  public class BaseCell 
11  {
12      private String          cachedName;
13  
14      private transient int   cachedNameMaxWidth;
15  
16      /***
17       * Creates a new instance of AbstractCell
18       */
19      protected BaseCell() {
20          super();
21      }
22  
23      /***
24       * Returns the cached name. The cached name is the name (shortened) to fit
25       * the latest set max width.
26       * 
27       * @return the cached name.
28       */
29      public String getCachedName() {
30          return cachedName;
31      }
32  
33      /***
34       * Returns the max width that the cached name was created for.
35       * 
36       * @return the max width that the cached name was created for.
37       */
38      public int getCachedNameMaxWidth() {
39          return cachedNameMaxWidth;
40      }
41  
42      /***
43       * Sets the cached name
44       * 
45       * @param name the shortened name
46       * @param maxWidth the max width of the cached name
47       */
48      private void setCachedName(final String name, final int maxWidth) {
49          cachedName = name;
50          cachedNameMaxWidth = maxWidth;
51      }
52  
53      /***
54       * Recache the name for the cell
55       * 
56       * @param fullName the full name that should be shortened
57       * @param maxWidth the max size of the name (of the cached name)
58       * @param fontMetrics a fontmetrics that is used to calculate the width of a string
59       */
60      public void recacheName(final String fullName, final int maxWidth, final FontMetrics fontMetrics) {
61          if (maxWidth != cachedNameMaxWidth) 
62          {
63              String name = fullName;
64              int messageWidth = fontMetrics.stringWidth(name);
65  
66              while (messageWidth > (maxWidth)) 
67              {
68                  name = name.substring(0, name.length() - 4) + "...";
69                  messageWidth = fontMetrics.stringWidth(name);
70              }
71              setCachedName(name, maxWidth);
72          }
73      }
74  }