www.pudn.com > Caliph_v0.9.13+Emirv0.8.5-src.zip > NewEventDialog.java


/* 
 * This file is part of Caliph & Emir. 
 * 
 * Caliph & Emir is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version. 
 * 
 * Caliph & Emir 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 General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with Caliph & Emir; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 * 
 * Copyright statement: 
 * -------------------- 
 * (c) 2002-2004 by Mathias Lux (mathias@juggle.at) 
 * http://www.juggle.at 
 */ 
/* 
 * 
 * 
 * 
 * @author Mathias Lux, mathias@juggle.at 
 * Date: 13.09.2002 
 * Time: 11:02:20 
 */ 
package at.lux.fotoannotation.dialogs; 
 
import at.lux.fotoannotation.AnnotationToolkit; 
import org.jdom.Element; 
import org.jdom.Namespace; 
 
import javax.swing.*; 
import javax.swing.border.Border; 
import javax.swing.table.AbstractTableModel; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Iterator; 
import java.util.StringTokenizer; 
import java.util.Vector; 
 
public class NewEventDialog extends JDialog implements ActionListener, NewDescriptorDialogInterface { 
    private JTable datatable; 
    private JTextArea address, freeText; 
    Element eventDescriptor = null; 
 
    public NewEventDialog(Frame owner) { 
        super(owner, true); 
        init(); 
    } 
 
    private void init() { 
        this.setTitle("Create new Event"); 
        datatable = new JTable(new AbstractTableModel() { 
            String[] values = {"", "", ""}; 
            String[] keys = {"Event", "Date", "Place"}; 
 
            public int getColumnCount() { 
                return 2; 
            } 
 
            public int getRowCount() { 
                return keys.length; 
            } 
 
            public Object getValueAt(int rowIndex, int columnIndex) { 
                if (columnIndex == 0) 
                    return keys[rowIndex]; 
                else 
                    return values[rowIndex]; 
            } 
 
            public boolean isCellEditable(int rowIndex, int columnIndex) { 
                return (columnIndex == 1); 
            } 
 
            public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 
                if (columnIndex == 1) values[rowIndex] = aValue.toString(); 
            } 
 
            public String getColumnName(int column) { 
                if (column == 0) 
                    return "tag"; 
                else 
                    return "value"; 
            } 
        }); 
        datatable.getColumnModel().getColumn(0).setMaxWidth(80); 
        datatable.getColumnModel().getColumn(0).setMinWidth(80); 
        datatable.setValueAt(AnnotationToolkit.getMpeg7Time(), 1, 1); 
        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 
        split.setDividerLocation(250); 
 
        address = new JTextArea(4, 12); 
        freeText = new JTextArea(4, 12); 
        JPanel spane = new JPanel(new BorderLayout()); 
        JPanel bpane = new JPanel(new FlowLayout()); 
        JPanel apane = new JPanel(new BorderLayout()); 
        JPanel tpane = new JPanel(new BorderLayout()); 
        JPanel fpane = new JPanel(new BorderLayout()); 
        Border border = BorderFactory.createEtchedBorder(); 
        tpane.setBorder(BorderFactory.createTitledBorder(border, "Definition")); 
        apane.setBorder(BorderFactory.createTitledBorder(border, "Address")); 
        fpane.setBorder(BorderFactory.createTitledBorder(border, "Free text")); 
        apane.add(new JScrollPane(address), BorderLayout.CENTER); 
        tpane.add(new JScrollPane(datatable), BorderLayout.CENTER); 
        fpane.add(new JScrollPane(freeText), BorderLayout.CENTER); 
        spane.add(fpane, BorderLayout.CENTER); 
        spane.add(apane, BorderLayout.NORTH); 
 
        JButton ok = new JButton("OK"); 
        ok.addActionListener(this); 
        ok.setActionCommand("ok"); 
        JButton cancel = new JButton("Cancel"); 
        cancel.addActionListener(this); 
        cancel.setActionCommand("cancel"); 
        bpane.add(ok); 
        bpane.add(cancel); 
 
        split.add(tpane, JSplitPane.TOP); 
        split.add(spane, JSplitPane.BOTTOM); 
        this.getContentPane().add(split, BorderLayout.CENTER); 
        this.getContentPane().add(bpane, BorderLayout.SOUTH); 
    } 
 
    /** 
     * Invoked when an action occurs. 
     */ 
    public void actionPerformed(ActionEvent e) { 
        if (e.getActionCommand().equals("ok")) { 
            if (createDocument()) { 
                setVisible(false); 
            } 
        } else if (e.getActionCommand().equals("cancel")) { 
            setVisible(false); 
        } 
    } 
 
    private boolean createDocument() { 
 
        boolean eventValid = false; 
        String event = datatable.getValueAt(0, 1).toString(); 
        String date = datatable.getValueAt(1, 1).toString(); 
        String place = datatable.getValueAt(2, 1).toString(); 
        Vector addressLines = null; 
        if (address.getText().length() > 0) { 
            addressLines = new Vector(); 
            StringTokenizer t = new StringTokenizer(address.getText(), "\n", false); 
            while (t.hasMoreElements()) { 
                String s = t.nextToken(); 
                addressLines.add(s.trim()); 
            } 
        } 
        if (event.length() < 1) event = null; 
        if (date.length() < 1) date = null; 
        if (place.length() < 1) place = null; 
 
        if (place != null) { 
            System.out.println("[at.lux.fotoannotation.NewEventDialog] Generating MPEG-7"); 
            Namespace mpeg7, xsi; 
            mpeg7 = Namespace.getNamespace("", "urn:mpeg:mpeg7:schema:2001"); 
            xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
            eventDescriptor = new Element("SemanticBase", mpeg7).setAttribute("type", "EventType", xsi); 
            Element label = new Element("Label", mpeg7).addContent(new Element("Name", mpeg7).addContent(event)); 
            Element definition = null; 
            if (freeText.getText().length() > 0) { 
                definition = new Element("Definition", mpeg7).addContent(new Element("FreeTextAnnotation", mpeg7).addContent(freeText.getText())); 
            } 
            eventDescriptor.addContent(label); 
            if (definition != null) { 
                eventDescriptor.addContent(definition); 
            } 
 
            // Place && Time: 
            Element elem_place = new Element("SemanticPlace", mpeg7); 
            Element elem_time = new Element("SemanticTime", mpeg7); 
            Element elem_event = new Element("Event", mpeg7); 
            elem_event.addContent((Element) label.clone()); 
            if (place != null) { 
                elem_place.addContent(new Element("Label", mpeg7).addContent(new Element("Name", mpeg7).addContent(place))); 
                if (addressLines != null) { 
                    Element elem_address = new Element("PostalAddress", mpeg7); 
                    for (Iterator it1 = addressLines.iterator(); it1.hasNext();) { 
                        String s = (String) it1.next(); 
                        elem_address.addContent(new Element("AddressLine", mpeg7).addContent(s)); 
                    } 
                    elem_place.addContent(new Element("Place", mpeg7).addContent(elem_address)); 
                } 
                elem_event.addContent(elem_place); 
            } 
            if (date != null) { 
                elem_time.addContent(new Element("Label", mpeg7).addContent(new Element("Name", mpeg7).addContent("Time of " + event))); 
                elem_time.addContent(new Element("Time", mpeg7).addContent(new Element("TimePoint", mpeg7).addContent(date))); 
                elem_event.addContent(elem_time); 
            } 
            eventDescriptor.addContent(elem_event); 
            eventValid = true; 
        } else { 
            JOptionPane.showMessageDialog(this, "At least the event name is needed!"); 
        } 
        return eventValid; 
    } 
 
    public Element createXML() { 
        return eventDescriptor; 
    } 
}