KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javahelp > BrowserDisplayer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javahelp;
21
22 import com.sun.java.help.impl.ViewAwareComponent;
23 import java.awt.Color JavaDoc;
24 import java.awt.Cursor JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.FontMetrics JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.awt.event.MouseListener JavaDoc;
33 import javax.swing.Icon JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.UIManager JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import javax.swing.SwingConstants JavaDoc;
38 import javax.swing.plaf.basic.BasicButtonUI JavaDoc;
39 import javax.swing.border.EmptyBorder JavaDoc;
40 import javax.swing.text.html.HTMLDocument JavaDoc;
41 import javax.swing.text.html.StyleSheet JavaDoc;
42 import javax.swing.text.AttributeSet JavaDoc;
43 import javax.swing.text.StyleConstants JavaDoc;
44 import javax.swing.text.SimpleAttributeSet JavaDoc;
45 import javax.swing.text.View JavaDoc;
46 import java.net.MalformedURLException JavaDoc;
47 import java.net.URL JavaDoc;
48
49 import org.openide.awt.HtmlBrowser;
50 import org.openide.util.NbBundle;
51
52 /**
53  * This class is a lightweight component to be included in HTML content within
54  * JHContentViewer. It invokes default IDE html browser to show external URL.
55  * (Default browser should be external browser to show external URL properly.
56  * Component is displayed as a mouse enabled Label. Only text is supported.
57  * <p>
58  * To use this class within HTML content use the &ltobject&gt tag. Below is an
59  * example usage:
60  * <p><pre>
61  * &ltobject CLASSID="java:org.netbeans.module.javahelp.BrowserDisplayer"&gt
62  * &ltparam name="content" value="http://www.netbeans.org"&gt
63  * &ltparam name="text" value="Click here"&gt
64  * &ltparam name="textFontFamily" value="SansSerif"&gt
65  * &ltparam name="textFontSize" value="x-large"&gt
66  * &ltparam name="textFontWeight" value="plain"&gt
67  * &ltparam name="textFontStyle" value="italic"&gt
68  * &ltparam name="textColor" value="red"&gt
69  * &lt/object&gt
70  * </pre><p>
71  * Valid parameters are:
72  * <ul>
73  * <li>content - a valid external url like http://java.sun.com
74  * @see setContent
75  * <li>text - the text of the activator
76  * @see setText
77  * <li>textFontFamily - the font family of the activator text
78  * @see setTextFontFamily
79  * <li>textFontSize - the size of the activator text font. Size is specified
80  * in a css terminology. See the setTextFontSize for acceptable syntax.
81  * @see setTextFontSize
82  * <li>textFontWeight - the activator text font weight
83  * @see setTextFontWeight
84  * <li>textFontStyle - the activator text font style
85  * @see setTextFontStyle
86  * <li>textColor - the activator text color
87  * @see setTextColor
88  * <ul>
89  *
90  * @author Marek Slama
91  */

92 public class BrowserDisplayer extends JButton JavaDoc implements ActionListener JavaDoc, ViewAwareComponent {
93     private View JavaDoc myView;
94     private SimpleAttributeSet JavaDoc textAttribs;
95     private HTMLDocument JavaDoc doc;
96     private URL JavaDoc base;
97
98     private final static Cursor JavaDoc handCursor =
99     Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
100
101     private Cursor JavaDoc origCursor;
102
103     /**
104      * Create a secondaryviewer. By default the viewer creates a button with
105      * the text of ">"
106      */

107     public BrowserDisplayer() {
108     super();
109     setMargin(new Insets JavaDoc(0,0,0,0));
110     createLinkLabel();
111     addActionListener(this);
112     origCursor = getCursor();
113         getAccessibleContext().setAccessibleDescription
114         (NbBundle.getMessage(BrowserDisplayer.class,"ACSD_Label"));
115     addMouseListener(new MouseListener JavaDoc() {
116         public void mouseClicked(MouseEvent JavaDoc e) {
117         }
118
119         public void mouseEntered(MouseEvent JavaDoc e) {
120         setCursor(handCursor);
121         }
122
123         public void mouseExited(MouseEvent JavaDoc e) {
124         setCursor(origCursor);
125         }
126
127         public void mousePressed(MouseEvent JavaDoc e) {
128         }
129
130         public void mouseReleased(MouseEvent JavaDoc e) {
131         }
132     });
133     }
134     
135     /**
136      * Sets data optained from the View
137      */

138     public void setViewData(View JavaDoc v) {
139     myView = v;
140     doc = (HTMLDocument JavaDoc) myView.getDocument();
141     base = doc.getBase();
142
143     // Set the current font information in the local text attributes
144
Font JavaDoc font = getFont();
145     textAttribs = new SimpleAttributeSet JavaDoc();
146     textAttribs.removeAttribute(StyleConstants.FontSize);
147     textAttribs.removeAttribute(StyleConstants.Bold);
148     textAttribs.removeAttribute(StyleConstants.Italic);
149     textAttribs.addAttribute(StyleConstants.FontFamily,
150                  font.getName());
151     textAttribs.addAttribute(StyleConstants.FontSize,
152                  new Integer JavaDoc(font.getSize()));
153     textAttribs.addAttribute(StyleConstants.Bold,
154                  Boolean.valueOf(font.isBold()));
155     textAttribs.addAttribute(StyleConstants.Italic,
156                  Boolean.valueOf(font.isItalic()));
157     }
158     
159     /**
160      * properties
161      */

162     private String JavaDoc content = "";
163
164     /**
165      * Set the content for the secondary viewer
166      * @param content a valid URL
167      */

168     public void setContent(String JavaDoc content) {
169     this.content = content;
170     }
171
172     /**
173      * Returns the content of the secondary viewer
174      */

175     public String JavaDoc getContent() {
176     return content;
177     }
178
179     /**
180      * Creates a link label. A link label is a form of a JButton but without a
181      * button like appearance.
182      */

183     private void createLinkLabel() {
184     setBorder(new EmptyBorder JavaDoc(1,1,1,1));
185     setBorderPainted(false);
186     setFocusPainted(false);
187     setAlignmentY(getPreferredLabelAlignment());
188     setContentAreaFilled(false);
189         setHorizontalAlignment(SwingConstants.LEFT);
190     setBackground(UIManager.getColor("EditorPane.background"));
191     if (textAttribs != null &&
192         textAttribs.isDefined(StyleConstants.Foreground)) {
193         setForeground((Color JavaDoc)textAttribs.getAttribute(StyleConstants.Foreground));
194     } else {
195         setForeground(Color.blue);
196     }
197     invalidate();
198     }
199
200     /**
201      * Determine the alignment offset so the text is aligned with other views
202      * correctly.
203      */

204     private float getPreferredLabelAlignment() {
205         Icon JavaDoc icon = (Icon JavaDoc)getIcon();
206         String JavaDoc text = getText();
207
208         Font JavaDoc font = getFont();
209         FontMetrics JavaDoc fm = getToolkit().getFontMetrics(font);
210           
211         Rectangle JavaDoc iconR = new Rectangle JavaDoc();
212         Rectangle JavaDoc textR = new Rectangle JavaDoc();
213         Rectangle JavaDoc viewR = new Rectangle JavaDoc(Short.MAX_VALUE, Short.MAX_VALUE);
214
215         SwingUtilities.layoutCompoundLabel(
216             this, fm, text, icon,
217             getVerticalAlignment(), getHorizontalAlignment(),
218             getVerticalTextPosition(), getHorizontalTextPosition(),
219             viewR, iconR, textR,
220         (text == null ? 0 : ((BasicButtonUI JavaDoc)ui).getDefaultTextIconGap(this))
221         );
222
223         // The preferred size of the button is the size of
224
// the text and icon rectangles plus the buttons insets.
225
Rectangle JavaDoc r = iconR.union(textR);
226
227         Insets JavaDoc insets = getInsets();
228         r.height += insets.top + insets.bottom;
229
230         // Ensure that the height of the button is odd,
231
// to allow for the focus line.
232
if(r.height % 2 == 0) {
233         r.height += 1;
234     }
235
236     float offAmt = fm.getMaxAscent() + insets.top;
237     return offAmt/(float)r.height;
238     }
239     
240     /**
241      * Sets the text Font family for the activator text.
242      * For JDK 1.1 this must a family name of Dialog, DialogInput, Monospaced,
243      * Serif, SansSerif, or Symbol.
244      */

245     public void setTextFontFamily(String JavaDoc family) {
246     textAttribs.removeAttribute(StyleConstants.FontFamily);
247     textAttribs.addAttribute(StyleConstants.FontFamily, family);
248     setFont(getAttributeSetFont(textAttribs));
249     Font JavaDoc font = getFont();
250     }
251
252     /**
253      * Returns the text Font family name of the activator text
254      */

255     public String JavaDoc getTextFontFamily() {
256     return StyleConstants.getFontFamily(textAttribs);
257     }
258
259     /**
260      * Sets the text size for the activator text.
261      * The String size is a valid Cascading Style Sheet value for
262      * text size. Acceptable values are as follows:
263      * <ul>
264      * <li>xx-small
265      * <li>x-small
266      * <li>small
267      * <li>medium
268      * <li>large
269      * <li>x-large
270      * <li>xx-large
271      * <li>bigger - increase the current base font size by 1
272      * <li>smaller - decrease the current base font size by 1
273      * <li>xxpt - set the font size to a specific pt value of "xx"
274      * <li>+x - increase the current base font size by a value of "x"
275      * <li>-x - decrease the current base font size by a value of "x"
276      * <li>x - set the font size to the point size associated with
277      * the index "x"
278      * </ul>
279      */

280     public void setTextFontSize(String JavaDoc size) {
281     int newsize;
282     StyleSheet JavaDoc css = doc.getStyleSheet();
283     try {
284         if (size.equals("xx-small")) {
285         newsize = (int)css.getPointSize(0);
286         } else if (size.equals("x-small")) {
287         newsize = (int)css.getPointSize(1);
288         } else if (size.equals("small")) {
289         newsize = (int)css.getPointSize(2);
290         } else if (size.equals("medium")) {
291         newsize = (int)css.getPointSize(3);
292         } else if (size.equals("large")) {
293         newsize = (int)css.getPointSize(4);
294         } else if (size.equals("x-large")) {
295         newsize = (int)css.getPointSize(5);
296         } else if (size.equals("xx-large")) {
297         newsize = (int)css.getPointSize(6);
298         } else if (size.equals("bigger")) {
299         newsize = (int)css.getPointSize("+1");
300         } else if (size.equals("smaller")) {
301         newsize = (int)css.getPointSize("-1");
302         } else if (size.endsWith("pt")) {
303         String JavaDoc sz = size.substring(0, size.length() - 2);
304         newsize = Integer.parseInt(sz);
305         } else {
306         newsize = (int) css.getPointSize(size);
307         }
308     } catch (NumberFormatException JavaDoc nfe) {
309         return;
310     }
311     if (newsize == 0) {
312         return;
313     }
314     textAttribs.removeAttribute(StyleConstants.FontSize);
315     textAttribs.addAttribute(StyleConstants.FontSize,
316                  new Integer JavaDoc(newsize));
317     setFont(getAttributeSetFont(textAttribs));
318     Font JavaDoc font = getFont();
319     }
320     
321     /**
322      * Returns the text Font family name of the activator text
323      */

324     public String JavaDoc getTextFontSize() {
325     return Integer.toString(StyleConstants.getFontSize(textAttribs));
326     }
327
328     /**
329      * Sets the text Font Weigth for the activator text.
330      * Valid weights are
331      * <ul>
332      * <li>plain
333      * <li>bold
334      * </ul>
335      */

336     public void setTextFontWeight(String JavaDoc weight) {
337     boolean isBold=false;
338     if ("bold".equals(weight)) {
339         isBold = true;
340     } else {
341         isBold = false;
342     }
343     textAttribs.removeAttribute(StyleConstants.Bold);
344     textAttribs.addAttribute(StyleConstants.Bold, Boolean.valueOf(isBold));
345     setFont(getAttributeSetFont(textAttribs));
346     Font JavaDoc font = getFont();
347     }
348
349     /**
350      * Returns the text Font weight of the activator text
351      */

352     public String JavaDoc getTextFontWeight() {
353     if (StyleConstants.isBold(textAttribs)) {
354         return "bold";
355     }
356     return "plain";
357     }
358
359     /**
360      * Sets the text Font Style for the activator text.
361      * Valid font styles are
362      * <ul>
363      * <li>plain
364      * <li>italic
365      * </ul>
366      */

367     public void setTextFontStyle(String JavaDoc style) {
368     boolean isItalic=false;
369     if ("italic".equals(style)) {
370         isItalic = true;
371     } else {
372         isItalic = false;
373     }
374     textAttribs.removeAttribute(StyleConstants.Italic);
375     textAttribs.addAttribute(StyleConstants.Italic, Boolean.valueOf(isItalic));
376     setFont(getAttributeSetFont(textAttribs));
377     Font JavaDoc font = getFont();
378     }
379
380     /**
381      * Returns the text Font style of the activator text
382      */

383     public String JavaDoc getTextFontStyle() {
384     if (StyleConstants.isItalic(textAttribs)) {
385         return "italic";
386     }
387     return "plain";
388     }
389
390     /**
391      * Sets the text Color for the activator text.
392      * The following is a list of supported Color names
393      * <ul>
394      * <li>black
395      * <li>blue
396      * <li>cyan
397      * <li>darkGray
398      * <li>gray
399      * <li>green
400      * <li>lightGray
401      * <li>magenta
402      * <li>orange
403      * <li>pink
404      * <li>red
405      * <li>white
406      * <li>yellow
407      * </ul>
408      */

409     public void setTextColor(String JavaDoc name) {
410     Color JavaDoc color=null;
411     if ("black".equals(name)) {
412         color = Color.black;
413     } else if ("blue".equals(name)) {
414         color = Color.blue;
415     } else if ("cyan".equals(name)) {
416         color = Color.cyan;
417     } else if ("darkGray".equals(name)) {
418         color = Color.darkGray;
419     } else if ("gray".equals(name)) {
420         color = Color.gray;
421     } else if ("green".equals(name)) {
422         color = Color.green;
423     } else if ("lightGray".equals(name)) {
424         color = Color.lightGray;
425     } else if ("magenta".equals(name)) {
426         color = Color.magenta;
427     } else if ("orange".equals(name)) {
428         color = Color.orange;
429     } else if ("pink".equals(name)) {
430         color = Color.pink;
431     } else if ("red".equals(name)) {
432         color = Color.red;
433     } else if ("white".equals(name)) {
434         color = Color.white;
435     } else if ("yellow".equals(name)) {
436         color = Color.yellow;
437     }
438
439     if (color == null) {
440         return;
441     }
442     textAttribs.removeAttribute(StyleConstants.Foreground);
443     textAttribs.addAttribute(StyleConstants.Foreground, color);
444     setForeground(color);
445     }
446
447     /**
448      * Returns the text Color of the activator text
449      */

450     public String JavaDoc getTextColor() {
451     Color JavaDoc color = getForeground();
452     return color.toString();
453     }
454
455     /**
456      * Gets the font from an attribute set. This is
457      * implemented to try and fetch a cached font
458      * for the given AttributeSet, and if that fails
459      * the font features are resolved and the
460      * font is fetched from the low-level font cache.
461      * Font's are cached in the StyleSheet of a document
462      *
463      * @param attr the attribute set
464      * @return the font
465      */

466     private Font JavaDoc getAttributeSetFont(AttributeSet JavaDoc attr) {
467         // PENDING(prinz) add cache behavior
468
int style = Font.PLAIN;
469         if (StyleConstants.isBold(attr)) {
470             style |= Font.BOLD;
471         }
472         if (StyleConstants.isItalic(attr)) {
473             style |= Font.ITALIC;
474         }
475         String JavaDoc family = StyleConstants.getFontFamily(attr);
476         int size = StyleConstants.getFontSize(attr);
477
478     /**
479      * if either superscript or subscript is
480      * is set, we need to reduce the font size
481      * by 2.
482      */

483     if (StyleConstants.isSuperscript(attr) ||
484         StyleConstants.isSubscript(attr)) {
485         size -= 2;
486     }
487
488     // fonts are cached in the StyleSheet so use that
489
return doc.getStyleSheet().getFont(family, style, size);
490     }
491
492     /**
493      * Displays the viewer according to the viewerType
494      */

495     public void actionPerformed(ActionEvent JavaDoc e) {
496         URL JavaDoc link;
497         try {
498             link = new URL JavaDoc(content);
499         } catch (MalformedURLException JavaDoc exc) {
500             //XXX log something to ide.log??
501
return;
502         }
503         HtmlBrowser.URLDisplayer.getDefault().showURL(link);
504     }
505
506 }
507
Popular Tags