1 package cz
.cvut
.promod
.services
.menuService
;
3 import cz
.cvut
.promod
.services
.actionService
.actionUtils
.ProModAction
;
4 import cz
.cvut
.promod
.services
.menuService
.utils
.MenuItemPosition
;
5 import org
.apache
.log4j
.Logger
;
11 * Created by IntelliJ IDEA.
15 * To change this template use File | Settings | File Templates.
17 public class MenuValidator
{
18 private final Logger LOG
= Logger
.getLogger(MenuControlServiceImpl
.class);
20 protected boolean hasCorrectParentMenu(JMenu parentMenu
, JMenuBar parentMenuBar
, JPopupMenu parentPopupMenu
) {
21 if((parentMenu
!= null && parentMenuBar
!= null)
22 || (parentMenu
!= null && parentPopupMenu
!= null)
23 || (parentMenuBar
!= null && parentPopupMenu
!= null)){
24 LOG
.error("Obscurity in parent of menu item");
31 protected boolean isItemLocationValid(String
[] placementParts
) {
32 if(placementParts
.length
< 1){
33 LOG
.error("Illegal menu item location.");
39 protected boolean isPopupSupported(String notationIdentifier
, JPopupMenu popupMenu
) {
40 if(popupMenu
== null){
41 LOG
.error("Notation (" + notationIdentifier
+") doesn't support popup menu.");
47 protected boolean itemHasOnlyOneParent(JMenu parentMenu
, JPopupMenu parentPopupMenu
) {
48 if(parentMenu
!= null && parentPopupMenu
!= null){
49 LOG
.error("Obscurity in menu item's parent. - There can be just one parent menu item for new menu item insertion.");
55 protected boolean isActionDefined(ProModAction proModAction
) {
56 if(proModAction
== null){
57 LOG
.error("Nullary action to be inserted.");
60 if(proModAction
.getValue(Action
.NAME
) == null || ((String
)proModAction
.getValue(Action
.NAME
)).isEmpty()){
61 LOG
.error("Action with no name to be inserted");
67 protected boolean isMenuItemPositionSet(MenuItemPosition menuItemPosition
) {
68 if(menuItemPosition
== null || menuItemPosition
.getHierarchicalPlacement() == null){
69 LOG
.error("Nullary MenuItemPosition info.");
76 * Checks the menu item placement parts not to be null or an empty strings.
78 * @param placementParts is an array representing hierarchical menu item placement
79 * @return true if all the placement parts are correct, false otherwise
81 protected boolean areValidPlacementParts(final String
[] placementParts
) {
82 for(final String placement
: placementParts
){
83 if(placement
== null || placement
.isEmpty()){
84 LOG
.error("Invalid any/all part(s) of MenuItemPosition info.");
93 * Checks whether no the same action has been already inserted under the same menu parent.
95 * @param parentMenu is to menu parent
96 * @param parentPopupMenu is the popup menu parent
97 * @param proModAction is the action supposed to be inserted
98 * @return true if there is no the same action under the parent, false otherwise
100 protected boolean isUniqueAction(final JMenu parentMenu
,
101 final JPopupMenu parentPopupMenu
,
102 final ProModAction proModAction
) {
104 final Component
[] menuComponets
;
105 if(parentMenu
!= null){
106 menuComponets
= parentMenu
.getMenuComponents();
107 } else if(parentPopupMenu
!= null){
108 menuComponets
= parentPopupMenu
.getComponents();
110 LOG
.error("Parent of menu item cannot be determined.");
114 for(final Component component
: menuComponets
){
115 if(component
instanceof JMenuItem
){
116 final JMenuItem menuItem
= (JMenuItem
) component
;
117 final Action action
= menuItem
.getAction();
119 if(action
!= null && action
== proModAction
){
120 LOG
.error("Inserting the same action to the one menu more than once.");
131 * This function protect to add MenuSeparator next to an other MenuSeparator
133 * @param menuComponents menu items
134 * @param insertionPosition required menu item position
135 * @param menuSeparator menu separator info if one is supposed to be insert
136 * @return true if there could be the separator added
138 protected boolean isSeparatorAlreadyDefinedAtPosition(final Component
[] menuComponents
,
139 final int insertionPosition
,
140 final MenuService
.MenuSeparator menuSeparator
) {
141 if(menuSeparator
!= null){
142 if (isSeparatorAtPosition(menuComponents
, insertionPosition
)) return true;
143 switch(menuSeparator
){
145 if (isSeparatorAtPosition(menuComponents
, insertionPosition
+ 1)) return true;
148 if (isSeparatorAtPosition(menuComponents
, insertionPosition
- 1)) return true;
158 private boolean isSeparatorAtPosition(Component
[] menuComponents
, int insertionPosition
) {
159 if(isValidMenuPosition(insertionPosition
, menuComponents
.length
)){
160 if(menuComponents
[insertionPosition
] instanceof JPopupMenu
.Separator
){
168 * @param position menu position
169 * @param menuItems menu items
170 * @return true if the given position is valid
172 protected boolean isValidMenuPosition(final int position
, final int menuItems
){
173 return !(0 > position
|| position
> (menuItems
- 1));