New Jide License
[indepmod/experimental.git] / IndependentModeler / src / cz / cvut / promod / services / menuService / MenuValidator.java
blob94b6add18fe0bc014430fec3d639e04d16ebd9e0
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;
7 import javax.swing.*;
8 import java.awt.*;
10 /**
11 * Created by IntelliJ IDEA.
12 * User: Ondra
13 * Date: 22.8.2010
14 * Time: 12:39:09
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");
25 return false;
28 return true;
31 protected boolean isItemLocationValid(String[] placementParts) {
32 if(placementParts.length < 1){
33 LOG.error("Illegal menu item location.");
34 return false;
36 return true;
39 protected boolean isPopupSupported(String notationIdentifier, JPopupMenu popupMenu) {
40 if(popupMenu == null){
41 LOG.error("Notation (" + notationIdentifier +") doesn't support popup menu.");
42 return false;
44 return true;
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.");
50 return false;
52 return true;
55 protected boolean isActionDefined(ProModAction proModAction) {
56 if(proModAction == null){
57 LOG.error("Nullary action to be inserted.");
58 return false;
60 if(proModAction.getValue(Action.NAME) == null || ((String)proModAction.getValue(Action.NAME)).isEmpty()){
61 LOG.error("Action with no name to be inserted");
62 return false;
64 return true;
67 protected boolean isMenuItemPositionSet(MenuItemPosition menuItemPosition) {
68 if(menuItemPosition == null || menuItemPosition.getHierarchicalPlacement() == null){
69 LOG.error("Nullary MenuItemPosition info.");
70 return false;
72 return true;
75 /**
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.");
85 return false;
89 return true;
92 /**
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();
109 } else {
110 LOG.error("Parent of menu item cannot be determined.");
111 return false;
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.");
121 return false;
127 return true;
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){
144 case AFTER:
145 if (isSeparatorAtPosition(menuComponents, insertionPosition + 1)) return true;
146 break;
147 case BEFORE:
148 if (isSeparatorAtPosition(menuComponents, insertionPosition - 1)) return true;
149 break;
150 default:
151 return false;
155 return false;
158 private boolean isSeparatorAtPosition(Component[] menuComponents, int insertionPosition) {
159 if(isValidMenuPosition(insertionPosition, menuComponents.length)){
160 if(menuComponents[insertionPosition] instanceof JPopupMenu.Separator){
161 return true;
164 return false;
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));