Package net.sf.jpluck.apps.jpluckx.ui

Source Code of net.sf.jpluck.apps.jpluckx.ui.AutoUpdateFrame$CloseAction

package net.sf.jpluck.apps.jpluckx.ui;

import java.awt.event.ActionEvent;
import java.io.File;
import java.text.DateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import net.sf.jpluck.ClientConfiguration;
import net.sf.jpluck.apps.jpluckx.JPluckX;
import net.sf.jpluck.conversion.Conversion;
import net.sf.jpluck.conversion.ConversionListener;
import net.sf.jpluck.jxl.Document;
import net.sf.jpluck.jxl.JXL;
import net.sf.jpluck.swing.GUIUtil;
import net.sf.jpluck.swing.WindowUtil;

import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.FormLayout;


public class AutoUpdateFrame extends JFrame implements ConversionListener {
  private Conversion conversion;
  private JLabel statusLabel = new JLabel();
  private JProgressBar statusProgress = new JProgressBar();
  private StartAction startAction = new StartAction();
  private StopAction stopAction = new StopAction();
  private int documentCount;

  public AutoUpdateFrame() {
    initComponents();
  }

  public void conversionCompleted(Document jxlDocument, final int current, final int total, final boolean success) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          setTitle("AutoUpdate (" + current + "/" + total + ")");
          statusProgress.setValue(statusProgress.getValue() + 1);
          if (success) {
            documentCount++;
          }
        }
      });
  }

  public void conversionFinished() {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          Date now = new Date();
          String date = DateFormat.getDateInstance(DateFormat.MEDIUM).format(now);
          String time = DateFormat.getTimeInstance(DateFormat.SHORT).format(now);
          statusLabel.setText("Last converted " + documentCount + " document(s) on " +
                    date + " at " + time);
          statusProgress.setValue(0);
          statusProgress.setString("Idle");
          setTitle("AutoUpdate active");
        }
      });
  }

  public void conversionStarted(final Document jxlDocument, final int current, final int total) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          if (current==1) {
            documentCount=0;
          }
          setTitle("AutoUpdate (" + current + "/" + total + ")");
          statusLabel.setText(jxlDocument.getName());
          statusProgress.setString(current + "/" + total);
          statusProgress.setValue(current - 1);
          statusProgress.setMaximum(total);
        }
      });
  }

  public void generationCompleted(Document jxlDocument) {
  }

  public void generationStarted(Document jxlDocument) {
  }

  public void statusMessage(String message) {
  }

  public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
    AutoUpdateFrame dlg = new AutoUpdateFrame();
    JXL jxl = new JXL(new File(args[0]));
    Conversion conversion = new Conversion(jxl.getDocuments());
    conversion.addConversionListener(dlg);
    WindowUtil.center(dlg);
    dlg.show();
    dlg.runConversion(conversion);

    //System.exit(0);
  }

  public void runConversion(Conversion conversion) {
    this.conversion = conversion;
    conversion.addConversionListener(this);
    conversion.run();
    conversion.removeConversionListener(this);
  }

  private void initComponents() {
    try {
      setIconImage(ImageIO.read(ListFrame.class.getClassLoader().getResource("net/sf/jpluck/res/jpluck16.gif")));
    } catch (Exception e) {
      // Should not occur;
    }
    setTitle("AutoUpdate inactive");
    setResizable(false);

    statusLabel.setText("No documents converted yet");
    statusLabel.setEnabled(false);
    statusProgress.setStringPainted(true);
    statusProgress.setString("Idle");
    statusProgress.setEnabled(false);

    CloseAction closeAction = new CloseAction();
    GUIUtil.configureEscapeKey(getRootPane(), closeAction);

    ButtonBarBuilder bbb = new ButtonBarBuilder();
    JButton startButton = new JButton(startAction);
    JButton stopButton = new JButton(stopAction);
    JButton closeButton = new JButton(closeAction);
    bbb.addGridded(startButton);
    bbb.addRelatedGap();
    bbb.addGridded(stopButton);
    bbb.addGlue();
    bbb.addGridded(closeButton);

    FormLayout formLayout = new FormLayout("fill:200dlu:grow", "pref, 4dlu, pref, 7dlu, pref");
    PanelBuilder panelBuilder = new PanelBuilder(formLayout);
    panelBuilder.setDefaultDialogBorder();
    panelBuilder.add(statusLabel, "1,1");
    panelBuilder.add(statusProgress, "1,3");
    panelBuilder.add(bbb.getPanel(), "1,5");
    setContentPane(panelBuilder.getPanel());

    pack();
  }

  private class CloseAction extends AbstractAction {
    CloseAction() {
      super("Close");
    }

    public void actionPerformed(ActionEvent e) {
      hide();
    }
  }

  private class StartAction extends AbstractAction {
    public StartAction() {
      super("Start");
    }

    public void actionPerformed(ActionEvent e) {
      JPluckX.getInstance().startAutoUpdate();
      ClientConfiguration.getDefault().setAutoUpdateEnabled(true);     
      setAutoUpdate(true);     
    }
  }

  private class StopAction extends AbstractAction {
    public StopAction() {
      super("Stop");
      setEnabled(false);
    }

    public void actionPerformed(ActionEvent e) {
      JPluckX.getInstance().stopAutoUpdate();
      ClientConfiguration.getDefault().setAutoUpdateEnabled(false);     
      setAutoUpdate(false);
    }
  }
 
  public void setAutoUpdate(boolean enabled) {
    if (enabled) {
      setTitle("AutoUpdate active");
    }else {
      setTitle("AutoUpdate inactive");
    }
    startAction.setEnabled(!enabled);
    stopAction.setEnabled(enabled);
    statusLabel.setEnabled(enabled);;
    statusProgress.setEnabled(enabled);
  }
}
TOP

Related Classes of net.sf.jpluck.apps.jpluckx.ui.AutoUpdateFrame$CloseAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.