Advertisement
Guest User

WidgetViewer.java

a guest
Jun 11th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.53 KB | None | 0 0
  1. package lab05;
  2.  
  3. import java.awt.event.ActionListener;
  4. import java.util.concurrent.locks.Condition;
  5. import java.util.concurrent.locks.Lock;
  6. import java.util.concurrent.locks.ReentrantLock;
  7.  
  8. import javax.swing.AbstractButton;
  9. import javax.swing.JComponent;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12. import javax.swing.JTextField;
  13. import javax.swing.JButton;
  14.  
  15. /**
  16.  * A really simple class to display Swing widgets in absolute Layout GUI.
  17.  * <p>
  18.  *
  19.  * @author parks
  20.  */
  21. public class WidgetViewer {
  22.  
  23.     /**
  24.      * This is the width of the JFrame if no value is specified on the constructor
  25.      */
  26.     public static final int DEFAULT_X_SIZE = 800;
  27.    
  28.     /**
  29.      * This is the height of the JFrame if no value is specified on the constructor
  30.      */
  31.     public static final int DEFAULT_Y_SIZE = 400;
  32.  
  33.     private JFrame jframe;
  34.     private JPanel anchor;
  35.     private boolean userClicked = false;
  36.  
  37.     private Lock lock;
  38.     private Condition waitingForUser;
  39.  
  40.     private JComponent userInputComponent = null;
  41.     private ActionListener eventHandler;
  42.    
  43.     // last-used positions.  Start with some reasonable assumptions
  44.     private int lastUsedX = 20;
  45.     private int lastUsedY = 20;
  46.     private int lastWidth = 200;
  47.     private int lastHeight = 20;
  48.  
  49.     /**
  50.      * Default constructor will display an empty JFrame that has an Absolute layout
  51.      * JPanel as its content pane and initialize the frame to a default size.
  52.      */
  53.     public WidgetViewer() {
  54.         this(DEFAULT_X_SIZE, DEFAULT_Y_SIZE);
  55.     }
  56.  
  57.     /**
  58.      * Constructor will display an empty JFrame that has an Absolute layout JPanel as its
  59.      * content pane and initialize the frame to a given size.
  60.      *
  61.      * @param pixelSizeInX The width, in pixels, of this WidgetViewer frame
  62.      * @param pixelSizeInY The height, in pixels, of this WidgetViewer frame
  63.      *
  64.      */
  65.     public WidgetViewer(int pixelSizeInX, int pixelSizeInY) {
  66.         lock = new ReentrantLock();
  67.         waitingForUser = lock.newCondition();
  68.         // lambda expression requires Java 8
  69.         eventHandler = e -> {
  70.             if (e.getSource() != userInputComponent) {
  71.                 return;
  72.             }
  73.             lock.lock();
  74.             userClicked = true;
  75.             waitingForUser.signalAll();
  76.             lock.unlock();
  77.             JComponent jbx = (JComponent) e.getSource();
  78.             anchor.remove(jbx);
  79.         };
  80.  
  81.         jframe = new JFrame();
  82.         anchor = new JPanel();
  83.         anchor.setLayout(null);
  84.         jframe.setContentPane(anchor);
  85.         jframe.setSize(pixelSizeInX, pixelSizeInY);
  86.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  87.         jframe.setVisible(true);
  88.     }
  89.  
  90.     /**
  91.      * Add a Swing widget to the GUI.
  92.      * GUI coordinates start at the top left corner of the frame, and
  93.      * are measured in pixels.
  94.      * <ul>
  95.      * <li>x increases horizontally to (to the right)
  96.      * <li>y increases vertically GOING DOWN.
  97.      * </ul>
  98.      *
  99.      * @param jcomp
  100.      *            Swing widget (subclasses of JComponent--like JLabel and
  101.      *            JTextField) to be added to the JFrame
  102.      *
  103.      * @param x the x value of this widget's top left corner
  104.      * @param y the y value of this widget's top left corner
  105.      * @param w the width, in pixels, of this widget
  106.      * @param h the height, in pixels, of this widget
  107.      */
  108.     public void add(JComponent jcomp, int x, int y, int w, int h) {
  109.         jcomp.setBounds(x, y, w, h);
  110.         anchor.add(jcomp);
  111.         jframe.setContentPane(anchor);
  112.         lastUsedX = x;
  113.         lastUsedY = y;
  114.         lastWidth = w;
  115.         lastHeight = h;
  116.     }
  117.     /**
  118.      * Add a widget to the frame.  We take a guess at the positioning and size
  119.      * based on the previously added widget.  If the guess turns out
  120.      * badly, switch to the other signature for the <em>add</em> method.
  121.      *
  122.      * @param jcomp widget to be added
  123.      */
  124.    
  125.     public void add(JComponent jcomp) {
  126.         lastUsedY += lastHeight;
  127.         add(jcomp, lastUsedX, lastUsedY, lastWidth, lastHeight);
  128.     }
  129.  
  130.     /**
  131.      * Add an Abstract Button (like a JButton) to the JFrame. Create an action
  132.      * listener to wait (suspend the caller) until it is clicked.
  133.      *
  134.      * @param absButton
  135.      *            Button (like a JButton) to add to the JFrame
  136.      */
  137.     public void addAndWait(AbstractButton absButton) {
  138.         userInputComponent = absButton;
  139.         absButton.addActionListener(eventHandler);
  140.  
  141.         addWait(absButton, absButton.getText().length() + 2);
  142.     }
  143.  
  144.     /**
  145.      * Convenience method to create a JButton with the given text and use it
  146.      * in an addAndWait.
  147.      *
  148.      * @param prompt Text for the JButton to display
  149.      */
  150.     public void addButtonAndWait(String prompt) {
  151.         JButton jb = new JButton(prompt);
  152.         addAndWait(jb);
  153.     }
  154.  
  155.     /**
  156.      * Add a JTextField to the JFrame, and wait for the user to put the cursor in
  157.      * the field and click Enter. The caller is suspended until enter is clicked.
  158.      *
  159.      * @param jTextField
  160.      *            Field to add to the JFrame
  161.      */
  162.     public void addAndWait(JTextField jTextField) {
  163.         userInputComponent = jTextField;
  164.         jTextField.addActionListener(eventHandler);
  165.  
  166.         addWait(jTextField, jTextField.getText().length() + 2);
  167.     }
  168.  
  169.     @SuppressWarnings("unused")
  170.     private void addWait(JComponent jcomp) {
  171.         addWait(jcomp, 5);
  172.     }
  173.  
  174.     private void addWait(JComponent jcomp, int charWidth) {
  175.         int guessAtWidth = Math.min(charWidth * 10, jframe.getWidth());
  176.         add(jcomp, 0, 10, guessAtWidth, 20);
  177.         lock.lock();
  178.         try {
  179.             while (!userClicked) {
  180.                 waitingForUser.await();
  181.             }
  182.             // we need this to make the just clicked widget disappear in some circumstances
  183.             jframe.setContentPane(anchor);
  184.         } catch (InterruptedException e1) {
  185.             System.err.println("WidgetViewer reports that something really bad just happened");
  186.             e1.printStackTrace();
  187.             System.exit(0);
  188.         }
  189.         userClicked = false;
  190.         waitingForUser.signalAll();
  191.         lock.unlock();
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement