diff options
author | Chris Goldthorpe | 2009-06-23 21:00:26 +0000 |
---|---|---|
committer | Chris Goldthorpe | 2009-06-23 21:00:26 +0000 |
commit | 039a7fe0df957563c310c507b897bb1e1118fcd7 (patch) | |
tree | 024829f8bcad3f6f0c32ecb7a8877c940801c543 /org.eclipse.help/src/org/eclipse/help/internal/toc/TopicSorter.java | |
parent | d74fea377c672738f114c8594ab37a51a05b5c4d (diff) | |
download | eclipse.platform.ua-039a7fe0df957563c310c507b897bb1e1118fcd7.tar.gz eclipse.platform.ua-039a7fe0df957563c310c507b897bb1e1118fcd7.tar.xz eclipse.platform.ua-039a7fe0df957563c310c507b897bb1e1118fcd7.zip |
Bug 38052 [Help] Add alphabetical ordering attribute to TOC DTD
Diffstat (limited to 'org.eclipse.help/src/org/eclipse/help/internal/toc/TopicSorter.java')
-rw-r--r-- | org.eclipse.help/src/org/eclipse/help/internal/toc/TopicSorter.java | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/org.eclipse.help/src/org/eclipse/help/internal/toc/TopicSorter.java b/org.eclipse.help/src/org/eclipse/help/internal/toc/TopicSorter.java new file mode 100644 index 000000000..5d90ec301 --- /dev/null +++ b/org.eclipse.help/src/org/eclipse/help/internal/toc/TopicSorter.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * 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(); + + 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]); + } + } + // sort children's children + for (int i=0; i< children.length;++i) { + ITopic child = children[i]; + sort((UAElement)children[i], child.getSubtopics()); + } + } +} + +
\ No newline at end of file |