001/*
002 * Copyright (c) 2004-2012, Willem Cazander
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
006 * that the following conditions are met:
007 * 
008 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009 *   following disclaimer.
010 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
011 *   the following disclaimer in the documentation and/or other materials provided with the distribution.
012 * 
013 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
015 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
016 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
018 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
019 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
020 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
021 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
022 */
023
024package org.x4o.xml.test.swixml.conv;
025
026import java.awt.BorderLayout;
027import java.awt.FlowLayout;
028import java.awt.GridBagLayout;
029import java.awt.GridLayout;
030import java.awt.LayoutManager;
031import java.util.Locale;
032
033import org.x4o.xml.conv.AbstractStringObjectConverter;
034import org.x4o.xml.conv.ObjectConverter;
035import org.x4o.xml.conv.ObjectConverterException;
036
037/**
038 * 
039 * 
040 * @author Willem Cazander
041 * @version 1.0 Aug 17, 2012
042 */
043public class LayoutConverter extends AbstractStringObjectConverter {
044        
045        private static final long serialVersionUID = 6729812931433525103L;
046        
047        public Class<?> getObjectClassTo() {
048                return LayoutManager.class;
049        }
050                
051        public String convertStringBack(Object obj,Locale locale) throws ObjectConverterException {
052                return ((LayoutManager)obj).toString();
053        }
054
055        public Object convertStringTo(String str, Locale locale) throws ObjectConverterException {
056                try {
057                        if ("borderlayout".equals(str)) {
058                                return new BorderLayout();
059                        } else if (str.startsWith("FlowLayout")) {
060                                
061                                if (str.contains("RIGHT")) {
062                                        return new FlowLayout(FlowLayout.RIGHT);
063                                } else if (str.contains("LEFT")) {
064                                        return new FlowLayout(FlowLayout.LEFT);
065                                } else if (str.contains("CENTER")) {
066                                        return new FlowLayout(FlowLayout.CENTER);
067                                } else if (str.contains("LEADING")) {
068                                        return new FlowLayout(FlowLayout.LEADING);
069                                } else if (str.contains("TRAILING")) {
070                                        return new FlowLayout(FlowLayout.TRAILING);
071                                } else {
072                                        return new FlowLayout();
073                                }
074                        
075                        } else if (str.startsWith("GridLayout")) {
076                                
077                                int indexStart = str.indexOf('(');
078                                int indexMid = str.indexOf(',');
079                                int indexEnd = str.indexOf(')');
080                                if (indexStart>0 && indexMid>0 && indexEnd>0) {
081                                        
082                                        Integer rows = new Integer(str.substring(indexStart+1,indexMid));
083                                        Integer cols = new Integer(str.substring(indexMid+1,indexEnd));
084                                        
085                                        return new GridLayout(rows,cols);
086                                        
087                                } else {
088                                        throw new ObjectConverterException(this,"Could not parse arguments: "+str);     
089                                }
090                                
091                        } else if (str.startsWith("GridBagLayout")) {
092                                
093                                return new GridBagLayout();
094                                
095                        } else {
096                                throw new ObjectConverterException(this,"Unknow layout requested: "+str);       
097                        }
098                } catch (Exception e) {
099                        throw new ObjectConverterException(this,e.getMessage(),e);
100                }
101        }
102
103        @Override
104        public ObjectConverter clone() throws CloneNotSupportedException {
105                LayoutConverter result = new LayoutConverter();
106                result.converters=cloneConverters();
107                return result;
108        }
109        
110        
111}