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.bind; 025 026import java.awt.BorderLayout; 027 028import javax.swing.JComponent; 029import javax.swing.JFrame; 030import javax.swing.JMenuBar; 031 032import org.x4o.xml.element.AbstractElementBindingHandler; 033import org.x4o.xml.element.Element; 034import org.x4o.xml.element.ElementBindingHandlerException; 035 036/** 037 * 038 * 039 * @author Willem Cazander 040 * @version 1.0 Aug 16, 2012 041 */ 042public class JFrameBindingHandler extends AbstractElementBindingHandler { 043 044 /** 045 * @see org.x4o.xml.element.ElementBindingHandler#getBindParentClass() 046 */ 047 public Class<?> getBindParentClass() { 048 return JFrame.class; 049 } 050 051 /** 052 * @see org.x4o.xml.element.ElementBindingHandler#getBindChildClasses() 053 */ 054 public Class<?>[] getBindChildClasses() { 055 return new Class[] {JMenuBar.class,JComponent.class}; 056 } 057 058 /** 059 * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) 060 */ 061 public void doBind(Object parentObject, Object childObject, Element childElement) throws ElementBindingHandlerException { 062 JFrame frame = (JFrame)parentObject; 063 if (childObject instanceof JMenuBar) { 064 JMenuBar child = (JMenuBar)childObject; 065 frame.getRootPane().setJMenuBar(child); 066 } else if (childObject instanceof JComponent) { 067 JComponent child = (JComponent)childObject; 068 069 String c = childElement.getAttributes().get("constraints"); 070 Object con = null; 071 if ("BorderLayout.CENTER".equals(c)) { 072 con = BorderLayout.CENTER; 073 } else if ("BorderLayout.NORTH".equals(c)) { 074 con = BorderLayout.NORTH; 075 } else if ("BorderLayout.CENTER".equals(c)) { 076 con = BorderLayout.CENTER; 077 } else if ("BorderLayout.SOUTH".equals(c)) { 078 con = BorderLayout.SOUTH; 079 } 080 081 if (con==null) { 082 frame.getContentPane().add(child); 083 } else { 084 frame.getContentPane().add(child,con); 085 } 086 } 087 } 088}