/*
* (c) Copyright 2004 by Heng Yuan
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package cookxml.cookswing.creator;
import java.awt.*;
import javax.swing.*;
import org.w3c.dom.Element;
import cookxml.core.DecodeEngine;
import cookxml.core.converter.ObjectConverter;
import cookxml.core.exception.ConverterException;
import cookxml.core.exception.CreatorException;
import cookxml.core.interfaces.Creator;
/**
* This creator is necessary to be possible to set the parent of the JDialog and
* pack the window after all inner components have been setup.
*
* @cxattr ctor
* Optionally specifies the parent dialog or frame reference which is
* obtained using {@link cookxml.core.converter.ObjectConverter}.
* @cxattr packed
* If the value is "true", then it tells the dialog to pack itself.
* It only works if the dialog is created, rather than referenced using
* idref/varref.
* @cxattr locationrelativeto
* Optionally specifies the frame/dialog to set the dialog center to.
* It only works if the dialog is created, rather than referenced using
* idref/varref.
*
* @see javax.swing.JDialog
* @author Heng Yuan
* @version $Id: DialogCreator.java 233 2007-06-06 08:08:49Z coconut $
* @since CookSwing 1.0
*/
public class DialogCreator implements Creator
{
public static String PACKED_ATTR = "packed";
public static String LOCATION_RELATIVE_TO_ATTR = "locationrelativeto";
public Object create (String parentNS, String parentTag, Element elm, Object parentObj, DecodeEngine decodeEngine)
{
Object parentWindow = null;
String value = elm.getAttribute ("ctor");
if (value.length () > 0)
{
try
{
parentWindow = ObjectConverter.getInstance ().convert (value, decodeEngine);
}
catch (Exception ex)
{
if (!(ex instanceof ConverterException))
ex = new ConverterException (decodeEngine, ex, ObjectConverter.getInstance (), value);
decodeEngine.handleException (null, ex);
}
}
if (parentWindow == null)
return new JDialog ();
if (parentWindow instanceof JFrame)
return new JDialog ((JFrame)parentWindow);
if (parentWindow instanceof JDialog)
return new JDialog ((JDialog)parentWindow);
throw new CreatorException (decodeEngine, null, this, parentNS, parentTag, elm, parentObj);
}
public Object editFinished (String parentNS, String parentTag, Element elm, Object parentObj, Object obj, DecodeEngine decodeEngine)
{
doPackWindow (obj, elm, decodeEngine);
return obj;
}
/**
* This function is used to check if the window has a PACKED_ATTR and if so and its value is
* true, then pack the window. It also deals with LOCATION_RELATIVE_TO_ATTR, which requires
* the value to be in the format described in ObjectConverter.
* @param window
* the window object itself.
* @param elm
* the complete element information.
* @param decodeEngine
* the DecodeEngine that is been used.
* @see cookxml.core.converter.ObjectConverter
*/
public static void doPackWindow (Object window, Element elm, DecodeEngine decodeEngine)
{
if (window == null)
return;
String packedAttr = elm.getAttribute (PACKED_ATTR);
boolean b = Boolean.valueOf (packedAttr).booleanValue ();
if (b)
{
if ((window instanceof Window))
((Window)window).pack ();
else if (window instanceof JInternalFrame)
((JInternalFrame)window).pack ();
}
// now check set location relative to
String locationRelativeTo = elm.getAttribute (LOCATION_RELATIVE_TO_ATTR);
if (locationRelativeTo != null && locationRelativeTo.length () > 0)
{
Object comp = null;
try
{
comp = ObjectConverter.getInstance ().convert (locationRelativeTo, decodeEngine);
}
catch (Exception ex)
{
}
if (comp == null || comp instanceof Component)
{
if ((window instanceof Window))
((Window)window).setLocationRelativeTo ((Component)comp);
}
}
}
}