Skip to content

Commit

Permalink
otros-systems#575 | Updated FavoriteListCellRenderer to highlight the…
Browse files Browse the repository at this point in the history
… cell when it's selected | Updated the behavior of the right click on the list of Favorite locations so that the cell clicked over gets selected and then the popup menu is displayed
  • Loading branch information
benlazaro committed Apr 23, 2022
1 parent 5a8971f commit aa0c446
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
28 changes: 26 additions & 2 deletions olv-vfs/src/main/java/pl/otros/vfs/browser/VfsBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ protected void saveFavorites() {
favoritesUserList.setTransferHandler(new MutableListDropHandler(favoritesUserList));
new MutableListDragListener(favoritesUserList);
favoritesUserList.setCellRenderer(new FavoriteListCellRenderer());
favoritesUserList.addFocusListener(new SelectFirstElementFocusAdapter());
// favoritesUserList.addFocusListener(new SelectFirstElementFocusAdapter());

addOpenActionToList(favoritesUserList);
addEditActionToList(favoritesUserList, favoritesUserListModel);
Expand All @@ -459,6 +459,30 @@ public void actionPerformed(ActionEvent e) {
InputMap favoritesListInputMap = favoritesUserList.getInputMap(JComponent.WHEN_FOCUSED);
favoritesListInputMap.put(KeyStroke.getKeyStroke("DELETE"), ACTION_DELETE);

JPopupMenu favoritesPopupMenu = new JPopupMenu();
String[] actions = new String[]{ ACTION_OPEN, ACTION_EDIT, ACTION_DELETE };
for (String action : actions) {
favoritesPopupMenu.add(favoritesUserList.getActionMap().get(action));
}
// favoritesUserList.addKeyListener(new PopupListener(favoritesPopupMenu));

favoritesUserList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
JList<Favorite> list = (JList<Favorite>) e.getSource();
int selectedIndex = list.locationToIndex(e.getPoint());
list.setSelectedIndex(selectedIndex);
favoritesPopupMenu.getSelectionModel().clearSelection();
favoritesPopupMenu.updateUI();
show((Component) e.getSource(), e.getX(), e.getY());
}
}

public void show(Component invoker, int x, int y) {
favoritesPopupMenu.show(invoker, x, y);
}
});

ActionMap actionMap = tableFiles.getActionMap();
actionMap.put(ACTION_OPEN, new BaseNavigateActionOpen(this));
Expand All @@ -478,7 +502,7 @@ public void actionPerformed(ActionEvent e) {

inputMap.put(KeyStroke.getKeyStroke("BACK_SPACE"), ACTION_GO_UP);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), ACTION_GO_UP);
addPopupMenu(favoritesUserList, ACTION_OPEN, ACTION_EDIT, ACTION_DELETE);
// addPopupMenu(favoritesUserList, ACTION_OPEN, ACTION_EDIT, ACTION_DELETE);

JList favoriteSystemList = new JList(new Vector<Object>(favSystemLocations));
favoriteSystemList.setCellRenderer(new FavoriteListCellRenderer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,35 @@

package pl.otros.vfs.browser.table;

import pl.otros.vfs.browser.util.VFSUtils;
import pl.otros.vfs.browser.favorit.Favorite;
import pl.otros.vfs.browser.util.VFSUtils;

import javax.swing.*;
import java.awt.*;

/**
*
*/
public class FavoriteListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component component = super.getListCellRendererComponent(list, value, index, isSelected & cellHasFocus, cellHasFocus);
if (component instanceof JLabel) {
JLabel label = (JLabel) component;
Favorite f = (Favorite) value;
label.setText(f.getName());
label.setIcon(VFSUtils.getIconForFileSystem(f.getUrl()));
label.setToolTipText(VFSUtils.getFriendlyName(f.getUrl()));
Favorite favorite = (Favorite) value;
label.setText(favorite.getName());
label.setIcon(VFSUtils.getIconForFileSystem(favorite.getUrl()));
label.setToolTipText(VFSUtils.getFriendlyName(favorite.getUrl()));
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
}

return component;
Expand Down

0 comments on commit aa0c446

Please sign in to comment.