Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 312ba5de4507ad29e50b48036d1ebf36a8984fab (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************
 * Copyright (c) 2011 Red Hat Inc. 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:
 * Red Hat Inc. - Initial implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.cdt.libhover.devhelp;

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

import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.filesystem.IFileSystem;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.help.AbstractTocProvider;
import org.eclipse.help.IToc;
import org.eclipse.help.ITocContribution;
import org.eclipse.help.ITopic;
import org.eclipse.help.IUAElement;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.linuxtools.cdt.libhover.devhelp.DevHelpPlugin;
import org.eclipse.linuxtools.internal.cdt.libhover.devhelp.preferences.PreferenceConstants;

public class DevHelpTocProvider extends AbstractTocProvider {

	public DevHelpTocProvider() {
	}
	
	private class DevhelpTopic implements ITopic {

		private String name;
		
		DevhelpTopic(String name) {
			this.name = name;
		}
		
		@Override
		public boolean isEnabled(IEvaluationContext context) {
			return true;
		}

		@Override
		public IUAElement[] getChildren() {
			return new IUAElement[0];
		}

		@Override
		public String getHref() {
			return "/" + DevHelpPlugin.PLUGIN_ID + "/" + name + "/index.html"; // $NON-NLS-1$ //$NON-NLS-2$" //$NON-NLS-3$
					
		}

		@Override
		public String getLabel() {
			return name;
		}

		@Override
		public ITopic[] getSubtopics() {
			return null;
		}
	}
	
	@Override
	public ITocContribution[] getTocContributions(String locale) {
		// TODO Auto-generated method stub
        ITocContribution contribution = new ITocContribution() {
            public String getId() {
               // a way to identify our book
               return "org.eclipse.linuxtools.cdt.libhover.devhelp.toc"; //$NON-NLS-1$
            }
            public String getCategoryId() {
               // our book does not belong to any category of books
               return null;
            }
            public boolean isPrimary() {
               // this is a primary, top-level contribution (a book)
               return true;
            }
            public IToc getToc() {
            	return new IToc() {
            		public String getLabel() {
            			return "Devhelp Documents"; //$NON-NLS-1$
            		}
            		public String getHref() {
            			return null;
            		}
            		@Override
            		public boolean isEnabled(IEvaluationContext context) {
            			// TODO Auto-generated method stub
            			return true;
            		}
            		@Override
            		public IUAElement[] getChildren() {
            			// TODO Auto-generated method stub
            			return getTopics();
            		}
            		@Override
            		public ITopic[] getTopics() {
            			try {
            				ArrayList<ITopic> topics = new ArrayList<ITopic>();
            				IPreferenceStore ps = DevHelpPlugin.getDefault().getPreferenceStore();
            				IPath devhelpLocation = new Path(ps.getString(PreferenceConstants.DEVHELP_DIRECTORY));
            				IFileSystem fs = EFS.getLocalFileSystem();
            				IFileStore htmlDir = fs.getStore(devhelpLocation);
            				IFileStore[] files = htmlDir.childStores(EFS.NONE, null);
            				Arrays.sort(files, new Comparator<IFileStore>() {

            					@Override
            					public int compare(IFileStore arg0, IFileStore arg1) {
            						return (arg0.getName().compareToIgnoreCase(arg1.getName()));
            					}
            					
            				});
            				for (int i = 0; i < files.length; ++i) {
            					IFileStore file = files[i];
            					String name = file.fetchInfo().getName();
            					ITopic topic = new DevhelpTopic(name);
            					topics.add(topic);
            				}
            				ITopic[] retval = new ITopic[topics.size()];
            				return topics.toArray(retval);
            			} catch (CoreException e) {
             			}
            			return null;
            		}
            		@Override
            		public ITopic getTopic(String href) {
            			// TODO Auto-generated method stub
            			return null;
            		}
            	};
            }
            public String getLocale() {
            	// this provider only provides content for the en_US locale
            	return "en_US"; //$NON-NLS-1$
            }
            public String[] getExtraDocuments() {
            	// there are no extra documents associated with this book
            	return new String[0];
            }
            public String getLinkTo() {
            	return "../org.eclipse.linuxtools.cdt.libhover.library.docs/libraries.xml#devhelpdocs";
            }
            @Override
            public String getContributorId() {
            	// TODO Auto-generated method stub
            	return "org.eclipse.linuxtools.cdt.libhover.devhelp"; //$NON-NLS-1$
            }
        };
        return new ITocContribution[] { contribution };
	}

}

Back to the top