JFrameContextView.java

  1. package org.jbehave.core.context;

  2. import static java.text.MessageFormat.format;
  3. import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;

  4. import java.awt.BorderLayout;
  5. import java.awt.Point;
  6. import java.awt.event.MouseEvent;
  7. import java.util.concurrent.TimeUnit;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JPanel;
  11. import javax.swing.border.EmptyBorder;
  12. import javax.swing.event.MouseInputAdapter;

  13. @SuppressWarnings("checkstyle:MemberName")
  14. public class JFrameContextView implements ContextView {

  15.     protected JFrame frame;
  16.     protected JLabel label;
  17.     protected int width;
  18.     protected int height;
  19.     protected int x;
  20.     protected int y;

  21.     /**
  22.      * Creates view frame of default size - (380 x 85)
  23.      */
  24.     public JFrameContextView() {
  25.         sized(380, 85);
  26.         located(0, 0); // origin by default
  27.     }

  28.     /**
  29.      * Builder-style way to set the preferred size for the frame
  30.      *
  31.      * @param width the width
  32.      * @param height height
  33.      * @return The JFrameContextView
  34.      */
  35.     public JFrameContextView sized(final int width, final int height) {
  36.         this.width = width;
  37.         this.height = height;
  38.         return this;
  39.     }

  40.     /**
  41.      * Builder-style way to set the preferred location for the frame
  42.      *
  43.      * @param x the x position on screen
  44.      * @param y the y position on screen
  45.      * @return The JFrameContextView
  46.      */
  47.     public JFrameContextView located(final int x, final int y) {
  48.         this.x = x;
  49.         this.y = y;
  50.         return this;
  51.     }

  52.     @Override
  53.     public synchronized void show(String story, String scenario, String step) {
  54.         if (frame == null) {
  55.             initialize();
  56.         }
  57.         label.setText(formatText(story, scenario, step));
  58.         try {
  59.             TimeUnit.MILLISECONDS.sleep(pauseInMillis());
  60.         } catch (InterruptedException e) {
  61.             // continue
  62.         }
  63.     }

  64.     protected String formatText(String story, String scenario, String step) {
  65.         return format(labelTemplate(), (story != null ? escapeHtml4(story) : ""),
  66.                 (scenario != null ? escapeHtml4(scenario) : ""), escapeHtml4(step));
  67.     }

  68.     protected String labelTemplate() {
  69.         return "<html><h3>{0}</h3><h4>{1}</h4><p>{2}</p></html>";
  70.     }

  71.     protected long pauseInMillis() {
  72.         return 250;
  73.     }

  74.     @Override
  75.     public synchronized void close() {
  76.         if (frame != null) {
  77.             frame.setVisible(false);
  78.             frame.dispose();
  79.             frame = null;
  80.             label = null;
  81.         }
  82.     }

  83.     protected void initialize() {
  84.         frame = new JFrame();
  85.         label = new JLabel();
  86.         frame.setAlwaysOnTop(true);
  87.         frame.setSize(width, height);
  88.         frame.setLocation(x, y);
  89.         frame.setUndecorated(true);
  90.         JPanel panel = new JPanel();
  91.         frame.setContentPane(panel);
  92.         panel.setLayout(new BorderLayout());
  93.         label.setBorder(new EmptyBorder(3, 3, 3, 3));
  94.         panel.add(label, BorderLayout.CENTER);

  95.         MouseInputAdapter mia = new MouseInputAdapter() {
  96.             private Point mousePressedScreenCoords;
  97.             private Point mousePressedCompCoords;

  98.             @Override
  99.             public void mouseReleased(MouseEvent e) {
  100.                 mousePressedScreenCoords = null;
  101.                 mousePressedCompCoords = null;
  102.             }

  103.             @Override
  104.             public void mousePressed(MouseEvent e) {
  105.                 mousePressedScreenCoords = e.getLocationOnScreen();
  106.                 mousePressedCompCoords = e.getPoint();
  107.             }

  108.             @Override
  109.             public void mouseDragged(MouseEvent e) {
  110.                 Point currCoords = e.getLocationOnScreen();
  111.                 x = mousePressedScreenCoords.x
  112.                         + (currCoords.x - mousePressedScreenCoords.x)
  113.                         - mousePressedCompCoords.x;
  114.                 y = mousePressedScreenCoords.y
  115.                         + (currCoords.y - mousePressedScreenCoords.y)
  116.                         - mousePressedCompCoords.y;
  117.                 frame.setLocation(x, y);
  118.             }
  119.         };

  120.         frame.addMouseListener(mia);
  121.         frame.addMouseMotionListener(mia);

  122.         frame.setVisible(true);
  123.     }

  124. }