Package de.achim.turnier

Source Code of de.achim.turnier.SpeichereDaten

package de.achim.turnier;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileFilter;

import de.achim.turnier.spielplan.DruckSpielPanel;
import de.achim.turnier.spielplan.SpielPanel;
import de.achim.turnier.spielplan.SpielPlanPanel;
import de.achim.turnier.tabellen.TabellenPanel;
import de.achim.turnier.utility.Begegnung;
import de.achim.turnier.utility.DateiSpeichern;

import java.awt.print.PageFormat;

public class Turnierverwaltung extends JFrame {
 
  private static final int WIDTH = 640;
  private static final int HEIGHT = 480;

 
  private JTabbedPane tabPane = new JTabbedPane();
  private AnlegenPaarungPanel aap = new AnlegenPaarungPanel();
  private SpielPlanPanel spiele = new SpielPlanPanel();
  private TabellenPanel tabelle = new TabellenPanel();
 
  /*
   * constructor
   */
  public Turnierverwaltung(){
   
    setSize(WIDTH, HEIGHT);
   
    Container contentPane = getContentPane();
   
    spiele.setTabellenPanel(tabelle);
    tabPane.addTab("Paarung anlegen", aap);
    aap.setSpielPanel(spiele);
    tabPane.addTab("Spielplan", spiele);
    tabPane.addTab("Tabelle", tabelle);
   
    JMenuBar menuBar = new JMenuBar();
   
    JMenu fileMenu = new JMenu( "Datei" );
    JMenu mnSpielplan = new JMenu("Spielplan");
    JMenu mnTabelle = new JMenu("Tabelle");
   
    JMenuItem sps = new JMenuItem("Spielplan speichern");
    sps.addActionListener(new SpeichereDaten(spiele));
    sps.setActionCommand("spielplan");
    mnSpielplan.add(sps);
    JMenuItem ts = new JMenuItem("Tabelle speichern");
    ts.addActionListener(new SpeichereDaten(tabelle));
    ts.setActionCommand("tabelle");
    mnTabelle.add(ts);
   
    JMenuItem lsp = new JMenuItem("Lade Spielplan");
    lsp.addActionListener(new LadeDaten(spiele));
    mnSpielplan.add(lsp);
    JMenuItem ltb = new JMenuItem("Lade Tabelle");
    ltb.addActionListener(new LadeDaten(tabelle));
    mnTabelle.add(ltb);
   
    JMenuItem ptb = new JMenuItem("Drucke Tabelle");
    ptb.addActionListener(new PrintDaten(tabelle));
    mnTabelle.add(ptb);
   
    JMenuItem pspp = new JMenuItem("Drucke Spielplan");
    pspp.addActionListener(new PrintDaten(spiele));
    mnSpielplan.add(pspp);
   
    JMenuItem schluss = new JMenuItem("Beenden");
    schluss.addActionListener(new ActionListener() {
     
      @Override
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
    fileMenu.add(schluss);
   
    menuBar.add( fileMenu );
    menuBar.add( mnSpielplan );
    menuBar.add( mnTabelle );
   
    contentPane.add(tabPane, BorderLayout.CENTER);
    contentPane.add(menuBar, BorderLayout.NORTH);
  }
 

  /**
   * @param args
   */
  public static void main(String[] args) {

    Turnierverwaltung tv = new Turnierverwaltung();
    tv.setDefaultCloseOperation(EXIT_ON_CLOSE);
    tv.setVisible(true);
  }

}

class SpeichereDaten implements ActionListener{

  SpielPlanPanel spp;
  TabellenPanel tabelle;
  JPanel panel;
  StringBuffer sb = new StringBuffer();
  String dateiendung ;
 
  public SpeichereDaten(JPanel panel){
    this.panel = panel;
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if(panel instanceof SpielPlanPanel){
      spp = (SpielPlanPanel)panel;
      Vector<SpielPanel> c = spp.getSpieleListe();
      Vector<Begegnung> vbg = spp.getBegegnungsListe();
      Iterator<Begegnung> iter = vbg.iterator();
      while(iter.hasNext()){
        sb.append(iter.next().toString());
      }
      dateiendung = ".txt";
    }

    if(panel instanceof TabellenPanel){
      tabelle = (TabellenPanel) panel;
      sb.append(tabelle.toString());
      dateiendung = ".csv";
    }

    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
    fc.setDialogType( JFileChooser.SAVE_DIALOG);

    int state = fc.showSaveDialog(null);
    if ( state == JFileChooser.APPROVE_OPTION ) {
      //file is a directory here

      File file = fc.getSelectedFile();
      String pfad = file.getPath() + dateiendung;
      System.out.println(file.getPath());
      DateiSpeichern.speichern(sb.toString(), pfad);
    }
    else
      System.out.println( "Auswahl abgebrochen" );


    System.out.println(sb.toString());
  }

}

class LadeDaten implements ActionListener {
 
  SpielPlanPanel spp;
  TabellenPanel tabelle;
  JPanel panel;
 
  StringBuffer sb = new StringBuffer();
  Vector<String> vLine  = new Vector<String>();
 
  public LadeDaten(JPanel panel){
    this.panel = panel;
  }

  @Override
  public void actionPerformed(ActionEvent e) {
   
    JFileChooser fc = new JFileChooser();
    //fc.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
     fc.setFileFilter( new FileFilter()
        {
          @Override public boolean accept( File f )
          {
            return f.isDirectory() ||
              f.getName().toLowerCase().endsWith( ".txt" )
            || f.getName().toLowerCase().endsWith( ".csv" );
          }
          @Override public String getDescription()
          {
            return "Turnier Daten";
          }
        } );
    
    fc.setDialogType( JFileChooser.OPEN_DIALOG);

    int state = fc.showOpenDialog(null);
    if ( state == JFileChooser.APPROVE_OPTION ) {
      File file = fc.getSelectedFile();
      try {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        while((line = reader.readLine())!=null){
          vLine.add(line);
        }
      } catch (FileNotFoundException e1) {
        e1.printStackTrace();
      } catch (IOException e2) {
        e2.printStackTrace();
      }
    }
   
    // now that we have the data lets do soemthing
   
    if(panel instanceof SpielPlanPanel){
      spp = (SpielPlanPanel)panel;
      spp.initFromFile(vLine);
    }
   
    if(panel instanceof TabellenPanel){
      tabelle = (TabellenPanel) panel;
      tabelle.initFromFile(vLine);
    }
   
  } // end of actionperformed
 
}

class PrintDaten implements ActionListener {
 
  TabellenPanel tabelle;
  SpielPlanPanel spp;
 
  public PrintDaten(JPanel panel){
    if(panel instanceof TabellenPanel)
      tabelle =(TabellenPanel) panel;
    if(panel instanceof SpielPlanPanel)
      spp = (SpielPlanPanel) panel;
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if(tabelle != null){
      MessageFormat headerFormat = new MessageFormat("Seite {0}");
      MessageFormat footerFormat = new MessageFormat("- {0} -");
      try {
        tabelle.getTable().print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
      } catch (PrinterException e1) {
        e1.printStackTrace();
     
    }
    if (spp != null){
      PrinterJob job = PrinterJob.getPrinterJob();
      job.setCopies(1);
      String title = spp.getTitle().getText();
     
      job.setJobName("Spielplan");
      //noch anpassen uhrzeit, datum
     
      /*    // set the Printable to the PrinterJob
       * resulst in some sort of screen shot
          job.setPrintable(new Printable() {

            public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
              if (pageIndex == 0) {
                spp.print(graphics);
                return Printable.PAGE_EXISTS;
              }
              return Printable.NO_SUCH_PAGE;
            }
          });*/
      DruckSpielPanel dsp = new DruckSpielPanel();
      dsp.setSps(spp);
      dsp.setTitle(title);
      dsp.setGetPrintInfo(true);
      try {
        job.setPrintable(dsp);
        job.print();
      //  job.setPrintable(dsp); 
        dsp.setGetPrintInfo(false);
        // show the dialog
        if (job.printDialog()) {
          job.print();
        }
      } catch (HeadlessException e1) {
        e1.printStackTrace();
      } catch (PrinterException e1) {
        e1.printStackTrace();
      }

    }
  }
}
TOP

Related Classes of de.achim.turnier.SpeichereDaten

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.