Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainClass {
- public static void main(String[] args) {
- ExportFunction exporter = new ExportFunction();
- exporter.perform();
- }
- }
- public class ExportFunction implements ActionPerformedListener {
- private ExportUIDialog uiDialog;
- public void perform() {
- uiDialog = new ExportUIDialog();
- uiDialog.addActionPerformedListener(this);
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- uiDialog.pack();
- uiDialog.setVisible(true);
- }
- });
- }
- @Override
- public void actionPerformed(int action) {
- if (ActionPerformedListener.STARTJOB == action) {
- ExportJob exportJob = new ExportJob();
- exportJob.addJobFinishedListener(uiDialog);
- exportJob.addLogListener(uiDialog);
- exportJob.start();
- }
- }
- }
- public class ExportJob extends Thread {
- private List<LogListener> logListeners = new ArrayList<LogListener>();
- private List<JobFinishedListener> jobDoneListeners = new ArrayList<JobFinishedListener>();
- public void addJobFinishedListener(JobFinishedListener listener) {
- jobDoneListeners.add(listener);
- }
- public void addLogListener(LogListener listener) {
- logListeners.add(listener);
- }
- @Override
- public void run() {
- // do some excel creating stuff
- try {
- fireLogListener("Job started");
- Thread.sleep(1000);
- fireLogListener("Excel Sheet 1 created");
- Thread.sleep(1000);
- fireLogListener("Excel Sheet 2 created");
- Thread.sleep(1000);
- fireJobFinishedListener("Here comes your Excel Sheet: Hokus Pokus!");
- Thread.sleep(500);
- fireLogListener("Job finished successfully.");
- } catch (InterruptedException e) {
- fireErrorOccuredListener(e);
- }
- }
- private void fireLogListener(final String message) {
- for (final LogListener listener : logListeners) {
- if (listener.shouldRunInUIThread()) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- listener.reportLogging(message);
- }
- });
- } else {
- listener.reportLogging(message);
- }
- }
- }
- private void fireJobFinishedListener(final String resultMessage) {
- for (final JobFinishedListener listener : jobDoneListeners) {
- if (listener.shouldRunInUIThread()) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- listener.jobDone(resultMessage);
- }
- });
- } else {
- listener.jobDone(resultMessage);
- }
- }
- }
- private void fireErrorOccuredListener(Exception e) {
- // demonstrating purpose
- }
- }
- public interface ActionPerformedListener {
- public static final int STARTJOB = 0;
- public static final int CANCELJOB = 1;
- public void actionPerformed(int action);
- }
- public interface JobFinishedListener extends ThreadListener {
- public void jobDone(Object result);
- }
- public interface LogListener extends ThreadListener{
- public void reportLogging(String message);
- }
- public interface ThreadListener {
- public boolean shouldRunInUIThread();
- }
- public class ExportUIDialog extends JDialog implements LogListener,
- JobFinishedListener {
- private static final long serialVersionUID = 8349630311908151734L;
- private List<ActionPerformedListener> actionListeners = new ArrayList<>();
- private JLabel label;
- private JList<String> list;
- public ExportUIDialog() {
- initGui();
- }
- public void addActionPerformedListener(ActionPerformedListener listener) {
- actionListeners.add(listener);
- }
- private void initGui() {
- setPreferredSize(new Dimension(400, 400));
- setLayout(new GridBagLayout());
- JButton button = new JButton("Save to Excel");
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- if (startConditionsFulfilled()) {
- fireActionListener(ActionPerformedListener.STARTJOB);
- }
- }
- });
- GridBagConstraints cons = new GridBagConstraints();
- cons.gridx = 0;
- cons.gridy = 0;
- add(button, cons);
- label = new JLabel("ResultLabel");
- cons.gridy = 2;
- add(label, cons);
- list = new JList<String>();
- list.setVisibleRowCount(5);
- list.setModel(new DefaultListModel<String>());
- cons.gridy = 1;
- cons.fill = GridBagConstraints.BOTH;
- add(list, cons);
- }
- private void fireActionListener(int action) {
- for (ActionPerformedListener listener : actionListeners) {
- listener.actionPerformed(action);
- }
- }
- private boolean startConditionsFulfilled() {
- // do some UI prerequisites checks then tell the interface the
- // action can be performed.
- return true;
- }
- @Override
- public boolean shouldRunInUIThread() {
- // inherited from listener interface. This method is called from
- // Business logic to decide whether SwingUtilies.invokeLater is
- // necessary or not.
- return true;
- }
- @Override
- public void reportLogging(String message) {
- ((DefaultListModel<String>) list.getModel()).addElement(message);
- }
- @Override
- public void jobDone(Object result) {
- if (result instanceof String) {
- label.setText((String) result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement