Mercurial Hosting > junotu
changeset 12:b4fd74d0680d
Card deletion button
author | Fox |
---|---|
date | Fri, 08 Apr 2022 23:58:44 +0200 |
parents | 587b69a38915 |
children | ecc0cb60b845 |
files | src/junotu/Database.java src/junotu/TabEdit.java |
diffstat | 2 files changed, 56 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/src/junotu/Database.java Fri Apr 08 13:13:31 2022 +0200 +++ b/src/junotu/Database.java Fri Apr 08 23:58:44 2022 +0200 @@ -171,6 +171,23 @@ } + public void cardDelete( long identifier ) throws Exception + { + Query query = NumericRangeQuery.newLongRange( TAG_IDENTIFIER, identifier, identifier, true, true ); + TopDocs topDocuments = luceneSearcher.search( query, 1 ); + + if( topDocuments.scoreDocs.length == 0 ) { + throw new RuntimeException( "Failed to delete card with identifier "+Long.toString( identifier )+", not found." ); + } + + int documentNumber = topDocuments.scoreDocs[0].doc; + + luceneWriter.deleteDocuments( query ); + System.out.print("Deleted card with identifier "+Long.toString(identifier)+"\n"); + searcherRefresh(); + + } + public Card cardGetByIdentifier( long identifier ) throws Exception {
--- a/src/junotu/TabEdit.java Fri Apr 08 13:13:31 2022 +0200 +++ b/src/junotu/TabEdit.java Fri Apr 08 23:58:44 2022 +0200 @@ -30,6 +30,8 @@ private JTextField title; private JTextArea content; + + JButton delete; public TabEdit() { @@ -41,8 +43,9 @@ content = new JTextArea(); Box bottom = Box.createHorizontalBox(); - JButton back = new JButton("Cancel"); - JButton save = new JButton("Save"); + JButton back = new JButton("Cancel"); + delete = new JButton("Delete"); + JButton save = new JButton("Save"); title.setFont( new Font( "Monospaced", Font.PLAIN, 32 ) ); content.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) ); @@ -59,6 +62,7 @@ this.add( bottom, BorderLayout.SOUTH ); bottom.add( back ); bottom.add( Box.createHorizontalGlue() ); + bottom.add( delete ); bottom.add( save ); //scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); @@ -73,6 +77,16 @@ } ); + delete.addActionListener( + new ActionListener() { + @Override + public void actionPerformed( ActionEvent e ) + { + buttonClickedDelete(); + } + } + ); + save.addActionListener( new ActionListener() { @Override @@ -111,6 +125,7 @@ newCard = true; identifier = -1; updateTitle(); + delete.setVisible(false); } public void cardEdit( Card card ) @@ -120,6 +135,7 @@ title.setText( card.title ); content.setText( card.content ); updateTitle(); + delete.setVisible(true); } private void clearWidgets() @@ -149,6 +165,26 @@ clearWidgets(); window.tabSwitch( Tab.SEARCH ); } + + private void buttonClickedDelete() + { + + if( newCard ) { + return; + } + + try { + Main.database.cardDelete( identifier ); + } catch( Exception e ) { + throw new RuntimeException(e); + } + + Window window = (Window)this.getTopLevelAncestor(); + window.tabSearch.search(); + clearWidgets(); + window.tabSwitch( Tab.SEARCH ); + + } private void buttonClickedSave() { @@ -166,7 +202,7 @@ Main.database.cardUpdate( card ); } } catch( Exception e ) { - + throw new RuntimeException(e); } Window window = (Window)this.getTopLevelAncestor();