Jetzt, wo ich wieder dran bin, will ich auch noch einen Codeabriß liefern. Zur Erklärung: Die Methoden summon() und die() kommen aus einer Factory (Creature ist die abstrakte Methode dazu) und werden von einer zentralen Stelle aus aufgerufen. summon() bastelt erst das Design und ruft dann den Kopierthread in einer asyncExec-Methode auf. Aus dem heraus wieder sollen die ProgressBar und das "Kopiert gerade Datei: xy"-Label aktualisiert werden.
Code:
public class Ftpcopy extends Creature {
private static ProgressBar pbar;
private static Label currentFileLabel;
// TODO replace this with some value read from some options file
private static final String sourceFileName = "source.zip";
private static ZipFile productArchive;
class Copyer extends Thread {
private ZipEntry zipentry;
private int iter = 1;
public Copyer() {
super("Copyer");
}
public synchronized void run () {
// set wait cursor
innerArea.getShell().setCursor
(new Cursor(innerArea.getDisplay(),SWT.CURSOR_WAIT));
try {
productArchive = new ZipFile(sourceFileName);
pbar.setSelection(0);
pbar.setMaximum(productArchive.size());
FtpManager.initConnection();
FtpManager.changeToDestDir();
for(Enumeration e = productArchive.entries();
e.hasMoreElements();) {
iter++;
final ZipEntry zipentry = (ZipEntry)e.nextElement();
pbar.setSelection(iter);
setCurrentFileLabelDesc(zipentry.getName());
if(zipentry.isDirectory())
FtpManager.makeDirectory(zipentry.getName());
else
FtpManager.uploadZipEntry(productArchive,zipentry);
}
} catch (Exception e) { // TODO DEBUG
System.err.println("Exception when uploading: "+e.toString()
+"\n- - - - - - - - -\n");
e.printStackTrace();
}
// restore layout
proceedBtn.setEnabled(true);
cancelBtn.setEnabled(true);
innerArea.getShell().setCursor
(new Cursor(innerArea.getDisplay(),SWT.CURSOR_ARROW));
}
}
/**
* Patch label together. Saves typework. :P
* @param desc
**/
private void setCurrentFileLabelDesc(String desc) {
if(desc.length() > 80) {
desc = "..."+desc.substring((desc.length()-77),desc.length());
}
currentFileLabel.setText(
WizardDialogueFactory.getP("Ftpcopy","currentfilelabeldesc")
+" "+desc
);
}
/* * *
* (non-Javadoc)
* @see de.trionic.mpi.gui.creatures.Creature#summon(de.trionic.mpi.gui.DialoguePage)
* * */
public void summon(DialoguePage diag) {
/* some init stuff */
innerArea = diag.innerArea;
proceedBtn = diag.submitBtn;
cancelBtn = diag.cancelBtn;
proceedBtn.setEnabled(false);
cancelBtn.setEnabled(false);
/* end init stuf */
/* BEGIN layout creation */
// ... SWT layout blabla...
/* END layout creation */
/* BEGIN copy */
innerArea.getDisplay().asyncExec(new Copyer());
/* END copy */
}
/* (non-Javadoc)
* @see de.trionic.mpi.gui.creatures.Creature#die()
*/
public void die() {
FtpManager.closeConnection();
}
}
Lesezeichen