001package jmri.swing; 002 003import javax.swing.*; 004 005import jmri.*; 006 007/** 008 * The parent interface for configuring permissions with Swing. 009 * 010 * @author Daniel Bergqvist Copyright 2024 011 */ 012public interface PermissionSwing { 013 014 /** 015 * Get a label for the permission component. 016 * 017 * @param permission the permission to configure with this component 018 * @return a component that configures the permission or null if no label 019 * @throws IllegalArgumentException if this class does not support the class 020 * with the name given in parameter 'className' 021 */ 022 public default JLabel getLabel(Permission permission) 023 throws IllegalArgumentException { 024 return null; 025 } 026 027 /** 028 * Get a component that configures this permission. 029 * This method initializes the panel with an empty configuration. 030 * 031 * @param role the role 032 * @param permission the permission to configure with this component 033 * @param onChange executes on change, used mainly to set dirty flag 034 * @return a component that configures the permission 035 * @throws IllegalArgumentException if this class does not support the class 036 * with the name given in parameter 'className' 037 */ 038 public JComponent getComponent(Role role, Permission permission, Runnable onChange) 039 throws IllegalArgumentException; 040 041 042 /** 043 * The default swing implementation for BooleanPermission. 044 * The class is abstract since it should never be instantiated directly. 045 */ 046 public static abstract class BooleanPermissionSwing implements PermissionSwing { 047 048 @Override 049 public JComponent getComponent(Role role, Permission permission, Runnable onChange) throws IllegalArgumentException { 050 JCheckBox checkBox = new JCheckBox(permission.getName()); 051 PermissionValue value = role.getPermissionValue(permission); 052 if (!(value instanceof BooleanPermission.BooleanValue)) { 053 throw new IllegalArgumentException("Permission value is not a BooleanValue: " + value.getClass().getName()); 054 } 055 checkBox.setSelected(((BooleanPermission.BooleanValue)value).get()); 056 checkBox.addActionListener((evt) -> { 057 role.setPermission(permission, BooleanPermission.BooleanValue.get(checkBox.isSelected())); 058 onChange.run(); 059 }); 060 return checkBox; 061 } 062 063 } 064 065}