1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| package fr.packagea;
//Classe qui possède plusieurs méthodes pour le scénario
///InsererElement
//CreerXML
//EnregistrerXML
//copier
//supprTableau
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.MultimediaInfo;
import java.util.ArrayList;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import javafx.scene.control.TextInputDialog;
import javax.activation.MimetypesFileTypeMap;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.*;
/**
*
*
*/
public class Scenario {
///Création d'un tableau "tabElement" de type Array list
ArrayList<ElementInit> tabElement = new ArrayList<>();
//On créer la racine que l'on nomme scénario
Element scenario = new Element("scenario");
org.jdom2.Document document = new Document(scenario);
//méthode pour mettre un objet de la classe ElementInit dans un tableau et ainsi le sauvegarder
void ajouterElement(ElementInit elem) {
tabElement.add(elem); //On ajoute un objet dans le tableau
}
//méthode pour supprimer tous les obets du tableau
void supprTableau(){
tabElement.clear(); //On enleve tous les objets du tableau
}
//méthode pour déplacer les éléments dans la collection et dans le tableView
void deplacerElement(int x,int y){
if(x >= 0) {
ElementInit toMove = tabElement.get(x);
tabElement.set(x, tabElement.get(x-y));
tabElement.set(x-y, toMove);
tabElement.get(x-y).rang=tabElement.get(x).rang;
tabElement.get(x).rang=tabElement.get(x).rang+y;
}
}
void supprimerElement(int eleSupprimer){
tabElement.get(eleSupprimer+1).rang=tabElement.get(eleSupprimer).rang+1;
for(int j=eleSupprimer+1;j<tabElement.size();j++){
tabElement.get(j).rang=tabElement.get(j).rang-1;
}
tabElement.remove(eleSupprimer);
}
//méthode pour créer le fichier XML
void creerXML(LocalTime hDebut,LocalTime hFin){
//On met l'heure de début du scénario
Element heureDebut=new Element("heureDebut");
heureDebut.setText(hDebut.toString());
scenario.addContent(heureDebut);
//On met l'heure de fin du scénario
Element heureFin=new Element("heureFin");
heureFin.setText(hFin.toString());
scenario.addContent(heureFin);
//Création d'une boucle pour mettre les elements du tableau
for(ElementInit elem : tabElement){
//On créer un Element que l'on nomme "element" et dans lequel on va mettre tout les champs dedans
Element element =new Element("element");
scenario.addContent(element);
//On créer un Element pour le champ rang et on l'associe a element
Element rang =new Element("rang");
rang.setText(String.valueOf(elem.rang));
element.addContent(rang);
//On créer un Element pour le champ type et on l'associe a element
Element type =new Element ("type");
type.setText(String.valueOf(elem.type));
element.addContent(type);
//On créer un Element pour le champ duree et on l'associe a element
Element duree =new Element("duree");
duree.setText(String.valueOf(elem.duree));
element.addContent(duree);
//On créer un Element pour le champ nom et on l'associe a element
Element nom =new Element("nom");
nom.setText(elem.nom);
element.addContent(nom);
//On créer un Element pour le champ descriptif et on l'associe a element
Element descriptif =new Element("descriptif");
descriptif.setText(elem.descriptif);
element.addContent(descriptif);
//On créer un Element pour le champ chemin et on l'associe a element
Element chemin =new Element("chemin");
chemin.setText(elem.cheminFichier);
element.addContent(chemin);
}
}
void creerXMLVide(){
Locale loc = new Locale("fr", "ca");
DateFormat df = new SimpleDateFormat("hh:mm", loc);
Date d=new Date();
String dateF = df.format(d.getTime());
Element heureDebut=new Element("heureDebut");
heureDebut.setText("00:00");
scenario.addContent(heureDebut);
//On met l'heure de fin du scénario
Element heureFin=new Element("heureFin");
heureFin.setText(dateF.toString());
scenario.addContent(heureFin);
}
//méthode pour enregistrer le fichier XML
void enregistrerXML(String fichier){
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, new FileOutputStream(fichier+".xml"));
}
catch (java.io.IOException e){}
}
void insererElement(int pos,ElementInit ele){
tabElement.add(pos,ele);
tabElement.get(pos).rang=tabElement.get(pos).rang-1;
pos+=1;
for(int i=pos;i<tabElement.size();i++){
tabElement.get(i).rang = tabElement.get(i).rang+1;
}
}
int recupXML(File fichier){
SAXBuilder sax=new SAXBuilder();
try
{
//On crée un nouveau document JDOM avec en argument le fichier XML
document = sax.build(fichier);
}
catch(IOException | JDOMException e){}
//On recrée la racine du XML
Element racine = document.getRootElement();
//On crée un tableau dynamique pour mettre tous les éléments du XML dedans
List<Element> elementXML = racine.getChildren("element");
//On crée un iterator pour parcourir la liste
Iterator o = elementXML.iterator();
//Parcours du tableau
for(Element z : elementXML){
while(o.hasNext()){
Element courant = (Element)o.next();
ElementInit elem2=new ElementInit();
elem2.rang=Integer.parseInt(courant.getChildText("rang"));
elem2.type=courant.getChildText("type");
elem2.duree=Integer.parseInt(courant.getChildText("duree"));
elem2.nom=courant.getChildText("nom");
elem2.descriptif=courant.getChildText("descriptif");
elem2.cheminFichier=courant.getChildText("chemin");
ajouterElement(elem2);
}
}
return elementXML.size();
}
ElementInit getElement(int i){
ElementInit e=tabElement.get(i);
return e;
}
void modifierElement(int i,ElementInit ele){
tabElement.set(i, ele);
}
//méthode pour copier les fichiers choisis dans un emplacement réseau
public boolean copier() throws IOException {
ObtentionVerification ob=new ObtentionVerification();
String destCopie=ob.propriete("destinationCopie");
for(ElementInit elem : tabElement){
Path source = Paths.get(elem.cheminFichier);
Path destination=Paths.get(destCopie+elem.nom); //Chemin pour la copie des ressources
try {
//écrase le fichier destination s'il existe et copie les attributs de la source sur la destination.
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
} catch (IOException e) {
e.printStackTrace(); //affiche l'exception et l'état de la pile d'exécution au moment de son appel
return false;
}
elem.cheminFichier=destination.toString(); //met dans le tableau le chemin des fichiers copier à la place de ceux qui ont été sélectionné
}
return true;
}
} |
Partager