001package jmri.jmrit.display; 002 003import jmri.*; 004 005import org.openide.util.lookup.ServiceProvider; 006 007/** 008 * Permissions for panels. 009 * 010 * @author Daniel Bergqvist (C) 2024 011 */ 012public class EditorPermissions { 013 014 public static final PermissionOwner EDITOR_PERMISSION_OWNER = 015 new EditorPermissionOwner(); 016 017 public static final Permission EDITOR_PERMISSION = 018 new EditorPermission(); 019 020 021 @ServiceProvider(service = PermissionFactory.class) 022 public static class Factory implements PermissionFactory { 023 024 @Override 025 public void register(PermissionManager manager) { 026 manager.registerOwner(EDITOR_PERMISSION_OWNER); 027 manager.registerPermission(EDITOR_PERMISSION); 028 } 029 030 } 031 032 033 public static class EditorPermissionOwner implements PermissionOwner { 034 035 @Override 036 public String getName() { 037 return Bundle.getMessage("EditorPermissions_EditorPermissionOwner"); 038 } 039 040 } 041 042 043 public static class EditorPermission implements EnumPermission<EditorPermissionEnum> { 044 045 @Override 046 public EditorPermissionEnum[] getValues() { 047 return EditorPermissionEnum.values(); 048 } 049 050 @Override 051 public PermissionOwner getOwner() { 052 return EDITOR_PERMISSION_OWNER; 053 } 054 055 @Override 056 public String getName() { 057 return Bundle.getMessage("EditorPermissions_EditorPermission"); 058 } 059 060 @Override 061 public String getValue(PermissionValue value) { 062 if (!(value instanceof EditorPermissionEnum)) { 063 throw new IllegalArgumentException("value is not a EditorPermissionEnum: " 064 + (value != null ? value.getClass().getName() : "null")); 065 } 066 return ((EditorPermissionEnum)value).name(); 067 } 068 069 @Override 070 public PermissionValue valueOf(String value) { 071 // Temporary fix due to change during 5.9.5 development. 072 // Can be removed once 5.9.5 is merged. 073 if ("Read".equals(value)) value = "View"; 074 if ("ReadWrite".equals(value)) value = "ViewControl"; 075 if ("ReadWriteEdit".equals(value)) value = "ViewControlEdit"; 076 // Temporary fix due to change during 5.9.5 development. 077 // Can be removed once 5.9.5 is merged. 078 079 return EditorPermissionEnum.valueOf(value); 080 } 081 082 @Override 083 public PermissionValue getDefaultPermission() { 084 return EditorPermissionEnum.None; 085 } 086 087 @Override 088 public PermissionValue getDefaultPermission(Role role) { 089 return EditorPermissionEnum.ViewControlEdit; 090 } 091 092 @Override 093 public int compare(PermissionValue o1, PermissionValue o2) { 094 if (o1 instanceof EditorPermissionEnum && o2 instanceof EditorPermissionEnum) { 095 EditorPermissionEnum ep1 = (EditorPermissionEnum) o1; 096 EditorPermissionEnum ep2 = (EditorPermissionEnum) o2; 097 return Integer.compare(ep1.ordinal(), ep2.ordinal()); 098 } else { 099 throw new IllegalArgumentException("Cannot compare o1 and o2 since one or both is not an EditorPermissionEnum"); 100 } 101 } 102 103 } 104 105 public static enum EditorPermissionEnum implements PermissionValue { 106 Default(true, Bundle.getMessage("EditorPermissions_EditorPermission_Default")), 107 None(false, Bundle.getMessage("EditorPermissions_EditorPermission_None")), 108 View(false, Bundle.getMessage("EditorPermissions_EditorPermission_View")), 109 ViewControl(false, Bundle.getMessage("EditorPermissions_EditorPermission_ViewControl")), 110 ViewControlEdit(false, Bundle.getMessage("EditorPermissions_EditorPermission_ViewControlEdit")); 111 112 final boolean _isDefault; 113 final String _text; 114 115 private EditorPermissionEnum(boolean isDefault, String text) { 116 this._isDefault = isDefault; 117 this._text = text; 118 } 119 120 @Override 121 public boolean isDefault() { 122 return _isDefault; 123 } 124 125 @Override 126 public String toString() { 127 return _text; 128 } 129 130 } 131 132}