GUIMappingEditor.java :  » Content-Management-System » contelligent » de » finix » contelligent » client » gui » configuration » Java Open Source

Java Open Source » Content Management System » contelligent 
contelligent » de » finix » contelligent » client » gui » configuration » GUIMappingEditor.java
/*
 * Copyright 2001-2006 C:1 Financial Services GmbH
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License Version 2.1, as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
 */

package de.finix.contelligent.client.gui.configuration;

import java.awt.BorderLayout;
import java.io.IOException;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import de.finix.contelligent.client.base.ComponentFactory;
import de.finix.contelligent.client.base.resource.ContelligentTextResource;
import de.finix.contelligent.client.event.ContelligentEvent;
import de.finix.contelligent.client.event.ResourceModifiedEvent;
import de.finix.contelligent.client.gui.AbstractComponentResourceEditor;
import de.finix.contelligent.client.gui.GlobalGUIMapping;
import de.finix.contelligent.client.gui.module.MappingPane;
import de.finix.contelligent.client.util.xml.DumpImportHandler;
import de.zeigermann.xml.XMLEncode;
import de.zeigermann.xml.XMLStringWriter;
import de.zeigermann.xml.XMLWriter;
import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler;
import de.zeigermann.xml.simpleImporter.SimpleImporter;
import de.zeigermann.xml.simpleImporter.SimplePath;

public class GUIMappingEditor extends AbstractComponentResourceEditor {
    private static Logger logger = Logger.getLogger(GUIMappingEditor.class.getName());

    private MappingPane mappingPane = new MappingPane(null, null, null, null);

    public void init() {
        setResourceComponent(this);
        add(mappingPane, BorderLayout.CENTER);
        update();
    }

    public void setEditable(boolean editable) {
        super.setEditable(editable);
        if (mappingPane != null)
            mappingPane.setEditable(editable);
    }

    protected void displayResource() {
        // Parse resource and update editor
        ContelligentTextResource resource = (ContelligentTextResource) getGUI().getResource();
        if (resource != null) {
            SimpleImporter importer = new SimpleImporter();
            importer.setIncludeLeadingCDataIntoStartElementCallback(true);
            importer.addSimpleImportHandler(new GUIModificationHandler());
            importer.addSimpleImportHandler(new DumpImportHandler(Level.FINEST, false));
            try {
                importer.parse(new InputSource(new StringReader(resource.getText())));
            } catch (ParserConfigurationException e) {
            } catch (SAXException e) {
            } catch (IOException e) {
            }
        }
    }

    protected void updateResource() {
        ContelligentTextResource resource = (ContelligentTextResource) getGUI().getResource();
        if (resource == null || getGUI().isResourceInherited()) {
            resource = new ContelligentTextResource(getGUI().getResourceCategoryMap(), getGUIModificationAsXML());
            resource.setModified(true);
            getGUI().setResource(resource);
        } else {
            resource.setText("");
        }
        if (resource.isModified()) {
            getComponent().setResourceModified(true);
        }
        ComponentFactory.getInstance().fireResourceModifyEvent(
                new ResourceModifiedEvent(this, getComponent().getPath(), getGUI().getResourceIdentifier(), resource,
                        getGUI().getResourceMode()));
    }

    protected void updateComponent() {
        updateResource();
        ContelligentTextResource resource = (ContelligentTextResource) getGUI().getResource();
        if (resource != null && !getGUI().isResourceInherited()) {
            resource.setText(getGUIModificationAsXML());
            if (resource.isModified()) {
                getComponent().setResourceModified(true);
            }
        }
    }

    protected void componentChanged(ContelligentEvent event) {
        update();
    }

    protected void childComponentAdded(ContelligentEvent event) {
    }

    protected void childComponentRemoved(ContelligentEvent event) {
    }

    protected void childComponentChanged(ContelligentEvent event) {
    }

    protected void descendentComponentChanged(ContelligentEvent event) {
    }

    private String getGUIModificationAsXML() {
        XMLStringWriter configurationWriter = XMLStringWriter.create();
        configurationWriter.writeElementWithPCData(XMLWriter.createStartTag("gui-configuration", new String[] { "type",
                "command", "gui" }, new String[] { mappingPane.getType(), mappingPane.getCommand(),
                ":" + mappingPane.getGUIImpl() }), XMLEncode.xmlEncodeTextAsCDATABlock(mappingPane.getConfiguration()),
                XMLWriter.createEndTag("gui-configuration"));
        return configurationWriter.toString();
    }

    private final class GUIModificationHandler extends DefaultSimpleImportHandler {
        public void startElement(SimplePath path, String name, AttributesImpl attrs, String leadingCData) {
            try {
                if (path.matches(GlobalGUIMapping.DOMAIN)) {
                    mappingPane.setType(attrs.getValue("type"));
                    mappingPane.setCommand(attrs.getValue("command"));
                    mappingPane.setConfiguration(XMLEncode.xmlDecodeTextToCDATA(leadingCData));
                    mappingPane.setGUI(attrs.getValue("gui"));
                }
            } catch (Throwable t) {
                logger.log(Level.SEVERE, "Caught exception while gui-configuration import", t);
            }
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.