HotlinkConfigurationEditor.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 » HotlinkConfigurationEditor.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.awt.event.ActionEvent;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
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.ContelligentAction;
import de.finix.contelligent.client.i18n.Resources;
import de.finix.contelligent.client.util.ButtonComposer;
import de.finix.contelligent.client.util.TableLayout;
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 HotlinkConfigurationEditor extends AbstractComponentResourceEditor {

    private static Logger logger = Logger.getLogger(HotlinkConfigurationEditor.class.getName());

    private String hotlinkName;

    private JTextField hotlinkNameField, hotlinkURLField, parameterName;

    private JTable table;

    private DefaultTableModel tableModel;

    private DeleteParameterAction deleteParameterAction = new DeleteParameterAction();

    private AddParameterAction addParameterAction = new AddParameterAction();

    public void init() {
        setResourceComponent(this);
        JPanel messagePanel = new JPanel(new TableLayout(new double[][] { { TableLayout.FILL },
                { TableLayout.PREFERRED, 5, TableLayout.MINIMUM, 3, TableLayout.FILL } }));
        messagePanel.setOpaque(false);
        JPanel hotlinkNamePanel = new JPanel(new BorderLayout());
        hotlinkNamePanel.setOpaque(false);
        hotlinkNamePanel.setBorder(new TitledBorder(Resources.getLocalString("hotlink_name")));
        hotlinkNameField = new JTextField();
        hotlinkNamePanel.add(hotlinkNameField, BorderLayout.CENTER);
        messagePanel.add(hotlinkNamePanel, "0,0");
        JPanel hotlinkURLPanel = new JPanel(new BorderLayout());
        hotlinkURLPanel.setOpaque(false);
        hotlinkURLPanel.setBorder(new TitledBorder(Resources.getLocalString("hotlink_url")));
        hotlinkURLField = new JTextField();
        hotlinkURLPanel.add(hotlinkURLField, BorderLayout.CENTER);
        messagePanel.add(hotlinkURLPanel, "0,2");
        JPanel parameterPanel = new JPanel(new BorderLayout());
        parameterPanel.setOpaque(false);
        parameterPanel.setBorder(new TitledBorder(Resources.getLocalString("hotlink_parameter")));
        JToolBar parameterModificationPanel = new JToolBar();
        parameterModificationPanel.setRollover(true);
        parameterModificationPanel.setFloatable(false);
        AbstractButton addParameter = ButtonComposer.createButton(addParameterAction, false, null, true, true);
        AbstractButton deleteParameter = ButtonComposer.createButton(deleteParameterAction, false, null, true, true);
        parameterName = new JTextField();
        parameterModificationPanel.add(parameterName);
        parameterModificationPanel.add(Box.createHorizontalGlue());
        parameterModificationPanel.add(addParameter);
        parameterModificationPanel.add(deleteParameter);
        parameterModificationPanel.add(Box.createVerticalStrut(28));
        parameterPanel.add(parameterModificationPanel, BorderLayout.NORTH);
        tableModel = new ParameterTableModel();
        tableModel.setDataVector(new Object[0][0], new String[] { Resources.getLocalString("hotlink_parameter_name") });
        table = new JTable(tableModel);
        table.setOpaque(false);
        table.setRowHeight(25);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scroller = new JScrollPane(table);
        scroller.getViewport().setOpaque(false);
        scroller.setOpaque(false);
        parameterPanel.add(scroller, BorderLayout.CENTER);
        messagePanel.add(parameterPanel, "0,4");
        add(messagePanel, BorderLayout.CENTER);
        update();
    }

    public void setEditable(boolean editable) {
        super.setEditable(editable);
        if (hotlinkNameField != null)
            hotlinkNameField.setEditable(editable);
        if (hotlinkURLField != null)
            hotlinkURLField.setEditable(editable);
        if (table != null)
            table.setEnabled(editable);
        if (parameterName != null)
            parameterName.setEnabled(editable);
        if (addParameterAction != null)
            addParameterAction.setEnabled(editable);
        if (deleteParameterAction != null)
            deleteParameterAction.setEnabled(editable);
    }

    protected void displayResource() {
        // Parse resource and update editor
        tableModel.setDataVector(new Object[0][0], new String[] { Resources.getLocalString("hotlink_parameter_name") });
        HotlinkHandler handler = new HotlinkHandler();
        ContelligentTextResource resource = (ContelligentTextResource) getGUI().getResource();
        if (resource != null) {
            SimpleImporter importer = new SimpleImporter();
            importer.addSimpleImportHandler(handler);
            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(), getPreviewAsXML());
            resource.setModified(true);
            getGUI().setResource(resource);
        } else {
            resource.setText(getPreviewAsXML());
        }
        if (resource.isModified()) {
            getComponent().setResourceModified(true);
        }
        ComponentFactory.getInstance().fireResourceModifyEvent(
                new ResourceModifiedEvent(this, getComponent().getPath(), getGUI().getResourceIdentifier(), resource,
                        getGUI().getResourceMode()));
    }

    private String getPreviewAsXML() {
        StringWriter writer = new StringWriter();
        XMLWriter xmlWriter = new XMLWriter(writer);
        try {
            xmlWriter.writeStartTag(XMLWriter.createStartTag("hotlink", new String[][] {
                    { "name", hotlinkNameField.getText() }, { "url", hotlinkURLField.getText() } }));
            for (int i = 0; i < tableModel.getRowCount(); i++) {
                String parameter = (String) tableModel.getValueAt(i, 0);
                xmlWriter.writeEmptyElement(XMLWriter.createEmptyTag("parameter",
                        new String[][] { { "name", parameter } }));
            }
            xmlWriter.writeEndTag(XMLWriter.createEndTag("hotlink"));
            return writer.toString();
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Error while creating XML from preview-editor!", e);
        }
        return null;
    }

    protected void updateComponent() {
        updateResource();
        ContelligentTextResource resource = (ContelligentTextResource) getGUI().getResource();
        if (resource != null && !getGUI().isResourceInherited()) {
            resource.setText(getPreviewAsXML());
            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) {
    }

    public final class HotlinkHandler extends DefaultSimpleImportHandler {
        public void startElement(SimplePath path, String name, AttributesImpl attrs, String leadingCData) {
            try {
                if (path.matches("hotlink")) {
                    hotlinkNameField.setText(attrs.getValue("name"));
                    hotlinkURLField.setText(attrs.getValue("url"));
                } else if (path.matches("hotlink/parameter")) {
                    String parameterName = attrs.getValue("name");
                    tableModel.addRow(new String[] { parameterName });
                    tableModel.fireTableDataChanged();
                }
            } catch (Throwable t) {
                logger.log(Level.SEVERE, "Caught exception while preview import", t);
            }
        }
    }

    private final class AddParameterAction extends AbstractAction implements ContelligentAction {
        public AddParameterAction() {
            super("add", Resources.genericAddIcon);
            putValue(ROLLOVER_ICON, Resources.genericAddIconRollOver);
            putValue(TYPE, PUSH_ACTION);
            putValue(ACTION_TYPE, EDIT_ACTION);
            putValue(MENU_TARGET, MENU);
            putValue(BUTTON_TARGET, TOOLBAR);
        }

        public void actionPerformed(ActionEvent e) {
            if (parameterName.getText().length() > 0) {
                tableModel.addRow(new String[] { parameterName.getText() });
            }
        }
    }

    private final class DeleteParameterAction extends AbstractAction implements ContelligentAction {
        public DeleteParameterAction() {
            super("delete", Resources.genericDeleteIcon);
            putValue(ROLLOVER_ICON, Resources.genericDeleteIconRollOver);
            putValue(TYPE, PUSH_ACTION);
            putValue(ACTION_TYPE, EDIT_ACTION);
            putValue(MENU_TARGET, MENU);
            putValue(BUTTON_TARGET, TOOLBAR);
        }

        public void actionPerformed(ActionEvent e) {
            if (table.getSelectedRow() >= 0)
                tableModel.removeRow(table.getSelectedRow());
        }
    }

    private final class ParameterTableModel extends DefaultTableModel {
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    }
}
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.