001: /*
002: * Page.java - List of page for the help topic
003: * Copyright (C) 2003 Alexandre THOMAS
004: * alexthomas@free.fr
005: * http://helpgui.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package salomeTMF_plug.helpgui.page;
023:
024: import java.util.ArrayList;
025: import java.util.Enumeration;
026: import java.util.Hashtable;
027: import java.util.Iterator;
028:
029: import javax.swing.tree.MutableTreeNode;
030: import javax.swing.tree.TreeNode;
031: import javax.swing.tree.TreePath;
032:
033: import org.objectweb.salome_tmf.api.Util;
034:
035: /**
036: * Class where the date are showed
037: *
038: * @author Alexandre THOMAS
039: */
040: public class Page implements MutableTreeNode {
041:
042: /** Title of the page. */
043: private String text = null;
044:
045: /** The image icon uri. */
046: private String image = null;
047:
048: /** Target url for the page. */
049: private String target = null;
050:
051: /** Target signet for the page. */
052: private String signet = null;
053:
054: private String link = null;
055:
056: /** subpages list */
057: protected PageList pages;
058:
059: /** project this page belong to */
060: protected PageRoot rootPage = null;
061:
062: /** Page this page is a child of */
063: protected Page parentPage = null;
064:
065: /** User object */
066: protected Object userObject = null;
067:
068: /** If this page is home */
069: protected boolean home = false;
070:
071: //static private String root = null;
072:
073: protected String rootSuffixe;
074:
075: boolean plugins = false;
076:
077: String pluginsName = "";
078:
079: static Hashtable rootBranch = new Hashtable();
080:
081: ////////////////////////////////////////////////////////////////////
082:
083: /** Constructor */
084: public Page(String text, String image, String target, boolean home,
085: Page parent, boolean plugins, String pluginsName) {
086: this .plugins = plugins;
087: this .pluginsName = pluginsName;
088: this .text = text;
089: this .image = image;
090: this .link = target;
091: if (target != null) {
092: Util.log("[helpgui:Page] Parse parent target " + target);
093: int begin_signet = target.indexOf("#");
094: if (begin_signet == -1) {
095: this .target = target;
096: } else {
097: this .signet = target.substring(begin_signet + 1, target
098: .length());
099: this .target = target.substring(0, begin_signet);
100: Util.log("[helpgui:Page] found signet " + this .signet);
101: }
102: //System.out.println("Target is " + this.target);
103: }
104: this .parentPage = parent;
105: pages = new PageList();
106: this .home = home;
107: }
108:
109: /** Constructor */
110: public Page(String text, String image, String target, boolean home,
111: PageRoot root, boolean plugins, String pluginsName) {
112: this .plugins = plugins;
113: this .pluginsName = pluginsName;
114: this .text = text;
115: this .image = image;
116: this .link = target;
117: Object pRoot = rootBranch.get(text);
118: if (pRoot != null) {
119: Util.log("[helpgui:Page] Use root target " + text);
120: this .rootPage = (PageRoot) pRoot;
121: } else {
122: Util.log("[helpgui:Page] Add root target " + text);
123: this .rootPage = root;
124: rootBranch.put(text, root);
125: }
126:
127: if (target != null) {
128: Util.log("[helpgui:Page] Parse root target " + target);
129: int begin_signet = target.indexOf("#");
130: if (begin_signet == -1) {
131: this .target = target;
132: } else {
133: this .signet = target.substring(begin_signet + 1, target
134: .length());
135: this .target = target.substring(0, begin_signet);
136: //System.out.println("Signet found : " + this.signet);
137: }
138: //System.out.println("Target is " + this.target);
139: }
140:
141: pages = new PageList();
142: this .home = home;
143: }
144:
145: /** Return true is the page is the same. */
146: public boolean equals(Page page) {
147: return page.text.equals(text) && page.image.equals(image)
148: && page.target.equals(target);
149: }
150:
151: /** Return the Title of the Page. */
152: public String getText() {
153: return text;
154: }
155:
156: /** Return the icon name for this page. */
157: public String getImage() {
158: return image;
159: }
160:
161: /** Return the target of the page (the HTML URI). */
162: public String getTarget() {
163: return target;
164: }
165:
166: /** Return the target of the page (the HTML URI). */
167: public String getSignet() {
168: return signet;
169: }
170:
171: /** Return the target of the page (the HTML URI). */
172: public String getLink() {
173: return link;
174: }
175:
176: /** Convert the Page into String. */
177: public String toString() {
178: return text;
179: }
180:
181: /** Convert the Page into String. */
182: public boolean getHome() {
183: return home;
184: }
185:
186: public boolean isPluginPage() {
187: return plugins;
188: }
189:
190: public String getPluginsName() {
191: return pluginsName;
192: }
193:
194: /// MutableTreeNode Implementation /////////////////////////////////
195:
196: /** Adds child to the receiver at index. */
197: public void insert(MutableTreeNode child, int index) {
198: if (child.getClass() == Page.class)
199: addSubPage((Page) child);
200: }
201:
202: /** Removes the child at index from the receiver. */
203: public void remove(int index) {
204: if ((0 < index) && (index < pages.size()))
205: remove(pages.get(index));
206: }
207:
208: /** Removes node from the receiver. */
209: public void remove(MutableTreeNode node) {
210: // can deal only with pages
211: /*
212: * if (node.getClass() == Page.class) { Page page = (Page)node; //
213: * removes the page from the tree removeSubPage(page); }
214: */
215: }
216:
217: /** Removes the receiver from its parent. */
218: public void removeFromParent() {
219: if (null != parentPage)
220: parentPage.remove(this );
221: else if (null != rootPage)
222: rootPage.remove(this );
223: }
224:
225: /** Sets the parent of the receiver to newParent. */
226: public void setParent(MutableTreeNode newParent) {
227: if (newParent.getClass() == Page.class) {
228: // removes from old parent
229: removeFromParent();
230: // sets the new parent for this page
231: parentPage = (Page) newParent;
232: // adds this to its new parent
233: ((Page) newParent).addSubPage(this );
234: } else if (newParent.getClass() == PageRoot.class) {
235: // removes from old parent
236: removeFromParent();
237: // sets the new parent for this page
238: rootPage = (PageRoot) newParent;
239: // adds this to its new parent
240: ((PageRoot) newParent).add(this );
241: }
242: }
243:
244: /** Resets the user object of the receiver to object. */
245: public void setUserObject(Object object) {
246: userObject = object;
247: }
248:
249: /** Returns the child TreeNode at index childIndex. */
250: public TreeNode getChildAt(int iChildIndex) {
251: return pages.get(iChildIndex);
252: }
253:
254: /** Returns the number of children TreeNodes the receiver contains. */
255: public int getChildCount() {
256: return pages.size();
257: }
258:
259: /** Returns the parent TreeNode of the receiver. */
260: public TreeNode getParent() {
261: if (null != parentPage)
262: return parentPage;
263: return rootPage;
264: }
265:
266: /** Returns the index of node in the receivers children. */
267: public int getIndex(TreeNode node) {
268: if (node.getClass() != Page.class)
269: return -1;
270: return pages.getIndex((Page) node);
271: }
272:
273: /** Returns true if the receiver is a leaf. */
274: public boolean isLeaf() {
275: return pages.isEmpty();
276: }
277:
278: /** Returns true if the receiver allows children. */
279: public boolean getAllowsChildren() {
280: return true;
281: }
282:
283: /** Returns the children of the receiver as an Enumeration. */
284: public Enumeration children() {
285: return new PageEnumeration(pages);
286: }
287:
288: /** Return the path of the Page on the Tree */
289: public TreePath getPath() {
290: MutableTreeNode node = (MutableTreeNode) getParent();
291: ArrayList pathList = new ArrayList();
292: pathList.add(this );
293:
294: while (node != null) {
295: pathList.add(node);
296: node = (MutableTreeNode) node.getParent();
297: }
298:
299: Object[] path = new Object[pathList.size()];
300:
301: int i = pathList.size() - 1;
302: for (Iterator it = pathList.iterator(); it.hasNext(); --i)
303: path[i] = it.next();
304:
305: pathList = null;
306: return new TreePath(path);
307: }
308:
309: /**
310: * Adds a sub page to this page. This function is for internal use only. A
311: * subpage "A" is added automatically to a page "B" when "A"'s constructor
312: * is called given "B" as parameter.
313: */
314: protected void addSubPage(Page page) {
315: // check if not already in the list
316: if (pages.indexOf(page) < 0) {
317: pages.add(page);
318: }
319: }
320:
321: }
|