/** Initializes this dialog. */
  private void initDialog() {
    setMinimumSize(new Dimension(640, 480));

    final JComponent settingsPane = createSettingsPane();
    final JComponent previewPane = createPreviewPane();

    final JPanel contentPane = new JPanel(new GridBagLayout());
    contentPane.add(
        settingsPane,
        new GridBagConstraints(
            0,
            0,
            1,
            1,
            0.0,
            0.0,
            GridBagConstraints.NORTH,
            GridBagConstraints.NONE,
            new Insets(2, 0, 2, 0),
            0,
            0));
    contentPane.add(
        previewPane,
        new GridBagConstraints(
            1,
            0,
            1,
            1,
            1.0,
            1.0,
            GridBagConstraints.NORTH,
            GridBagConstraints.BOTH,
            new Insets(2, 0, 2, 0),
            0,
            0));

    final JButton runAnalysisButton = ToolUtils.createRunAnalysisButton(this);
    this.runAnalysisAction = (RestorableAction) runAnalysisButton.getAction();

    final JButton exportButton = ToolUtils.createExportButton(this);
    this.exportAction = exportButton.getAction();
    this.exportAction.setEnabled(false);

    final JButton closeButton = ToolUtils.createCloseButton();
    this.closeAction = closeButton.getAction();

    final JComponent buttons =
        SwingComponentUtils.createButtonPane(runAnalysisButton, exportButton, closeButton);

    SwingComponentUtils.setupWindowContentPane(this, contentPane, buttons, runAnalysisButton);
  }
  /** @return */
  private JPanel createPreviewPane() {
    final JPanel panTable = new JPanel(new GridLayout(1, 1, 0, 0));

    this.outText = new JEditorPane("text/html", getEmptyHtmlPage());
    this.outText.setEditable(false);

    panTable.add(new JScrollPane(this.outText));

    return panTable;
  }
  /**
   * Sets up the given dialog's content pane by setting its border to provide a good default spacer,
   * and setting the content pane to the given components.
   *
   * @param aDialog the dialog to setup, cannot be <code>null</code>;
   * @param aCenterComponent the component that should appear at the center of the dialog;
   * @param aButtonPane the component that should appear at the bottom of the dialog
   * @param defaultButton the default button for this dialog; can be null for "none".
   * @see javax.swing.JRootPane#setDefaultButton(javax.swing.JButton)
   */
  public static void setupDialogContentPane(
      final JDialog aDialog,
      final Component aCenterComponent,
      final Component aButtonPane,
      final JButton defaultButton) {
    final JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setBorder(
        BorderFactory.createEmptyBorder(
            DIALOG_PADDING,
            DIALOG_PADDING, //
            DIALOG_PADDING,
            DIALOG_PADDING));

    contentPane.add(aCenterComponent, BorderLayout.CENTER);
    contentPane.add(aButtonPane, BorderLayout.PAGE_END);

    aDialog.setContentPane(contentPane);
    aDialog.getRootPane().setDefaultButton(defaultButton);
    aDialog.pack();
  }
  /**
   * Creates a button pane in which the given buttons are neatly aligned with proper spacings.
   *
   * @param aButtons the buttons to add to the created button pane, will be added in the given
   *     order.
   * @return the button pane, never <code>null</code>.
   */
  public static JComponent createButtonPane(final JButton... aButtons) {
    if ((aButtons == null) || (aButtons.length < 1)) {
      throw new IllegalArgumentException("Need at least one button!");
    }

    final JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.setBorder(
        BorderFactory.createEmptyBorder(
            BUTTONS_PADDING_TOP,
            BUTTONS_PADDING_LEFT,
            BUTTONS_PADDING_BOTTOM,
            BUTTONS_PADDING_RIGHT));

    buttonPane.add(Box.createHorizontalGlue());

    // we want equally sized buttons, so we are recording preferred sizes while
    // adding the buttons and set them to the maximum afterwards...
    int width = 1;
    int height = 1;

    for (JButton button : aButtons) {
      width = Math.max(width, button.getPreferredSize().width);
      height = Math.max(height, button.getPreferredSize().height);

      buttonPane.add(Box.createHorizontalStrut(BUTTONS_SPACING_DEFAULT));
      buttonPane.add(button);
    }

    buttonPane.add(Box.createHorizontalStrut(BUTTONS_SPACING_DEFAULT));

    final Dimension newDims = new Dimension(width, height);

    // everything added; let's set all sizes
    for (final JButton button : aButtons) {
      button.setPreferredSize(newDims);
    }

    return buttonPane;
  }
  /** @return */
  private JPanel createSettingsPane() {
    final int channelCount = getData().getChannels();

    final Integer[] baudrates = new Integer[AsyncSerialDataDecoder.COMMON_BAUDRATES.length];
    for (int i = 0; i < baudrates.length; i++) {
      baudrates[i] = Integer.valueOf(AsyncSerialDataDecoder.COMMON_BAUDRATES[i]);
    }
    final String[] bitarray = new String[10];
    // allow symbol lengths between 5 and 14 bits...
    for (int i = 0; i < bitarray.length; i++) {
      bitarray[i] = String.format("%d", Integer.valueOf(i + 5));
    }

    final JPanel settings = new JPanel(new SpringLayout());

    SpringLayoutUtils.addSeparator(settings, "Settings");

    settings.add(createRightAlignedLabel("RxD"));
    this.rxd = SwingComponentUtils.createOptionalChannelSelector(channelCount);
    settings.add(this.rxd);

    settings.add(createRightAlignedLabel("TxD"));
    this.txd = SwingComponentUtils.createOptionalChannelSelector(channelCount);
    settings.add(this.txd);

    settings.add(createRightAlignedLabel("CTS"));
    this.cts = SwingComponentUtils.createOptionalChannelSelector(channelCount);
    settings.add(this.cts);

    settings.add(createRightAlignedLabel("RTS"));
    this.rts = SwingComponentUtils.createOptionalChannelSelector(channelCount);
    settings.add(this.rts);

    settings.add(createRightAlignedLabel("DTR"));
    this.dtr = SwingComponentUtils.createOptionalChannelSelector(channelCount);
    settings.add(this.dtr);

    settings.add(createRightAlignedLabel("DSR"));
    this.dsr = SwingComponentUtils.createOptionalChannelSelector(channelCount);
    settings.add(this.dsr);

    settings.add(createRightAlignedLabel("DCD"));
    this.dcd = SwingComponentUtils.createOptionalChannelSelector(channelCount);
    settings.add(this.dcd);

    settings.add(createRightAlignedLabel("RI"));
    this.ri = SwingComponentUtils.createOptionalChannelSelector(channelCount);
    settings.add(this.ri);

    settings.add(createRightAlignedLabel("Baudrate"));
    this.autoDetectBaudRate = new JCheckBox("Auto detect");
    settings.add(this.autoDetectBaudRate);

    settings.add(new JLabel(""));
    this.baudrate = new JComboBox(baudrates);
    // Issue #90: allow custom baudrates to be specified...
    this.baudrate.setEditable(true);
    this.baudrate.setSelectedIndex(0);
    settings.add(this.baudrate);

    this.autoDetectBaudRate.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(final ItemEvent aEvent) {
            final JCheckBox cb = (JCheckBox) aEvent.getSource();
            UARTProtocolAnalysisDialog.this.baudrate.setEnabled(!cb.isSelected());
          }
        });

    settings.add(createRightAlignedLabel("Parity"));
    this.parity = new JComboBox(Parity.values());
    this.parity.setSelectedIndex(0);
    this.parity.setRenderer(new UARTParityItemRenderer());
    settings.add(this.parity);

    settings.add(createRightAlignedLabel("Bits"));
    this.bits = new JComboBox(bitarray);
    this.bits.setSelectedIndex(3);
    settings.add(this.bits);

    settings.add(createRightAlignedLabel("Stopbits"));
    this.stop = new JComboBox(StopBits.values());
    this.stop.setSelectedIndex(0);
    this.stop.setRenderer(new UARTStopBitsItemRenderer());
    settings.add(this.stop);

    settings.add(createRightAlignedLabel("Idle level"));
    this.idleLevel = new JComboBox(BitLevel.values());
    this.idleLevel.setSelectedIndex(0);
    this.idleLevel.setRenderer(new UARTIdleLevelItemRenderer());
    settings.add(this.idleLevel);

    settings.add(createRightAlignedLabel("Bit encoding"));
    this.bitEncoding = new JComboBox(BitEncoding.values());
    this.bitEncoding.setSelectedIndex(0);
    this.bitEncoding.setRenderer(new UARTBitEncodingItemRenderer());
    settings.add(this.bitEncoding);

    settings.add(createRightAlignedLabel("Bit order"));
    this.bitOrder = new JComboBox(BitOrder.values());
    this.bitOrder.setSelectedIndex(0);
    this.bitOrder.setRenderer(new UARTBitOrderItemRenderer());
    settings.add(this.bitOrder);

    SpringLayoutUtils.makeEditorGrid(settings, 10, 4);

    return settings;
  }
  /** @return */
  private JPanel createSettingsPane() {
    final String channels[] = new String[33];
    channels[0] = "Unused";
    for (int i = 0; i < 32; i++) {
      channels[i + 1] = new String("Channel " + i);
    }

    final JPanel settings = new JPanel(new SpringLayout());

    SpringLayoutUtils.addSeparator(settings, "Settings");

    settings.add(createRightAlignedLabel("RxD"));
    this.rxd = new JComboBox(channels);
    this.rxd.setSelectedIndex(0);
    settings.add(this.rxd);

    settings.add(createRightAlignedLabel("TxD"));
    this.txd = new JComboBox(channels);
    this.txd.setSelectedIndex(0);
    settings.add(this.txd);

    settings.add(createRightAlignedLabel("CTS"));
    this.cts = new JComboBox(channels);
    this.cts.setSelectedIndex(0);
    settings.add(this.cts);

    settings.add(createRightAlignedLabel("RTS"));
    this.rts = new JComboBox(channels);
    this.rts.setSelectedIndex(0);
    settings.add(this.rts);

    settings.add(createRightAlignedLabel("DTR"));
    this.dtr = new JComboBox(channels);
    this.dtr.setSelectedIndex(0);
    settings.add(this.dtr);

    settings.add(createRightAlignedLabel("DSR"));
    this.dsr = new JComboBox(channels);
    this.dsr.setSelectedIndex(0);
    settings.add(this.dsr);

    settings.add(createRightAlignedLabel("DCD"));
    this.dcd = new JComboBox(channels);
    this.dcd.setSelectedIndex(0);
    settings.add(this.dcd);

    settings.add(createRightAlignedLabel("RI"));
    this.ri = new JComboBox(channels);
    this.ri.setSelectedIndex(0);
    settings.add(this.ri);

    settings.add(createRightAlignedLabel("Parity"));
    this.parity = new JComboBox(UARTParity.values());
    this.parity.setSelectedIndex(0);
    this.parity.setRenderer(new UARTParityItemRenderer());
    settings.add(this.parity);

    settings.add(createRightAlignedLabel("Bits"));
    final String[] bitarray = new String[4];
    for (int i = 0; i < bitarray.length; i++) {
      bitarray[i] = String.format("%d", Integer.valueOf(i + 5));
    }
    this.bits = new JComboBox(bitarray);
    this.bits.setSelectedIndex(3);
    settings.add(this.bits);

    settings.add(createRightAlignedLabel("Stopbits"));
    this.stop = new JComboBox(UARTStopBits.values());
    this.stop.setSelectedIndex(0);
    this.stop.setRenderer(new UARTStopBitsItemRenderer());
    settings.add(this.stop);

    this.inv = new JCheckBox();
    this.inv.setSelected(false);
    settings.add(createRightAlignedLabel("Invert?"));
    settings.add(this.inv);

    SpringLayoutUtils.makeEditorGrid(settings, 10, 4);

    return settings;
  }