![JAR search and dependency download from the Maven repository](/logo.png)
tinylaf-1_4_0_src.src.de.muntjak.tinylookandfeel.ThemeDescription Maven / Gradle / Ivy
Show all versions of tinylaf Show documentation
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of the Tiny Look and Feel *
* Copyright 2003 - 2008 Hans Bickel *
* *
* For licensing information and credits, please refer to the *
* comment in file de.muntjak.tinylookandfeel.TinyLookAndFeel *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package de.muntjak.tinylookandfeel;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
/**
* ThemeDescription
describes a TinyLaF theme by
* its URL and its name.
*
* You can call
* {@link de.muntjak.tinylookandfeel.Theme#getAvailableThemes()}
* to get an array of ThemeDescription
objects, each
* describing a unique TinyLaF theme resource.
*
* @author Hans Bickel
* @version 1.0
*/
public class ThemeDescription {
private URI uri;
private URL url;
private File file;
private boolean fileHasBeenSet = false;
private String name = null;
/**
* Constructs a ThemeDescription
using the specified URL.
* @param url a non-null URL
* @throws IllegalArgumentException if argument is null
*/
public ThemeDescription(URL url) {
if(url == null) {
throw new IllegalArgumentException("url may not be null");
}
try {
// Note: When using getPath() the "jar:" prefix
// of JAR URLs is omitted
if(!"file".equals(url.getProtocol())) {
uri = new URI(url.getPath());
}
else {
uri = new URI(url.toExternalForm());
}
// no error
this.url = url;
}
catch(URISyntaxException ex) {
uri = null;
}
}
/**
* Constructs a ThemeDescription
using the specified URI.
*
* Note: You can get an URI from a file by calling File.toURI()
.
* @param uri a non-null URI
* @throws IllegalArgumentException if argument is null
*/
public ThemeDescription(URI uri) {
if(uri == null) {
throw new IllegalArgumentException("uri may not be null");
}
try {
url = uri.toURL();
// no error
this.uri = uri;
}
catch(MalformedURLException ex) {
url = null;
}
}
/**
* Two ThemeDescription
objects are equal if
* their URIs are equal.
*/
public boolean equals(Object o) {
if(o == this) return true;
if(o == null || !(o instanceof ThemeDescription)) return false;
ThemeDescription other = (ThemeDescription)o;
if(isValid() != other.isValid()) return false;
if(isValid()) return uri.equals(other.uri);
// both are invalid -> null equals null
return true;
}
/**
* Returns true
if the URI
or URL
* argument given at construction time was valid,
* false
otherwise.
* @return true
if the URI
or URL
* argument given at construction time was valid,
* false
otherwise.
*/
public boolean isValid() {
return (uri != null && url != null);
}
/**
* Returns the name of the TinyLaF theme, for example: "Golden"
* (for a URL of .../Golden.theme
).
* The returned string will be null
if the URL
or
* URI
specified at construction time was invalid.
* @return the name of the TinyLaF theme
*/
public String getName() {
if(uri == null) return "? URI == null ?";
if(name == null) {
name = getName(uri.getPath());
}
return name;
}
private static String getName(String path) {
if(path == null) return "? uri.getPath() == null ?";
String namePart = path.substring(path.lastIndexOf("/") + 1);
int index = namePart.indexOf(".");
if(index == -1) return namePart;
return namePart.substring(0, index);
}
/**
* Returns the URL of the TinyLaF theme. Note that,
* if {@link #isValid()} returns false
,
* the returned URL is probably null
.
* @return the URL of the TinyLaF theme.
*/
public URL getURL() {
return url;
}
/**
* Returns true
if the URI or URL
* argument given at construction time was a file URI/URL,
* false
otherwise.
* @return true
if the URI or URL
* argument given at construction time was a file URI/URL,
* false
otherwise.
*/
public boolean isFile() {
if(!isValid()) return false;
if(Theme.YQ_URL.equals(url)) return false;
return "file".equals(url.getProtocol());
}
/**
* Returns the theme file or null
.
* @return the theme file or null
.
* @see #isFile()
*/
public File getFile() {
if(fileHasBeenSet) return file;
fileHasBeenSet = true;
if(!isFile()) return null; // file stays null
try {
file = new File(uri);
}
catch(IllegalArgumentException ex) {
System.err.println(getClass().getName() + ".getFile() " + ex.toString());
System.err.println("URI=" + uri + "\nURL=" + url);
}
return file;
}
/**
* Returns the same string as would be returned from {@link #getName()}.
*/
public String toString() {
return getName();
}
}