Visual Color Imbalance Detector: Reconstructed project directories and files
[VistaCID.git] / org / tjworld / components / TitledURLItem.java
1 /*\r
2         * TitledURLItem.java\r
3  *\r
4         * Created on 15 November 2001, 12:52\r
5         * $Header: /VisTA/CID/Components/TitledURLItem.java 1     15/11/01 15:34 Tj $\r
6         *\r
7         * $History: TitledURLItem.java $ \r
8  * \r
9  * *****************  Version 1  *****************\r
10  * User: Tj           Date: 15/11/01   Time: 15:34\r
11  * Created in $/VisTA/CID/Components\r
12  * Generic list item\r
13  */\r
14 \r
15 package org.tjworld.components;\r
16 \r
17 import java.net.URL;\r
18 import java.net.MalformedURLException;\r
19 \r
20 /** Maintains a separate record of the filename.extension part of the URL's path, such\r
21         * that calling getFilename() will return ONLY the simple filename, with no path\r
22         * components.\r
23         *\r
24         * If the URL were: http://host.domain.tld:8080/root/path/filename.extension \r
25         * getFilename() will return: filename.extension\r
26         *\r
27         * Overrides toString() in order to return a value that allows an instance of\r
28         * this Class to be used in Lists and ComboBoxes. Calling toString() is functionally\r
29         * the same as using the accessor method getTitle().\r
30  *\r
31         * @see URL\r
32         * @see JComboBox\r
33         * @see JList\r
34  *\r
35         * @author  TJ\r
36  * @version 1.0\r
37  */\r
38 public class TitledURLItem {\r
39         /** URL associated with the Title */\r
40         protected URL url = null;\r
41 \r
42         /** The descriptive Title (usually displayed in list items rather than the raw URL) */\r
43         protected String title = null;\r
44         \r
45         /** The simple filename.ext without any path components */\r
46         protected String filename = null;\r
47         \r
48         /** Creates new TitledURLItem */\r
49  public TitledURLItem() {\r
50                 this((String)null, (URL)null);\r
51  }\r
52         \r
53         /** Creates new TitledURLItem.\r
54                 * Parses the URL to identify the Filename component\r
55                 * @param title The Title associated with the URL\r
56                 * @param url The URL\r
57                 */\r
58         TitledURLItem(String title, URL url) {\r
59                 this.url = url;\r
60                 this.title = title;\r
61                 this.filename = parseFilename(url);\r
62         }\r
63         \r
64         /** Return the Title associated to the underlying URL.\r
65                 * Applications may want to determine this dynamically by getting the title of the\r
66                 * document addressed by the URL.\r
67                 * @see #setTitle\r
68                 * @see #toString\r
69                 * @return Title associated with the URL\r
70                 */\r
71         public String getTitle() {\r
72                 return title;\r
73         }\r
74         \r
75         /** Sets the Title associated with the underlying URL.\r
76                 * Applications may want to determine this dynamically by using the title of the\r
77                 * document addressed by the URL.\r
78                 * @see #getTitle\r
79                 */\r
80         public void setTitle() {\r
81                 title = null;\r
82         }\r
83 \r
84         /** Gets the simple filename.extension part of the URL\r
85                 * This is useful for applications that use paths to differentiate, for example, \r
86                 * multiple language versions of the same file by path.\r
87                 *\r
88                 * @see #setURL\r
89                 * @return The simple filename\r
90                 */\r
91         public String getFilename() {\r
92                 return filename;\r
93         }\r
94 \r
95         /** Get the underlying URL\r
96                 * @return The URL associated with the Title\r
97                 */\r
98         public URL getURL() {\r
99                 return url;\r
100         }\r
101         \r
102         /** Set the URL associated with this Title and updates the underlying filename returned by getFilename\r
103                 * @param url the new URL\r
104                 * @see #getURL\r
105                 * @see URL\r
106                 */\r
107         public void setURL(URL url) throws MalformedURLException {\r
108                 this.url = new URL(url, "");\r
109                 filename = parseFilename(this.url);\r
110         }\r
111 \r
112  /** Extract the Filename part of a URL\r
113                 * @param u the URL\r
114                 * @return Everything to the right of the last path separator returned by URL.getFile(), or null if url is null\r
115                 * @see #setFilename\r
116                 * @see #getFilename\r
117                 * @see URL#getFile\r
118                 */\r
119  public static String parseFilename(URL u) {\r
120                 String result = null;\r
121                 if(u != null) { // only work with proper URL\r
122                  String file = u.getFile(); // the /path/file component\r
123                  int start = file.lastIndexOf('/');\r
124                         if(start == -1) // when the path separator isn't found, treat the url as relative\r
125                                 start = 0;\r
126                         result = file.substring(start+1);\r
127                 }\r
128                 return result;\r
129         }\r
130 \r
131         /** Gets the Title (functionally identical to getTitle()).\r
132                 * Used by listCellRender objects to determine the text to display\r
133                 * @return the Title\r
134                 */\r
135         public String toString() {\r
136                 return title;\r
137         }                       \r
138 }