KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > comp > CompCSGroupValidator


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.pde.internal.ui.editor.cheatsheet.comp;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.jface.dialogs.IMessageProvider;
18 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCS;
19 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSConstants;
20 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup;
21 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject;
22 import org.eclipse.pde.internal.core.util.PDETextHelper;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.ui.forms.widgets.Form;
25
26 /**
27  * CompCSTreeValidator
28  *
29  */

30 public class CompCSGroupValidator {
31
32     private Form fForm;
33
34     private HashSet JavaDoc fGroups;
35     
36     private String JavaDoc fErrorCategory;
37     
38     // TODO: MP: LOW: CompCS: Can augment the model to have isValid() methods to simplify validation
39

40     /**
41      *
42      */

43     public CompCSGroupValidator(ICompCS cheatsheet, Form form,
44             String JavaDoc errorCategory) {
45         fForm = form;
46         fErrorCategory = errorCategory;
47         
48         fGroups = new HashSet JavaDoc();
49         populateGroups(cheatsheet);
50     }
51
52     /**
53      * @param cheatsheet
54      */

55     private void populateGroups(ICompCS cheatsheet) {
56         // Register all existing groups in the present workspace model to be
57
// validated
58
if (cheatsheet.getFieldTaskObject().getType() ==
59             ICompCSConstants.TYPE_TASKGROUP) {
60             addGroup((ICompCSTaskGroup)cheatsheet.getFieldTaskObject());
61         }
62     }
63
64     /**
65      * @param group
66      */

67     public void addGroup(ICompCSTaskGroup group) {
68         fGroups.add(group);
69         // Check to see if the group has any children
70
if (group.hasFieldTaskObjects() == false) {
71             return;
72         }
73         // Recursively add any sub-groups
74
ICompCSTaskObject[] taskObjects = group.getFieldTaskObjects();
75         for (int i = 0; i < taskObjects.length; i++) {
76             if (taskObjects[i].getType() == ICompCSConstants.TYPE_TASKGROUP) {
77                 addGroup((ICompCSTaskGroup)taskObjects[i]);
78             }
79         }
80     }
81     
82     /**
83      * @param group
84      */

85     public void removeGroup(ICompCSTaskGroup group) {
86         fGroups.remove(group);
87         // Check to see if the group has any children
88
if (group.hasFieldTaskObjects() == false) {
89             return;
90         }
91         // Recursively remove any sub-groups
92
ICompCSTaskObject[] taskObjects = group.getFieldTaskObjects();
93         for (int i = 0; i < taskObjects.length; i++) {
94             if (taskObjects[i].getType() == ICompCSConstants.TYPE_TASKGROUP) {
95                 removeGroup((ICompCSTaskGroup)taskObjects[i]);
96             }
97         }
98     }
99     
100     /**
101      * @return
102      */

103     public boolean validate() {
104         // Check to see if there is anything to validate
105
if (fGroups.isEmpty()) {
106             fForm.setMessage(null);
107             return true;
108         }
109         Iterator JavaDoc iterator = fGroups.iterator();
110         // Validate all registered groups
111
while (iterator.hasNext()) {
112             ICompCSTaskGroup group = (ICompCSTaskGroup)iterator.next();
113             if (validate(group) == false) {
114                 return false;
115             }
116         }
117         fForm.setMessage(null);
118         return true;
119     }
120     
121     /**
122      * @param group
123      * @return
124      */

125     private boolean validate(ICompCSTaskGroup group) {
126         if (group.getFieldTaskObjectCount() == 0) {
127             String JavaDoc message = '[' +
128                              fErrorCategory +
129                              ']' +
130                              ' ' +
131                              PDETextHelper.translateReadText(
132                                      group.getFieldName()) +
133                              ':' +
134                              ' ' +
135                              PDEUIMessages.CompCSGroupValidator_errorChildlessGroup;
136             fForm.setMessage(message, IMessageProvider.INFORMATION);
137             return false;
138         }
139         return true;
140     }
141     
142 }
143
Popular Tags