1 /*
2  * @(#)BrowserApiDemo.java	1.13 06/10/30
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistribution of source code must retain the above copyright
11  *   notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in
15  *   the documentation and/or other materials provided with the
16  *   distribution.
17  *
18  * Neither the name of Sun Microsystems, Inc. or the names of
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * This software is provided "AS IS," without a warranty of any
23  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
24  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
26  * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
27  * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
28  * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
29  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
30  * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
31  * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
32  * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
33  * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGES.
35  *
36  * You acknowledge that this software is not designed, licensed or
37  * intended for use in the design, construction, operation or
38  * maintenance of any nuclear facility.
39  */
40 
41 /*
42  * @(#) BrowserApiDemo.java 1.13 - last change made 10/30/06
43  *
44  * (c) 1997-1998 Sun Microsystems, Inc.  All rights reserved.  Use is
45  * subject to license terms. Sun, Sun Microsystems, the Sun Logo, Solaris,
46  * Java, the Java Coffee Cup Logo, and JavaHelp are trademarks or registered
47  * trademarks of Sun Microsystems, Inc. in  the U.S. and other countries.
48  *
49  */
50 
51 package sunw.demo.browser;
52 
53 import java.util.*;
54 import java.net.*;
55 import java.awt.*;
56 import java.awt.event.*;
57 import java.applet.*;
58 import javax.help.*;
59 import javax.swing.text.*;
60 import java.beans.PropertyVetoException;
61 import javax.swing.*;
62 
63 /**
64  * This class demostrates how to use JavaHelp in a browser
65  *
66  * @author Roger D. Brinkley
67  * @version	1.13	10/30/06
68  */
69 
70 public class BrowserApiDemo extends JApplet implements ActionListener{
71 
72     // The Main Help
73     String helpsetName = "IdeHelp";
74     String helpsetLabel = "demo JDE Help";
75     String helpsetURL;
76     JFrame helpFrame = null;
77     HelpSet mainhs=null;
78     HelpBroker mainHB;
79 
80     // Auxiliaries
81     JRootPane rootpane;
82     JPanel top=null;
83     JSplitPane split;
84     JMenuItem menuItem, menu_help, menu_open;
85     JTabbedPane messages;
86     int miscTabIndex;
87 
88     // Workaround for 1.1.4 compiler bug
89     JMenuItem item1;
90 
91     // The internal desktop
92     JDesktopPane desktop;
93 
94     // For the Source JInternalFrame
95     JInternalFrame sourceIFrame;
96 
97     JButton helpbutton;
98 
init()99     public void init() {
100         helpsetName = getParameter("HELPSETNAME");
101 	System.err.println("helpsetName: " + helpsetName);
102 
103         helpsetURL = getParameter("HELPSETURL");
104 	System.err.println("helpsetURL: " + helpsetURL);
105 
106 	URL url = null;		// for the helpSet
107         URL codebase = getCodeBase();
108 	System.err.println("codeBase: "+codebase);
109 
110 	ClassLoader loader = this.getClass().getClassLoader();
111 	System.err.println("loader: "+loader);
112 
113 	if (helpsetURL == null || helpsetURL.equals("")) {
114 	    url = HelpSet.findHelpSet(loader, helpsetName);
115 	    System.err.println("url after findHelpSet: "+url);
116 	} else {
117 	    try {
118 		debug ("helpsetURL=" + helpsetURL);
119 		url = new URL(codebase, helpsetURL);
120 		System.err.println("url after explicit parameter: "+url);
121 	    } catch (MalformedURLException e) {
122 		System.err.println("Malformed URL to HelpSet");
123 		System.err.println("  codebase: "+codebase);
124 		System.err.println("  helpsetURL: "+helpsetURL);
125 		System.exit(1);
126 	    }
127 	}
128 
129 	try {
130 	    mainhs = new HelpSet(loader, url);
131 	} catch (Exception ee) {
132 	    System.out.println ("Help Set "+helpsetName+" not found");
133 	    ee.printStackTrace();
134 	    //return;
135 	}
136 	mainHB = mainhs.createHelpBroker();
137 
138 	resources = ResourceBundle.getBundle("sunw.demo.browser.ApiDemo");
139         if (resources == null) {
140             System.err.println("Resources for application ApiDemo not found");
141         }
142 
143 	rootpane = this.getRootPane();
144 	mainHB.enableHelpKey(rootpane, "top", null);
145 
146 	top = new JPanel();
147 	top.setLayout(new BorderLayout());
148 
149 	JPanel header=new JPanel();
150 	header.setLayout(new BorderLayout());
151 	header.add(createMenus(),"North");
152 	header.add(createToolbar(),"South");
153 
154 	top.add(header,"North");
155 
156 	desktop = new JDesktopPane();
157 	desktop.setOpaque(true);
158 	desktop.setDoubleBuffered(true);
159 
160 	createSourceIFrame();
161 	desktop.add(sourceIFrame, JLayeredPane.PALETTE_LAYER);
162 
163 	JPanel panel=new JPanel();
164 	panel.setLayout(new BorderLayout());
165 	messages=new JTabbedPane();
166 
167 	JTextArea newtext=new JTextArea();
168 	newtext.setBackground(Color.white);
169 	CSH.setHelpIDString(newtext, "build.build");
170 	messages.addTab("Build", newtext);
171 	newtext = new JTextArea();
172 	CSH.setHelpIDString(newtext, "debug.overview");
173 	messages.addTab("Debug", newtext);
174 	newtext = new JTextArea();
175 	CSH.setHelpIDString(newtext, "browse.strings");
176 	messages.addTab("String Search", newtext);
177 	miscTabIndex = messages.getTabCount();
178 	messages.insertTab("Misc", null, new JTextArea(), null, miscTabIndex);
179 
180 	// Find out when we are selected.  When doing so...
181 
182 	messages.setSelectedIndex(0);
183 	messages.setDoubleBuffered(true);
184 	panel.add(messages,"Center");
185 	split=new JSplitPane(JSplitPane.VERTICAL_SPLIT,desktop,panel);
186 	//split.setDoubleBuffered(true);
187 	split.setOneTouchExpandable(true);
188 
189 	top.add(split,"Center");
190 
191 	this.getContentPane().add(top);
192     }
193 
194     /**
195      * Create a JButton out of a resource name
196      */
createButton(String name)197     private JButton createButton(String name) {
198 	java.net.URL url = this.getClass().getResource(name);
199 	ImageIcon icon = new ImageIcon(url);
200 	return new JButton(icon);
201     }
202 
createSourceIFrame()203     private void createSourceIFrame() {
204 	if (sourceIFrame != null) {
205 	    return;
206 	}
207 
208 	sourceIFrame = new JInternalFrame("Source", true, true, true, true);
209 	CSH.setHelpIDString(sourceIFrame, "edit.editsource");
210 	JComponent c = (JComponent) sourceIFrame.getContentPane();
211 	c.setLayout(new BorderLayout());
212 	c.setDoubleBuffered(true);
213 	sourceIFrame.setDoubleBuffered(true);
214 	sourceIFrame.setBounds(10,10,550,310);
215 
216 	JTextArea sourceText = new JTextArea("");
217 	sourceText.setFont(new Font("Courier",Font.PLAIN,12));
218 	sourceText.setBackground(Color.white);
219 
220 	sourceText.append("/* To view JavaHelp click, Help, Java API Help */"+
221 		    "\n\nimport java.applet.Applet;"+
222 		    "\nimport java.awt.Graphics;"+
223 		    "\n\npublic class HelloWorld extends Applet {"+
224 		    "\n\n    public void paint(Graphics g) {"+
225 		    "\n        g.drawString(\"Hello world!\", 50, 25);"+
226 		    "\n    }\n}\n");
227 
228 	c.add(sourceText,"Center");
229     }
230 
231     ResourceBundle resources;
232 
addButton(JToolBar toolbar, String img, String tipKey)233     private JButton addButton(JToolBar toolbar, String img, String tipKey) {
234 	JButton button = createButton(img);
235 	if (tipKey != null) {
236 	    try {
237 		String tipText =
238 		    resources.getString("toolbar."+tipKey+".tip");
239 		button.setToolTipText(tipText);
240 	    } catch (Exception ex) {
241 		System.err.println("Could not find a resource for "+tipKey);
242 	    }
243 	}
244 	toolbar.add(button);
245 	return button;
246     }
247 
createToolbar()248     public JToolBar createToolbar() {
249 	JToolBar toolbar=new JToolBar();
250 	CSH.setHelpIDString(toolbar,"toolbar.main");
251 
252 
253 	addButton(toolbar, "images/open.gif", "open");
254 	addButton(toolbar, "images/save.gif", "save");
255 	toolbar.addSeparator();
256 	addButton(toolbar, "images/start.gif", "start");
257 	addButton(toolbar, "images/break.gif", "stop");
258 	addButton(toolbar, "images/setbreak.gif", "setbreak");
259 	addButton(toolbar, "images/resume.gif", "resume");
260 	addButton(toolbar, "images/goto.gif", "goto");
261 	addButton(toolbar, "images/goend.gif", "goend");
262 	addButton(toolbar, "images/skip.gif", "skip");
263 	toolbar.addSeparator();
264 	addButton(toolbar, "images/down.gif", "down");
265 	addButton(toolbar, "images/up.gif", "up");
266 	toolbar.addSeparator();
267 	helpbutton= addButton(toolbar, "images/help.gif", "help");
268 	helpbutton.addActionListener(new CSH.DisplayHelpAfterTracking(mainHB));
269 
270 	return toolbar;
271     }
272 
addMenuItem(JMenu menu, String label, String tipKey)273   private JMenuItem addMenuItem(JMenu menu, String label, String tipKey) {
274     JMenuItem item = new JMenuItem(label);
275     if (tipKey != null) {
276 	try {
277 	    String tipText = resources.getString("menu."+tipKey+".tip");
278 	    item.setToolTipText(tipText);
279 	 } catch (Exception ex) {
280 	     System.err.println("Could not find a resource for "+tipKey);
281 	 }
282     }
283     menu.add(item);
284     return item;
285   }
286 
287     // An Option Dialog.  Not really good for help
288 
showDialog1()289     private void showDialog1() {
290 	Object options[] = {"OK", "CANCEL", "HELP"};
291 	int index =
292 	    JOptionPane.showOptionDialog(null, // parent
293 					 "Exit?", // message object
294 					 "Quit", // string title
295 					 JOptionPane.DEFAULT_OPTION,
296 					 JOptionPane.QUESTION_MESSAGE,
297 					 null, // Icon
298 					 options,
299 					 options[0]
300 					 );
301 	System.err.println("index is: "+index);
302 	switch (index) {
303 	case 0:
304 	    System.exit(0);
305 	case 1:
306 	    break;
307 	case 2:
308 	    System.err.println("will ask for help");
309 	    break;
310 	}
311     }
312 
createMenus()313     public JMenuBar createMenus() {
314 	JMenuBar menuBar = new JMenuBar();
315 	menuBar.setBackground(getBackground());
316 	//	menuBar.setOpaque(true);
317 	JMenu menu = new JMenu("File");
318 	CSH.setHelpIDString(menu, "menus.file");
319 	menu.setToolTipText("File operations");
320 	menuBar.add(menu);
321 	addMenuItem(menu, "New", "file.new");
322 	addMenuItem(menu, "Open...", "file.open");
323 	menu.addSeparator();
324 	addMenuItem(menu, "Save", "file.save");
325 	addMenuItem(menu, "Save As...", "file.saveas");
326 
327 	menu = new JMenu("Edit");
328 	CSH.setHelpIDString(menu, "menus.edit");
329 	menuBar.add(menu);
330 	addMenuItem(menu, "Undo", null);
331 	addMenuItem(menu, "Redo", null);
332 	addMenuItem(menu, "Cut", null);
333 	addMenuItem(menu, "Copy", null);
334 	addMenuItem(menu, "Paste", null);
335 	menu.addSeparator();
336 
337 	menuItem = addMenuItem(menu, "Find", null);
338 	menuItem.addActionListener(new ActionListener() {
339 	    public void actionPerformed(ActionEvent e){
340 		JDialog dialog = new FindDialog(BrowserApiDemo.this, null); //  non-modal
341 		dialog.show();
342 	    }
343 	});
344 
345 	menu.addSeparator();
346 	addMenuItem(menu, "Go to...", null);
347 
348        	menu = new JMenu("Build");
349 	CSH.setHelpIDString(menu,"menus.build");
350 	menuBar.add(menu);
351 	addMenuItem(menu, "Build", null);
352 	addMenuItem(menu, "Build All", null);
353 	addMenuItem(menu, "Compile File", null);
354 
355 	menu = new JMenu("Debug");
356 	CSH.setHelpIDString(menu,"menus.debug");
357 	menuBar.add(menu);
358 	addMenuItem(menu, "Start/Restart", null);
359 	addMenuItem(menu, "Stop", null);
360 
361 	menu = new JMenu("Window");
362 	CSH.setHelpIDString(menu,"menus.windows");
363 	menuBar.add(menu);
364 
365 	item1 = menuItem = addMenuItem(menu, "Source", null);
366 	item1.addActionListener(this);
367 
368 	JMenu help=new JMenu("Help");
369 	CSH.setHelpIDString(menu,"menus.help");
370 	menuBar.add(help);
371 	menu_help=new JMenuItem(helpsetLabel);
372 	menu_help.addActionListener(this);
373 	help.add(menu_help);
374 	help.addSeparator();
375 
376 	help.add(new JMenuItem("About..."));
377 	//	menuBar.setHelpMenu(help);
378 
379 	return menuBar;
380     }
381 
actionPerformed(ActionEvent e)382     public void actionPerformed(ActionEvent e) {
383 	Object source = e.getSource();
384 	if (source == menu_help) {
385 	    mainHB.setDisplayed(true);
386 	} else if (source == item1) {
387 	    try {
388 		createSourceIFrame();
389 		desktop.add(sourceIFrame, JLayeredPane.PALETTE_LAYER);
390 		sourceIFrame.setIcon(false);
391 	    } catch (PropertyVetoException ex) {
392 		// Oh well, ignore
393 	    }
394 	}
395     }
396 
397     /**
398      * For printf debugging.
399      */
400     private final static boolean debug = false;
debug(String str)401     private static void debug(String str) {
402         if (debug) {
403             System.out.println("BroserApiDemo: " + str);
404         }
405     }
406 }
407 
408 class FindDialog extends JDialog {
409     private JButton helpButton;
410     private JButton closeButton;
411     private JFrame frame;
412     private BrowserApiDemo demo;
413 
initComponents()414     private void initComponents() {
415 
416 	// playing with Boxes
417 	Box topBox = Box.createVerticalBox();
418 
419 	Box box1 = Box.createHorizontalBox();
420 	JLabel findLabel = new JLabel("Find: ");
421 	JTextField textField = new JTextField(20);
422 	box1.add(Box.createHorizontalStrut(5));
423 	box1.add(findLabel);
424 	box1.add(textField);
425 	box1.add(Box.createHorizontalStrut(5));
426 
427 	topBox.add(Box.createVerticalStrut(5));
428 	topBox.add(box1);
429 
430 	Box box3 = Box.createHorizontalBox();
431 	box3.add(Box.createHorizontalGlue());
432 	JButton findButton = new JButton("Find Next");
433 	JButton prevButton = new JButton("Find Previous");
434 	box3.add(findButton);
435 	box3.add(Box.createHorizontalStrut(10));
436 	box3.add(prevButton);
437 	box3.add(Box.createHorizontalGlue());
438 
439 	topBox.add(box3);
440 
441 	Box box4 = Box.createHorizontalBox();
442 	Box box5 = Box.createHorizontalBox();
443 
444 	JCheckBox backwardsCheck = new JCheckBox("Find Backward");
445 	JCheckBox ignoreCaseCheck = new JCheckBox("Ignore Case");
446 
447 	box4.add(Box.createHorizontalGlue());
448 	box4.add(backwardsCheck);
449 	box4.add(Box.createHorizontalGlue());
450 
451 	box5.add(Box.createHorizontalGlue());
452 	box5.add(ignoreCaseCheck);
453 	box5.add(Box.createHorizontalGlue());
454 
455 	topBox.add(box4);
456 	topBox.add(box5);
457 
458 	Box box2 = Box.createHorizontalBox();
459 	closeButton = new JButton("Close");
460 	helpButton = new JButton("Help");
461 
462 	box2.add(closeButton);
463 	box2.add(helpButton);
464 
465 	Box box6 = Box.createHorizontalBox();
466 	box6.add(Box.createHorizontalStrut(5));
467 	box6.add(new JSeparator());
468 	box6.add(Box.createHorizontalStrut(5));
469 
470 	topBox.add(Box.createVerticalStrut(10));
471 	topBox.add(box6);
472 	topBox.add(box2);
473 	getContentPane().add(topBox);
474     }
475 
FindDialog(BrowserApiDemo demo, JFrame f)476     public FindDialog(BrowserApiDemo demo, JFrame f) {
477 	super(f, "Find", false);
478 	this.frame = f;
479 	this.demo = demo;
480 	initComponents();
481 	pack();
482 
483 	closeButton.addActionListener(new ActionListener() {
484 	    public void actionPerformed(ActionEvent e) {
485 		setVisible(false);
486 		dispose();
487 	    }
488 	});
489 	demo.mainHB.enableHelp(helpButton, "browse.strings", null);
490 	placeDialog();
491 
492 	show();
493     }
494 
placeDialog()495     protected void placeDialog() {
496 	if (frame != null) {
497 	    int x = frame.getLocation().x + 30;
498 	    int y = frame.getLocation().y + 100;
499 	    setLocation(x, y);
500 	}
501     }
502 
503 }
504