/**
*
*/
package mvc.model;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.channels.FileChannel;
import java.util.Observable;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import mvc.controll.Controller;
/**
* @author henrichj
*
*/
public class SystemCD implements Serializable {
private static final long serialVersionUID = 4257325752674191118L;
private static Controller controller;
private String s_name;
private File s_file;
private Vector<SysDataFile> s_documents;
// Arrays für die Sprachen
private static final String[][] langArray = new String[6][6];
private static final String[] GERMAN = new String[6];
private static final String[] ENGLISCH = new String[6];
private static final String[] SPANISH = new String[6];
private static final String[] FRENCH = new String[6];
private static final String[] ITALIAN = new String[6];
private static final String[] PORTUGESE = new String[6];
public SystemCD() {
}
public SystemCD(Controller controller, String name, File file,
Vector<SysDataFile> files) {
SystemCD.controller = controller;
this.s_name = name;
this.s_file = file;
this.s_documents = files;
}
/**
* Saves a given System CD File
*
* @param fileToSave
* The file to Save
*/
public void saveSysDataFile(SystemCD fileToSave) {
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("C:/test/SystemCD"));
int returnVal = fc.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fc.getSelectedFile()
.getAbsolutePath() + ".sdata");
oos = new ObjectOutputStream(fos);
// Speichern des Objects
oos.writeObject(fileToSave);
} catch (IOException e) {
e.printStackTrace();
} finally {
// Prüfen ob die Streams geschlossen sind, wenn nicht, dann
// werden
// sie geschlossen
if (oos != null)
try {
oos.close();
} catch (IOException e) {
}
if (fos != null)
try {
fos.close();
} catch (IOException e) {
}
}
}
}
/**
* Saves a given System CD File
*
* @param fileToLoad
* The File to save
*/
public void loadSysDataFile() {
JFileChooser fc = new JFileChooser();
// Setz die den default Pfad
fc.setCurrentDirectory(new File("C:/test/SystemCD"));
// Setzen des FileFilter, so dass nur Dateiuen angezeigt werden, die mit
// sData enden
fc.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return "System CD Data Files";
}
@Override
public boolean accept(File f) {
if (f == null)
return false;
// Ordner anzeigen
if (f.isDirectory())
return true;
// true, wenn File gewuenschte Endung besitzt
return f.getName().toLowerCase().endsWith(".sdata");
}
});
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
ObjectInputStream ois = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(fc.getSelectedFile()
.getAbsolutePath());
ois = new ObjectInputStream(fis);
Object loadedObject = ois.readObject();
// ist das geladene Object ein Object der SysData Klasse?
if (loadedObject instanceof SystemCD) {
SystemCD tmp = (SystemCD) loadedObject;
// Setzen der geladenen SystemCD als aktive System cd
controller.setCoreModel(tmp);
}
JOptionPane.showMessageDialog(null, "Laden erfolgreich");
} catch (ClassNotFoundException e) {
JOptionPane
.showMessageDialog(
null,
"Datei konnte nicht geladen werden. Es gab ein Problem beim Laden der Datei",
"Inane error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} catch (IOException e) {
JOptionPane
.showMessageDialog(
null,
"Datei konnte nicht geladen werden. Es gab ein Problem beim Laden der Datei",
"Inane error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} finally {
// Prüfen ob die Streams geschlossen sind
if (ois != null)
try {
ois.close();
} catch (IOException e) {
}
if (fis != null)
try {
fis.close();
} catch (IOException e) {
}
}
}
if (returnVal == JFileChooser.CANCEL_OPTION) {
return;
}
}
/**
* Copies to given Files
*
* @param in
* The file to copy
* @param out
* The Folder and Filename where it should be copied to
*/
public static void copyFiles(File in, File out) {
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
inChannel = new FileInputStream(in).getChannel();
outChannel = new FileOutputStream(out).getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
"The Files could not be copied", "Copy Error",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} finally {
// Streams schließen
if (inChannel != null) {
try {
inChannel.close();
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void createSystemCD() {
// Holen der Dateien der aktuellen System CD
Vector<SysDataFile> documents = controller.getCoreModel()
.getDocuments();
String share = "Z:/test/";
File dir = new File(share + controller.getCoreModel().getName());
dir.mkdir();
// Schleife um die Dokumente diurchzugehen
for (int i = 0; i < documents.size(); i++) {
// Name vom alten Dokument, damit man es weiter verwenden kann
String lang = getNameByLanguageId(documents.get(i).getLanguage());
File dirTmp = new File(share + controller.getCoreModel().getName()
+ "/" + lang);
dirTmp.mkdir();
String name = getLanguageAndType(controller.getCoreModel()
.getDocuments().get(i).getLanguage(), controller
.getCoreModel().getDocuments().get(i).getType());
// Holt sich nach der Reihe die Dokumente und speichert sie dann im
// gegebenen Pfad mit dem alten Namen ab
copyFiles(documents.get(i).getFile(), new File(share + "/"
+ controller.getCoreModel().getName() + "/" + lang + "/"
+ name + ".pdf"));
}
JOptionPane.showMessageDialog(null, "System CD Created");
}
/**
* Returns the Type of the given Document
*
* @param type
* The Type of the Document as Integer
* @return The Type of the Document as String
*/
@SuppressWarnings("unused")
private static String getTypeByTypeId(int type) {
switch (type) {
case 0:
return "Bedienungsanleitung";
case 1:
return "Information";
case 2:
return "Datenblatt";
case 3:
return "Sicherheitsinformationen";
case 4:
return "Schnittstellenhinweis";
case 5:
return "Broschüre";
default:
return "Keine Angabe";
}
}
/**
* Returns the Language of the given Document
*
* @param language
* The Document of the Document as integer
* @return the Language of the Document as String
*/
private static String getNameByLanguageId(int language) {
switch (language) {
case 0:
return "Deutsch";
case 1:
return "Englisch";
case 2:
return "Spanisch";
case 3:
return "Italienisch";
case 4:
return "Portugiesisch";
case 5:
return "Französisch";
default:
return "Keine Angabe";
}
}
public static String getLanguageAndType(int language, int type) {
createArrays();
return langArray[language][type];
}
private static void createArrays() {
// Erzeugen der Arrays
langArray[0] = GERMAN;
langArray[1] = ENGLISCH;
langArray[2] = SPANISH;
langArray[3] = FRENCH;
langArray[4] = ITALIAN;
langArray[5] = PORTUGESE;
// Deutsch
GERMAN[0] = "Bedienungsanleitung";
GERMAN[1] = "Datenblatt";
GERMAN[2] = "Sicherheitsdatenblatt";
GERMAN[3] = "Sicherheitsinfos";
GERMAN[4] = "Broschüre";
GERMAN[5] = "Information";
// Englisch
ENGLISCH[0] = "Instruction Manual";
ENGLISCH[1] = "Data Sheet";
ENGLISCH[2] = "Safety Information";
ENGLISCH[3] = "Safety Infos";
ENGLISCH[4] = "Brochure";
ENGLISCH[5] = "Information";
// Spanish
SPANISH[0] = "Bedienungsanleitung - sp";
SPANISH[1] = "Datenblatt - sp";
SPANISH[2] = "Sicherheitsdatenblatt - sp";
SPANISH[3] = "Sicherheitsinfos - sp";
SPANISH[4] = "Broschüre - sp";
SPANISH[5] = "Information - sp";
// Französisch
FRENCH[0] = "Bedienungsanleitung - fr";
FRENCH[1] = "Datenblatt - fr";
FRENCH[2] = "Sicherheitsdatenblatt - fr";
FRENCH[3] = "Sicherheitsinfos - fr";
FRENCH[4] = "Broschüre - fr";
FRENCH[5] = "Information - fr";
// Italienisch
ITALIAN[0] = "Bedienungsanleitung - it";
ITALIAN[1] = "Datenblatt - it";
ITALIAN[2] = "Sicherheitsdatenblatt - it";
ITALIAN[3] = "Sicherheitsinfos - it";
ITALIAN[4] = "Broschüre - it";
ITALIAN[5] = "Information - it";
// Portugiesisch
PORTUGESE[0] = "Bedienungsanleitung - por";
PORTUGESE[1] = "Datenblatt - por";
PORTUGESE[2] = "Sicherheitsdatenblatt - por";
PORTUGESE[3] = "Sicherheitsinfos - por";
PORTUGESE[4] = "Broschüre - por";
PORTUGESE[5] = "Information - por";
}
/**
* @return the s_name
*/
public String getName() {
return s_name;
}
/**
* @param s_name
* the s_name to set
*/
public void setS_name(String s_name) {
this.s_name = s_name;
}
/**
* @return the s_file
*/
public File getFile() {
return s_file;
}
/**
* @param s_file
* the s_file to set
*/
public void setS_file(File s_file) {
this.s_file = s_file;
}
/**
* @return the s_documents
*/
public Vector<SysDataFile> getDocuments() {
return s_documents;
}
/**
* @param s_documents
* the s_documents to set
*/
public void setDocuments(Vector<SysDataFile> s_documents) {
this.s_documents = s_documents;
}
}