1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
9 | *
|
---|
10 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | *########################################################################
|
---|
26 | */
|
---|
27 | package org.greenstone.gatherer.help;
|
---|
28 |
|
---|
29 | import java.io.*;
|
---|
30 | import java.util.HashMap;
|
---|
31 | import javax.swing.tree.DefaultTreeModel;
|
---|
32 | import org.greenstone.gatherer.cdm.CommandTokenizer;
|
---|
33 | import org.greenstone.gatherer.help.HelpItem;
|
---|
34 | import org.greenstone.gatherer.util.Utility;
|
---|
35 | /** This is a hardcoded tree model which serves as the help pages contents page.
|
---|
36 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
37 | * @version 2.3
|
---|
38 | */
|
---|
39 | public class ContentModel
|
---|
40 | extends DefaultTreeModel {
|
---|
41 | /** Constructor that builds the contents model.
|
---|
42 | * @see org.greenstone.gatherer.help.HelpItem
|
---|
43 | */
|
---|
44 | public ContentModel() {
|
---|
45 | super(new HelpItem("Contents"));
|
---|
46 | // Load the contents file and build the contents structure found within.
|
---|
47 | try {
|
---|
48 | FileReader file_reader = new FileReader(Utility.HELP_DIR + "index.txt");
|
---|
49 | BufferedReader buffered_reader = new BufferedReader(file_reader);
|
---|
50 | String line = null;
|
---|
51 | HashMap known_entries = new HashMap();
|
---|
52 | while((line = buffered_reader.readLine()) != null) {
|
---|
53 | // We'll use my ever popular command tokenizer to ensure we can parse index | title.
|
---|
54 | CommandTokenizer tokenizer = new CommandTokenizer(line);
|
---|
55 | // The first thing off the tokenizer is an index of the form xx.xx.xx
|
---|
56 | String index_str = tokenizer.nextToken();
|
---|
57 | // And the second is a title for this entry
|
---|
58 | String title = tokenizer.nextToken();
|
---|
59 | // Now we determine the parent of this entry. In general this involves removing the last portion of the index so 2.3 becomes 2. However there is a special case when the index has only one part, ie 2, in which case it get added to the root, then the index is replaced with 2.0.
|
---|
60 | int position = -1;
|
---|
61 | if((position = index_str.lastIndexOf(".")) != -1) {
|
---|
62 | // Find parent index
|
---|
63 | String parent_index = index_str.substring(0, position);
|
---|
64 | // Retrieve parent item
|
---|
65 | HelpItem parent_item = (HelpItem) known_entries.get(parent_index);
|
---|
66 | // Otherwise append ".0" and search again...
|
---|
67 | if(parent_item == null) {
|
---|
68 | parent_item = (HelpItem) known_entries.get(parent_index + ".0");
|
---|
69 | }
|
---|
70 | // Otherwise the parent item must be the root.
|
---|
71 | if(parent_item == null) {
|
---|
72 | parent_item = (HelpItem) root;
|
---|
73 | }
|
---|
74 | // Filename has - instead of .
|
---|
75 | String file_name = index_str.replace('.', '-');
|
---|
76 | // Create the new item
|
---|
77 | HelpItem item;
|
---|
78 | File temp_file = new File(Utility.HELP_DIR + file_name + ".xml");
|
---|
79 | if(temp_file.exists()) {
|
---|
80 | item = new HelpItem(index_str + " " + title, Utility.HELP_DIR + file_name + ".xml");
|
---|
81 | }
|
---|
82 | else {
|
---|
83 | item = new HelpItem(index_str + " " + title, Utility.HELP_DIR + file_name + ".htm");
|
---|
84 | }
|
---|
85 | temp_file = null;
|
---|
86 | // Insert into parent
|
---|
87 | insertNodeInto(item, parent_item, parent_item.getChildCount());
|
---|
88 | // Record in known entries
|
---|
89 | known_entries.put(index_str, item);
|
---|
90 | item = null;
|
---|
91 | file_name = null;
|
---|
92 | parent_item = null;
|
---|
93 | parent_index = null;
|
---|
94 | }
|
---|
95 | title = null;
|
---|
96 | index_str = null;
|
---|
97 | tokenizer = null;
|
---|
98 | }
|
---|
99 | known_entries.clear();
|
---|
100 | known_entries = null;
|
---|
101 | line = null;
|
---|
102 | buffered_reader.close();
|
---|
103 | buffered_reader = null;
|
---|
104 | file_reader.close();
|
---|
105 | file_reader = null;
|
---|
106 | }
|
---|
107 | catch (Exception error) {
|
---|
108 | System.err.println("Cannot build help contents. Possible error in index.txt");
|
---|
109 | error.printStackTrace();
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|