www.pudn.com > Caliph_v0.9.13+Emirv0.8.5-src.zip > NewAgentDialog.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.AgentComboBoxModel; 
import at.lux.fotoannotation.mpeg7.Mpeg7DescriptionMetadata; 
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.StringTokenizer; 
import java.util.Vector; 
 
public class NewAgentDialog extends JDialog implements ActionListener, NewDescriptorDialogInterface { 
    private JTable datatable; 
    private JTextArea address, freeText; 
    private AgentComboBoxModel model; 
    Element agent = null; 
 
    public NewAgentDialog(AgentComboBoxModel model) { 
        this.model = model; 
        init(); 
    } 
 
    public NewAgentDialog(AgentComboBoxModel model, Frame owner) { 
        super(owner, true); 
        this.model = model; 
        init(); 
    } 
 
    public NewAgentDialog(Frame owner) { 
        super(owner, true); 
        model = null; 
        init(); 
    } 
 
    private void init() { 
        this.setTitle("Create new Agent"); 
        datatable = new JTable(new AbstractTableModel() { 
            String[] values = {"", "", "", "", "", "", "", ""}; 
            String[] keys = {"Given name", "Family name", "Organization", "Email", "Phone", "Fax", "URL", "Nickname"}; 
 
            public int getColumnCount() { 
                return 2; 
            } 
 
            public int getRowCount() { 
                return 8; 
            } 
 
            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"; 
            } 
        }); 
        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 (model != null) { 
                addAgent(); 
            } else { 
                createDocument(); 
            } 
            setVisible(false); 
        } else if (e.getActionCommand().equals("cancel")) { 
            setVisible(false); 
        } 
    } 
 
    private void addAgent() { 
        boolean agentadded = false; 
        String fname = datatable.getValueAt(1, 1).toString(); 
        String gname = datatable.getValueAt(0, 1).toString(); 
        String org = datatable.getValueAt(2, 1).toString(); 
        String email = datatable.getValueAt(3, 1).toString(); 
        String phone = datatable.getValueAt(4, 1).toString(); 
        String fax = datatable.getValueAt(5, 1).toString(); 
        String url = datatable.getValueAt(6, 1).toString(); 
        String nick = datatable.getValueAt(7, 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 (org.length() < 1) org = null; 
        if (phone.length() < 1) phone = null; 
        if (fax.length() < 1) fax = null; 
        if (url.length() < 1) url = null; 
        if (email.length() < 1) email = null; 
        if (nick.length() < 1) nick = null; 
 
        System.out.println("[at.lux.fotoannotation.NewAgentDialog] Agents name: " + gname + " " + fname); 
        // ToDo: 
        // ===== 
        // Free Text anhängen!!!!! 
        if (fname.length() > 1 && gname.length() > 1) { 
            System.out.println("[at.lux.fotoannotation.NewAgentDialog] Generating MPEG-7"); 
            Mpeg7DescriptionMetadata m = new Mpeg7DescriptionMetadata("1.0", gname, fname, org, addressLines, phone, fax, email, url, null, null, null); 
            Element e = m.getDescriptionMetadata(); 
            Element tmpAgent = (Element) ( e.getChild("Creator", e.getNamespace()).getChild("Agent", e.getNamespace())).detach(); 
            if (nick != null) { 
                tmpAgent.getChild("Name", tmpAgent.getNamespace()).addContent(new Element("GivenName", tmpAgent.getNamespace()).addContent(nick)); 
            } 
            model.addAgent(tmpAgent); 
        } else { 
            JOptionPane.showMessageDialog(this, "At least given name and family name are needed!"); 
        } 
 
        if (agentadded) { 
            System.out.println("[at.lux.fotoannotation.NewAgentDialog] Agent added!"); 
        } else { 
            System.out.println("[at.lux.fotoannotation.NewAgentDialog] Agent not added!"); 
        } 
 
    } 
 
    private void createDocument() { 
        boolean agentadded = false; 
        String fname = datatable.getValueAt(1, 1).toString(); 
        String gname = datatable.getValueAt(0, 1).toString(); 
        String org = datatable.getValueAt(2, 1).toString(); 
        String email = datatable.getValueAt(3, 1).toString(); 
        String phone = datatable.getValueAt(4, 1).toString(); 
        String fax = datatable.getValueAt(5, 1).toString(); 
        String url = datatable.getValueAt(6, 1).toString(); 
        String nick = datatable.getValueAt(7, 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 (org.length() < 1) org = null; 
        if (phone.length() < 1) phone = null; 
        if (fax.length() < 1) fax = null; 
        if (url.length() < 1) url = null; 
        if (email.length() < 1) email = null; 
        if (nick.length() < 1) nick = null; 
 
        System.out.println("[at.lux.fotoannotation.NewAgentDialog] Agents name: " + gname + " " + fname); 
        if (fname.length() > 1 && gname.length() > 1) { 
            System.out.println("[at.lux.fotoannotation.NewAgentDialog] Generating MPEG-7"); 
            Mpeg7DescriptionMetadata m = new Mpeg7DescriptionMetadata("1.0", gname, fname, org, addressLines, phone, fax, email, url, null, null, null); 
            Element e = m.getDescriptionMetadata(); 
            Element tmpagent = (Element) (e.getChild("Creator", e.getNamespace()).getChild("Agent", e.getNamespace())).detach(); 
            if (nick != null) { 
                tmpagent.getChild("Name", tmpagent.getNamespace()).addContent(new Element("GivenName", tmpagent.getNamespace()).addContent(nick)); 
            } 
            Namespace mpeg7, xsi; 
            mpeg7 = Namespace.getNamespace("", "urn:mpeg:mpeg7:schema:2001"); 
            xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
            agent = new Element("SemanticBase", mpeg7).setAttribute("type", "AgentObjectType", xsi); 
            Element label = new Element("Label", mpeg7).addContent(new Element("Name", mpeg7).addContent(gname + " " + fname)); 
            Element definition = null; 
            if (freeText.getText().length() > 0) { 
                definition = new Element("Definition", mpeg7).addContent(new Element("FreeTextAnnotation", mpeg7).addContent(freeText.getText())); 
            } 
            agent.addContent(label); 
            if (definition != null) { 
                agent.addContent(definition); 
            } 
            agent.addContent(tmpagent); 
        } else { 
            JOptionPane.showMessageDialog(this, "At least given name and family name are needed!"); 
        } 
    } 
 
    public Element createXML() { 
        return agent; 
    } 
}