001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.javahelp;
043:
044: import com.sun.java.help.impl.ViewAwareComponent;
045: import java.awt.Color;
046: import java.awt.Cursor;
047: import java.awt.Font;
048: import java.awt.FontMetrics;
049: import java.awt.Insets;
050: import java.awt.Rectangle;
051: import java.awt.event.ActionListener;
052: import java.awt.event.ActionEvent;
053: import java.awt.event.MouseEvent;
054: import java.awt.event.MouseListener;
055: import javax.swing.Icon;
056: import javax.swing.JButton;
057: import javax.swing.UIManager;
058: import javax.swing.SwingUtilities;
059: import javax.swing.SwingConstants;
060: import javax.swing.plaf.basic.BasicButtonUI;
061: import javax.swing.border.EmptyBorder;
062: import javax.swing.text.html.HTMLDocument;
063: import javax.swing.text.html.StyleSheet;
064: import javax.swing.text.AttributeSet;
065: import javax.swing.text.StyleConstants;
066: import javax.swing.text.SimpleAttributeSet;
067: import javax.swing.text.View;
068: import java.net.MalformedURLException;
069: import java.net.URL;
070:
071: import org.openide.awt.HtmlBrowser;
072: import org.openide.util.NbBundle;
073:
074: /**
075: * This class is a lightweight component to be included in HTML content within
076: * JHContentViewer. It invokes default IDE html browser to show external URL.
077: * (Default browser should be external browser to show external URL properly.
078: * Component is displayed as a mouse enabled Label. Only text is supported.
079: * <p>
080: * To use this class within HTML content use the <object> tag. Below is an
081: * example usage:
082: * <p><pre>
083: * <object CLASSID="java:org.netbeans.module.javahelp.BrowserDisplayer">
084: * <param name="content" value="http://www.netbeans.org">
085: * <param name="text" value="Click here">
086: * <param name="textFontFamily" value="SansSerif">
087: * <param name="textFontSize" value="x-large">
088: * <param name="textFontWeight" value="plain">
089: * <param name="textFontStyle" value="italic">
090: * <param name="textColor" value="red">
091: * </object>
092: * </pre><p>
093: * Valid parameters are:
094: * <ul>
095: * <li>content - a valid external url like http://java.sun.com
096: * @see setContent
097: * <li>text - the text of the activator
098: * @see setText
099: * <li>textFontFamily - the font family of the activator text
100: * @see setTextFontFamily
101: * <li>textFontSize - the size of the activator text font. Size is specified
102: * in a css terminology. See the setTextFontSize for acceptable syntax.
103: * @see setTextFontSize
104: * <li>textFontWeight - the activator text font weight
105: * @see setTextFontWeight
106: * <li>textFontStyle - the activator text font style
107: * @see setTextFontStyle
108: * <li>textColor - the activator text color
109: * @see setTextColor
110: * <ul>
111: *
112: * @author Marek Slama
113: */
114: public class BrowserDisplayer extends JButton implements
115: ActionListener, ViewAwareComponent {
116: private View myView;
117: private SimpleAttributeSet textAttribs;
118: private HTMLDocument doc;
119: private URL base;
120:
121: private final static Cursor handCursor = Cursor
122: .getPredefinedCursor(Cursor.HAND_CURSOR);
123:
124: private Cursor origCursor;
125:
126: /**
127: * Create a secondaryviewer. By default the viewer creates a button with
128: * the text of ">"
129: */
130: public BrowserDisplayer() {
131: super ();
132: setMargin(new Insets(0, 0, 0, 0));
133: createLinkLabel();
134: addActionListener(this );
135: origCursor = getCursor();
136: getAccessibleContext().setAccessibleDescription(
137: NbBundle.getMessage(BrowserDisplayer.class,
138: "ACSD_Label"));
139: addMouseListener(new MouseListener() {
140: public void mouseClicked(MouseEvent e) {
141: }
142:
143: public void mouseEntered(MouseEvent e) {
144: setCursor(handCursor);
145: }
146:
147: public void mouseExited(MouseEvent e) {
148: setCursor(origCursor);
149: }
150:
151: public void mousePressed(MouseEvent e) {
152: }
153:
154: public void mouseReleased(MouseEvent e) {
155: }
156: });
157: }
158:
159: /**
160: * Sets data optained from the View
161: */
162: public void setViewData(View v) {
163: myView = v;
164: doc = (HTMLDocument) myView.getDocument();
165: base = doc.getBase();
166:
167: // Set the current font information in the local text attributes
168: Font font = getFont();
169: textAttribs = new SimpleAttributeSet();
170: textAttribs.removeAttribute(StyleConstants.FontSize);
171: textAttribs.removeAttribute(StyleConstants.Bold);
172: textAttribs.removeAttribute(StyleConstants.Italic);
173: textAttribs.addAttribute(StyleConstants.FontFamily, font
174: .getName());
175: textAttribs.addAttribute(StyleConstants.FontSize, new Integer(
176: font.getSize()));
177: textAttribs.addAttribute(StyleConstants.Bold, Boolean
178: .valueOf(font.isBold()));
179: textAttribs.addAttribute(StyleConstants.Italic, Boolean
180: .valueOf(font.isItalic()));
181: }
182:
183: /**
184: * properties
185: */
186: private String content = "";
187:
188: /**
189: * Set the content for the secondary viewer
190: * @param content a valid URL
191: */
192: public void setContent(String content) {
193: this .content = content;
194: }
195:
196: /**
197: * Returns the content of the secondary viewer
198: */
199: public String getContent() {
200: return content;
201: }
202:
203: /**
204: * Creates a link label. A link label is a form of a JButton but without a
205: * button like appearance.
206: */
207: private void createLinkLabel() {
208: setBorder(new EmptyBorder(1, 1, 1, 1));
209: setBorderPainted(false);
210: setFocusPainted(false);
211: setAlignmentY(getPreferredLabelAlignment());
212: setContentAreaFilled(false);
213: setHorizontalAlignment(SwingConstants.LEFT);
214: setBackground(UIManager.getColor("EditorPane.background"));
215: if (textAttribs != null
216: && textAttribs.isDefined(StyleConstants.Foreground)) {
217: setForeground((Color) textAttribs
218: .getAttribute(StyleConstants.Foreground));
219: } else {
220: setForeground(Color.blue);
221: }
222: invalidate();
223: }
224:
225: /**
226: * Determine the alignment offset so the text is aligned with other views
227: * correctly.
228: */
229: private float getPreferredLabelAlignment() {
230: Icon icon = (Icon) getIcon();
231: String text = getText();
232:
233: Font font = getFont();
234: FontMetrics fm = getToolkit().getFontMetrics(font);
235:
236: Rectangle iconR = new Rectangle();
237: Rectangle textR = new Rectangle();
238: Rectangle viewR = new Rectangle(Short.MAX_VALUE,
239: Short.MAX_VALUE);
240:
241: SwingUtilities.layoutCompoundLabel(this , fm, text, icon,
242: getVerticalAlignment(), getHorizontalAlignment(),
243: getVerticalTextPosition(), getHorizontalTextPosition(),
244: viewR, iconR, textR, (text == null ? 0
245: : ((BasicButtonUI) ui)
246: .getDefaultTextIconGap(this )));
247:
248: // The preferred size of the button is the size of
249: // the text and icon rectangles plus the buttons insets.
250: Rectangle r = iconR.union(textR);
251:
252: Insets insets = getInsets();
253: r.height += insets.top + insets.bottom;
254:
255: // Ensure that the height of the button is odd,
256: // to allow for the focus line.
257: if (r.height % 2 == 0) {
258: r.height += 1;
259: }
260:
261: float offAmt = fm.getMaxAscent() + insets.top;
262: return offAmt / (float) r.height;
263: }
264:
265: /**
266: * Sets the text Font family for the activator text.
267: * For JDK 1.1 this must a family name of Dialog, DialogInput, Monospaced,
268: * Serif, SansSerif, or Symbol.
269: */
270: public void setTextFontFamily(String family) {
271: textAttribs.removeAttribute(StyleConstants.FontFamily);
272: textAttribs.addAttribute(StyleConstants.FontFamily, family);
273: setFont(getAttributeSetFont(textAttribs));
274: Font font = getFont();
275: }
276:
277: /**
278: * Returns the text Font family name of the activator text
279: */
280: public String getTextFontFamily() {
281: return StyleConstants.getFontFamily(textAttribs);
282: }
283:
284: /**
285: * Sets the text size for the activator text.
286: * The String size is a valid Cascading Style Sheet value for
287: * text size. Acceptable values are as follows:
288: * <ul>
289: * <li>xx-small
290: * <li>x-small
291: * <li>small
292: * <li>medium
293: * <li>large
294: * <li>x-large
295: * <li>xx-large
296: * <li>bigger - increase the current base font size by 1
297: * <li>smaller - decrease the current base font size by 1
298: * <li>xxpt - set the font size to a specific pt value of "xx"
299: * <li>+x - increase the current base font size by a value of "x"
300: * <li>-x - decrease the current base font size by a value of "x"
301: * <li>x - set the font size to the point size associated with
302: * the index "x"
303: * </ul>
304: */
305: public void setTextFontSize(String size) {
306: int newsize;
307: StyleSheet css = doc.getStyleSheet();
308: try {
309: if (size.equals("xx-small")) {
310: newsize = (int) css.getPointSize(0);
311: } else if (size.equals("x-small")) {
312: newsize = (int) css.getPointSize(1);
313: } else if (size.equals("small")) {
314: newsize = (int) css.getPointSize(2);
315: } else if (size.equals("medium")) {
316: newsize = (int) css.getPointSize(3);
317: } else if (size.equals("large")) {
318: newsize = (int) css.getPointSize(4);
319: } else if (size.equals("x-large")) {
320: newsize = (int) css.getPointSize(5);
321: } else if (size.equals("xx-large")) {
322: newsize = (int) css.getPointSize(6);
323: } else if (size.equals("bigger")) {
324: newsize = (int) css.getPointSize("+1");
325: } else if (size.equals("smaller")) {
326: newsize = (int) css.getPointSize("-1");
327: } else if (size.endsWith("pt")) {
328: String sz = size.substring(0, size.length() - 2);
329: newsize = Integer.parseInt(sz);
330: } else {
331: newsize = (int) css.getPointSize(size);
332: }
333: } catch (NumberFormatException nfe) {
334: return;
335: }
336: if (newsize == 0) {
337: return;
338: }
339: textAttribs.removeAttribute(StyleConstants.FontSize);
340: textAttribs.addAttribute(StyleConstants.FontSize, new Integer(
341: newsize));
342: setFont(getAttributeSetFont(textAttribs));
343: Font font = getFont();
344: }
345:
346: /**
347: * Returns the text Font family name of the activator text
348: */
349: public String getTextFontSize() {
350: return Integer
351: .toString(StyleConstants.getFontSize(textAttribs));
352: }
353:
354: /**
355: * Sets the text Font Weigth for the activator text.
356: * Valid weights are
357: * <ul>
358: * <li>plain
359: * <li>bold
360: * </ul>
361: */
362: public void setTextFontWeight(String weight) {
363: boolean isBold = false;
364: if ("bold".equals(weight)) {
365: isBold = true;
366: } else {
367: isBold = false;
368: }
369: textAttribs.removeAttribute(StyleConstants.Bold);
370: textAttribs.addAttribute(StyleConstants.Bold, Boolean
371: .valueOf(isBold));
372: setFont(getAttributeSetFont(textAttribs));
373: Font font = getFont();
374: }
375:
376: /**
377: * Returns the text Font weight of the activator text
378: */
379: public String getTextFontWeight() {
380: if (StyleConstants.isBold(textAttribs)) {
381: return "bold";
382: }
383: return "plain";
384: }
385:
386: /**
387: * Sets the text Font Style for the activator text.
388: * Valid font styles are
389: * <ul>
390: * <li>plain
391: * <li>italic
392: * </ul>
393: */
394: public void setTextFontStyle(String style) {
395: boolean isItalic = false;
396: if ("italic".equals(style)) {
397: isItalic = true;
398: } else {
399: isItalic = false;
400: }
401: textAttribs.removeAttribute(StyleConstants.Italic);
402: textAttribs.addAttribute(StyleConstants.Italic, Boolean
403: .valueOf(isItalic));
404: setFont(getAttributeSetFont(textAttribs));
405: Font font = getFont();
406: }
407:
408: /**
409: * Returns the text Font style of the activator text
410: */
411: public String getTextFontStyle() {
412: if (StyleConstants.isItalic(textAttribs)) {
413: return "italic";
414: }
415: return "plain";
416: }
417:
418: /**
419: * Sets the text Color for the activator text.
420: * The following is a list of supported Color names
421: * <ul>
422: * <li>black
423: * <li>blue
424: * <li>cyan
425: * <li>darkGray
426: * <li>gray
427: * <li>green
428: * <li>lightGray
429: * <li>magenta
430: * <li>orange
431: * <li>pink
432: * <li>red
433: * <li>white
434: * <li>yellow
435: * </ul>
436: */
437: public void setTextColor(String name) {
438: Color color = null;
439: if ("black".equals(name)) {
440: color = Color.black;
441: } else if ("blue".equals(name)) {
442: color = Color.blue;
443: } else if ("cyan".equals(name)) {
444: color = Color.cyan;
445: } else if ("darkGray".equals(name)) {
446: color = Color.darkGray;
447: } else if ("gray".equals(name)) {
448: color = Color.gray;
449: } else if ("green".equals(name)) {
450: color = Color.green;
451: } else if ("lightGray".equals(name)) {
452: color = Color.lightGray;
453: } else if ("magenta".equals(name)) {
454: color = Color.magenta;
455: } else if ("orange".equals(name)) {
456: color = Color.orange;
457: } else if ("pink".equals(name)) {
458: color = Color.pink;
459: } else if ("red".equals(name)) {
460: color = Color.red;
461: } else if ("white".equals(name)) {
462: color = Color.white;
463: } else if ("yellow".equals(name)) {
464: color = Color.yellow;
465: }
466:
467: if (color == null) {
468: return;
469: }
470: textAttribs.removeAttribute(StyleConstants.Foreground);
471: textAttribs.addAttribute(StyleConstants.Foreground, color);
472: setForeground(color);
473: }
474:
475: /**
476: * Returns the text Color of the activator text
477: */
478: public String getTextColor() {
479: Color color = getForeground();
480: return color.toString();
481: }
482:
483: /**
484: * Gets the font from an attribute set. This is
485: * implemented to try and fetch a cached font
486: * for the given AttributeSet, and if that fails
487: * the font features are resolved and the
488: * font is fetched from the low-level font cache.
489: * Font's are cached in the StyleSheet of a document
490: *
491: * @param attr the attribute set
492: * @return the font
493: */
494: private Font getAttributeSetFont(AttributeSet attr) {
495: // PENDING(prinz) add cache behavior
496: int style = Font.PLAIN;
497: if (StyleConstants.isBold(attr)) {
498: style |= Font.BOLD;
499: }
500: if (StyleConstants.isItalic(attr)) {
501: style |= Font.ITALIC;
502: }
503: String family = StyleConstants.getFontFamily(attr);
504: int size = StyleConstants.getFontSize(attr);
505:
506: /**
507: * if either superscript or subscript is
508: * is set, we need to reduce the font size
509: * by 2.
510: */
511: if (StyleConstants.isSuperscript(attr)
512: || StyleConstants.isSubscript(attr)) {
513: size -= 2;
514: }
515:
516: // fonts are cached in the StyleSheet so use that
517: return doc.getStyleSheet().getFont(family, style, size);
518: }
519:
520: /**
521: * Displays the viewer according to the viewerType
522: */
523: public void actionPerformed(ActionEvent e) {
524: URL link;
525: try {
526: link = new URL(content);
527: } catch (MalformedURLException exc) {
528: //XXX log something to ide.log??
529: return;
530: }
531: HtmlBrowser.URLDisplayer.getDefault().showURL(link);
532: }
533:
534: }
|