-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/main/java/com/tomczarniecki/jpasskeep/AssignCategoriesDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.tomczarniecki.jpasskeep; | ||
|
||
import com.jgoodies.forms.builder.ButtonBarBuilder; | ||
|
||
import javax.swing.DefaultCellEditor; | ||
import javax.swing.JButton; | ||
import javax.swing.JComboBox; | ||
import javax.swing.JDialog; | ||
import javax.swing.JFrame; | ||
import javax.swing.JScrollPane; | ||
import javax.swing.JTable; | ||
import java.awt.BorderLayout; | ||
import java.awt.Container; | ||
|
||
public class AssignCategoriesDialog extends JDialog implements EventListener<AssignCategoriesDialog.Event> { | ||
public enum Event { | ||
SUBMIT, CANCEL; | ||
} | ||
|
||
private final MainListController controller; | ||
private final AssignCategoriesModel model; | ||
private boolean success; | ||
|
||
public AssignCategoriesDialog(JFrame owner, MainListController controller) { | ||
super(owner, "Assign Categories", true); | ||
this.model = new AssignCategoriesModel(controller.getEntries()); | ||
this.controller = controller; | ||
|
||
var table = new JTable(this.model); | ||
var columnModel = table.getColumnModel(); | ||
var categoryColumn = columnModel.getColumn(1); | ||
var comboBox = new JComboBox<>(Category.values()); | ||
categoryColumn.setCellEditor(new DefaultCellEditor(comboBox)); | ||
|
||
ButtonBarBuilder buttonBar = ButtonBarBuilder.createLeftToRightBuilder(); | ||
buttonBar.setDefaultDialogBorder(); | ||
buttonBar.addGlue(); | ||
buttonBar.addGridded(new JButton(EventAction.create("OK", AssignCategoriesDialog.Event.SUBMIT, this))); | ||
buttonBar.addRelatedGap(); | ||
buttonBar.addGridded(new JButton(EventAction.create("Cancel", AssignCategoriesDialog.Event.CANCEL, this))); | ||
buttonBar.addGlue(); | ||
|
||
Container cc = getContentPane(); | ||
cc.setLayout(new BorderLayout()); | ||
cc.add(new JScrollPane(table), BorderLayout.CENTER); | ||
cc.add(buttonBar.getPanel(), BorderLayout.SOUTH); | ||
setResizable(false); | ||
pack(); | ||
} | ||
|
||
public void assign() { | ||
success = false; | ||
setLocationRelativeTo(getOwner()); | ||
setVisible(true); | ||
// setVisible(true) blocks until set to false | ||
if (success) { | ||
var entries = controller.getEntries(); | ||
for (int i = 0; i < entries.size(); i++) { | ||
var entry = entries.get(i); | ||
var category = model.getCategory(i); | ||
if (entry.getCategory() != category) { | ||
entry.setCategory(category); | ||
controller.updateEntryAt(i, entry); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void processEvent(Event event) { | ||
switch (event) { | ||
case SUBMIT: | ||
success = true; | ||
setVisible(false); | ||
break; | ||
case CANCEL: | ||
setVisible(false); | ||
break; | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/tomczarniecki/jpasskeep/AssignCategoriesModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.tomczarniecki.jpasskeep; | ||
|
||
import javax.swing.table.AbstractTableModel; | ||
import java.util.List; | ||
|
||
public class AssignCategoriesModel extends AbstractTableModel { | ||
|
||
private static class Pair { | ||
String name; | ||
Category category; | ||
} | ||
|
||
private final static String[] columns = {"Name", "Category"}; | ||
private final List<Pair> entries; | ||
|
||
public AssignCategoriesModel(List<Entry> entries) { | ||
this.entries = entries.stream().map(AssignCategoriesModel::toPair).toList(); | ||
} | ||
|
||
private static Pair toPair(Entry entry) { | ||
var pair = new Pair(); | ||
pair.name = entry.getDescription(); | ||
pair.category = entry.getCategory(); | ||
return pair; | ||
} | ||
|
||
@Override | ||
public String getColumnName(int column) { | ||
return columns[column]; | ||
} | ||
|
||
@Override | ||
public int getRowCount() { | ||
return entries.size(); | ||
} | ||
|
||
@Override | ||
public int getColumnCount() { | ||
return columns.length; | ||
} | ||
|
||
@Override | ||
public Object getValueAt(int row, int col) { | ||
var pair = entries.get(row); | ||
return (col == 0) ? pair.name : pair.category; | ||
} | ||
|
||
public Category getCategory(int row) { | ||
return entries.get(row).category; | ||
} | ||
|
||
@Override | ||
public Class<?> getColumnClass(int columnIndex) { | ||
return getValueAt(0, columnIndex).getClass(); | ||
} | ||
|
||
@Override | ||
public boolean isCellEditable(int rowIndex, int columnIndex) { | ||
return columnIndex == 1; | ||
} | ||
|
||
@Override | ||
public void setValueAt(Object value, int rowIndex, int columnIndex) { | ||
if (columnIndex == 1) { | ||
entries.get(rowIndex).category = (Category) value; | ||
fireTableCellUpdated(rowIndex, columnIndex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters