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.MalformedURLException;
21 import java.net.URL;
22
23 /**
24 * @author Andres Almiray
25 * @since 1.1.0
26 */
27 public class URLPropertyEditor extends AbstractPropertyEditor {
28 public void setAsText(String value) throws IllegalArgumentException {
29 setValue(value);
30 }
31
32 public void setValue(Object value) {
33 if (null == value) return;
34 if (value instanceof CharSequence) {
35 handleAsString(String.valueOf(value));
36 } else if (value instanceof File) {
37 handleAsFile((File) value);
38 } else if (value instanceof URL) {
39 super.setValue(value);
40 } else {
41 throw illegalValue(value, URL.class);
42 }
43 }
44
45 private void handleAsString(String str) {
46 try {
47 super.setValue(new URL(str));
48 } catch (MalformedURLException e) {
49 throw illegalValue(str, URL.class, e);
50 }
51 }
52
53 private void handleAsFile(File file) {
54 try {
55 super.setValue(file.toURI().toURL());
56 } catch (MalformedURLException e) {
57 throw illegalValue(file, URL.class);
58 }
59 }
60 }
|