KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > data > IncompatibleSiblingChecker


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.cheatsheets.data;
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.ui.internal.cheatsheets.Messages;
17 import org.eclipse.ui.internal.cheatsheets.composite.parser.IStatusContainer;
18 import org.w3c.dom.Node JavaDoc;
19
20 /**
21  * This class checks for incompatible children of items and subitems
22  */

23
24 public class IncompatibleSiblingChecker {
25     
26     private IStatusContainer statusContainer;
27     private Node JavaDoc parentNode;
28     private String JavaDoc existingChild;
29     private boolean errorReported = false;
30
31     public IncompatibleSiblingChecker(IStatusContainer statusContainer, Node JavaDoc parentNode) {
32         this.statusContainer = statusContainer;
33         this.parentNode = parentNode;
34     }
35     
36     /**
37      * Check to see that adding this new element does not create an error based on
38      * other elements. The rules are the only one action, command or perform-when can
39      * be declared per item or subitem and none of these can coexist with a subitem,
40      * conditional-subitem or repeated-subitem
41      * @param elementKind
42      */

43     public void checkElement(String JavaDoc elementKind) {
44         if (isExecutable(elementKind)) {
45             if (existingChild == null) {
46                 existingChild = elementKind;
47             } else {
48                 reportIncompatible(elementKind);
49             }
50         } else if (isSubitem(elementKind)) {
51             if (isExecutable(existingChild)) {
52                 reportIncompatible(elementKind);
53             }
54         }
55     }
56
57     private boolean isSubitem(String JavaDoc elementKind) {
58         return IParserTags.SUBITEM.equals(elementKind)
59         || IParserTags.CONDITIONALSUBITEM.equals(elementKind)
60         || IParserTags.REPEATEDSUBITM.equals(elementKind);
61     }
62
63     private boolean isExecutable(String JavaDoc elementKind) {
64         return IParserTags.ACTION.equals(elementKind)
65         || IParserTags.COMMAND.equals(elementKind)
66         || IParserTags.PERFORMWHEN.equals(elementKind);
67     }
68
69     private void reportIncompatible(String JavaDoc elementKind) {
70         if (errorReported) {
71             return; // One message is enough
72
}
73         errorReported = true;
74         String JavaDoc message;
75         if (elementKind.equals(existingChild)) {
76              message = NLS.bind(Messages.ERROR_PARSING_DUPLICATE_CHILD, (new Object JavaDoc[] {parentNode.getNodeName(), elementKind}));
77         } else {
78              message = NLS.bind(Messages.ERROR_PARSING_INCOMPATIBLE_CHILDREN, (new Object JavaDoc[] {parentNode.getNodeName(), existingChild, elementKind}));
79         }
80         statusContainer.addStatus(IStatus.ERROR, message, null);
81     }
82 }
83
Popular Tags