/*
 * File:     FormattedMessageDlg.java
 * Project:  MPI Linguistic Application
 * Date:     03 April 2006
 *
 * Copyright (C) 2001-2006  Max Planck Institute for Psycholinguistics
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package mpi.eudico.client.annotator.gui;

import mpi.eudico.client.annotator.ElanLocale;

import mpi.eudico.client.annotator.player.*;

import mpi.eudico.client.util.TimeFormatter;

import javax.swing.JLabel;
import javax.swing.JOptionPane;


/**
 * Shows a dialog with a formatted message.
 *
 * @author Han Sloetjes
 */
public class FormattedMessageDlg {
    private JLabel label;

    /**
     * Creates a new FormattedMessageDlg instance.
     *
     * @param mes a two-dimensional array of Strings, that will be shown in  a
     *        html table in a message dialog
     */
    public FormattedMessageDlg(String[][] mes) {
        showMessage(mes);
    }

    /**
     * Creates a new FormattedMessageDlg instance that extracts information
     * from the player's media descriptor.
     *
     * @param player an Elan Media Player
     */
    public FormattedMessageDlg(ElanMediaPlayer player) {
        String[][] mes = createMediaInfo(player);
        showMessage(mes);
    }

    /**
     * Creates a two-dimensional String array.
     *
     * @param player an Elan media player
     *
     * @return a two-dimensional String array
     */
    private String[][] createMediaInfo(ElanMediaPlayer player) {
        if ((player == null) || (player.getMediaDescriptor() == null)) {
            return null;
        }

        String[][] info = new String[6][2];
        info[0][0] = ElanLocale.getString("LinkedFilesDialog.Label.MediaURL");
        info[0][1] = player.getMediaDescriptor().mediaURL;
        info[1][0] = ElanLocale.getString("LinkedFilesDialog.Label.MimeType");
        info[1][1] = player.getMediaDescriptor().mimeType;
        info[2][0] = ElanLocale.getString("LinkedFilesDialog.Label.MediaOffset");
        info[2][1] = String.valueOf(player.getMediaDescriptor().timeOrigin);
        info[3][0] = ElanLocale.getString("Player.duration");
        info[3][1] = TimeFormatter.toString(player.getMediaDuration());
        info[4][0] = ElanLocale.getString("Player.FrameRate");
        info[4][1] = String.valueOf((float) 1000 / player.getMilliSecondsPerSample());
        info[5][0] = ElanLocale.getString("Player.Framework");
        info[5][1] = player.getFrameworkDescription();

        return info;
    }

    private void showMessage(String[][] mes) {
        if (mes == null) {
            return;
        }

        label = new JLabel();

        StringBuffer sb = new StringBuffer();
        sb.append("<html><table>");

        for (int i = 0; i < mes.length; i++) {
            sb.append("<tr>");

            for (int j = 0; j < mes[i].length; j++) {
                sb.append("<td>");
                sb.append(mes[i][j]);
                sb.append("</td>");
            }

            sb.append("</tr>");
        }

        sb.append("</table></html>");
        label.setText(sb.toString());

        JOptionPane.showMessageDialog(null, label, "Elan",
            JOptionPane.INFORMATION_MESSAGE);
    }
}
