~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/javahelp/jhMaster/JavaHelp/src/new/javax/help/.svn/text-base/WindowPresentation.java.svn-base

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)WindowPresentation.java  1.22 06/10/30
 
3
 * 
 
4
 * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
 
5
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
6
 * 
 
7
 * This code is free software; you can redistribute it and/or modify it
 
8
 * under the terms of the GNU General Public License version 2 only, as
 
9
 * published by the Free Software Foundation.  Sun designates this
 
10
 * particular file as subject to the "Classpath" exception as provided
 
11
 * by Sun in the LICENSE file that accompanied this code.
 
12
 * 
 
13
 * This code is distributed in the hope that it will be useful, but WITHOUT
 
14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
15
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
16
 * version 2 for more details (a copy is included in the LICENSE file that
 
17
 * accompanied this code).
 
18
 * 
 
19
 * You should have received a copy of the GNU General Public License version
 
20
 * 2 along with this work; if not, write to the Free Software Foundation,
 
21
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
22
 * 
 
23
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 
24
 * CA 95054 USA or visit www.sun.com if you need additional information or
 
25
 * have any questions.
 
26
 */
 
27
 
 
28
package javax.help;
 
29
 
 
30
import javax.swing.JDialog;
 
31
import javax.swing.JFrame;
 
32
import javax.swing.SwingUtilities;
 
33
import java.awt.Component;
 
34
import java.awt.Dialog;
 
35
import java.awt.Dimension;
 
36
import java.awt.Font;
 
37
import java.awt.Frame;
 
38
import java.awt.GraphicsConfiguration;
 
39
import java.awt.GraphicsDevice;
 
40
import java.awt.GraphicsEnvironment;
 
41
import java.awt.Image;
 
42
import java.awt.MenuComponent;
 
43
import java.awt.Point;
 
44
import java.awt.Rectangle;
 
45
import java.awt.Window;
 
46
import java.awt.event.WindowAdapter;
 
47
import java.awt.event.WindowEvent;
 
48
import java.awt.event.WindowListener;
 
49
import java.beans.PropertyChangeEvent;
 
50
import java.beans.PropertyChangeListener;
 
51
import java.lang.reflect.Method;
 
52
import java.net.URL;
 
53
import java.util.Enumeration;
 
54
import java.util.Locale;
 
55
import javax.swing.ImageIcon;
 
56
 
 
57
/**
 
58
 * Window Presentation is an abstract class providing a generic interface for
 
59
 * the development of Window Presentations. Each implementation of 
 
60
 * Presentation will need to override the static method getPresentation
 
61
 * according to it's own needs. 
 
62
 *
 
63
 * WindowPresentation implements several generic methods required in all 
 
64
 * window presentations. Includes the ability to handle modal and non-modal
 
65
 * activation of the help window.
 
66
 *
 
67
 * @author Roger D.Brinkley
 
68
 * @version     1.22    10/30/06
 
69
 * @since 2.0
 
70
 *
 
71
 * @see javax.help.HelpSet
 
72
 * @see javax.help.JHelpNavigator
 
73
 * @see javax.help.HelpVisitListener
 
74
 */
 
75
 
 
76
public abstract class WindowPresentation extends Presentation {
 
77
 
 
78
    private HelpSet.Presentation hsPres=null;
 
79
    private JFrame frame = null;
 
80
    private JHelp jhelp = null;
 
81
    private JDialog dialog = null;
 
82
    private Window ownerWindow = null;
 
83
    private boolean modallyActivated = false;
 
84
    private Point location = null;
 
85
    private String title = null;
 
86
    private Image image = null;
 
87
    private String currentView = null;
 
88
    private boolean viewDisplayed = true;
 
89
    private boolean toolbarDisplayed = true;
 
90
    private boolean destroyOnExit = false;
 
91
    private boolean titleFromDocument = false;
 
92
    private WindowPropertyChangeListener propertyChangeListener = null;
 
93
    private int screen = 0;
 
94
 
 
95
    public WindowPresentation (HelpSet hs) {
 
96
        setHelpSet(hs);
 
97
    }
 
98
 
 
99
    /**
 
100
     * Set the Presentation attributes specific to WindowPresentations from a 
 
101
     * named presentation in a HelpSet.
 
102
     * 
 
103
     * @params hsPres - the HelpSet.Presentation to retrieve the presentation 
 
104
     *                  information from
 
105
     * 
 
106
     * @see HelpSet.Presentation
 
107
     */
 
108
    public void setHelpSetPresentation (HelpSet.Presentation hsPres) {
 
109
        debug("setHelpSetPrsentation");
 
110
        if (hsPres == null) {
 
111
            return;
 
112
        }
 
113
 
 
114
        // make sure the underlying presentation attributes are set
 
115
        super.setHelpSetPresentation(hsPres);
 
116
 
 
117
        // get the presentation location
 
118
        Point location = hsPres.getLocation();
 
119
        if (location != null) {
 
120
            setLocation(location);
 
121
        }
 
122
 
 
123
        // get the Title
 
124
        String title = hsPres.getTitle();
 
125
        if (title != null) {
 
126
            setTitle(title);
 
127
        }
 
128
 
 
129
        // get the imageID
 
130
        javax.help.Map.ID imageID = hsPres.getImageID();
 
131
        if (imageID != null) {
 
132
            ImageIcon icon = null;
 
133
            try {
 
134
                javax.help.Map map = getHelpSet().getCombinedMap();
 
135
                URL url = map.getURLFromID(imageID);
 
136
                icon = new ImageIcon(url);
 
137
                image = icon.getImage();
 
138
            } catch (Exception e) {
 
139
            }
 
140
        }
 
141
 
 
142
        if (hsPres.isToolbar()) {
 
143
            setToolbarDisplayed(true);
 
144
        }
 
145
 
 
146
        if (hsPres.isViewDisplayed()) {
 
147
            setViewDisplayed(true);
 
148
        }
 
149
        
 
150
        this.hsPres = hsPres;
 
151
    }
 
152
 
 
153
    /**
 
154
     * Return the HelpSet.Presentation if one was set
 
155
     * @returns HelpSet.Presentation - the HelpSet.Presentation used in this
 
156
     *          Presentation.
 
157
     * 
 
158
     * @see HelpSet.Presentation
 
159
     */
 
160
    public HelpSet.Presentation getHelpSetPresentation() {
 
161
        return hsPres;
 
162
    }
 
163
 
 
164
    /**
 
165
     * Get the activation window. 
 
166
     *
 
167
     * @returns Window - the activation window if activatated from a modal
 
168
     *                   modal dialog, otherwise null.
 
169
     */
 
170
    public Window getActivationWindow() {
 
171
        debug("getActivationWindow");
 
172
        return ownerWindow;
 
173
    }
 
174
 
 
175
    /**
 
176
     * Set the activation window. If the window is an instance of a
 
177
     * Dialog and the is modal, modallyActivated help is set to true and 
 
178
     * ownerDialog is set to the window. In all other instances 
 
179
     * modallyActivated is set to false and ownerDialog is set to null.
 
180
     * @param window the activating window
 
181
     */
 
182
    public void setActivationWindow(Window window) {
 
183
        debug("setActivationWindow");
 
184
        if (window != null && window instanceof Dialog) {
 
185
            Dialog tmpDialog = (Dialog) window;
 
186
            if (tmpDialog.isModal()) {
 
187
                ownerWindow = window;
 
188
                modallyActivated = true;
 
189
            } else {
 
190
                ownerWindow = null;
 
191
                modallyActivated = false;
 
192
            }
 
193
        } else {
 
194
            ownerWindow = null;
 
195
            modallyActivated = false;
 
196
        }
 
197
    }
 
198
                    
 
199
    /**
 
200
     * Set the activation window from given Component or MenuItem. It find Window component
 
201
     * in the component tree from given Component or MenuItem end call 
 
202
     * <pre>setActivationWindow</pre>.
 
203
     * @parem comp the activation Component or MenuItem
 
204
     * @since 2.0
 
205
     *
 
206
     * @see setActivationWindow
 
207
     */
 
208
    public void setActivationObject(Object comp) {
 
209
        debug("setActivationObject");
 
210
        while (comp instanceof MenuComponent) {
 
211
            comp = ((MenuComponent)comp).getParent();
 
212
        }
 
213
        
 
214
        Window owner = null;
 
215
        if (comp instanceof Frame) {
 
216
            owner = (Window)comp;
 
217
        } else if (comp instanceof Component) {
 
218
            owner = SwingUtilities.windowForComponent((Component)comp);
 
219
        }
 
220
        
 
221
        setActivationWindow(owner);
 
222
    }
 
223
    
 
224
    /**
 
225
     * Determines the current navigator.
 
226
     */
 
227
    public String getCurrentView() {
 
228
        debug("getCurrentView");
 
229
        // always use the current view if the jhelp exists.
 
230
        if (jhelp != null) {
 
231
            currentView = jhelp.getCurrentNavigator().getNavigatorName();
 
232
        }
 
233
        return currentView;
 
234
    }
 
235
 
 
236
 
 
237
    /**
 
238
     * Set the currentView to the navigator with the same 
 
239
     * name as the <tt>name</tt> parameter.
 
240
     *
 
241
     * @param name The name of the navigator to set as the 
 
242
     * current view. If nav is null or not a valid Navigator 
 
243
     * in this WindowPresentation then an 
 
244
     * IllegalArgumentException is thrown.
 
245
     * @throws IllegalArgumentException if nav is null or not a valid Navigator.
 
246
     */
 
247
    public void setCurrentView(String name) {
 
248
        debug("setCurrentView");
 
249
        // if the jhelp already exists then set the currentview
 
250
        if (jhelp != null) {
 
251
            
 
252
            JHelpNavigator nav = getNavigatorByName(name);
 
253
            
 
254
            if (nav == null) {
 
255
                throw new IllegalArgumentException("Invalid view name");
 
256
            }
 
257
            jhelp.setCurrentNavigator(nav);
 
258
        } else {
 
259
            // jhelp didn't exist so make sure view is in HelpSet
 
260
            HelpSet hs = getHelpSet();
 
261
            NavigatorView view = hs.getNavigatorView(name);
 
262
            if (view == null) {
 
263
                throw new IllegalArgumentException("Invalid view name");
 
264
            }
 
265
        }
 
266
        currentView = name;
 
267
    }
 
268
 
 
269
    /*
 
270
     * Internal method to return a Navigator by name from a jhelp
 
271
     */
 
272
    private JHelpNavigator getNavigatorByName(String name) {
 
273
        JHelpNavigator nav = null;
 
274
        if (jhelp != null) {
 
275
            for (Enumeration e = jhelp.getHelpNavigators();
 
276
                 e.hasMoreElements(); ) {
 
277
                nav = (JHelpNavigator) e.nextElement();
 
278
                if (nav.getNavigatorName().equals(name)) {
 
279
                    break;
 
280
                }
 
281
                nav = null;
 
282
            }
 
283
        }
 
284
        return nav;
 
285
    }
 
286
 
 
287
 
 
288
    /**
 
289
     * Determines if the presentation should be distroyed on exit
 
290
     */
 
291
    public boolean isDestroyedOnExit() {
 
292
        debug("isDestoryedOnExit");
 
293
        return destroyOnExit;
 
294
    }
 
295
 
 
296
    /**
 
297
     * Destory the window on exit
 
298
     */
 
299
    public void setDestroyOnExit(boolean destroy) {
 
300
        debug("setDestoryOnExit");
 
301
        destroyOnExit = destroy;
 
302
    }
 
303
 
 
304
    /**
 
305
     * Destroy this object. Implementation of WindowPresentation that
 
306
     * maintian a list of objects should override this method and call 
 
307
     * super.destroy to clear up the WindowPresentation internal fields.
 
308
     */
 
309
    public void destroy() {
 
310
        frame = null;
 
311
        jhelp = null;
 
312
        dialog = null;
 
313
        ownerWindow = null;
 
314
        location = null;
 
315
        title = null;
 
316
        currentView = null;
 
317
        propertyChangeListener = null;
 
318
        screen = 0;
 
319
    }
 
320
 
 
321
    /**
 
322
     * Changes the HelpSet for this presentation.
 
323
     * @param hs The HelpSet to set for this presentation. 
 
324
     * A null hs is valid parameter.
 
325
     */
 
326
    public void setHelpSet(HelpSet hs) {
 
327
        debug("setHelpSet");
 
328
 
 
329
        HelpSet helpset = super.getHelpSet();
 
330
        // If we already have a model check if the HelpSet has changed.
 
331
        // If so change the model
 
332
        // This could be made smarter to cache the helpmodels per HelpSet
 
333
        if (hs != null && helpset != hs) {
 
334
            super.setHelpSet(hs);
 
335
            if (jhelp != null) {
 
336
                jhelp.setModel(super.getHelpModel());
 
337
            }
 
338
        }
 
339
    }
 
340
 
 
341
    /**
 
342
     * Displays the presentation to the user.
 
343
     */
 
344
    public void setDisplayed(boolean b) {
 
345
        debug ("setDisplayed");
 
346
        // if the jhelp is null and they don't want it displayed just return
 
347
        if (jhelp == null && !b) {
 
348
            return;
 
349
        }
 
350
 
 
351
        // The call to createHelpWindow is necessary as the modality
 
352
        // might have been changed and we need to change from a dialog
 
353
        // to a frame. This is only done in createHelpWindow.
 
354
        createHelpWindow();
 
355
        if (modallyActivated) {
 
356
            if (b) {
 
357
                dialog.show();
 
358
            } else {
 
359
                dialog.hide();
 
360
            }
 
361
        } else {
 
362
            frame.setVisible(b);
 
363
 
 
364
        // We should be able to just 
 
365
        // try {
 
366
        //      frame.setState(Frame.NORMAL)
 
367
        // } catch (NoSuchMethodError ex) {
 
368
        // }
 
369
        // but IE4.0 barfs very badly at this
 
370
        // So...
 
371
 
 
372
            try {
 
373
                Class types[] = {Integer.TYPE};
 
374
                Method m = Frame.class.getMethod("setState", types);
 
375
 
 
376
                if (m != null) {
 
377
                    Object args[] = {new Integer(0)}; // Frame.NORMAL
 
378
                    m.invoke(frame, args);
 
379
                }
 
380
            } catch (NoSuchMethodError ex) {
 
381
                // as in JDK1.1
 
382
            } catch (NoSuchMethodException ex) {
 
383
                // as in JDK1.1
 
384
            } catch (java.lang.reflect.InvocationTargetException ex) {
 
385
                //
 
386
            } catch (java.lang.IllegalAccessException ex) {
 
387
                //
 
388
            }
 
389
        }
 
390
    }
 
391
 
 
392
    /**
 
393
     * Determines if the presentation is displayed.
 
394
     */
 
395
    public boolean isDisplayed() {
 
396
        debug ("isDisplayed");
 
397
        if (jhelp == null) {
 
398
            return false;
 
399
        }
 
400
        if (modallyActivated) {
 
401
            if (dialog != null) {
 
402
                return dialog.isShowing();
 
403
            } else {
 
404
                return false;
 
405
            }
 
406
        } else {
 
407
            if (frame != null) {
 
408
                if (! frame.isShowing()) {
 
409
                    return false;
 
410
                }
 
411
                else {
 
412
                    // We should be able to just 
 
413
                    // try {
 
414
                    //  return (frame.getState() == Frame.NORMAL)
 
415
                    // } catch (NoSuchMethodError ex) {
 
416
                    // }
 
417
                    // but IE4.0 barfs very badly at this
 
418
                    // So...
 
419
 
 
420
                    try {
 
421
                        Method m = Frame.class.getMethod("getState", 
 
422
                                                         (java.lang.Class[]) null);
 
423
 
 
424
                        if (m != null) {
 
425
                            int value =((Integer)(m.invoke(frame, 
 
426
                                                           (java.lang.Object[])null))).intValue();
 
427
                            if (value == 0)
 
428
                                return true;
 
429
                            else 
 
430
                                return false;
 
431
 
 
432
                        }
 
433
                    } catch (NoSuchMethodError ex) {
 
434
                        // as in JDK1.1
 
435
                    } catch (NoSuchMethodException ex) {
 
436
                        // as in JDK1.1
 
437
                    } catch (java.lang.reflect.InvocationTargetException ex) {
 
438
                        //
 
439
                    } catch (java.lang.IllegalAccessException ex) {
 
440
                        //
 
441
                    }
 
442
                    // On 1.1 I can't tell if it's raised or not.
 
443
                    // It's on the screen so true.
 
444
                    return true;
 
445
                }
 
446
            } else {
 
447
                return false;
 
448
            }
 
449
        }
 
450
    }
 
451
 
 
452
    /**
 
453
     * Sets the font for this this WindowPresentation.
 
454
     * @param f The font.
 
455
     */
 
456
    public void setFont (Font f) {
 
457
        debug("setFont");
 
458
        super.setFont(f);
 
459
        if (jhelp != null && f != null) {
 
460
            jhelp.setFont(f);
 
461
        }
 
462
    }
 
463
 
 
464
    /**
 
465
     * Gets the font for this WindowPresentation
 
466
     */
 
467
    public Font getFont() {
 
468
        debug("getFont");
 
469
        Font font = super.getFont();
 
470
        if (font == null) {
 
471
            if (jhelp == null) {
 
472
                createHelpWindow();
 
473
            }
 
474
            return jhelp.getFont();
 
475
        }
 
476
        return font;
 
477
    }
 
478
 
 
479
    /**
 
480
     * Sets the locale of this Presentation. The locale is propagated to
 
481
     * the presentation.
 
482
     * @param l The locale to become this component's locale. A null locale
 
483
     * is the same as the defaultLocale.
 
484
     * @see #getLocale
 
485
     */
 
486
    public void setLocale(Locale l) { 
 
487
        debug("setLocale");
 
488
        super.setLocale(l);
 
489
        if (jhelp != null) {
 
490
            jhelp.setLocale(l);
 
491
        }
 
492
    }
 
493
 
 
494
 
 
495
    /**
 
496
     * internal method to test for Xinerama mode
 
497
     */
 
498
    private boolean isXinerama () {
 
499
        GraphicsEnvironment ge = 
 
500
            GraphicsEnvironment.getLocalGraphicsEnvironment();
 
501
        GraphicsDevice[] gds = ge.getScreenDevices();
 
502
        if (gds.length == 1) {
 
503
            return false;
 
504
        } else {
 
505
            for (int i=0; i<gds.length; i++) {
 
506
                GraphicsConfiguration loopgc =
 
507
                    gds[i].getDefaultConfiguration();
 
508
                Rectangle bounds = loopgc.getBounds();
 
509
                if (bounds.x != 0 || bounds.y !=0) {
 
510
                    return true;
 
511
                }
 
512
            }
 
513
        }
 
514
        return false;
 
515
    }
 
516
 
 
517
   /**
 
518
     * Requests the location of the presentation.
 
519
     * 
 
520
     * @returns Point the location of the presentation.
 
521
     */
 
522
    public Point getLocation() {
 
523
        debug("getLocation");
 
524
        if (location != null && jhelp == null) {
 
525
            return location;
 
526
        }
 
527
        if (jhelp == null) {
 
528
            createHelpWindow();
 
529
        }
 
530
        if (modallyActivated) {
 
531
            Point dlocation = dialog.getLocation();
 
532
            if (isXinerama()) {
 
533
                GraphicsConfiguration gc = dialog.getGraphicsConfiguration();
 
534
                Rectangle gcBounds = gc.getBounds();
 
535
                return new Point(dlocation.x - gcBounds.x,
 
536
                                 dlocation.y - gcBounds.y);
 
537
            } 
 
538
            return dlocation;
 
539
        } else {
 
540
            Point flocation = frame.getLocation();
 
541
            if (isXinerama()) {
 
542
                GraphicsConfiguration gc = frame.getGraphicsConfiguration();
 
543
                Rectangle gcBounds = gc.getBounds();
 
544
                return new Point(flocation.x - gcBounds.x,
 
545
                                 flocation.y - gcBounds.y);
 
546
            } 
 
547
            return flocation;
 
548
        }
 
549
    }
 
550
 
 
551
    /**
 
552
     * Requests the presentation be located at a given position.
 
553
     */
 
554
    public void setLocation(Point p) {
 
555
        debug("setLocation");
 
556
        location = p;
 
557
 
 
558
 
 
559
        if (jhelp != null) {
 
560
            if (modallyActivated) {
 
561
                if (dialog != null) {
 
562
                    GraphicsConfiguration gc = 
 
563
                        dialog.getGraphicsConfiguration();
 
564
                    Rectangle gcBounds = gc.getBounds();
 
565
                    Point loc = new Point (gcBounds.x + p.x, 
 
566
                                           gcBounds.y + p.y);
 
567
                    dialog.setLocation(loc);
 
568
                }
 
569
            } else {
 
570
                if (frame != null) {
 
571
                    GraphicsConfiguration gc = 
 
572
                        frame.getGraphicsConfiguration();
 
573
                    Rectangle gcBounds = gc.getBounds();
 
574
                    Point loc = new Point (gcBounds.x + p.x, 
 
575
                                           gcBounds.y + p.y);
 
576
                    frame.setLocation(loc);
 
577
                }
 
578
            }
 
579
        }
 
580
    }
 
581
 
 
582
    /** 
 
583
     * Requests the screen of the presentation
 
584
     * @returns int the screen of the presentation
 
585
     */
 
586
    public int getScreen() {
 
587
        debug("getScreen");
 
588
        // If there is no jhelp componet then it hasn't been "realized"
 
589
        // yet so just return the screen
 
590
        if (jhelp == null) {
 
591
            return screen;
 
592
        }
 
593
 
 
594
        // Help is showing so get the screen from the presentation
 
595
        GraphicsConfiguration gc = null;
 
596
        if (modallyActivated) {
 
597
            if (dialog != null) {
 
598
                gc = dialog.getGraphicsConfiguration();
 
599
            }
 
600
        } else {
 
601
            if (frame != null) {
 
602
                gc = frame.getGraphicsConfiguration();
 
603
            }
 
604
        }
 
605
        if (gc != null) {
 
606
            GraphicsDevice device = gc.getDevice();
 
607
            GraphicsEnvironment ge = 
 
608
                GraphicsEnvironment.getLocalGraphicsEnvironment();
 
609
            GraphicsDevice[] gs = ge.getScreenDevices();
 
610
            for (int i=0; i < gs.length; i++) {
 
611
                if (gs[i] == device) {
 
612
                    // got our match
 
613
                    screen = i;
 
614
                    return screen;
 
615
                }
 
616
            }
 
617
        }
 
618
        return screen;
 
619
    }
 
620
 
 
621
    /**
 
622
     * Sets the screen of the presentation
 
623
     * @param screen the screen number
 
624
     * @throws IllegalArgumentException if the screen is invalid
 
625
     */
 
626
    public void setScreen(int screen) {
 
627
        debug ("setScreen");
 
628
 
 
629
        if (screen == this.screen) {
 
630
            // There is nothing to do as it is either already the screen
 
631
            // or the 
 
632
            return;
 
633
        }
 
634
 
 
635
        if (screen < 0) {
 
636
            throw new IllegalArgumentException("Invalid screen");
 
637
        }
 
638
 
 
639
        GraphicsEnvironment ge = 
 
640
            GraphicsEnvironment.getLocalGraphicsEnvironment();
 
641
        GraphicsDevice[]gs = ge.getScreenDevices();
 
642
 
 
643
        // make sure there is a screen device
 
644
        if (gs.length <= screen) {
 
645
            throw new IllegalArgumentException ("Invalid Screen");
 
646
        }
 
647
 
 
648
        this.screen = screen;
 
649
 
 
650
        if (jhelp != null) {
 
651
 
 
652
            boolean xinerama = isXinerama();
 
653
            
 
654
            GraphicsDevice gd = gs[screen];
 
655
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
 
656
            Rectangle gcBounds = gc.getBounds();
 
657
            if (modallyActivated) {
 
658
                if (dialog != null) {
 
659
                    if (xinerama) {
 
660
                        Point p = getLocation();
 
661
                        Point loc = new Point (gcBounds.x + p.x, 
 
662
                                               gcBounds.y + p.y);
 
663
                        dialog.setLocation(loc);
 
664
                    } else {
 
665
                        location = getLocation();
 
666
                        dialog.hide();
 
667
                        dialog = null;
 
668
                        createHelpWindow();
 
669
                    }
 
670
                }
 
671
            } else {
 
672
                if (frame != null) {
 
673
                    if (xinerama) {
 
674
                        Point p = getLocation();
 
675
                        Point loc = new Point (gcBounds.x + p.x, 
 
676
                                               gcBounds.y + p.y);
 
677
                        frame.setLocation(loc);
 
678
                    } else {
 
679
                        location = getLocation();
 
680
                        frame.setVisible(false);
 
681
                        frame = null;
 
682
                        createHelpWindow();
 
683
                    }
 
684
                }
 
685
            }
 
686
        }
 
687
    }
 
688
 
 
689
    /**
 
690
     * Requests the size of the presentation.
 
691
     * @returns Point the location of the presentation.
 
692
     */
 
693
    public Dimension getSize() {
 
694
        debug("getSize");
 
695
        // if the jhelp is created then just use the current sizes 
 
696
        if (jhelp != null) {
 
697
            if (modallyActivated) {
 
698
                if (dialog != null) {
 
699
                    return dialog.getSize();
 
700
                }
 
701
            } else {
 
702
                if (frame != null) {
 
703
                    return frame.getSize();
 
704
                }
 
705
            }
 
706
        }
 
707
        return super.getSize();
 
708
    }
 
709
 
 
710
    /**
 
711
     * Requests the presentation be set to a given size. Updates the
 
712
     * the presentation on the fly. This is an override of 
 
713
     * Presentation.SetSize.
 
714
     */
 
715
    public void setSize(Dimension d) {
 
716
        debug("setSize");
 
717
        super.setSize(d);
 
718
        if (jhelp != null) {
 
719
            if (modallyActivated) {
 
720
                dialog.setSize(d);
 
721
                dialog.validate();
 
722
            } else {
 
723
                frame.setSize(d);
 
724
                frame.validate();
 
725
            }
 
726
        }
 
727
    }
 
728
 
 
729
    public String getTitle() {
 
730
        debug("getTitle");
 
731
 
 
732
        // if the title comes from the document use that first if 
 
733
        // jhelp exists
 
734
        if (titleFromDocument && jhelp != null) {
 
735
            String docTitle = jhelp.getContentViewer().getDocumentTitle();
 
736
            if (docTitle != null) {
 
737
                return docTitle;
 
738
            }
 
739
        }
 
740
        
 
741
        // otherwise use the title that has been set...
 
742
        if (title != null) {
 
743
            return title;
 
744
        } else {
 
745
            // Unless there wasn't a title set and then use the HelpSet
 
746
            // title
 
747
            HelpSet hs = getHelpSet();
 
748
            if (hs != null) {
 
749
                title = hs.getTitle();
 
750
            }
 
751
        }
 
752
        return title;
 
753
    }
 
754
 
 
755
    public void setTitle(String title) {
 
756
        debug("setTitle");
 
757
        this.title = title;
 
758
        if (jhelp != null) {
 
759
            if (modallyActivated) {
 
760
                dialog.setTitle(title);
 
761
                dialog.validate();
 
762
            } else {
 
763
                frame.setTitle(title);
 
764
                frame.validate();
 
765
            }
 
766
        }
 
767
 
 
768
    }
 
769
 
 
770
    /**
 
771
     * Is the title set from the Document. This is generally useful
 
772
     * in SecondaryWindows.
 
773
     * @return boolean True if title is set from the Document, false otherwise.
 
774
     */
 
775
    public boolean isTitleSetFromDocument() {
 
776
        debug("isTitleSetFromDocument");
 
777
        return titleFromDocument;
 
778
    }
 
779
 
 
780
    /**
 
781
     * Set the title from the Document. 
 
782
     * @param b if true will set the title form the document, otherwise will
 
783
     *  set the title from the HelpSet.
 
784
     */
 
785
    public void setTitleFromDocument(boolean b) {
 
786
        debug("setTitleFromDocument");
 
787
        if (titleFromDocument != b) {
 
788
            titleFromDocument = b;
 
789
            if (titleFromDocument) {
 
790
                propertyChangeListener = new WindowPropertyChangeListener();
 
791
                if (jhelp != null) {
 
792
                    jhelp.getContentViewer().
 
793
                        addPropertyChangeListener("page",
 
794
                                                  propertyChangeListener);
 
795
                }
 
796
            } else {
 
797
                if (jhelp != null) {
 
798
                    jhelp.getContentViewer().
 
799
                        removePropertyChangeListener("page",
 
800
                                                     propertyChangeListener);
 
801
                }
 
802
            }
 
803
        }
 
804
    }
 
805
 
 
806
    /**
 
807
     * Determines if the current view is visible.
 
808
     */
 
809
    public boolean isViewDisplayed() {
 
810
        debug ("isViewDisplayed");
 
811
        if (jhelp != null) {
 
812
            return jhelp.isNavigatorDisplayed();
 
813
        }
 
814
        return viewDisplayed;
 
815
    }
 
816
 
 
817
    /**
 
818
     * Hides/Shows view.
 
819
     */
 
820
    public void setViewDisplayed(boolean displayed) {
 
821
        debug ("setViewDisplayed");
 
822
        if (jhelp != null) {
 
823
            jhelp.setNavigatorDisplayed(displayed);
 
824
        }
 
825
        viewDisplayed = displayed;
 
826
    }
 
827
 
 
828
    /**
 
829
     * Determines if the toolbar is visible.
 
830
     */
 
831
    public boolean isToolbarDisplayed() {
 
832
        debug ("isToolbarDisplayed");
 
833
        if (jhelp != null) {
 
834
            return jhelp.isToolbarDisplayed();
 
835
        }
 
836
        return toolbarDisplayed;
 
837
    }
 
838
 
 
839
    /**
 
840
     * Hides/Shows Toolbar
 
841
     */
 
842
    public void setToolbarDisplayed(boolean displayed) {
 
843
        debug ("setToolbarDisplayed=" + displayed);
 
844
        if (jhelp != null) {
 
845
            jhelp.setToolbarDisplayed(displayed);
 
846
        }
 
847
        toolbarDisplayed = displayed;
 
848
    }
 
849
 
 
850
    private synchronized void createJHelp() {
 
851
        debug ("createJHelp");
 
852
        if (jhelp == null) {
 
853
            jhelp = new JHelp(getHelpModel(), null, getHelpSetPresentation());
 
854
            Font font = super.getFont();
 
855
            if (font != null) {
 
856
                jhelp.setFont(font);
 
857
            }
 
858
            Locale locale = getLocale();
 
859
            if (locale != null) {
 
860
                jhelp.setLocale(locale);
 
861
            }
 
862
            jhelp.setToolbarDisplayed(toolbarDisplayed);
 
863
            jhelp.setNavigatorDisplayed(viewDisplayed);
 
864
            if (currentView != null) {
 
865
                JHelpNavigator nav = getNavigatorByName(currentView);
 
866
                if (nav != null) {
 
867
                    jhelp.setCurrentNavigator(nav);
 
868
                }
 
869
            }
 
870
            if (titleFromDocument) {
 
871
                jhelp.getContentViewer().
 
872
                    addPropertyChangeListener("page", propertyChangeListener);
 
873
            }
 
874
        }
 
875
    }
 
876
 
 
877
    WindowListener dl;
 
878
    boolean modalDeactivated = true;
 
879
 
 
880
    public synchronized void createHelpWindow() {
 
881
        debug ("createHelpWindow");
 
882
        // pos is used for determining the screen adjust location of a 
 
883
        // dialog or frame that already exist. This should only be used
 
884
        // when a switch is required. If it is null this is a creation.
 
885
        Point pos = null;
 
886
        Dimension size = getSize();
 
887
        JDialog tmpDialog = null;
 
888
 
 
889
        createJHelp();
 
890
 
 
891
        
 
892
        // The graphics variables below are only used during the initial
 
893
        // creation. If there is a modality change then the actual position
 
894
        // of the Window would be used instead.
 
895
        GraphicsEnvironment ge = 
 
896
            GraphicsEnvironment.getLocalGraphicsEnvironment();
 
897
        GraphicsDevice[] gds = ge.getScreenDevices();
 
898
        GraphicsDevice gd = gds[screen];
 
899
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
 
900
        Rectangle gcBounds = gc.getBounds();
 
901
 
 
902
        if (modallyActivated) {
 
903
            // replace dialog.getOwner() with the following code
 
904
            Window owner=null;
 
905
            try {
 
906
                Method m = Window.class.getMethod("getOwner", 
 
907
                                                  (java.lang.Class[]) null);
 
908
                
 
909
                if (m != null && dialog != null) {
 
910
                    owner = (Window) m.invoke(dialog, 
 
911
                                              (java.lang.Class[]) null);
 
912
                }
 
913
            } catch (NoSuchMethodError ex) {
 
914
                // as in JDK1.1
 
915
            } catch (NoSuchMethodException ex) {
 
916
                // as in JDK1.1
 
917
            } catch (java.lang.reflect.InvocationTargetException ex) {
 
918
                //
 
919
            } catch (java.lang.IllegalAccessException ex) {
 
920
                //
 
921
            }
 
922
            
 
923
            if (dialog == null || owner != ownerWindow || modalDeactivated) {
 
924
                if (frame != null) {
 
925
                    // pos is already screen adjusted
 
926
                    pos = frame.getLocation();
 
927
                    size = frame.getSize();
 
928
                    frame = null;
 
929
                }
 
930
                if (dialog != null) {
 
931
                    // pos is already screen adjusted
 
932
                    pos = dialog.getLocation();
 
933
                    size = dialog.getSize();
 
934
                    tmpDialog = dialog;
 
935
                }
 
936
                dialog = new JDialog((Dialog)ownerWindow, getTitle(), 
 
937
                                     false, gc);
 
938
 
 
939
                // Modal dialogs are really tricky. When the modal dialog
 
940
                // is dismissed the JDialog will be dismissed as well.
 
941
                // When that happens we need to make sure the ownerWindow
 
942
                // is set to null so that a new dialog will be created so
 
943
                // that events aren't blocked in the HelpViewer.
 
944
                dl = new WindowAdapter() {
 
945
                    public void windowClosing(WindowEvent e) {
 
946
                        debug ("modal window closing");
 
947
                        if (destroyOnExit) {
 
948
                            destroy();
 
949
                            return;
 
950
                        }
 
951
                        // JDK1.2.1 bug not closing owned windows
 
952
                        if (dialog.isShowing()) {
 
953
                            dialog.hide();
 
954
                        }
 
955
                        if (ownerWindow != null)
 
956
                            ownerWindow.removeWindowListener(dl);
 
957
                        ownerWindow = null;
 
958
                        modalDeactivated = true;
 
959
                    }
 
960
 
 
961
                    public void windowClosed(WindowEvent e) {
 
962
                        debug ("modal window closing");
 
963
                        if (destroyOnExit) {
 
964
                            destroy();
 
965
                            return;
 
966
                        }
 
967
                    }
 
968
                };
 
969
                debug ("adding windowlistener");
 
970
 
 
971
                ownerWindow.addWindowListener(dl);
 
972
                modalDeactivated = false;
 
973
                if (size != null) {
 
974
                    dialog.setSize(size);
 
975
                } else {
 
976
                    dialog.setSize(getSize());
 
977
                }
 
978
 
 
979
                // if the pos variable is not null then either a frame
 
980
                // or a dialog already exists and you should use that position
 
981
                // instead. If it is null then base the point on the location
 
982
                // and screen or just the screen
 
983
                if (pos != null) {
 
984
                    dialog.setLocation(pos);
 
985
                } else {
 
986
                    // set based on location and screen or just screen
 
987
                    Point loc = null;
 
988
                    if (location != null) {
 
989
                        if (isXinerama()) {
 
990
                            loc = new Point (gcBounds.x + location.x, 
 
991
                                             gcBounds.y + location.y);
 
992
                        } else {
 
993
                            loc = location;
 
994
                        }
 
995
                        dialog.setLocation(loc);
 
996
                    }
 
997
                }
 
998
                dialog.setTitle(getTitle());
 
999
                dialog.getContentPane().add(jhelp);
 
1000
                if (tmpDialog != null) {
 
1001
                    tmpDialog.hide();
 
1002
                    tmpDialog = null;
 
1003
                }
 
1004
            }
 
1005
        } else {
 
1006
            if (frame == null) { 
 
1007
                frame = new JFrame(getTitle(), gc);
 
1008
 
 
1009
                WindowListener l = new WindowAdapter() {
 
1010
                    public void windowClosing(WindowEvent e) {
 
1011
                        if (destroyOnExit) {
 
1012
                            destroy();
 
1013
                            return;
 
1014
                        }
 
1015
                        frame.setVisible(false);
 
1016
                    }
 
1017
                    public void windowClosed(WindowEvent e) {
 
1018
                        frame.setVisible(false);
 
1019
                        if (destroyOnExit) {
 
1020
                            destroy();
 
1021
                            return;
 
1022
                        }
 
1023
                    }
 
1024
                };
 
1025
                frame.addWindowListener(l);
 
1026
                if (image != null) {
 
1027
                    frame.setIconImage(image);
 
1028
                }
 
1029
            }
 
1030
            if (dialog != null) {
 
1031
                // pos is already screen adjusted
 
1032
                pos = dialog.getLocation();
 
1033
                size = dialog.getSize();
 
1034
                dialog.hide();
 
1035
                dialog = null;
 
1036
                ownerWindow = null;
 
1037
            }
 
1038
            if (size != null) {
 
1039
                frame.setSize(size);
 
1040
            } else {
 
1041
                frame.setSize(getSize());
 
1042
            }
 
1043
 
 
1044
 
 
1045
            // if the pos variable is not null then either a frame
 
1046
            // or a dialog already exists and you should use that position
 
1047
            // instead. If it is null then base the point on the location
 
1048
            // and screen or just the screen
 
1049
            if (pos != null) {
 
1050
                frame.setLocation(pos);
 
1051
            } else {
 
1052
                // set based on location and screen or just screen
 
1053
                Point loc = null;
 
1054
                if (location != null) {
 
1055
                    if (isXinerama()) {
 
1056
                        loc = new Point (gcBounds.x + location.x, 
 
1057
                                         gcBounds.y + location.y);
 
1058
                    } else {
 
1059
                        loc = location;
 
1060
                    }
 
1061
                    frame.setLocation(loc);
 
1062
                }
 
1063
            }
 
1064
            frame.getContentPane().add(jhelp);
 
1065
            frame.setTitle(getTitle());
 
1066
        }
 
1067
 
 
1068
    }                 
 
1069
 
 
1070
    /**
 
1071
     * Get the current window that help is displayed in
 
1072
     *
 
1073
     * @returns Window the current Window
 
1074
     */
 
1075
    public Window getHelpWindow() {
 
1076
        if (modallyActivated) {
 
1077
            return dialog;
 
1078
        }
 
1079
        return frame;
 
1080
    }
 
1081
 
 
1082
    private class WindowPropertyChangeListener implements PropertyChangeListener {
 
1083
        public void propertyChange(PropertyChangeEvent event) {
 
1084
            String changeName = event.getPropertyName();
 
1085
            if (changeName.equals("page")) {
 
1086
                String title = getTitle();
 
1087
                if (modallyActivated) {
 
1088
                    dialog.setTitle(title); 
 
1089
                } else {
 
1090
                    frame.setTitle(title);
 
1091
                }
 
1092
            }
 
1093
        }
 
1094
    }
 
1095
 
 
1096
    /**
 
1097
     * Debugging code...
 
1098
     */
 
1099
 
 
1100
    private static final boolean debug = false;
 
1101
    private static void debug(Object msg) {
 
1102
        if (debug) {
 
1103
            System.err.println("WindowPresentation: "+msg);
 
1104
        }
 
1105
    }
 
1106
 
 
1107
}
 
1108
 
 
1109