public void save() { boolean isNoticed = false; if (!notice.isVisible()) { isNoticed = true; notice.setVisible(true); notice.setText("Saving..."); } if (getOptions() != null) { DB database = null; try { database = factory.open(this.leveldbStore.getSelectedFile(), getOptions()); DBIterator iterator = database.iterator(); HashSet<byte[]> keys = new HashSet<>(); for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) { keys.add(iterator.peekNext().getKey()); } iterator.close(); for (byte[] key : keys) { database.delete(key); } for (int i = 0; i < dataList.getModel().getSize(); ++i) { DBItem item = dataList.getModel().getElementAt(i); database.put(item.key, item.value); } } 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(); } } saveButton.setEnabled(false); } } if (isNoticed) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } notice.setVisible(false); notice.setText(""); } }
public void update(JTextArea area) { DBItem item = dataList.getSelectedValue(); if (item != null && !isSet) { isSet = true; try { if (area == hexKey) { if (area.getText().isEmpty()) { return; } item.key = new BigInteger(area.getText().replaceAll("\n", "").replaceAll("%\\{EOL}", "\n"), 16) .toByteArray(); stringKey.setText(cutToLine(new String(item.key), 64)); dataList.updateUI(); } else if (area == stringKey) { if (area.getText().isEmpty()) { return; } item.key = area.getText().replaceAll("\n", "").replaceAll("%\\{EOL}", "\n").getBytes(); hexKey.setText(cutToLine(LevelDBViewer.toHexString(item.key), 64)); dataList.updateUI(); } else if (area == hexValue) { item.value = new BigInteger(area.getText().replaceAll("\n", "").replaceAll("%\\{EOL}", "\n"), 16) .toByteArray(); stringValue.setText(cutToLine(new String(item.value), 64)); } else if (area == stringValue) { item.value = area.getText().replaceAll("\n", "").replaceAll("%\\{EOL}", "\n").getBytes(); hexValue.setText(cutToLine(LevelDBViewer.toHexString(item.value), 64)); } notice.setVisible(false); notice.setText(""); saveButton.setEnabled(true); } catch (Exception e) { notice.setVisible(true); notice.setText("Invalid number!"); } finally { lengthLabel.setText(String.valueOf(item.value.length + item.key.length)); keyLength.setText(String.valueOf(item.key.length)); valueLength.setText(String.valueOf(item.value.length)); isSet = false; } } }
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(); } } } } }