URIPropertyEditor.java
01 /*
02  * Copyright 2010-2013 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *      http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package griffon.core.resources.editors;
18 
19 import java.io.File;
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 
23 /**
24  @author Andres Almiray
25  @since 1.1.0
26  */
27 public class URIPropertyEditor extends AbstractPropertyEditor {
28     public void setAsText(String valuethrows IllegalArgumentException {
29         setValue(value);
30     }
31 
32     public void setValue(Object value) {
33         if (null == valuereturn;
34         if (value instanceof CharSequence) {
35             handleAsString(String.valueOf(value));
36         else if (value instanceof File) {
37             handleAsFile((Filevalue);
38         else if (value instanceof URI) {
39             super.setValue(value);
40         else {
41             throw illegalValue(value, URI.class);
42         }
43     }
44 
45     private void handleAsString(String str) {
46         try {
47             super.setValue(new URI(str));
48         catch (URISyntaxException e) {
49             throw illegalValue(str, URI.class, e);
50         }
51     }
52 
53     private void handleAsFile(File file) {
54         super.setValue(file.toURI());
55     }
56 }