Exemplo n.º 1
0
  @Override
  protected void buildGui() {
    XToolBar toolBar = new XToolBar();
    toolBarsBox.add(toolBar);

    toolBar.add(new XLabel("Data source:").verticalBorder(7));
    dataSourceComboBox.addActionListener(rebuilderListener);
    dataSourceComboBox.setMaximumRowCount(dataSourceComboBox.getItemCount());
    toolBar.add(dataSourceComboBox);

    toolBar.addSeparator();
    LGuiUtils.autoCreateDisabledImage(toolBar.add(saveAction));

    toolBar.addSeparator();
    toolBar.add(new XLabel("Line size:"));
    toolBar.add(hexLineSizeComboBox);
    toolBar.add(new XLabel(Settings.HEX_LINE_SIZE.viewHints.getSubsequentText()));

    toolBar.addSeparator();
    toolBar.add(new XLabel("Jump to pos:"));
    final XTextField jumpTextField = new XTextField(null, 6, true);
    jumpTextField.setValidator(
        new IValidator() {
          @Override
          public boolean validate(final String text) {
            final boolean result = validateLogic(text);
            if (!result) Sound.beepOnEmptyTxtSearchRslt();
            return result;
          }

          private boolean validateLogic(final String text) {
            if (text.isEmpty() || "0x".equals(text)) return true;
            try {
              final int pos =
                  text.startsWith("0x")
                      ? Integer.parseInt(text.substring(2), 16)
                      : Integer.parseInt(text);
              if (pos < 0 || pos >= data.length) return false;
              final int line = pos >> hexLineSizeComboBox.getSelectedItem().shiftCount;
              textList.setSelectedIndex(line);
              textList.scrollRectToVisible(textList.getCellBounds(line, line));
              return true;
            } catch (final NumberFormatException nfe) {
              return false;
            }
          }
        });
    jumpTextField.addActionListener(
        new ActionAdapter() {
          @Override
          public void actionPerformed(final ActionEvent event) {
            final String text = jumpTextField.getText();
            jumpTextField.setText(null);
            jumpTextField.setText(text);
          }
        });
    jumpTextField.setToolTipText(
        "<html>Enter a byte position to jump to, either decimal or hexa starting with <code>\"0x\"</code>.</html>");
    // Focus Jump field when pressing CTRL+J
    jumpTextField.registerFocusHotkey(
        this, KeyStroke.getKeyStroke(KeyEvent.VK_J, InputEvent.CTRL_MASK));
    toolBar.add(jumpTextField);

    toolBar.addSeparator();
    toolBar.add(dataSizeLabel);

    toolBar.finalizeLayout();

    super.buildGui();

    // Extend the original tool bar
    toolBar = this.toolBar;
    // Move the info component (currently the last) to the end
    final Component infoComp = toolBar.getComponent(toolBar.getComponentCount() - 1);
    toolBar.remove(toolBar.getComponentCount() - 1);
    // Insert tip to the text search, before the separator:
    toolBar.add(new TipIcon(Tips.BINARY_DATA_TEXT_SEARCH), toolBar.getComponentCount() - 1);
    // Add Hex searcher
    hexSearchComp.textField.setValidator(
        new IValidator() {
          @Override
          public boolean validate(String text) {
            text = text.replace(" ", ""); // Remove spaces
            // Check: length must be even, must contain only hex digits
            return (text.length() & 0x01) == 0 && text.matches("[\\da-f]*");
          }
        });
    hexSearchComp.textField.setToolTipText(
        "<html>Search hex data, for example <code>\"06 1f\"</html>");
    // Register CTRL+F for hex search (CTRL+S is taken by the other search component)
    // (CTRL+H is already used by JTextField!)
    hexSearchComp.registerFocusHotkey(
        this, KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK));
    hexSearchComp.setSearcher(hexSearcher);
    toolBar.add(hexSearchComp);
    toolBar.addSeparator();
    toolBar.add(infoComp);
    toolBar.finalizeLayout();
  }