Skip to content

Commit

Permalink
fix: bring back the disappeared popup menu
Browse files Browse the repository at this point in the history
  • Loading branch information
astrapi69 committed Sep 23, 2023
1 parent bcab465 commit 5867c50
Showing 1 changed file with 87 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import javax.swing.tree.TreeModel;

import io.github.astrapi69.design.pattern.observer.event.EventSource;
import io.github.astrapi69.swing.listener.mouse.MouseDoubleClickListener;
import io.github.astrapi69.swing.renderer.tree.renderer.state.NewGenericBaseTreeNodeCellRenderer;
import net.miginfocom.layout.AC;
import net.miginfocom.layout.CC;
Expand Down Expand Up @@ -112,10 +113,66 @@ protected JTree newTree()
@Override
protected JScrollPane newTreeScrollPane()
{
JScrollPane scroller = super.newTreeScrollPane();
scroller.getViewport().setOpaque(false);
scroller.setOpaque(false);
return scroller;
JScrollPane treeScrollPane = super.newTreeScrollPane();
treeScrollPane.getViewport().setOpaque(false);
treeScrollPane.setOpaque(false);
return treeScrollPane;
}

@Override
protected JScrollPane newTableScrollPane()
{
JScrollPane tableScrollPane = super.newTableScrollPane();
tableScrollPane.getViewport().setOpaque(false);
tableScrollPane.setOpaque(false);
tableScrollPane.addMouseListener(new MouseDoubleClickListener()
{
@Override
public void onSingleClick(MouseEvent mouseEvent)
{
List<MysticCryptEntryModelBean> data = getTblTreeEntryTable().getGenericTableModel()
.getData();
List<MysticCryptEntryModelBean> allSelectedRowData = getTblTreeEntryTable()
.getAllSelectedRowData();

boolean noRowSelected = allSelectedRowData.isEmpty();
boolean emptyTable = data.isEmpty();
if(emptyTable || noRowSelected) {
if (mouseEvent.getButton() == MouseEvent.BUTTON1)
{
SecretKeyTreeWithContentPanel.this.onTableSingleLeftClick(mouseEvent);
}
if (mouseEvent.getButton() == MouseEvent.BUTTON2)
{

SecretKeyTreeWithContentPanel.this.onTableSingleMiddleClick(mouseEvent);
}
if (mouseEvent.getButton() == MouseEvent.BUTTON3)
{
SecretKeyTreeWithContentPanel.this.onTableSingleRightClick(mouseEvent);
}
}
}

@Override
public void onDoubleClick(MouseEvent mouseEvent)
{
if (mouseEvent.getButton() == MouseEvent.BUTTON1)
{
// SecretKeyTreeWithContentPanel.this.onTableDoubleLeftClick(mouseEvent);
}
if (mouseEvent.getButton() == MouseEvent.BUTTON2)
{

// SecretKeyTreeWithContentPanel.this.onTableDoubleMiddleClick(mouseEvent);
}
if (mouseEvent.getButton() == MouseEvent.BUTTON3)
{
// SecretKeyTreeWithContentPanel.this.onTableDoubleRightClick(mouseEvent);
}
}
});
return tableScrollPane;
}

@Override
Expand Down Expand Up @@ -502,30 +559,32 @@ protected void onTableSingleRightClick(MouseEvent mouseEvent)
{
int x = mouseEvent.getX();
int y = mouseEvent.getY();
MysticCryptEntryModelBean singleSelectedRow;
MysticCryptEntryModelBean selectedRow;

List<MysticCryptEntryModelBean> allSelectedRowData = getTblTreeEntryTable()
.getAllSelectedRowData();

boolean isSingleSelectedRow = allSelectedRowData.size() == 1;
boolean noRowSelected = allSelectedRowData.isEmpty();
boolean singleSelectedRow = allSelectedRowData.size() == 1;
boolean rowsSelected = !noRowSelected;
boolean validUrl = false;
if (isSingleSelectedRow)
if (singleSelectedRow)
{
singleSelectedRow = allSelectedRowData.get(0);
String urlString = singleSelectedRow.getUrl();
selectedRow = allSelectedRowData.get(0);
String urlString = selectedRow.getUrl();
validUrl = validateUrlString(urlString);
}

JPopupMenu popup = JPopupMenuFactory.newJPopupMenu();

JMenuItem copyUsername = JMenuItemFactory.newJMenuItem("Copy Username",
actionEvent -> this.onCopyUsernameTableEntry());
copyUsername.setEnabled(isSingleSelectedRow);
copyUsername.setEnabled(singleSelectedRow);
popup.add(copyUsername);

JMenuItem copyPassword = JMenuItemFactory.newJMenuItem("Copy Password",
actionEvent -> this.onCopyPasswordTableEntry());
copyPassword.setEnabled(allSelectedRowData.size() == 1);
copyPassword.setEnabled(singleSelectedRow);
popup.add(copyPassword);

JMenuItem openUrl = JMenuItemFactory.newJMenuItem("Open url",
Expand All @@ -535,7 +594,7 @@ protected void onTableSingleRightClick(MouseEvent mouseEvent)

JMenuItem openUrlAndAutotype = JMenuItemFactory.newJMenuItem("Autotype",
actionEvent -> this.onOpenUrlAndAutotypeOfTableEntry());
openUrl.setEnabled(validUrl);
openUrlAndAutotype.setEnabled(validUrl);
popup.add(openUrlAndAutotype);

// Separator
Expand All @@ -547,26 +606,33 @@ protected void onTableSingleRightClick(MouseEvent mouseEvent)

JMenuItem edit = JMenuItemFactory.newJMenuItem("edit...",
actionEvent -> this.onEditTableEntry());
edit.setEnabled(allSelectedRowData.size() == 1);
edit.setEnabled(singleSelectedRow);
popup.add(edit);

JMenuItem duplicate = JMenuItemFactory.newJMenuItem("duplicate...",
actionEvent -> this.onDuplicateTableEntry());
duplicate.setEnabled(allSelectedRowData.size() == 1);
duplicate.setEnabled(singleSelectedRow);
popup.add(duplicate);

JMenuItem delete = JMenuItemFactory.newJMenuItem("delete",
actionEvent -> this.onDeleteTableEntry());
delete.setEnabled(!allSelectedRowData.isEmpty());
delete.setEnabled(rowsSelected);
popup.add(delete);
// Separator
popup.addSeparator();

JMenuItem selectAll = JMenuItemFactory.newJMenuItem("select all",
actionEvent -> this.onSelectAllTableEntries());
actionEvent -> this.onSelectAllTableEntries());
selectAll.setEnabled(0 < getTblTreeEntryTable().getRowCount());

popup.add(selectAll);

JMenuItem clearSelection = JMenuItemFactory.newJMenuItem("clear selection",
actionEvent -> this.onDeselectAllTableEntries());
clearSelection.setEnabled(rowsSelected);

popup.add(clearSelection);

popup.show(getTblTreeEntryTable(), x, y);
}

Expand Down Expand Up @@ -616,6 +682,11 @@ protected void onSelectAllTableEntries()
getTblTreeEntryTable().selectAll();
}

protected void onDeselectAllTableEntries()
{
getTblTreeEntryTable().clearSelection();
}

protected void onDuplicateTableEntry()
{
getTblTreeEntryTable().getSingleSelectedRowData().ifPresent(selectedTableEntry -> {
Expand Down

0 comments on commit 5867c50

Please sign in to comment.