KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > util > FilePathCellRenderer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.util;
21
22 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
23 import java.awt.FontMetrics JavaDoc;
24 import java.awt.Graphics JavaDoc;
25
26 /**
27  * Treats values in cells as file paths and renders them so that the end of the path is always visible.
28  *
29  * @author Maros Sandor
30  */

31 public class FilePathCellRenderer extends DefaultTableCellRenderer JavaDoc {
32
33     private static final int VISIBLE_START_CHARS = 0;
34
35     private String JavaDoc computeFitText(String JavaDoc text) {
36         if (text.length() <= VISIBLE_START_CHARS + 3) return text;
37
38         FontMetrics JavaDoc fm = getFontMetrics(getFont());
39         int width = getSize().width;
40             
41         String JavaDoc prefix = text.substring(0, VISIBLE_START_CHARS) + "...";
42         int prefixLength = fm.stringWidth(prefix);
43         int desired = width - prefixLength - 2;
44         if (desired <= 0) return text;
45         
46         for (int i = text.length() - 1; i >= 0; i--) {
47             String JavaDoc suffix = text.substring(i);
48             int swidth = fm.stringWidth(suffix);
49             if (swidth >= desired) {
50                 return suffix.length() > 0 ? prefix + suffix.substring(1) : text;
51             }
52         }
53         return text;
54     }
55     
56     protected void paintComponent(Graphics JavaDoc g) {
57         setText(computeFitText(getText()));
58         super.paintComponent(g);
59     }
60 }
61
Popular Tags