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
|
package com.promotelec.exemple;
import org.eclipse.core.runtime.IPlatformRunnable;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* This class controls all aspects of the application's execution
*/
public class Application implements IPlatformRunnable {
private File getMostRecentFile(String directoryPath) {
File directory = new File(directoryPath);
File moreRecentFile = null;
// verification des parametres
if(!directory.exists()){
System.out.println("Le fichier/répertoire '"+directoryPath+"' n'existe pas");
return null;
}
if(!directory.isDirectory()){
System.out.println("Le chemin '"+directoryPath+"' correspond à un fichier et non à un répertoire");
return null;
}
// liste des fichiers "*.txt"
File[] files = directory.listFiles(
new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt");
}
}
);
// cas alternatif: pas de fichiers "*.txt"
if (files.length==0) {
System.out.println("Pas de fichier texte");
return null;
}
// cas general: parcours et memorise le plus recent fichier
moreRecentFile=files[0];
for(File f:files) {
if (f.lastModified()>moreRecentFile.lastModified())
moreRecentFile = f;
}
System.out.println("Le fichier le plus récent du répertoire "+directoryPath +" est "+moreRecentFile.getName());
return moreRecentFile;
}
private Map<String,String> getFileContentAsMap(File file) throws IOException {
// verification des parametres
if (file==null) return null;
// flux de lecture du fichier
BufferedReader in = new BufferedReader(new FileReader(file));
// lecture 1ere ligne
String str = in.readLine();
String[] column = str.split("\t");
// lecture 2nde ligne
Map<String,String> map = new HashMap<String,String>();
str = in.readLine();
String values[] = str.split("\t");
for(int i=0;i<values.length;i++) {
if (i>=column.length) break; // plus de données que de colonne -> exit
map.put(column[i],values[i]);
System.out.println(column[i]+"="+values[i]);
}
in.close();
return map;
}
public CR_promotelec CR; //instanciation d'un objet de la classe CR_promotelec dans laquelle je décrit mes labels
//public MyView VF;
public Object run(Object args) throws Exception {
Display display = PlatformUI.createDisplay();
//CR = new CR_promotelec();
//VF = new MyView();
Properties prop = new Properties();
try {
prop.load(new FileInputStream("C:\\Temp\\promo_act.properties"));
String repertoire = prop.getProperty("repertoireEchange");
System.out.println(repertoire);
} catch (Exception e) {
System.out.println("Erreur Exception" + e);
}
Application finder = new Application();
File f = finder.getMostRecentFile("C:\\Temp");
Map<String,String> map = getFileContentAsMap(f);
String nom = map.get("contact"); // nom d'une colonne tel que dans la 1ere ligne du fichier
String societe = map.get("RS");
String date = map.get("date");
String duree = map.get("durée");
String categorie = map.get("categorie");
String heure = map.get("heure");
String fonction = map.get("fonction");
String adresse = map.get("adresse");
String ville = map.get("ville");
if (map!=null) {
System.out.println(nom + " travaille dans la societe " + societe + " dans la ville de" + ville);
//System.out.println(CR.getCategorie().toString());
/* CR.getAdresse().setText(adresse);
CR.getDate().setText(date);
CR.getFonction().setText(fonction);
CR.getVille().setText(ville);
CR.getHeure().setText(heure);
CR.getDurée().setText(duree);
CR.getRS().setText(societe);
CR.getContact().setText(nom);*/
//System.out.println(CR.getCatégorie());
try {
int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
if (returnCode == PlatformUI.RETURN_RESTART) {
return IPlatformRunnable.EXIT_RESTART;
}
return IPlatformRunnable.EXIT_OK;
} finally {
display.dispose();
}
}
return args;
}
} |