Example #1
0
  /**
   * 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();
  }
Example #2
0
  public Viewer() {
    leveldbStore.setMultiSelectionEnabled(false);
    leveldbStore.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    putButton.setEnabled(false);
    key.setEnabled(false);
    value.setEnabled(false);
    findField.setEnabled(false);
    deleteButton.setEnabled(false);
    saveButton.setEnabled(false);
    putType.setEnabled(false);
    putType.setEditable(false);
    signedBox.setEnabled(false);

    openButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            if (openButton.isEnabled()) {
              openButton.setEnabled(false);
              new Thread() {
                public void run() {
                  if (leveldbStore.showOpenDialog(pane) == JFileChooser.APPROVE_OPTION) {
                    File select = leveldbStore.getSelectedFile();
                    if (select.isDirectory()) {
                      new OpenLevelDBDialog(Viewer.this, select);
                      openDatabase(select);
                      dbPathField.setText(select.getAbsolutePath());
                    } else {
                      JOptionPane.showMessageDialog(
                          pane,
                          "The selecting item must be a directory",
                          "Unable to load database",
                          JOptionPane.WARNING_MESSAGE);
                    }
                  } else {
                    openButton.setEnabled(true);
                  }
                }
              }.start();
            }
          }
        });

    deleteButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            if (dataList.getSelectedValue() != null) {
              delete(dataList.getSelectedValue().key);
            }
            openDatabase(leveldbStore.getSelectedFile());
          }
        });

    putButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            put(
                ((PutType) putType.getSelectedItem()).getBytes(key.getText()),
                ((PutType) putType.getSelectedItem()).getBytes(value.getText()));
            openDatabase(leveldbStore.getSelectedFile());
          }
        });

    findField.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            openDatabase(leveldbStore.getSelectedFile());
          }
        });
    findField
        .getDocument()
        .addDocumentListener(
            new DocumentListener() {
              @Override
              public void insertUpdate(DocumentEvent e) {
                openDatabase(leveldbStore.getSelectedFile());
              }

              @Override
              public void removeUpdate(DocumentEvent e) {
                openDatabase(leveldbStore.getSelectedFile());
              }

              @Override
              public void changedUpdate(DocumentEvent e) {
                openDatabase(leveldbStore.getSelectedFile());
              }
            });
    findField
        .getDocument()
        .addUndoableEditListener(
            new UndoableEditListener() {
              @Override
              public void undoableEditHappened(UndoableEditEvent e) {
                openDatabase(leveldbStore.getSelectedFile());
              }
            });

    hexKey
        .getDocument()
        .addDocumentListener(
            new DocumentListener() {
              @Override
              public void insertUpdate(DocumentEvent e) {
                update(hexKey);
              }

              @Override
              public void removeUpdate(DocumentEvent e) {
                update(hexKey);
              }

              @Override
              public void changedUpdate(DocumentEvent e) {
                update(hexKey);
              }
            });
    hexKey
        .getDocument()
        .addUndoableEditListener(
            new UndoableEditListener() {
              @Override
              public void undoableEditHappened(UndoableEditEvent e) {
                update(hexKey);
              }
            });

    stringKey
        .getDocument()
        .addDocumentListener(
            new DocumentListener() {
              @Override
              public void insertUpdate(DocumentEvent e) {
                update(stringKey);
              }

              @Override
              public void removeUpdate(DocumentEvent e) {
                update(stringKey);
              }

              @Override
              public void changedUpdate(DocumentEvent e) {
                update(stringKey);
              }
            });
    stringKey
        .getDocument()
        .addUndoableEditListener(
            new UndoableEditListener() {
              @Override
              public void undoableEditHappened(UndoableEditEvent e) {
                update(stringKey);
              }
            });

    hexValue
        .getDocument()
        .addDocumentListener(
            new DocumentListener() {
              @Override
              public void insertUpdate(DocumentEvent e) {
                update(hexValue);
              }

              @Override
              public void removeUpdate(DocumentEvent e) {
                update(hexValue);
              }

              @Override
              public void changedUpdate(DocumentEvent e) {
                update(hexValue);
              }
            });
    hexValue
        .getDocument()
        .addUndoableEditListener(
            new UndoableEditListener() {
              @Override
              public void undoableEditHappened(UndoableEditEvent e) {
                update(hexValue);
              }
            });

    stringValue
        .getDocument()
        .addDocumentListener(
            new DocumentListener() {
              @Override
              public void insertUpdate(DocumentEvent e) {
                update(stringValue);
              }

              @Override
              public void removeUpdate(DocumentEvent e) {
                update(stringValue);
              }

              @Override
              public void changedUpdate(DocumentEvent e) {
                update(stringValue);
              }
            });
    stringValue
        .getDocument()
        .addUndoableEditListener(
            new UndoableEditListener() {
              @Override
              public void undoableEditHappened(UndoableEditEvent e) {
                update(stringValue);
              }
            });

    saveButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            save();
            openDatabase(leveldbStore.getSelectedFile());
          }
        });

    dataList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    dataList.addListSelectionListener(
        new ListSelectionListener() {
          @Override
          public void valueChanged(ListSelectionEvent e) {
            DBItem item = dataList.getSelectedValue();
            if (item != null) {
              hexValue.setText(cutToLine(LevelDBViewer.toHexString(item.value), 64));
              stringValue.setText(cutToLine(new String(item.value), 64));
              hexKey.setText(cutToLine(LevelDBViewer.toHexString(item.key), 64));
              stringKey.setText(cutToLine(new String(item.key), 64));

              lengthLabel.setText(String.valueOf(item.value.length + item.key.length));
              keyLength.setText(String.valueOf(item.key.length));
              valueLength.setText(String.valueOf(item.value.length));
            }
          }
        });

    signedBox.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            LevelDBViewer.DEFAULT_SINGED = signedBox.isSelected();
            int i = dataList.getSelectedIndex();
            dataList.clearSelection();
            dataList.updateUI();
            dataList.setSelectedIndex(i);
            update(hexKey);
            update(hexValue);
          }
        });

    for (PutType t : PutType.values()) {
      putType.addItem(t);
    }
    putType.setSelectedItem(PutType.STRING);
    putType.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            openDatabase(leveldbStore.getSelectedFile());
          }
        });
    putType.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            openDatabase(leveldbStore.getSelectedFile());
          }
        });

    dialog.setLocationByPlatform(true);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setContentPane(pane);
    dialog.setTitle("LevelDB Viewer By Marcus (https://github.com/SuperMarcus)");
    dialog.getRootPane().setDefaultButton(openButton);
    dialog.pack();
    dialog.setVisible(true);
  }
Example #3
0
  public void openDatabase(File select) {
    if (getOptions() != null) {
      DB database = null;
      try {
        database = factory.open(select, getOptions());
        DBIterator iterator = database.iterator();

        ArrayList<DBItem> data = new ArrayList<>();

        String reg = findField.getText().trim();

        for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
          if (reg.isEmpty()
              || new BigInteger(iterator.peekNext().getKey()).toString(16).contains(reg)
              || LevelDBViewer.toHexString(iterator.peekNext().getKey()).contains(reg)
              || new BigInteger(iterator.peekNext().getValue()).toString(16).contains(reg)
              || LevelDBViewer.toHexString(iterator.peekNext().getValue()).contains(reg)
              || new String(iterator.peekNext().getKey()).contains(reg)
              || new String(iterator.peekNext().getValue()).contains(reg)) {
            data.add(new DBItem(iterator.peekNext().getKey(), iterator.peekNext().getValue()));
          }
        }

        iterator.close();

        dialog.getRootPane().setDefaultButton(putButton);

        dataList.getSelectionModel().clearSelection();
        dataList.setListData(data.toArray(new DBItem[data.size()]));

        putButton.setEnabled(true);
        key.setEnabled(true);
        value.setEnabled(true);
        findField.setEnabled(true);
        deleteButton.setEnabled(true);
        putType.setEnabled(true);
        signedBox.setEnabled(true);
        // saveButton.setEnabled(true);

        hexValue.setText("");
        stringValue.setText("");
        hexKey.setText("");
        stringKey.setText("");

        lengthLabel.setText("");
        keyLength.setText("");
        valueLength.setText("");
      } catch (Exception e) {
        JOptionPane.showMessageDialog(pane, "Unable to open database:\n" + e);
        e.printStackTrace();
      } finally {
        if (database != null) {
          try {
            database.close();
          } catch (IOException e) {
            JOptionPane.showMessageDialog(pane, "Unable to close database:\n" + e);
            e.printStackTrace();
          }
        }
      }
    }
  }
Example #4
0
  public static boolean showLicensing() {
    if (Config.getLicenseResource() == null) return true;
    ClassLoader cl = Main.class.getClassLoader();
    URL url = cl.getResource(Config.getLicenseResource());
    if (url == null) return true;

    String license = null;
    try {
      URLConnection con = url.openConnection();
      int size = con.getContentLength();
      byte[] content = new byte[size];
      InputStream in = new BufferedInputStream(con.getInputStream());
      in.read(content);
      license = new String(content);
    } catch (IOException ioe) {
      Config.trace("Got exception when reading " + Config.getLicenseResource() + ": " + ioe);
      return false;
    }

    // Build dialog
    JTextArea ta = new JTextArea(license);
    ta.setEditable(false);
    final JDialog jd = new JDialog(_installerFrame, true);
    Container comp = jd.getContentPane();
    jd.setTitle(Config.getLicenseDialogTitle());
    comp.setLayout(new BorderLayout(10, 10));
    comp.add(new JScrollPane(ta), "Center");
    Box box = new Box(BoxLayout.X_AXIS);
    box.add(box.createHorizontalStrut(10));
    box.add(new JLabel(Config.getLicenseDialogQuestionString()));
    box.add(box.createHorizontalGlue());
    JButton acceptButton = new JButton(Config.getLicenseDialogAcceptString());
    JButton exitButton = new JButton(Config.getLicenseDialogExitString());
    box.add(acceptButton);
    box.add(box.createHorizontalStrut(10));
    box.add(exitButton);
    box.add(box.createHorizontalStrut(10));
    jd.getRootPane().setDefaultButton(acceptButton);
    Box box2 = new Box(BoxLayout.Y_AXIS);
    box2.add(box);
    box2.add(box2.createVerticalStrut(5));
    comp.add(box2, "South");
    jd.pack();

    final boolean accept[] = new boolean[1];
    acceptButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            accept[0] = true;
            jd.hide();
            jd.dispose();
          }
        });

    exitButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            accept[0] = false;
            jd.hide();
            jd.dispose();
          }
        });

    // Apply any defaults the user may have, constraining to the size
    // of the screen, and default (packed) size.
    Rectangle size = new Rectangle(0, 0, 500, 300);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    size.width = Math.min(screenSize.width, size.width);
    size.height = Math.min(screenSize.height, size.height);
    // Center the window
    jd.setBounds(
        (screenSize.width - size.width) / 2,
        (screenSize.height - size.height) / 2,
        size.width,
        size.height);

    // Show dialog
    jd.show();

    return accept[0];
  }