avatar

937

Add a 'Submit Log' button to the log viewer which sends me the log file via S3, and makes a note at my.jbidwatcher.com that it's been uploaded. by mrs, 30 May, 2009 10:51 PM
Diff this changeset:
OptionUI.java
mrs      1   package com.jbidwatcher.ui.util;
cyberfox 2   /*
cyberfox 3    * Copyright (c) 2000-2007, CyberFOX Software, Inc. All Rights Reserved.
cyberfox 4    *
cyberfox 5    * Developed by mrs (Morgan Schweers)
cyberfox 6    */
cyberfox 7   
mrs      8   import com.jbidwatcher.util.config.JConfig;
cyberfox 9   
cyberfox 10  import java.awt.event.*;
cyberfox 11  import java.awt.*;
cyberfox 12  import javax.swing.*;
cyberfox 13  import javax.swing.text.JTextComponent;
cyberfox 14  import java.util.List;
cyberfox 15  
cyberfox 16  public class OptionUI {
cyberfox 17    /**
cyberfox 18     * @brief Add the ability to past into a text field using a few different methods.
cyberfox 19     *
cyberfox 20     * @param jtc - The text field to be able to past into.
cyberfox 21     * @param preFill - The initial text of the field.
cyberfox 22     */
cyberfox 23    private void addPasting(JTextField jtc, String preFill) {
cyberfox 24      jtc.addMouseListener(JPasteListener.getInstance());
cyberfox 25      jtc.setText(preFill);
cyberfox 26  
cyberfox 27      ActionListener doDefault = new ActionListener() {
cyberfox 28          public void actionPerformed(ActionEvent ae) {
cyberfox 29            if(ae.getActionCommand().equals("Escape")) {
cyberfox 30              if(ae.getSource() instanceof JTextField) {
cyberfox 31                JTextField clearMe = (JTextField)ae.getSource();
cyberfox 32                clearMe.setText("");
cyberfox 33              }
cyberfox 34            }
cyberfox 35            JComponent source = (JComponent)ae.getSource();
cyberfox 36            JButton defButton = source.getRootPane().getDefaultButton();
cyberfox 37  
cyberfox 38            if(defButton != null) {
cyberfox 39              defButton.doClick();
cyberfox 40            }
cyberfox 41          }
cyberfox 42        };
cyberfox 43  
cyberfox 44      jtc.addActionListener(doDefault);
cyberfox 45      jtc.registerKeyboardAction(doDefault, "Escape", KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
cyberfox 46                                 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
cyberfox 47    }
cyberfox 48  
cyberfox 49    private boolean handleDialogResult(JOptionPane jopPrompt, String endResult) {
cyberfox 50      Integer buttonChoiceObject;
cyberfox 51      Object result;
cyberfox 52      int whatButton;
cyberfox 53  
cyberfox 54      result = jopPrompt.getValue();
cyberfox 55  
cyberfox 56      if(endResult == null || endResult.equals("") ||
cyberfox 57         result == null || result.equals(JOptionPane.UNINITIALIZED_VALUE)) {
cyberfox 58        buttonChoiceObject = JOptionPane.CANCEL_OPTION;
cyberfox 59      } else {
cyberfox 60        if(result.getClass() == String.class && result.equals("")) {
cyberfox 61          buttonChoiceObject = JOptionPane.OK_OPTION;
cyberfox 62        } else {
cyberfox 63          buttonChoiceObject = new Integer(result.toString());
cyberfox 64        }
cyberfox 65      }
cyberfox 66  
cyberfox 67      boolean is_cancelled = false;
cyberfox 68      //  Did they click cancel?
cyberfox 69      whatButton = buttonChoiceObject;
cyberfox 70      if(whatButton == JOptionPane.CANCEL_OPTION) {
cyberfox 71        is_cancelled = true;
cyberfox 72      }
cyberfox 73      return(is_cancelled);
cyberfox 74    }
cyberfox 75  
cyberfox 76    public String promptString(Component parent, String prePrompt, String preTitle) {
cyberfox 77      return(promptString(parent, prePrompt, preTitle, ""));
cyberfox 78    }
cyberfox 79  
cyberfox 80    public String promptString(Component parent, String prePrompt, String preTitle, String preFill) {
cyberfox 81      String[] result = promptString(parent, prePrompt, preTitle, preFill, null, null);
cyberfox 82  
cyberfox 83      if(result == null) return null;
cyberfox 84  
cyberfox 85      return result[0];
cyberfox 86    }
cyberfox 87  
cyberfox 88    public String[] promptString(Component parent, String prePrompt, String preTitle, String preFill, String postPrompt, String postFill) {
mrs      89      final Object[] myComponents;
cyberfox 90  
cyberfox 91      if(postPrompt != null) {
cyberfox 92        myComponents = new Object[4];
cyberfox 93        myComponents[2] = "Quantity";
cyberfox 94        myComponents[3] = new JTextField();
cyberfox 95      } else {
cyberfox 96        myComponents = new Object[2];
cyberfox 97      }
cyberfox 98      myComponents[0] = prePrompt;
cyberfox 99      myComponents[1] = new JTextField();
cyberfox 100 
mrs      101     JOptionPane jopPrompt = new JOptionPane(myComponents, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
cyberfox 102 
cyberfox 103     addPasting((JTextField)myComponents[1], preFill);
cyberfox 104 
cyberfox 105     if(postPrompt != null) {
cyberfox 106       addPasting((JTextField)myComponents[3], postFill);
cyberfox 107     }
cyberfox 108 
mrs      109     JDialog jdInput = jopPrompt.createDialog(parent, preTitle);
mrs      110     jdInput.addComponentListener(new ComponentAdapter() {
mrs      111       public void componentShown(ComponentEvent event) {
mrs      112         super.componentShown(event);
mrs      113         ((JTextField) myComponents[1]).requestFocus();
mrs      114       }
mrs      115     });
cyberfox 116     jdInput.addWindowListener(new WindowAdapter() {
mrs      117         public void windowActivated(WindowEvent ev) {
mrs      118           super.windowActivated(ev);
mrs      119           ((JTextField) myComponents[1]).requestFocus();
mrs      120         }
cyberfox 121         public void windowDeactivated(WindowEvent ev) {
mrs      122           super.windowDeactivated(ev);
cyberfox 123           ev.getWindow().toFront();
cyberfox 124         }
cyberfox 125       });
cyberfox 126     Rectangle rec = OptionUI.findCenterBounds(jdInput.getPreferredSize());
cyberfox 127     jdInput.setLocation(rec.x, rec.y);
cyberfox 128 
mrs      129     ((JTextField) myComponents[1]).requestFocus();
mrs      130     jdInput.setVisible(true);
mrs      131 
cyberfox 132     //    endResult = (String)jopPrompt.getInputValue();
mrs      133     String endResult = ((JTextComponent) myComponents[1]).getText();
cyberfox 134 
cyberfox 135     boolean is_cancelled = handleDialogResult(jopPrompt, endResult);
cyberfox 136     if(is_cancelled) return null;
cyberfox 137 
mrs      138     String[] results = new String[2];
cyberfox 139     results[0] = endResult;
cyberfox 140     if(postPrompt != null) {
cyberfox 141       results[1] = ((JTextComponent) myComponents[3]).getText();
cyberfox 142     }
cyberfox 143     return results;
cyberfox 144   }
cyberfox 145 
cyberfox 146   /**
cyberfox 147    * @brief Get a basic editor pane for text/html, that listens for
cyberfox 148    * hyperlinks properly, and chains to the Hyperactive module.
cyberfox 149    *
cyberfox 150    * @param sb - The StringBuffer to fill in as the text.
cyberfox 151    * @param inSize - The preferred size for the editor.
cyberfox 152    * @param fixed - Whether it's fixed in position or not.
mrs      153    * @param html - True if this is HTML data.
cyberfox 154    *
cyberfox 155    * @return - A JBEditorPane to be embedded in a frame.
cyberfox 156    */
mrs      157   public JBEditorPane getBasicEditor(StringBuffer sb, Dimension inSize, boolean fixed, boolean html) {
cyberfox 158     JBEditorPane jep;
cyberfox 159 
mrs      160     jep = new JBEditorPane(html ? "text/html" : "text/plain", sb.toString());
cyberfox 161     jep.setEditable(false);
mrs      162     jep.addHyperlinkListener(new Hyperactive(jep));
cyberfox 163 
cyberfox 164     if(fixed) {
cyberfox 165       jep.setPreferredSize(inSize);
cyberfox 166       jep.setMaximumSize(inSize);
cyberfox 167       jep.setMinimumSize(inSize);
cyberfox 168     }
cyberfox 169 
cyberfox 170     return jep;
cyberfox 171   }
cyberfox 172 
cyberfox 173   /**
mrs      174    * @brief Get a basic editor pane for text/html, that listens for
mrs      175    * hyperlinks properly, and chains to the Hyperactive module.
mrs      176    *
mrs      177    * @param s - The String to fill in as the text.
mrs      178    *
mrs      179    * @return - A JBEditorPane to be embedded in a frame.
mrs      180    */
mrs      181   public static JBEditorPane getHTMLLabel(String s) {
mrs      182     JBEditorPane jep;
mrs      183 
mrs      184     jep = new JBEditorPane("text/html", s);
mrs      185     jep.setEditable(false);
mrs      186     jep.setOpaque(false);
mrs      187     jep.addHyperlinkListener(new Hyperactive(jep));
mrs      188 
mrs      189     return jep;
mrs      190   }
mrs      191 
mrs      192   /**
cyberfox 193    * @brief Get the upper left point of a box which would be centered
cyberfox 194    * exactly, given the provided dimensions.
cyberfox 195    *
cyberfox 196    * @param inSize - The dimensions of the rectangle to place.
cyberfox 197    *
cyberfox 198    * @return - The upper left corner to place a window at.
cyberfox 199    */
cyberfox 200   public Point getCenter(Dimension inSize) {
cyberfox 201     Rectangle centerBounds = findCenterBounds(inSize);
cyberfox 202     Point screenCenter;
cyberfox 203     screenCenter = new Point( (centerBounds.width / 2) + centerBounds.x, (centerBounds.height / 2)+centerBounds.y);
cyberfox 204     screenCenter.x -= inSize.width/2;
cyberfox 205     screenCenter.y -= inSize.height/2;
cyberfox 206     return screenCenter;
cyberfox 207   }
cyberfox 208 
cyberfox 209   /**
cyberfox 210    * Helps client code place components on the center of the screen.  It
cyberfox 211    * handles multiple monitor configuration correctly
cyberfox 212    *
cyberfox 213    * @param componentSize the size of the component
cyberfox 214    * @return bounds of the centered component
cyberfox 215    * @since 2.5
cyberfox 216    */
cyberfox 217   public static Rectangle findCenterBounds(Dimension componentSize) {
cyberfox 218     return findCenterBounds(JMouseAdapter.getCurrentGraphicsConfiguration(), componentSize);
cyberfox 219   }
cyberfox 220 
cyberfox 221   /**
cyberfox 222    * Helps client code place components on the center of the screen.  It
cyberfox 223    * handles multiple monitor configuration correctly
cyberfox 224    *
cyberfox 225    * @param gconf         the GraphicsConfiguration of the monitor
cyberfox 226    * @param componentSize the size of the component
cyberfox 227    * @return bounds of the centered component
cyberfox 228    */
cyberfox 229   private static Rectangle findCenterBounds(GraphicsConfiguration gconf, Dimension componentSize) {
cyberfox 230     if (gconf == null) {
cyberfox 231       gconf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
cyberfox 232     }
cyberfox 233 
cyberfox 234     Rectangle bounds = gconf.getBounds();
cyberfox 235 
cyberfox 236     return new Rectangle(
cyberfox 237         bounds.x + ((bounds.width - componentSize.width) / 2),
cyberfox 238         bounds.y + ((bounds.height - componentSize.height) / 2), componentSize.width, componentSize.height
cyberfox 239     );
cyberfox 240   }
cyberfox 241 
mrs      242   public JFrame showTextDisplay(StringBuffer inSB, Dimension inSize, String frameName) {
mrs      243     return showTextDisplay(inSB, inSize, frameName, true);
mrs      244   }
mrs      245 
cyberfox 246   /**
cyberfox 247    * @brief Show a big HTML-formatted text display.
cyberfox 248    *
cyberfox 249    * @param inSB - The data to show.
cyberfox 250    * @param inSize - The size to show it at.
cyberfox 251    * @param frameName - The name of the frame to show.
mrs      252    * @param isHTML - Is the StringBuffer
cyberfox 253    *
cyberfox 254    * @return - The JFrame of the display.
cyberfox 255    */
mrs      256   public JFrame showTextDisplay(StringBuffer inSB, Dimension inSize, String frameName, boolean isHTML) {
mrs      257     JFrame otherFrame = getTextDisplay(inSB, inSize, frameName, isHTML);
mrs      258     otherFrame.pack();
mrs      259     otherFrame.setSize(inSize.width, inSize.height);
mrs      260     otherFrame.setVisible(true);
mrs      261 
mrs      262     return otherFrame;
mrs      263   }
mrs      264 
mrs      265   public JFrame getTextDisplay(StringBuffer inSB, Dimension inSize, String frameName, boolean isHTML) {
cyberfox 266     JFrame otherFrame;
mrs      267     JBEditorPane jep;
cyberfox 268     JScrollPane jsp;
cyberfox 269 
mrs      270     jep = getBasicEditor(inSB, inSize, false, isHTML);
cyberfox 271 
mrs      272     otherFrame = new JBidFrame(frameName);
cyberfox 273 
cyberfox 274     jsp = new JScrollPane(jep);
cyberfox 275     jsp.getVerticalScrollBar().setValue(0);
cyberfox 276     otherFrame.getContentPane().add(jsp);
cyberfox 277     jep.setCaretPosition(0);
cyberfox 278     otherFrame.setLocation(getCenter(inSize));
cyberfox 279     return otherFrame;
cyberfox 280   }
cyberfox 281 
cyberfox 282   public JFrame showTextDisplayWithButtons(StringBuffer inSB, Dimension inSize, String frameName, final String config, final String buttonText1, final String value1, final String buttonText2, final String value2) {
mrs      283     final JBEditorPane jep = getBasicEditor(inSB, inSize, false, true);
mrs      284     final JFrame otherFrame = new JBidFrame(frameName);
cyberfox 285     final JScrollPane jsp;
cyberfox 286 
cyberfox 287     otherFrame.getContentPane().setLayout(new BorderLayout());
cyberfox 288     JPanel buttonPanel = new JPanel(new BorderLayout());
cyberfox 289     JButton button1 = new JButton(buttonText1);
cyberfox 290     button1.addActionListener(new ActionListener() {
cyberfox 291       public void actionPerformed(ActionEvent e) {
cyberfox 292         if(e.getActionCommand().equals(buttonText1)) {
cyberfox 293           JConfig.setConfiguration(config, value1);
cyberfox 294           otherFrame.setVisible(false);
cyberfox 295         }
cyberfox 296       }
cyberfox 297     });
cyberfox 298 
cyberfox 299     JButton button2 = new JButton(buttonText2);
cyberfox 300     button2.addActionListener(new ActionListener() {
cyberfox 301       public void actionPerformed(ActionEvent e) {
cyberfox 302         if(e.getActionCommand().equals(buttonText2)) {
cyberfox 303           JConfig.setConfiguration(config, value2);
cyberfox 304           otherFrame.setVisible(false);
cyberfox 305         }
cyberfox 306       }
cyberfox 307     });
cyberfox 308 
cyberfox 309     buttonPanel.add(button1, BorderLayout.WEST);
cyberfox 310     buttonPanel.add(button2, BorderLayout.EAST);
cyberfox 311     otherFrame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
cyberfox 312 
cyberfox 313     jsp = new JScrollPane(jep);
cyberfox 314     jsp.getVerticalScrollBar().setValue(0);
cyberfox 315     otherFrame.getContentPane().add(jsp, BorderLayout.CENTER);
cyberfox 316     jep.setCaretPosition(0);
cyberfox 317     otherFrame.setLocation(getCenter(inSize));
cyberfox 318     otherFrame.pack();
cyberfox 319     otherFrame.setSize(inSize.width, inSize.height);
cyberfox 320     otherFrame.setVisible(true);
cyberfox 321 
cyberfox 322     return otherFrame;
cyberfox 323   }
cyberfox 324 
cyberfox 325   /**
cyberfox 326    * @brief Show a large HTML-formatted text display, with buttons
cyberfox 327    * below, to select what to do.
cyberfox 328    *
cyberfox 329    * @param inSB - The data to show in the buffer.
cyberfox 330    * @param inSize - The size to show it at.
cyberfox 331    * @param frameName - The name of the frame to show.
cyberfox 332    * @param choices - The array of choices to show.
cyberfox 333    * @param borderTitle - The title to surround the panel with.
cyberfox 334    * @param al - Who to notify that a choice was made.
cyberfox 335    *
cyberfox 336    * @return - The JFrame of the display.
cyberfox 337    */
cyberfox 338   public JFrame showChoiceTextDisplay(StringBuffer inSB, Dimension inSize, String frameName, List<String> choices, String borderTitle, ActionListener al) {
mrs      339     JBEditorPane jep = getBasicEditor(inSB, inSize, true, true);
cyberfox 340 
mrs      341     JFrame otherFrame = new JBidFrame(frameName);
cyberfox 342 
cyberfox 343     JPanel insidePanel = new JPanel(new BorderLayout());
cyberfox 344 
cyberfox 345     JScrollPane jsp = new JScrollPane(jep);
cyberfox 346     jsp.getVerticalScrollBar().setValue(0);
mrs      347     otherFrame.setPreferredSize(inSize);
mrs      348     otherFrame.setMaximumSize(inSize);
mrs      349     otherFrame.setMinimumSize(inSize);
cyberfox 350     otherFrame.getContentPane().add(insidePanel);
cyberfox 351     insidePanel.add(jsp, BorderLayout.CENTER);
cyberfox 352     insidePanel.setBorder(BorderFactory.createTitledBorder(borderTitle));
cyberfox 353     JPanel bottomPanel = new JPanel();
cyberfox 354     bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
cyberfox 355 
cyberfox 356     boolean isFirst = true;
cyberfox 357     JButton firstButton = null;
cyberfox 358     for (String info : choices) {
cyberfox 359       if (info.startsWith("CHECK")) {
cyberfox 360         JCheckBox tmpCheck = new JCheckBox(info.substring(6));
cyberfox 361         tmpCheck.addActionListener(al);
cyberfox 362         bottomPanel.add(tmpCheck);
cyberfox 363       } else if (info.startsWith("TEXT")) {
cyberfox 364         JLabel tmpLabel = new JLabel(info.substring(5));
cyberfox 365         bottomPanel.add(tmpLabel);
cyberfox 366       } else {
cyberfox 367         JButton step_button = new JButton(info);
cyberfox 368         step_button.addActionListener(al);
cyberfox 369         bottomPanel.add(step_button);
cyberfox 370         if (isFirst) {
cyberfox 371           isFirst = false;
cyberfox 372           firstButton = step_button;
cyberfox 373         }
cyberfox 374       }
cyberfox 375     }
cyberfox 376     insidePanel.add(bottomPanel, BorderLayout.SOUTH);
cyberfox 377     jep.setCaretPosition(0);
cyberfox 378     otherFrame.setLocation(getCenter(inSize));
cyberfox 379     otherFrame.pack();
cyberfox 380     otherFrame.setVisible(true);
cyberfox 381 
mrs      382     if(firstButton != null) firstButton.requestFocusInWindow();
cyberfox 383 
cyberfox 384     return otherFrame;
cyberfox 385   }
cyberfox 386 
cyberfox 387   public int promptWithCheckbox(Component parent, String message, String title, String tf_config, int optionType, int defaultOption) {
cyberfox 388     return promptWithCheckbox(parent, message, title, tf_config, null, optionType, defaultOption);
cyberfox 389   }
cyberfox 390 
cyberfox 391   public int promptWithCheckbox(Component parent, String message, String title, String tf_config) {
cyberfox 392     return promptWithCheckbox(parent, message, title, tf_config, null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.OK_OPTION);
cyberfox 393   }
cyberfox 394 
cyberfox 395   /**
cyberfox 396    * This function puts up a message, allows buttons to be clicked, and a
cyberfox 397    * checkbox to be set (most often for "don't ever show me this again."),
cyberfox 398    * and it will set the configuration appropriately.
cyberfox 399    *
cyberfox 400    * @param parent - The higher level component to become a child of; this is to attempt to handle modal blocking dialog issues.
cyberfox 401    * @param message - The message to display.
cyberfox 402    * @param title - The window title.
cyberfox 403    * @param tf_config - The configuration value that will be set to true if
cyberfox 404    *                    the checkbox is selected, or false otherwise.
cyberfox 405    * @param val_config - the configuration value that will be set to the
cyberfox 406    *                     result of their choice (OK or Cancel, for
cyberfox 407    *                     instance).
cyberfox 408    * @param optionType - The prompt type of the dialog box, for instance JOptionPane.OK_CANCEL_OPTION.
cyberfox 409    * @param defaultOption - The default option to be returned if the button
cyberfox 410    *                        to not show this dialog was clicked.  This only
cyberfox 411    *                        applies if val_config is null.
cyberfox 412    *
cyberfox 413    * @return - The button value selected, usually of
cyberfox 414    *           JOptionPane.CANCEL_OPTION or OK_OPTION, or the val_config
cyberfox 415    *           stored value, or the defaultOption.
cyberfox 416    */
cyberfox 417   public int promptWithCheckbox(Component parent, String message, String title, String tf_config, String val_config, int optionType, int defaultOption) {
cyberfox 418     Integer buttonChoiceObject;
cyberfox 419     Object[] myComponents;
cyberfox 420     JCheckBox dontShowBox;
cyberfox 421     JOptionPane jopPrompt;
cyberfox 422     JDialog jdInput;
cyberfox 423     Object result;
cyberfox 424 
cyberfox 425     //  If we were marked in the past as 'don't show this box'...
cyberfox 426     if(JConfig.queryConfiguration(tf_config, "false").equals("true")) {
cyberfox 427       if(val_config != null) {
cyberfox 428         String cfg_val = JConfig.queryConfiguration(val_config, null);
cyberfox 429 
cyberfox 430         if(cfg_val != null) {
cyberfox 431           return Integer.parseInt(cfg_val);
cyberfox 432         }
cyberfox 433       }
cyberfox 434       return defaultOption;
cyberfox 435     }
cyberfox 436 
cyberfox 437     dontShowBox = new JCheckBox("Don't show this dialog again.");
cyberfox 438 
cyberfox 439     myComponents = new Object[2];
cyberfox 440     myComponents[0] = message;
cyberfox 441     myComponents[1] = dontShowBox;
cyberfox 442 
cyberfox 443     jopPrompt = new JOptionPane(myComponents, JOptionPane.QUESTION_MESSAGE, optionType);
cyberfox 444 
cyberfox 445     jdInput = jopPrompt.createDialog(parent, title);
cyberfox 446     jdInput.addWindowListener(new WindowAdapter() {
cyberfox 447         public void windowDeactivated(WindowEvent ev) {
cyberfox 448           ev.getWindow().toFront();
cyberfox 449         }
cyberfox 450       });
cyberfox 451     jdInput.setVisible(true);
cyberfox 452 
cyberfox 453     result = jopPrompt.getValue();
cyberfox 454 
cyberfox 455     if(result == null || result.toString().equals("") || result.equals(JOptionPane.UNINITIALIZED_VALUE)) {
cyberfox 456       buttonChoiceObject = JOptionPane.CANCEL_OPTION;
cyberfox 457     } else {
cyberfox 458       buttonChoiceObject = new Integer(result.toString());
cyberfox 459     }
cyberfox 460 
cyberfox 461     if(tf_config != null) {
cyberfox 462       JConfig.setConfiguration(tf_config, dontShowBox.isSelected() ? "true" : "false");
cyberfox 463     }
cyberfox 464     if(val_config != null) {
cyberfox 465       JConfig.setConfiguration(val_config, buttonChoiceObject.toString());
cyberfox 466     }
cyberfox 467 
cyberfox 468     return buttonChoiceObject;
cyberfox 469   }
cyberfox 470 }

Check out the code: svn co jbidwatcher/trunk/src/com/jbidwatcher/ui/util/OptionUI.java