package com.marinilli.b2.c11;
import com.marinilli.b2.c11.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.jnlp.*;
import java.net.URL;
import java.io.*;
import java.util.Iterator;
import java.awt.*;
/**
 * Chapter 11 A test application
 * @author Mauro Marinilli
 * @version 1.0
 */
public class AnApplication extends JFrame {
  JPanel centerPanel = new JPanel();
  JButton pasteButton = new JButton();
  JLabel southLabel = new JLabel();
  JLabel centerLabel = new JLabel();
  JPanel northPanel = new JPanel();
  JNLPLabel northLabel = new JNLPLabel();
  JButton openFileButton = new JButton();
  public AnApplication() {
    setTitle(Utilities.getMsg("title"));
    setIconImage(Utilities.getImageIcon("bubu").getImage());
    this.getContentPane().setLayout(new BorderLayout());
    this.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    centerPanel.setLayout(new BorderLayout());
    pasteButton.setText("Paste");
    pasteButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        centerLabel.setText(pasteClipboardContent());
      }
    });
    southLabel.setText(Utilities.getMsg("hello"));
    centerLabel.setIcon(Utilities.getImageIcon("anIcon.gif"));
    northLabel.setText("please click here!");
    northLabel.setURL("http://server/b2/index.html");
    openFileButton.setIcon(Utilities.getImageIcon("open.gif"));
    openFileButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        openFile();
      }
    });
    this.getContentPane().add(centerPanel, BorderLayout.CENTER);
    centerPanel.add(pasteButton, BorderLayout.EAST);
    centerPanel.add(centerLabel, BorderLayout.CENTER);
    centerPanel.add(northPanel, BorderLayout.NORTH);
    northPanel.add(openFileButton, null);
    northPanel.add(northLabel, null);
    this.getContentPane().add(southLabel, BorderLayout.SOUTH);
    PersistenceService ps =
      (PersistenceService)Utilities.getService("javax.jnlp.PersistenceService");
    if (ps!=null) {
      doSomething(ps);
    }
    setSize(300,200);
    setVisible(true);
  }
  private void doSomething(PersistenceService ps){
    PersistentStorage pst = new PersistentStorage();
    pst.write("url0","salve");
    pst.synchronize("url0");

    Iterator iter = pst.getEntries("url0").iterator();
    while (iter.hasNext()) {
      Object obj = iter.next();
      System.out.println("item="+obj);
    }
  }
  private String pasteClipboardContent() {
    String s = "";
    ClipboardService cbs =
     (ClipboardService)Utilities.getService("javax.jnlp.ClipboardService");
    try {
      if (cbs.getContents()!=null)
        s=(String)cbs.getContents().getTransferData(java.awt.datatransfer.DataFlavor.stringFlavor);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return s;
  }
  public void openFile() {
    String[] suffixes = {"txt","jnlp","jar","java"};
    FileContents file = null;
    FileOpenService fs =
     (FileOpenService)Utilities.getService("javax.jnlp.FileOpenService");
    if (fs!=null) {
      try {
        file = fs.openFileDialog("mydir", suffixes);
      } catch (Exception e) {
        System.out.println("openFile: "+e);
      }
    }
  }
  public static void main(String[] args) {
    AnApplication anApplication1 = new AnApplication();
  }
}