Je suis actuellement en train de faire un programme d'upload en m'aidant de la librairie ftp4j.
Structure de mon code :
public class ElementsTab extends AbstractTableModel {
private static final ArrayList processList = new ArrayList();
...
public Object getValueAt(int rowIndex, int columnIndex) {
try {
Process p = processList.get(rowIndex);
switch(columnIndex){
case 0: //Filename
return p.getFileName();
case 1: //Size
return p.getSize();
case 2: //barre progression
return "0";
case 3: //statut
return p.getStatut();
case 4:
return p.getFile();
}
} catch(Exception e) {
System.out.println(e);
}
}
public class Process {
File file;
Process(File file) {
this.file = file;
}
public File getFile() {
return file;
}
public String getFileName() {
return file.getName();
}
public String getStatut(){
return ftp4j.FTPClient.getStatut();
}
...
}
évènement sur le bouton de lancement de transfert :
public ElementsTab e = new ElementsTab();
private void GO(java.awt.event.MouseEvent evt) {
for(int i=0; i<e.getRowCount();i++){
FTPUploadFichier.main(new Process(files[i]),FtpConnexion.connexion("****"));
}
}
public class FTPUploadFichier {
public static void main(Process fichierU, FTPClient client) {
try{
if(client != null){
try{
client.upload(fichierU.getFile(), new FTPStatut());
}catch(Exception ex){
System.out.println("Erreur de transfert fichier : " + ex);
}
}
}
...
}
}
public class FTPStatut implements FTPDataTransferListener
//rien de bien important, mais nécessaire.
Ajout dans la classe FTPClient de ftp4j :
public static String getStatut(){
return statutFichier;
}
//statutFichier étant juste une variable chaine qui change selon le listener
//par exemple :
//if (listener !null) {listener.started();statutFichier "En cours";}
Mon problème est le suivant :
Lorsque le statut change pendant un upload, il modifie toute la colonne avec la valeur de la variable statutFichier alors qu'il faudrait que le changement se fasse seulement sur la ligne du fichier uploadé.
Lorsque ElementsTab est appelé, ne devrait-t-il pas modifier uniquement la ligne concernée ? Je ne vois vraiment pas comment faire j'ai essayé moult code, sans succès.
Avec ton aide j'ai pu arriver au code suivant mais sans parvenir à la solution. Je ne vois pas d'ou viens le problème.
**class process**
public class Process implements ftp4j.FTPDataTransferListener {
private List liste = new ArrayList();
File file;
public Process(File file) {
this.file = file;
}
Process(int rowIndex, Object aValue) {
this.file = (File) aValue;
}
public File getFile() {
return file;
}
public String getFileName() {
return file.getName();
}
public long getSize() {
return file.length();
}
public String getStatut(){
if(status != null || !"En attente".equals(status)){
return status;
}else{
return "En attente";
}
}
//création de l'évènement
public void setStatus(String pNewStatus){
//appel à la méthode qui averti les différents listeners du changement de la variable.
firePropertyChange(new PropertyChangeEvent(this, "status", status, pNewStatus));
status = pNewStatus;
}
public String getSize(long bytes) {
if (bytes > 1048576) {
double div = bytes / 1048576;
return div + " MB";
} else if (bytes > 1024) {
double div = bytes / 1024;
return div + " KB";
} else {
return bytes + " bits";
}
}
private String status = "En attente";
@Override
public void started() {
this.setStatus("En cours");
System.out.println("parti!");
}
@Override
public void transferred(int length) {
}
@Override
public void completed() {
this.setStatus("Transféré");
System.out.println("fini!");
}
@Override
public void aborted() {
this.setStatus("Annulé");
}
@Override
public void failed() {
this.setStatus("Echoué");
}
public void addPropertyChangeListener(PropertyChangeListener listener){
liste.add(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener){
liste.remove(listener);
}
//envoie l'évènement aux listeners
private void firePropertyChange(PropertyChangeEvent propertyChangeEvent) {
for ( PropertyChangeListener listener : liste){
listener.propertyChange(propertyChangeEvent);
}
}
}
**class ElementsTab**
public class ElementsTab extends AbstractTableModel implements PropertyChangeListener{
private static final ArrayList processList = new ArrayList();
private String[] columnNames;
public ElementsTab() {
columnNames = new String[] {"Fichier", "Taille", "Progression","Status"};
}
public void addLigne(Process p){
p.addPropertyChangeListener(this);
processList.add(p);
//Fire table row insertion notification to table.
fireTableRowsInserted(getRowCount()-1,getRowCount()-1);
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return processList.size();
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
public Process getElement(String e){
if(processList.contains(e)){
return processList.get(processList.indexOf(e));
}else{
return null;
}
}
public void removed(int entier){
processList.remove(entier).addPropertyChangeListener(this);
fireTableDataChanged();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
try {
Process p = processList.get(rowIndex);
switch(columnIndex){
case 0: //Filename
return p.getFileName();
case 1: //Size
return p.getSize(p.getSize());
case 2: //barre progression
return "0";//new Integer(p.getBitsTranmis());
case 3: //statut
return p.getStatut();//"En attente";//p.getStatut(rowIndex);
case 4:
return p.getFile();
}
} catch(Exception e) {
///Exception occurs when user removes some rows and progress bar requesting old index.. Must catch this otherwise runtime error
System.out.println(e);
}
return "";
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
fireTableDataChanged();
System.out.println("PropertyChangeEvent");
}
}
**button action**
public static File[] files;
public ElementsTab e = new ElementsTab();
public Component frame = null;
private void jButtonPActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
// Enable multiple selections
chooser.setMultiSelectionEnabled(true);
chooser.setFileFilter(new ParcourirFiltre());
chooser.setAcceptAllFileFilterUsed(false);
chooser.setDialogTitle("Selection d'images");
// Show the dialog; wait until dialog is closed
chooser.showOpenDialog(this);
// Retrieve the selected files. This method returns empty
// if multiple-selection mode is not enabled.
files = chooser.getSelectedFiles();
//ProgressRenderer pr = new ProgressRenderer();
for( int z = 0; z<files.length; ++z){
if (!(files[z].length() > 5242880)){
if(verif(files[z].getName())==true){
e.addLigne(new Process(files[z]));
}
}else{
JOptionPane.showMessageDialog(frame, "Taille du fichier '" + files[z].getName() + "' supérieure à 5MB","Erreur fichier",JOptionPane.WARNING_MESSAGE);
}
}
jTable1.setModel(e);
jTable1.getColumnModel().getColumn(2).setCellRenderer(new ProgressRenderer());
}
En faites ça marche, ça ne marchait pas car lorsque j'uploadais, j'instanciais un nouveau process. Depuis que j'utilise le process qui à le listener ça marche ;)