Advertisement
Guest User

Swing Decoupling example

a guest
Jun 15th, 2014
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.05 KB | None | 0 0
  1. public class MainClass {
  2.  
  3.     public static void main(String[] args) {
  4.         ExportFunction exporter = new ExportFunction();
  5.         exporter.perform();
  6.     }
  7.  
  8. }
  9.  
  10. public class ExportFunction implements ActionPerformedListener {
  11.  
  12.     private ExportUIDialog uiDialog;
  13.  
  14.     public void perform() {
  15.         uiDialog = new ExportUIDialog();
  16.         uiDialog.addActionPerformedListener(this);
  17.  
  18.         SwingUtilities.invokeLater(new Runnable() {
  19.  
  20.             @Override
  21.             public void run() {
  22.                 uiDialog.pack();
  23.                 uiDialog.setVisible(true);
  24.             }
  25.         });
  26.  
  27.     }
  28.  
  29.     @Override
  30.     public void actionPerformed(int action) {
  31.         if (ActionPerformedListener.STARTJOB == action) {
  32.             ExportJob exportJob = new ExportJob();
  33.             exportJob.addJobFinishedListener(uiDialog);
  34.             exportJob.addLogListener(uiDialog);
  35.             exportJob.start();
  36.         }
  37.     }
  38.  
  39. }
  40.  
  41. public class ExportJob extends Thread {
  42.  
  43.     private List<LogListener> logListeners = new ArrayList<LogListener>();
  44.  
  45.     private List<JobFinishedListener> jobDoneListeners = new ArrayList<JobFinishedListener>();
  46.  
  47.     public void addJobFinishedListener(JobFinishedListener listener) {
  48.         jobDoneListeners.add(listener);
  49.     }
  50.  
  51.     public void addLogListener(LogListener listener) {
  52.         logListeners.add(listener);
  53.     }
  54.  
  55.     @Override
  56.     public void run() {
  57.  
  58.         // do some excel creating stuff
  59.         try {
  60.             fireLogListener("Job started");
  61.             Thread.sleep(1000);
  62.             fireLogListener("Excel Sheet 1 created");
  63.             Thread.sleep(1000);
  64.             fireLogListener("Excel Sheet 2 created");
  65.             Thread.sleep(1000);
  66.             fireJobFinishedListener("Here comes your Excel Sheet: Hokus Pokus!");
  67.             Thread.sleep(500);
  68.             fireLogListener("Job finished successfully.");
  69.         } catch (InterruptedException e) {
  70.             fireErrorOccuredListener(e);
  71.         }
  72.  
  73.     }
  74.  
  75.     private void fireLogListener(final String message) {
  76.         for (final LogListener listener : logListeners) {
  77.             if (listener.shouldRunInUIThread()) {
  78.                 SwingUtilities.invokeLater(new Runnable() {
  79.  
  80.                     @Override
  81.                     public void run() {
  82.                         listener.reportLogging(message);
  83.                     }
  84.                 });
  85.             } else {
  86.                 listener.reportLogging(message);
  87.             }
  88.         }
  89.     }
  90.  
  91.     private void fireJobFinishedListener(final String resultMessage) {
  92.         for (final JobFinishedListener listener : jobDoneListeners) {
  93.             if (listener.shouldRunInUIThread()) {
  94.                 SwingUtilities.invokeLater(new Runnable() {
  95.  
  96.                     @Override
  97.                     public void run() {
  98.                         listener.jobDone(resultMessage);
  99.                     }
  100.                 });
  101.             } else {
  102.                 listener.jobDone(resultMessage);
  103.             }
  104.         }
  105.     }
  106.  
  107.     private void fireErrorOccuredListener(Exception e) {
  108.         // demonstrating purpose
  109.     }
  110. }
  111.  
  112. public interface ActionPerformedListener {
  113.  
  114.     public static final int STARTJOB = 0;
  115.    
  116.     public static final int CANCELJOB = 1;
  117.    
  118.     public void actionPerformed(int action);
  119.    
  120. }
  121.  
  122.  
  123. public interface JobFinishedListener extends ThreadListener {
  124.  
  125.     public void jobDone(Object result);
  126.  
  127. }
  128.  
  129.  
  130. public interface LogListener extends ThreadListener{
  131.  
  132.     public void reportLogging(String message);
  133.    
  134. }
  135.  
  136. public interface ThreadListener {
  137.  
  138.     public boolean shouldRunInUIThread();
  139.    
  140. }
  141.  
  142. public class ExportUIDialog extends JDialog implements LogListener,
  143.         JobFinishedListener {
  144.  
  145.     private static final long serialVersionUID = 8349630311908151734L;
  146.  
  147.     private List<ActionPerformedListener> actionListeners = new ArrayList<>();
  148.  
  149.     private JLabel label;
  150.  
  151.     private JList<String> list;
  152.  
  153.     public ExportUIDialog() {
  154.         initGui();
  155.     }
  156.  
  157.     public void addActionPerformedListener(ActionPerformedListener listener) {
  158.         actionListeners.add(listener);
  159.     }
  160.  
  161.     private void initGui() {
  162.         setPreferredSize(new Dimension(400, 400));
  163.         setLayout(new GridBagLayout());
  164.  
  165.         JButton button = new JButton("Save to Excel");
  166.         button.addActionListener(new ActionListener() {
  167.  
  168.             @Override
  169.             public void actionPerformed(ActionEvent arg0) {
  170.                 if (startConditionsFulfilled()) {
  171.                     fireActionListener(ActionPerformedListener.STARTJOB);
  172.                 }
  173.             }
  174.  
  175.         });
  176.         GridBagConstraints cons = new GridBagConstraints();
  177.         cons.gridx = 0;
  178.         cons.gridy = 0;
  179.         add(button, cons);
  180.  
  181.         label = new JLabel("ResultLabel");
  182.         cons.gridy = 2;
  183.         add(label, cons);
  184.  
  185.         list = new JList<String>();
  186.         list.setVisibleRowCount(5);
  187.         list.setModel(new DefaultListModel<String>());
  188.         cons.gridy = 1;
  189.         cons.fill = GridBagConstraints.BOTH;
  190.         add(list, cons);
  191.  
  192.     }
  193.  
  194.     private void fireActionListener(int action) {
  195.         for (ActionPerformedListener listener : actionListeners) {
  196.             listener.actionPerformed(action);
  197.         }
  198.     }
  199.  
  200.     private boolean startConditionsFulfilled() {
  201.         // do some UI prerequisites checks then tell the interface the
  202.         // action can be performed.
  203.         return true;
  204.     }
  205.  
  206.     @Override
  207.     public boolean shouldRunInUIThread() {
  208.         // inherited from listener interface. This method is called from
  209.         // Business logic to decide whether SwingUtilies.invokeLater is
  210.         // necessary or not.
  211.         return true;
  212.     }
  213.  
  214.     @Override
  215.     public void reportLogging(String message) {
  216.         ((DefaultListModel<String>) list.getModel()).addElement(message);
  217.     }
  218.  
  219.     @Override
  220.     public void jobDone(Object result) {
  221.         if (result instanceof String) {
  222.             label.setText((String) result);
  223.         }
  224.     }
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement