Skip to main content
summaryrefslogtreecommitdiffstats
blob: 102a5c1508db4fb463cc857b2fb4ef9866042a0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.toc;

import java.util.Arrays;
import java.util.Comparator;

import org.eclipse.help.ITopic;
import org.eclipse.help.internal.Topic;
import org.eclipse.help.internal.UAElement;

import com.ibm.icu.text.Collator;

/*
 * Handles the "sort" attribute on topics and tocs
 */
public class TopicSorter {

	private Comparator comparator;

	public void sortChildren(Toc toc) {
		if (comparator == null) {
			comparator = new TopicComparator();
		}
		if (toc.isSorted()) {
			sort(toc, toc.getTopics());
	    }
		ITopic[] childTopics = toc.getTopics();
		for (int i = 0; i < childTopics.length; i++) {
			sortChildren((Topic)childTopics[i]);
		}
	}

	private void sortChildren(Topic topic) {
		if (topic.isSorted()) {
			sort(topic, topic.getSubtopics());
	    }
		ITopic[] childTopics = topic.getSubtopics();
		for (int i = 0; i < childTopics.length; i++) {
			sortChildren((Topic)childTopics[i]);
		}
	}

	private class TopicComparator implements Comparator {
		Collator collator = Collator.getInstance();

		@Override
		public int compare(Object o1, Object o2) {
			String label1 = ((ITopic)o1).getLabel();
			String label2 = ((ITopic)o2).getLabel();
			return collator.compare(label1, label2);
		}
	}

	/*
	 * Sort the given node's descendants recursively using the given
	 * Comparator.
	 */
	private void sort(UAElement element, ITopic[] children) {
		// sort children
		if (children.length > 1) {
			for (int i=0;i<children.length;++i) {
				element.removeChild((UAElement)children[i]);
			}
			Arrays.sort(children, comparator);
			for (int i=0;i<children.length;++i) {
				element.appendChild((UAElement)children[i]);
			}
		}
	}
}

Back to the top