001: /*
002: * HelpView.java - The View of the help (tree on left, text on right)
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.gui;
023:
024: import java.awt.Dimension;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.event.MouseEvent;
028: import java.awt.event.MouseListener;
029: import java.awt.print.PrinterJob;
030: import java.util.Enumeration;
031:
032: import javax.swing.JPanel;
033: import javax.swing.JScrollPane;
034: import javax.swing.JSplitPane;
035: import javax.swing.JTree;
036: import javax.swing.event.HyperlinkEvent;
037: import javax.swing.event.HyperlinkListener;
038: import javax.swing.tree.DefaultTreeModel;
039: import javax.swing.tree.TreePath;
040:
041: import org.objectweb.salome_tmf.api.Util;
042:
043: import salomeTMF_plug.helpgui.page.LinkedPage;
044: import salomeTMF_plug.helpgui.page.Page;
045: import salomeTMF_plug.helpgui.page.PageRoot;
046: import salomeTMF_plug.helpgui.util.BrowserControl;
047: import salomeTMF_plug.helpgui.util.Out;
048:
049: /**
050: * View for the project, contain the tree on the left and the text on the right
051: *
052: * @author Alexandre THOMAS
053: */
054: public class HelpView extends JPanel implements MouseListener,
055: HyperlinkListener {
056:
057: /** Tree on the left. */
058: private JTree tree;
059:
060: /** Panel on the right. */
061: private TextArea textarea;
062:
063: /** Model of the tree. */
064: private DefaultTreeModel model;
065:
066: /** Root of the Tree */
067: private PageRoot pageRoot;
068:
069: /** The linked page to store the list of the viewed pages */
070: LinkedPage linkedPage = new LinkedPage();
071:
072: /** Constructor. */
073: public HelpView() {
074: super ();
075:
076: //Construct the objects
077: pageRoot = new PageRoot();
078: model = new DefaultTreeModel(pageRoot);
079: tree = new JTree(model);
080: textarea = new TextArea();
081: textarea.addHyperlinkListener(this );
082:
083: //Tree parameters
084: tree.setShowsRootHandles(false);
085: tree.setRowHeight(20);
086: tree.setRootVisible(false);
087: tree.setCellRenderer(new TreeRenderer());
088:
089: //A split pane to put the
090: JScrollPane treeScrollPane = new JScrollPane(tree);
091: treeScrollPane.setPreferredSize(new Dimension(180, 550));
092: JScrollPane textAreaScrollPane = new JScrollPane(textarea);
093: textAreaScrollPane.setPreferredSize(new Dimension(470, 550));
094: JSplitPane splitPane = new JSplitPane(
095: JSplitPane.HORIZONTAL_SPLIT, treeScrollPane,
096: textAreaScrollPane);
097: //splitPane.setDividerSize(4);
098: splitPane.setOneTouchExpandable(true);
099:
100: GridBagLayout gbPanel = new GridBagLayout();
101: GridBagConstraints gbcPanel = new GridBagConstraints();
102: setLayout(gbPanel);
103:
104: //Put the split pane on the window
105: gbcPanel.gridx = 0;
106: gbcPanel.gridy = 0;
107: gbcPanel.gridwidth = 1;
108: gbcPanel.gridheight = 1;
109: gbcPanel.fill = GridBagConstraints.BOTH;
110: gbcPanel.weightx = 1;
111: gbcPanel.weighty = 1;
112: gbcPanel.anchor = GridBagConstraints.CENTER;
113: gbPanel.setConstraints(splitPane, gbcPanel);
114: add(splitPane);
115:
116: //Conect the listeners
117: //model.addTreeModelListener(textarea);
118: tree.addMouseListener(this );
119: }
120:
121: /** Get the JTree of the panel */
122: public JTree getJTree() {
123: return tree;
124: }
125:
126: /** Get the TextArea of the panel */
127: public TextArea getTextArea() {
128: return textarea;
129: }
130:
131: /**
132: * Invoked when the mouse button has been clicked (pressed and released) on
133: * a component.
134: */
135: public void mouseClicked(MouseEvent e) {
136: }
137:
138: /** Invoked when the mouse enters a component. */
139: public void mouseEntered(MouseEvent e) {
140: }
141:
142: /** Invoked when the mouse exits a component. */
143: public void mouseExited(MouseEvent e) {
144: }
145:
146: /** Invoked when a mouse button has been pressed on a component. */
147: public void mousePressed(MouseEvent e) {
148: //updateTreeSelection(e);
149: }
150:
151: /** Invoked when a mouse button has been released on a component. */
152: public void mouseReleased(MouseEvent e) {
153: updateTreeSelection(e);
154: }
155:
156: /** Updates tree selection on a mouse event. */
157: public void updateTreeSelection(MouseEvent e) {
158:
159: int selectedRow = tree.getRowForLocation(e.getX(), e.getY());
160:
161: if (selectedRow != -1) {
162: tree.setSelectionRow(selectedRow);
163: TreePath path = tree.getSelectionPath();
164: updatePage((Page) path.getLastPathComponent(), true);
165: } else
166: tree.clearSelection();
167: }
168:
169: /** Go to the home page */
170: public void goHome() {
171: if (!goHomePage(pageRoot.children())) {
172: Out.msg("Be carreful you've any Home page defined");
173: }
174: }
175:
176: /** Recursive function to parse all page to get the home page */
177: private boolean goHomePage(Enumeration e) {
178: while (e.hasMoreElements()) {
179: Page page = (Page) e.nextElement();
180: if (page.getHome()) {
181: updatePage(page, true);
182: return true;
183: }
184: if (goHomePage(page.children()))
185: return true;
186: }
187: return false;
188: }
189:
190: /** Expand the first node of the Tree */
191: public void firstNodeExpand() {
192: Page firstChild = pageRoot.getFirstChild();
193: if (firstChild != null) {
194: Enumeration e = firstChild.children();
195: while (e.hasMoreElements()) {
196: Page child = (Page) e.nextElement();
197: Object path[] = { pageRoot, firstChild, child };
198: tree.scrollPathToVisible(new TreePath(path));
199: }
200: }
201: }
202:
203: /** Set the previous page */
204: public void previousPage() {
205: updatePage(linkedPage.getPreviousPage(), false);
206: }
207:
208: /** Set the next page */
209: public void nextPage() {
210: updatePage(linkedPage.getNextPage(), false);
211: }
212:
213: /** Return the current page */
214: public Page getCurrentPage() {
215: return linkedPage.getCurrentPage();
216: }
217:
218: /** Action when a link is pressed (for HTML listener) */
219: public void hyperlinkUpdate(HyperlinkEvent e) {
220: if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
221: //get the URL
222: String url = e.getURL().toString();
223: Util.log("[HelpView->hyperlinkUpdate]-> will open " + url);
224: //Run the browser for http viewer
225: if (url.startsWith("http://") || url.startsWith("mailto:")
226: || url.startsWith("file:/")) {
227: try {
228: textarea.setPage(url);
229: //BrowserControl.displayURL(url);
230: } catch (Exception ex) {
231: BrowserControl.displayURL(url);
232: }
233: } else //It's perhaps a page on the help toppic
234: {
235:
236: //Serach the page from
237: int ind = url.lastIndexOf('!');
238: url = url.substring(ind + 1, url.length());
239: url = url.replaceFirst(MainPanel.helpPath + "/", "");
240: updatePage(getLinkedPage(pageRoot.children(), url),
241: true);
242:
243: }
244: //Else I don't know what it is
245: }
246: }
247:
248: /** Return the page with the specify url */
249: private Page getLinkedPage(Enumeration e, String url) {
250: while (e.hasMoreElements()) {
251: Page page = (Page) e.nextElement();
252: if (page.getTarget() != null
253: && page.getTarget().equals(url)) {
254: return page;
255: }
256: Page p = getLinkedPage(page.children(), url);
257: if (p != null)
258: return p;
259: }
260: return null;
261: }
262:
263: /**
264: * Set a new page on the browser and and it on the list of the last page
265: * view
266: */
267: public void updatePage(Page page, boolean insert) {
268: if (page == null)
269: return;
270:
271: linkedPage.addPage(page, insert);
272: textarea.update(page);
273: tree.setSelectionPath(page.getPath());
274:
275: //Out.msg (linkedPage.toString());
276: }
277:
278: /** Print the page */
279: public void print() {
280: PrinterJob printJob = PrinterJob.getPrinterJob();
281: printJob.setPrintable(textarea);
282: if (printJob.printDialog()) {
283: try {
284: printJob.print();
285: } catch (Exception ex) {
286: ex.printStackTrace();
287: }
288: }
289: }
290:
291: }
|