01: /**********************************************************************************
02: * $URL:https://source.sakaiproject.org/svn/osp/trunk/common/tool-lib/src/java/org/theospi/portfolio/shared/model/impl/MimeTypeCustomEditor.java $
03: * $Id:MimeTypeCustomEditor.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.theospi.portfolio.shared.model.impl;
21:
22: import java.beans.PropertyEditorSupport;
23: import java.util.Collection;
24: import java.util.HashSet;
25: import java.util.Iterator;
26: import java.util.Set;
27:
28: import org.apache.commons.logging.Log;
29: import org.apache.commons.logging.LogFactory;
30: import org.sakaiproject.metaobj.utils.mvc.intf.TypedPropertyEditor;
31: import org.theospi.portfolio.shared.model.ItemDefinitionMimeType;
32:
33: /**
34: * Created by IntelliJ IDEA.
35: * User: John Ellis
36: * Date: May 19, 2004
37: * Time: 2:23:28 PM
38: * To change this template use File | Settings | File Templates.
39: */
40: public class MimeTypeCustomEditor extends PropertyEditorSupport
41: implements TypedPropertyEditor {
42: protected final Log logger = LogFactory.getLog(this .getClass());
43:
44: public void setAsText(String text) throws IllegalArgumentException {
45: if (text == null || text.length() == 0) {
46: setValue(null);
47: } else {
48: String[] items = text.split(",");
49: Collection mimeTypes = new HashSet();
50: for (int i = 0; i < items.length; i++) {
51: ItemDefinitionMimeType item = new ItemDefinitionMimeType();
52: String[] values = items[i].split("/");
53: if (values.length < 1)
54: return;
55: item.setPrimary(values[0]);
56: if (values.length == 2) {
57: item.setSecondary(values[1]);
58: }
59: mimeTypes.add(item);
60: }
61: setValue(mimeTypes);
62: }
63: }
64:
65: public String getAsText() {
66: StringBuffer buffer = new StringBuffer();
67: for (Iterator i = ((Collection) getValue()).iterator(); i
68: .hasNext();) {
69: ItemDefinitionMimeType item = (ItemDefinitionMimeType) i
70: .next();
71: buffer.append(item.getPrimary());
72: if (item.getSecondary() != null) {
73: buffer.append("/" + item.getSecondary());
74: }
75: }
76: return buffer.toString();
77: }
78:
79: public Class getType() {
80: return Set.class;
81: }
82:
83: }
|