KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > webapp > data > EnabledTopicUtils


1 /*******************************************************************************
2  * Copyright (c) 2007 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.help.internal.webapp.data;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.help.IIndexEntry;
18 import org.eclipse.help.ITopic;
19 import org.eclipse.help.UAContentFilter;
20 import org.eclipse.help.internal.base.HelpEvaluationContext;
21
22 /**
23  * Utilities to test for enabled topics, index entries etc.
24  */

25
26 public class EnabledTopicUtils {
27
28     /**
29      * Test whether a topic is enabled
30      * @param topic
31      * @return
32      */

33     public static boolean isEnabled(ITopic topic) {
34         if (!topic.isEnabled(HelpEvaluationContext.getContext())) {
35             return false;
36         }
37         if (topic.getHref() != null) {
38             return true;
39         }
40         // A topic without an href is enabled only if at least one child is enabled
41
ITopic[] subtopics = topic.getSubtopics();
42         for (int i = 0; i < subtopics.length; i++) {
43             if (isEnabled(subtopics[i])) {
44                 return true;
45             }
46         }
47         return false;
48     }
49     
50     /**
51      * Test whether an entry is enabled
52      * @param entry
53      * @return
54      */

55     public static boolean isEnabled(IIndexEntry entry) {
56         if (UAContentFilter.isFiltered(entry, HelpEvaluationContext.getContext())) {
57             return false;
58         }
59         ITopic[] topics = entry.getTopics();
60         for (int i=0;i<topics.length;++i) {
61             if (isEnabled(topics[i])) {
62                 return true;
63             }
64         }
65         IIndexEntry[] subentries = entry.getSubentries();
66         for (int i=0;i<subentries.length;++i) {
67             if (isEnabled(subentries[i])) {
68                 return true;
69             }
70         }
71         return false;
72     }
73
74     /**
75      * Filter out any disabled entries from an array
76      * @param entries an array of entries
77      * @return an array containing only those entries which are enabled
78      */

79     public static IIndexEntry[] getEnabled(IIndexEntry[] entries) {
80         for (int i=0;i<entries.length;++i) {
81             if (!isEnabled(entries[i])) {
82                 List JavaDoc list = new ArrayList JavaDoc(entries.length);
83                 for (int j=0;j<entries.length;++j) {
84                     if (j < i || isEnabled(entries[j])) {
85                         list.add(entries[j]);
86                     }
87                 }
88                 return (IIndexEntry[])list.toArray(new IIndexEntry[list.size()]);
89             }
90         }
91         return entries;
92     }
93
94     /**
95      * Filter out any disable topics form an array
96      * @param topics an array of topics
97      * @return an array containing only those topics which are enabled
98      */

99     public static ITopic[] getEnabled(ITopic[] topics) {
100         for (int i=0;i<topics.length;++i) {
101             if (!isEnabled(topics[i])) {
102                 List JavaDoc list = new ArrayList JavaDoc(topics.length);
103                 for (int j=0;j<topics.length;++j) {
104                     if (j < i || isEnabled(topics[j])) {
105                         list.add(topics[j]);
106                     }
107                 }
108                 return (ITopic[])list.toArray(new ITopic[list.size()]);
109             }
110         }
111         return topics;
112     }
113
114 }
115
Popular Tags