Skip to content

Commit

Permalink
Added progressbars to make the progress visible.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasmiosczka committed Apr 27, 2018
1 parent e4905cf commit 096488a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.tobiasmiosczka.cinema.KDMManager.gui;

public interface IUpdate {
void onUpdateEmailBox(int current, int total, String host);
void onUpdateEmailLoading(int current, int total);
void onUpdateSending(int current, int total);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,23 @@
import org.jdom2.JDOMException;

import javax.mail.MessagingException;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.Collection;

public class Window extends JFrame {
public class Window extends JFrame implements IUpdate {

private JList<EmailLogin> lEmailLoginList;
private JList<FtpLogin> lFtpLoginList;

private final DefaultListModel<FtpLogin> dlmFtpLogins = new DefaultListModel<>();
private final DefaultListModel<EmailLogin> dlmEmailLogin = new DefaultListModel<>();

private JLabel lbResult;
private JProgressBar pbMajor, pbMinor;

private Config config = new Config();

Expand All @@ -55,12 +50,41 @@ public Window() throws IOException, JDOMException {
this.setVisible(true);
}

@Override
public void onUpdateEmailLoading(int current, int total) {
System.out.println("onUpdateEmailLoading " + current + "/" + total);
EventQueue.invokeLater(() -> {
pbMinor.setMaximum(total);
pbMinor.setValue(current);
});
}

@Override
public void onUpdateSending(int current, int total) {
System.out.println("onUpdateSending " + current + "/" + total);
System.out.println("onUpdateEmailLoading " + current + "/" + total);
EventQueue.invokeLater(() -> {
pbMajor.setMaximum(total);
pbMajor.setValue(current);
});
}

@Override
public void onUpdateEmailBox(int current, int total, String host) {
System.out.println("onUpdateEmailBox " + current + "/" + total + " " + host);
EventQueue.invokeLater(() -> {
pbMajor.setMaximum(total);
pbMajor.setValue(current);
pbMajor.setString("Loading Emails from: " + host);
});
}

private void init() {
this.setTitle("KDMManager");
this.setLayout(null);
this.setResizable(false);
Container c = this.getContentPane();
c.setPreferredSize(new Dimension(600, 635));
c.setPreferredSize(new Dimension(600, 665));

JLabel lbEmailLogins = new JLabel("Email Logins");
lbEmailLogins.setBounds(5, 5, 190, 30);
Expand Down Expand Up @@ -150,38 +174,46 @@ private void init() {
btDeleteFtpLogin.setBounds(405, 520, 190, 30);
c.add(btDeleteFtpLogin);

lbResult = new JLabel();
lbResult.setBounds(5, 555, 590, 30);
c.add(lbResult);
pbMajor = new JProgressBar();
pbMajor.setStringPainted(true);
pbMajor.setBounds(5, 555, 590, 30);
c.add(pbMajor);

pbMinor = new JProgressBar();
pbMinor.setStringPainted(true);
pbMinor.setBounds(5, 585, 590, 30);
c.add(pbMinor);

JButton btLoadKdms = new JButton("Load KDMs");
btLoadKdms.addActionListener(a -> loadKdms());
btLoadKdms.setBounds(5, 600, 590, 30);
btLoadKdms.setBounds(5, 625, 590, 30);
c.add(btLoadKdms);
}

private void loadKdms() {
Collection<KDM> kdms = null;
long start = System.currentTimeMillis();
try {
kdms = EmailHelper.getKdmsFromEmail(config.getEmailLogins());
} catch (MessagingException | JDOMException | ParseException | IOException e) {
//TODO: implement
e.printStackTrace();
}
if(kdms == null) {
//TODO: implement
return;
}
try {
FtpHelper ftpHelper = new FtpHelper(config.getFtpLogins());
ftpHelper.uploadFiles(kdms);
} catch (IOException | FtpException e) {
//TODO: implement
e.printStackTrace();
}
long diff = System.currentTimeMillis() - start;
lbResult.setText("Loaded " + kdms.size() + " KDMs after " + diff / 1000 + " seconds.");
new Thread(() -> {
Collection<KDM> kdms = null;
long start = System.currentTimeMillis();
try {
kdms = EmailHelper.getKdmsFromEmail(config.getEmailLogins(), this);
} catch (MessagingException | JDOMException | ParseException | IOException e) {
//TODO: implement
e.printStackTrace();
}
if(kdms == null) {
//TODO: implement
return;
}
try {
FtpHelper ftpHelper = new FtpHelper(config.getFtpLogins());
ftpHelper.uploadFiles(kdms, this);
} catch (IOException | FtpException e) {
//TODO: implement
e.printStackTrace();
}
long diff = System.currentTimeMillis() - start;
pbMajor.setString("Loaded " + kdms.size() + " KDMs after " + diff / 1000 + " seconds.");
}).start();
}

private void updateEmailLoginList() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.tobiasmiosczka.cinema.KDMManager.helper;

import com.github.tobiasmiosczka.cinema.KDMManager.gui.IUpdate;
import com.github.tobiasmiosczka.cinema.KDMManager.pojo.EmailLogin;
import com.github.tobiasmiosczka.cinema.KDMManager.pojo.KDM;
import org.jdom2.JDOMException;
Expand All @@ -15,7 +16,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;
Expand All @@ -32,9 +32,11 @@ private static Collection<KDM> getKdmsFromZip(InputStream inputStream) throws IO
return files;
}

private static Collection<KDM> handleMessages(Collection<Message> messages) throws IOException, JDOMException, ParseException, MessagingException {
private static Collection<KDM> handleMessages(Message[] messages, IUpdate iUpdate) throws IOException, JDOMException, ParseException, MessagingException {
Collection<KDM> kdms = new HashSet<>();
int current = 0;
for (Message message : messages) {
iUpdate.onUpdateEmailLoading(current++, messages.length);
if (message.getContentType().contains("multipart")) {
Multipart multipart = (Multipart) message.getContent();
for (int i = 0; i < multipart.getCount(); ++i) {
Expand All @@ -52,12 +54,15 @@ private static Collection<KDM> handleMessages(Collection<Message> messages) thro
}
}
}
iUpdate.onUpdateEmailLoading(current, messages.length);
return kdms;
}

public static Collection<KDM> getKdmsFromEmail(Collection<EmailLogin> emailLogins) throws MessagingException, IOException, JDOMException, ParseException {
public static Collection<KDM> getKdmsFromEmail(Collection<EmailLogin> emailLogins, IUpdate iUpdate) throws MessagingException, IOException, JDOMException, ParseException {
Collection<KDM> kdms = new HashSet<>();
int current = 0;
for (EmailLogin emailLogin : emailLogins) {
iUpdate.onUpdateEmailBox(current++, emailLogins.size(), emailLogin.toString());
Properties properties = new Properties();
properties.put("mail.pop3.host", emailLogin.getHost());
properties.put("mail.pop3.port", emailLogin.getPort());
Expand All @@ -67,10 +72,11 @@ public static Collection<KDM> getKdmsFromEmail(Collection<EmailLogin> emailLogin
store.connect(emailLogin.getHost(), emailLogin.getUser(), emailLogin.getPassword());
Folder emailFolder = store.getFolder(emailLogin.getFolder());
emailFolder.open(Folder.READ_ONLY);
kdms.addAll(handleMessages(Arrays.asList(emailFolder.getMessages())));
kdms.addAll(handleMessages(emailFolder.getMessages(),iUpdate));
emailFolder.close(false);
store.close();
}
iUpdate.onUpdateEmailBox(current, emailLogins.size(), "");
return kdms;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.tobiasmiosczka.cinema.KDMManager.helper;

import com.github.tobiasmiosczka.cinema.KDMManager.gui.IUpdate;
import com.github.tobiasmiosczka.cinema.KDMManager.pojo.FtpException;
import com.github.tobiasmiosczka.cinema.KDMManager.pojo.FtpLogin;
import com.github.tobiasmiosczka.cinema.KDMManager.pojo.KDM;
Expand Down Expand Up @@ -32,8 +33,10 @@ private FTPClient getFtpClient(FtpLogin ftpLogin) throws IOException, FtpExcepti
return ftpClient;
}

public void uploadFiles(Collection<KDM> files) throws IOException, FtpException {
public void uploadFiles(Collection<KDM> files, IUpdate iUpdate) throws IOException, FtpException {
int current = 0;
for (KDM file : files) {
iUpdate.onUpdateSending(current++, files.size());
FTPClient ftpClient = serverMap.get(file.getServer());
if (ftpClient == null) {//what should be done, if the kdm is for an unknown server? probalby just skip it
continue;
Expand All @@ -42,5 +45,6 @@ public void uploadFiles(Collection<KDM> files) throws IOException, FtpException
throw new FtpException(ftpClient.getReplyCode());
}
}
iUpdate.onUpdateSending(current, files.size());
}
}

0 comments on commit 096488a

Please sign in to comment.