/**
 * Represents the Panel that displays the Http request/response details table in the
 * Request/Response View section of the Diagnostic tab.
 */
public class RequestResponseDetailsPanel extends JPanel {
  private static final long serialVersionUID = 1L;

  @SuppressWarnings("unused")
  private static final Logger LOGGER =
      Logger.getLogger(RequestResponseDetailsPanel.class.getName());

  private static final ResourceBundle rb = ResourceBundleManager.getDefaultBundle();

  private JScrollPane scrollPane;
  private RequestResponseTableModel jRequestResponseTableModel = new RequestResponseTableModel();
  private DataTable<HttpRequestResponseInfo> jRequestResponseTable;
  private JPanel buttonsPanel;
  private JButton viewBtn;
  private JButton saveBtn;
  private ApplicationResourceOptimizer aro;

  /**
   * The default constructor that initializes a new instance of the RequestResponseDetailsPanel
   * class.
   *
   * @param aro ApplicationResourceOptimizer
   */
  public RequestResponseDetailsPanel(ApplicationResourceOptimizer aro) {
    // this.aro = aro;
    initialize();
  }

  /**
   * Marks the specified HttpRequestResponseInfo object in the request/response list as selected, by
   * highlighting it.
   *
   * @param rr - The Http request/response object to be marked as selected.
   */
  public void setHighlightedRequestResponse(HttpRequestResponseInfo rr) {
    getJRequestResponseTable().selectItem(rr);
  }

  /**
   * Sets the HttpRequestResponseInfo data to be displayed on this panel.
   *
   * @param data A collection of HttpRequestResponseInfo objects obtained from the trace analysis.
   *     Each HttpRequestResponseInfo object in the collection represents one row of data in the
   *     panel.
   */
  public void setData(Collection<HttpRequestResponseInfo> data) {
    jRequestResponseTableModel.setData(data);
  }

  /**
   * Convenience method to select the specified row in the table.
   *
   * @param rrInfo An HttpRequestResponseInfo object that indicates the specified row.
   */
  public void select(HttpRequestResponseInfo rrInfo) {
    jRequestResponseTable.selectItem(rrInfo);
  }

  /** Initializes the layout for the RequestResponse table. */
  private void initialize() {
    this.setLayout(new BorderLayout());
    this.add(getScrollPane(), BorderLayout.CENTER);
    this.add(getButtonsPanel(), BorderLayout.EAST);
  }

  /** Returns the ScrollPane that contains the RequestResponse table. */
  private JScrollPane getScrollPane() {
    if (scrollPane == null) {
      scrollPane = new JScrollPane(getJRequestResponseTable());
    }
    return scrollPane;
  }

  /**
   * Initializes and returns the the DataTable that contains Http request and response informations.
   */
  private DataTable<HttpRequestResponseInfo> getJRequestResponseTable() {
    if (jRequestResponseTable == null) {
      jRequestResponseTable = new DataTable<HttpRequestResponseInfo>(jRequestResponseTableModel);
      jRequestResponseTable.setAutoCreateRowSorter(true);
      jRequestResponseTable.setGridColor(Color.LIGHT_GRAY);
      jRequestResponseTable
          .getSelectionModel()
          .addListSelectionListener(
              new ListSelectionListener() {

                @Override
                public void valueChanged(ListSelectionEvent e) {
                  // Enable view and save as buttons appropriately
                  HttpRequestResponseInfo httpRRInfo = jRequestResponseTable.getSelectedItem();
                  boolean enabled =
                      httpRRInfo != null
                          && httpRRInfo.getContentLength() > 0
                          && httpRRInfo.getDirection() == Direction.RESPONSE
                          && httpRRInfo.getStatusCode() != 0;
                  getViewBtn().setEnabled(enabled);
                  getSaveBtn().setEnabled(enabled);
                }
              });
      jRequestResponseTable.addMouseListener(
          new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
              if (e.getClickCount() == 2 && getViewBtn().isEnabled()) {
                try {
                  viewContent(jRequestResponseTable.getSelectedItem());
                } catch (IOException ex) {
                  MessageDialogFactory.showUnexpectedExceptionDialog(
                      RequestResponseDetailsPanel.this.getTopLevelAncestor(), ex);
                }
              }
            }
          });
    }
    return jRequestResponseTable;
  }

  /**
   * Initializes and returns the JPanel that contains the View and Save As buttons for the
   * Request-Response Table.
   */
  private JPanel getButtonsPanel() {
    if (buttonsPanel == null) {
      buttonsPanel = new JPanel(new GridBagLayout());

      JPanel panel = new JPanel(new GridLayout(4, 1, 5, 5));
      panel.add(getViewBtn());
      panel.add(getSaveBtn());

      buttonsPanel.add(
          panel,
          new GridBagConstraints(
              0,
              0,
              1,
              1,
              1.0,
              1.0,
              GridBagConstraints.NORTH,
              GridBagConstraints.NONE,
              new Insets(5, 5, 5, 5),
              0,
              0));
    }
    return buttonsPanel;
  }

  /** Initializes and returns the "View" button for the Request-Response Table. */
  private JButton getViewBtn() {
    if (viewBtn == null) {
      viewBtn = new JButton(rb.getString("button.View"));
      viewBtn.setEnabled(false);
      viewBtn.addActionListener(
          new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
              try {
                viewContent(jRequestResponseTable.getSelectedItem());
              } catch (IOException e) {
                MessageDialogFactory.showUnexpectedExceptionDialog(
                    RequestResponseDetailsPanel.this.getTopLevelAncestor(), e);
              }
            }
          });
    }
    return viewBtn;
  }

  /** Initializes and returns the "Save AS" button for the Request-Response Table. */
  private JButton getSaveBtn() {
    if (saveBtn == null) {
      saveBtn = new JButton(rb.getString("button.Save"));
      saveBtn.setEnabled(false);
      saveBtn.addActionListener(
          new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
              ContentViewer.getInstance()
                  .saveContent(
                      RequestResponseDetailsPanel.this.getTopLevelAncestor(),
                      jRequestResponseTable.getSelectedItem());
            }
          });
    }
    return saveBtn;
  }

  /**
   * Creates a content viewer to view the response content.
   *
   * @param rrInfo
   * @throws IOException
   */
  private void viewContent(HttpRequestResponseInfo rrInfo) throws IOException {
    if (!rrInfo.getContentType().contains("video")) {
      if (rrInfo.getActualByteCount() < 5242880) {
        ContentViewer.getInstance().viewContent(rrInfo);
      } else {
        MessageDialogFactory.showErrorDialog(
            new Window(new Frame()), rb.getString("Error.fileSize"));
      }
    } else {
      MessageDialogFactory.showErrorDialog(
          new Window(new Frame()),
          MessageFormat.format(rb.getString("Error.videofile"), rrInfo.getContentType()));
    }
  }
}
Example #2
0
/** A factory class for displaying common message dialogs used by the ARO Data Analyzer. */
public class MessageDialogFactory extends JOptionPane {
  private static final long serialVersionUID = 1L;

  private static final ResourceBundle rb = ResourceBundleManager.getDefaultBundle();

  /**
   * Displays a dialog that is used for reporting unexpected exceptions to the user. The error
   * dialog is associated with the specified parent window, and contains the specified exception.
   * Unexpected exceptions can be I/O exceptions or other checked exceptions that can be handled
   * locally.
   *
   * @param parentComponent The parent window to associate with this dialog.
   * @param t The exception that should be thrown for this error.
   */
  public static void showUnexpectedExceptionDialog(Component parentComponent, Throwable t) {
    t.printStackTrace();
    String msg = t.getLocalizedMessage();
    if (msg != null && msg.length() > 200) {
      msg = rb.getString("Error.defaultMsg");
    }
    showMessageDialog(
        parentComponent,
        MessageFormat.format(rb.getString("Error.unexpected"), t.getClass().getName(), msg),
        rb.getString("Error.title"),
        ERROR_MESSAGE);
  }

  /**
   * Displays an error dialog for the specified invalid trace file name. The error dialog is
   * associated with the specified parent window, and contains the specified exception.
   *
   * @param strTraceDir The trace directory of the invalid trace file.
   * @param parentComponent The parent window to associate with this dialog.
   * @param t The exception that should be thrown for this error.
   */
  public static void showInvalidTraceDialog(
      String strTraceDir, Component parentComponent, Throwable t) {
    showMessageDialog(
        parentComponent,
        MessageFormat.format(
            rb.getString("Error.invalidTrace"), strTraceDir, t.getLocalizedMessage()),
        rb.getString("Error.title"),
        ERROR_MESSAGE);
  }

  /**
   * Displays an error dialog for the specified invalid directory. The error dialog is associated
   * with the specified parent window, and contains the specified exception.
   *
   * @param strTraceDir The invalid directory.
   * @param parentComponent The parent window to associate with this dialog.
   * @param t The exception that should be thrown for this error.
   */
  public static void showInvalidDirectoryDialog(
      String strTraceDir, Component parentComponent, Throwable t) {
    showMessageDialog(
        parentComponent,
        MessageFormat.format(
            rb.getString("Error.invalidDirecotry"), strTraceDir, t.getLocalizedMessage()),
        rb.getString("Error.title"),
        ERROR_MESSAGE);
  }

  /**
   * Displays an error dialog with the specified title. The error dialog is associated with the
   * specified parent window, and contains the specified message.
   *
   * @param window The parent window to associate with this dialog.
   * @param message The message to be displayed in the dialog.
   * @param title The dialog title.
   */
  public static void showErrorDialog(Window window, String message, String title) {
    showMessageDialog(window, message, title, ERROR_MESSAGE);
  }

  /**
   * Displays an error dialog using the default title. The error dialog is associated with the
   * specified parent window, and contains the specified message.
   *
   * @param window The parent window to associate with this dialog.
   * @param message The message to be displayed in the dialog.
   */
  public static void showErrorDialog(Window window, String message) {
    showMessageDialog(window, message, rb.getString("Error.title"), ERROR_MESSAGE);
  }

  /**
   * Displays a confirmation dialog using the default title. The confirmation dialog is associated
   * with the specified parent window, contains the specified message, and uses the specified
   * optionType.
   *
   * @param parentComponent The parent window to associate with this dialog.
   * @param message The message to be displayed in the dialog.
   * @param optionType An int that identifies the dialog option type.
   */
  public static int showConfirmDialog(Component parentComponent, String message, int optionType) {
    Object[] options = {rb.getString("jdialog.option.yes"), rb.getString("jdialog.option.no")};
    return JOptionPane.showOptionDialog(
        parentComponent,
        message,
        rb.getString("confirm.title"),
        optionType,
        JOptionPane.QUESTION_MESSAGE,
        null,
        options,
        options[0]);
  }

  /**
   * Displays a confirmation dialog for exporting data from a table. The dialog uses the default
   * title, and is associated with the specified parent window.
   *
   * @param parentComponent The parent window to associate with this dialog.
   */
  public static int showExportConfirmDialog(Component parentComponent) {
    Object[] options = {rb.getString("Button.open"), rb.getString("Button.ok")};
    return JOptionPane.showOptionDialog(
        parentComponent,
        rb.getString("table.export.success"),
        rb.getString("confirm.title"),
        JOptionPane.YES_OPTION,
        JOptionPane.OK_CANCEL_OPTION,
        null,
        options,
        options[1]);
  }
  /**
   * Display input dialog for user to enter password
   *
   * @param parent parent component
   * @param title what to show on the dialog box title
   * @param message what to show to user.
   * @return
   */
  public static String showInputPassword(Component parent, String title, String message) {
    return showInputPassword(parent, title, message, 14);
  }
  /**
   * Display input dialog for user to enter password
   *
   * @param parent parent component
   * @param title what to show on the dialog box title
   * @param message what to show to user.
   * @param fontsize how big should the font be
   * @return
   */
  public static String showInputPassword(
      Component parent, String title, String message, int fontsize) {
    JPanel panel = new JPanel();
    JLabel label = new JLabel(rb.getString("Message.entersudopassword") + "\r\n");
    label.setFont(new Font(label.getFont().getName(), Font.PLAIN, fontsize));
    JPasswordField pass = new JPasswordField(10);
    panel.add(label);
    panel.add(pass);
    String[] options = new String[] {"OK", "Cancel"};

    MessageDialogFactory.showOptionDialog(
        parent,
        panel,
        "Admin Right Required",
        JOptionPane.NO_OPTION,
        JOptionPane.PLAIN_MESSAGE,
        null,
        options,
        options[0]);
    char[] passarr = pass.getPassword();
    if (passarr == null) {
      return "";
    }
    return new String(passarr);
  }
}