001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.help;
020:
021: import java.awt.Component;
022: import java.awt.Dimension;
023: import java.awt.Font;
024: import java.awt.Frame;
025: import java.awt.Point;
026: import java.awt.Window;
027: import java.lang.reflect.Method;
028: import java.net.URL;
029: import java.util.Locale;
030:
031: import javax.help.BadIDException;
032: import javax.help.DefaultHelpBroker;
033: import javax.help.HelpSet;
034: import javax.help.InvalidHelpSetContextException;
035: import javax.help.JHelp;
036: import javax.help.Presentation;
037: import javax.help.WindowPresentation;
038: import javax.help.Map.ID;
039: import javax.swing.JFrame;
040:
041: public class JETAHelpBroker extends DefaultHelpBroker {
042: private WindowPresentation m_mw = null;
043:
044: /**
045: * Constructor
046: */
047: public JETAHelpBroker(HelpSet hs) {
048: super (null);
049: m_mw = new MyWindowPresentation(hs);
050: }
051:
052: public ID getCurrentID() {
053: return m_mw.getCurrentID();
054: }
055:
056: public URL getCurrentURL() {
057: return m_mw.getCurrentURL();
058: }
059:
060: public HelpSet getHelpSet() {
061: return m_mw.getHelpSet();
062: }
063:
064: public Locale getLocale() {
065: return m_mw.getLocale();
066: }
067:
068: public Font getFont() {
069: return m_mw.getFont();
070: }
071:
072: public String getCurrentView() {
073: return m_mw.getCurrentView();
074: }
075:
076: public void initPresentation() {
077: m_mw.createHelpWindow();
078: }
079:
080: public boolean isDisplayed() {
081: return m_mw.isDisplayed();
082: }
083:
084: public Point getLocation() {
085: return m_mw.getLocation();
086: }
087:
088: public Dimension getSize()
089: throws javax.help.UnsupportedOperationException {
090: return m_mw.getSize();
091: }
092:
093: public boolean isViewDisplayed() {
094: return m_mw.isViewDisplayed();
095: }
096:
097: private Presentation getPresentation(String presentation,
098: String presentationName) {
099: Presentation pres;
100:
101: HelpSet hs = m_mw.getHelpSet();
102: if (hs == null) {
103: return null;
104: }
105:
106: ClassLoader loader;
107: Class klass;
108: Class types[] = { HelpSet.class, String.class };
109: Object params[] = { hs, presentationName };
110: try {
111: loader = hs.getLoader();
112: if (loader == null) {
113: klass = Class.forName(presentation);
114: } else {
115: klass = loader.loadClass(presentation);
116: }
117: Method m = klass.getMethod("getPresentation", types);
118: pres = (Presentation) m.invoke(null, params);
119: } catch (ClassNotFoundException cnfex) {
120: throw new IllegalArgumentException(presentation + "error");
121: } catch (Exception ex) {
122: throw new RuntimeException("unable to invoke presentation");
123: }
124:
125: if (pres == null || pres instanceof javax.help.Popup) {
126: return null;
127: }
128: return pres;
129: }
130:
131: public void setCurrentID(String id) throws BadIDException {
132: m_mw.setCurrentID(id);
133: }
134:
135: public void setCurrentID(ID id)
136: throws InvalidHelpSetContextException {
137: m_mw.setCurrentID(id);
138: }
139:
140: public void setCurrentURL(URL url) {
141: m_mw.setCurrentURL(url);
142: }
143:
144: public void setActivationObject(Object comp) {
145: m_mw.setActivationObject(comp);
146: }
147:
148: public void setActivationWindow(Window window) {
149: m_mw.setActivationWindow(window);
150: }
151:
152: public void setHelpSet(HelpSet hs) {
153: m_mw.setHelpSet(hs);
154: }
155:
156: public void setHelpSetPresentation(HelpSet.Presentation hsPres) {
157: m_mw.setHelpSetPresentation(hsPres);
158: }
159:
160: public void setViewDisplayed(boolean displayed) {
161: m_mw.setViewDisplayed(displayed);
162: }
163:
164: public void setLocation(Point p) {
165: m_mw.setLocation(p);
166: }
167:
168: public void setDisplayed(boolean b) {
169: m_mw.setDisplayed(b);
170: }
171:
172: public void setFont(Font f) {
173: m_mw.setFont(f);
174: }
175:
176: public void setLocale(Locale l) {
177: m_mw.setLocale(l);
178: }
179:
180: public void setCurrentView(String name) {
181: m_mw.setCurrentView(name);
182: }
183:
184: public void setSize(Dimension d) {
185: m_mw.setSize(d);
186: }
187:
188: public class MyWindowPresentation extends WindowPresentation {
189: private Dimension m_last_size;
190:
191: public MyWindowPresentation(HelpSet hs) {
192: super (hs);
193: }
194:
195: public Dimension getSize() {
196: Dimension d = null;
197:
198: try {
199: d = super .getSize();
200: Frame[] frames = JFrame.getFrames();
201: for (int k = 0; k < frames.length; k++) {
202: if (!(frames[k] instanceof JFrame))
203: continue;
204: JFrame jf = (JFrame) frames[k];
205: if (jf.getContentPane().getComponentCount() == 0)
206: continue;
207: Component c = jf.getContentPane().getComponent(0);
208: if (c == null || !(c instanceof JHelp))
209: continue;
210:
211: if (jf.isVisible()) {
212: d = jf.getSize();
213: }
214: }
215: } catch (Exception e) {
216: /**
217: * this is a fix to get around a problem with JavaHelp. If you
218: * show help using a dialod and then show help using a frame, an
219: * exception is thrown
220: */
221: d = m_last_size;
222: }
223:
224: if (d == null)
225: d = new Dimension(600, 400);
226:
227: m_last_size = d;
228: return d;
229: }
230: }
231:
232: }
|