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; } } }