Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
   * Other names may be trademarks of their respective owners.
   *
   * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common
  * Development and Distribution License("CDDL") (collectively, the
  * "License"). You may not use this file except in compliance with the
  * License. You can obtain a copy of the License at
  * http://www.netbeans.org/cddl-gplv2.html
  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  * specific language governing permissions and limitations under the
  * License.  When distributing the software, include this License Header
  * Notice in each file and include the License file at
  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Oracle in the GPL Version 2 section of the License file that
  * accompanied this code. If applicable, add the following below the
  * License Header, with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  *
  * Contributor(s):
  *
  * The Original Software is NetBeans. The Initial Developer of the Original
  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  * Microsystems, Inc. All Rights Reserved.
  *
  * If you wish your version of this file to be governed by only the CDDL
  * or only the GPL Version 2, indicate your decision by adding
  * "[Contributor] elects to include this software in this distribution
  * under the [CDDL or GPL Version 2] license." If you do not indicate a
  * single choice of license, a recipient has the option to distribute
  * your version of this file under either the CDDL, the GPL Version 2 or
  * to extend the choice of license to its licensees as provided above.
  * However, if you add GPL Version 2 code and therefore, elected the GPL
  * Version 2 license, then the option applies only if the new code is
  * made subject to such option by the copyright holder.
  */
 
 
 package org.netbeans.modules.java.navigation;
 
 import java.awt.*;
 import java.net.URL;
 import java.util.List;
 import javax.swing.*;
 
 

Author(s):
Martin Roskanin, Dusan Balek
 
 public class DocumentationScrollPane extends JScrollPane {
 
     private static final String BACK = "org/netbeans/modules/java/navigation/resources/back.png"//NOI18N
     private static final String FORWARD = "org/netbeans/modules/java/navigation/resources/forward.png"//NOI18N
     private static final String GOTO_SOURCE = "org/netbeans/modules/java/navigation/resources/open_source_in_editor.png"//NOI18N
     private static final String SHOW_WEB = "org/netbeans/modules/java/navigation/resources/open_in_external_browser.png"//NOI18N
 
     private static final String JAVADOC_BACK = "javadoc-back"//NOI18N
     private static final String JAVADOC_FORWARD = "javadoc-forward"//NOI18N    
     private static final String JAVADOC_OPEN_IN_BROWSER = "javadoc-open-in-browser"//NOI18N    
     private static final String JAVADOC_OPEN_SOURCE = "javadoc-open-source"//NOI18N    
     
     private static final int ACTION_JAVADOC_ESCAPE = 0;
     private static final int ACTION_JAVADOC_BACK = 1;
     private static final int ACTION_JAVADOC_FORWARD = 2;
    private static final int ACTION_JAVADOC_OPEN_IN_BROWSER = 3;
    private static final int ACTION_JAVADOC_OPEN_SOURCE = 4;
    private static final RequestProcessor RP = new RequestProcessor(DocumentationScrollPane.class);
    private JButton bBackbForwardbGoToSourcebShowWeb;    
    private HTMLDocView view;
    
    // doc browser history
    private List<ElementJavadochistory = new ArrayList<ElementJavadoc>(5);
    private int currentHistoryIndex = -1;
    protected ElementJavadoc currentDocumentation = null;
    private Reference<FileObjectcurrentFile;
    //@GuardedBy("this")
    private RequestProcessor.Task task;    
    
    
Creates a new instance of ScrollJavaDocPane
    public DocumentationScrollPaneboolean keepDefaultBorder ) {
        super();
 
        // Add the completion doc view
        //XXX fix bg color
         = new HTMLDocViewgetDefaultBackground() );
        setViewportView();
        getAccessibleContext().setAccessibleName(NbBundle.getMessage(DocumentationScrollPane.class"ACSN_DocScrollPane"));
        getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DocumentationScrollPane.class"ACSD_DocScrollPane"));
        .getAccessibleContext().setAccessibleName(NbBundle.getMessage(DocumentationScrollPane.class"ACSN_DocScrollPane"));
        .getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DocumentationScrollPane.class"ACSD_DocScrollPane"));
        installTitleComponent();
//        installKeybindings(view);
        setFocusable(true);
        
        if( !keepDefaultBorder )
            setBorder( BorderFactory.createEmptyBorder() );
    }
    
    void setData(
        final FileObject owner,
        final ElementJavadoc doc) {
        assert SwingUtilities.isEventDispatchThread();
         = owner == null ? null : new WeakReference<FileObject>(owner);
        setData(doctrue);
    }
    void clearContent(final FileObject owner) {
        assert SwingUtilities.isEventDispatchThread();
        if (owner != null &&  != null && owner.equals(.get())) {
            setData(nulltrue);
        }
    }
    
    private void setData(ElementJavadoc docboolean clearHistory) {
        if ( doc == null || clearHistory ) {
             = new ArrayList<ElementJavadoc>(5);
        }
        setDocumentation(doc);
        ifnull != doc )
            addToHistory(doc);
    }
    
    private ImageIcon resolveIcon(String res){
        return ImageUtilities.loadImageIcon(resfalse);
    }
    private void installTitleComponent() {
        JToolBar toolbar = new JToolBar();
        toolbar.setFloatable(false);        
        toolbar.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, UIManager.getColor("controlDkShadow"))); //NOI18N
        toolbar.setLayout(new GridBagLayout());
        GridBagConstraints gdc = new GridBagConstraints();
        gdc.gridx = 0;
        gdc.gridy = 0;
        gdc.anchor = .;        
        ImageIcon icon = resolveIcon();
        if (icon != null) {            
             = new BrowserButton(icon);
            .addMouseListener(new MouseEventListener());
            .setEnabled(false);
            //bBack.setFocusable(false);
            .setContentAreaFilled(false);
            .setMargin(new Insets(0, 0, 0, 0));
            .setToolTipText(NbBundle.getMessage(DocumentationScrollPane.class"HINT_doc_browser_back_button")); //NOI18N
            toolbar.add(gdc);
        }
        
        gdc.gridx = 1;
        gdc.gridy = 0;
        gdc.anchor = .;        
        icon = resolveIcon();
        if (icon != null) {
             = new BrowserButton(icon);
            .setEnabled(false);
            //bForward.setFocusable(false);
            .setContentAreaFilled(false);
            .setToolTipText(NbBundle.getMessage(DocumentationScrollPane.class"HINT_doc_browser_forward_button")); //NOI18N
            .setMargin(new Insets(0, 0, 0, 0));
            toolbar.add(gdc);
        }
        
        gdc.gridx = 2;
        gdc.gridy = 0;
        gdc.anchor = .;        
        icon = resolveIcon();
        if (icon != null) {            
             = new BrowserButton(icon);
            .setEnabled(false);
            //bShowWeb.setFocusable(false);
            .setContentAreaFilled(false);
            .setMargin(new Insets(0, 0, 0, 0));
            .setToolTipText(NbBundle.getMessage(DocumentationScrollPane.class"HINT_doc_browser_show_web_button")); //NOI18N
            toolbar.add(gdc);
        }
        
        gdc.gridx = 3;
        gdc.gridy = 0;
        gdc.weightx = 1.0;
        gdc.anchor = .;                
        icon = resolveIcon();
        if (icon != null) {
             = new BrowserButton(icon);
            .setEnabled(false);
            //bGoToSource.setFocusable(false);
            .setContentAreaFilled(false);
            .setMargin(new Insets(0, 0, 0, 0));
            .setToolTipText(NbBundle.getMessage(DocumentationScrollPane.class"HINT_doc_browser_goto_source_button")); //NOI18N
            toolbar.add(gdc);
        }
        setColumnHeaderView(toolbar);
        installKeybindings();
    }
    
    private synchronized void setDocumentation(final ElementJavadoc doc) {
         = doc;
        ifnull != doc ) {
            if ( != null) {
                .cancel();
            }
            .setContent(NbBundle.getMessage(DocumentationScrollPane.class"TXT_LoadingJavadoc"), null);
             = .create(new Runnable(){
                @Override
                public void run() {
                    
                    final ElementJavadoc ed;
                    synchronized (DocumentationScrollPane.this) {
                        if ( != doc) {
                            return;
                        }                        
                        ed = ;
                    }
                    final String text = ed.getText();
                    final URL url = ed.getURL();
                    final boolean gtsEnabled = ed.getGotoSourceAction() != null;
                    synchronized (DocumentationScrollPane.this) {
                        if ( != doc) {
                            //Changed, do nothing.
                            return;
                        }                        
                    }
                    if (text != null){
                        SwingUtilities.invokeLater(new Runnable(){
                            @Override
                            public void run() {
                                Document document = .getDocument();
                                document.putProperty(.null);
                                if (url!=null){
                                    // fix of issue #58658
                                    if (document instanceof HTMLDocument){
                                        ((HTMLDocument)document).setBase(url);
                                    }
                                }
                                .setContent(textnull);
                                .setEnabled(url != null);
                                .setEnabled(gtsEnabled);
                            }
                        });
                    } else if (url != null){
                        SwingUtilities.invokeLater(new Runnable(){
                            @Override
                            public void run() {
                                try{
                                    .setPage(url);
                                    .setEnabled(url != null);
                                    .setEnabled(gtsEnabled);
                                }catch(IOException ioe){
                                    StatusDisplayer.getDefault().setStatusText(ioe.toString());
                                }
                            }
                        });
                    } else {
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                .setEnabled(false);
                                .setEnabled(false);
                            }
                        });
                    }
                }
            });
            .schedule(0);
        } else {
            .setEnabledfalse );
            .setEnabledfalse );
            .setContent(""null);//NOI18N
        }
    }
    
    private synchronized void addToHistory(ElementJavadoc doc) {
        int histSize = .size();
        for (int i =  + 1; i < histSizei++){
            .remove(.size() - 1);
        }
        .add(doc);
         = .size() - 1;
        if ( > 0)
            .setEnabled(true);
        .setEnabled(false);
    }
    
    private synchronized void backHistory() {
        if ( > 0 &&  <= .size()) {
            --;
            setDocumentation(.get());            
            if ( == 0)
                .setEnabled(false);
            .setEnabled(true);
        }
    }
    
    private synchronized void forwardHistory(){
        if ( <.size()-1){
            ++;
            if ( == .size() - 1)
                .setEnabled(false); 
            .setEnabled(true);
        }
    }
    
    synchronized void clearHistory(){
         = -1;
        .clear();
        .setEnabled(false);
        .setEnabled(false);
    }
    private void openInExternalBrowser(){
        if ( != null) {
            URL url = .getURL();
            if (url != null) {
                HtmlBrowser.URLDisplayer.getDefault().showURL(url);
            }
        }
    }
    
    private void goToSource() {
        Action action = .getGotoSourceAction();
        if (action != null)
            action.actionPerformed(new ActionEvent(, 0, null));        
    }

    
Attempt to find the editor keystroke for the given editor action.
    private KeyStroke[] findEditorKeys(String editorActionNameKeyStroke defaultKeyJTextComponent component) {
        // This method is implemented due to the issue
        // #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
        KeyStroke[] ret = new KeyStroke[] { defaultKey };
        if (component != null) {
            TextUI componentUI = component.getUI();
            Keymap km = component.getKeymap();
            if (componentUI != null && km != null) {
                EditorKit kit = componentUI.getEditorKit(component);
                if (kit instanceof BaseKit) {
                    Action a = ((BaseKit)kit).getActionByName(editorActionName);
                    if (a != null) {
                        KeyStroke[] keys = km.getKeyStrokesForAction(a);
                        if (keys != null && keys.length > 0) {
                            ret = keys;
                        }
                    }
                }
            }
        }
        return ret;
    }
    private void registerKeybinding(int actionString actionNameKeyStroke strokeString editorActionNameJTextComponent component){
        KeyStroke[] keys = findEditorKeys(editorActionNamestrokecomponent);
        for (int i = 0; i < keys.lengthi++) {
            .getInputMap().put(keys[i], actionName);
        }
        .getActionMap().put(actionNamenew DocPaneAction(action));
    }
    
    private void installKeybindings(JTextComponent component) {
        // Register javadoc back key
        KeyStroke.getKeyStroke(..),
        nullcomponent);
        // Register javadoc forward key
        KeyStroke.getKeyStroke(..),
        nullcomponent);
        // Register open in external browser key
        nullcomponent);
        // Register open the source in editor key
        KeyStroke.getKeyStroke(.. | .),
        nullcomponent);
        
        // Register movement keystrokes to be reachable through Shift+<orig-keystroke>
        mapWithShift(KeyStroke.getKeyStroke(., 0));
        mapWithShift(KeyStroke.getKeyStroke(., 0));
        mapWithShift(KeyStroke.getKeyStroke(., 0));
        mapWithShift(KeyStroke.getKeyStroke(., 0));
        mapWithShift(KeyStroke.getKeyStroke(., 0));
        mapWithShift(KeyStroke.getKeyStroke(., 0));
    }        
    
    private void mapWithShift(KeyStroke key) {
        Object actionKey = inputMap.get(key);
        if (actionKey != null) {
            key = KeyStroke.getKeyStroke(key.getKeyCode(), key.getModifiers() | .);
            .getInputMap().put(keyactionKey);
        }
    }
    
    private Color getDefaultBackground() {
        Color bgColor = new JEditorPane().getBackground();
        bgColor = new Color(
                Math.max(bgColor.getRed() - 8, 0 ), 
                Math.max(bgColor.getGreen() - 8, 0 ), 
                bgColor.getBlue());
        
        return bgColor;
    }
    
    private class BrowserButton extends JButton {
        public BrowserButton() {
            setBorderPainted(false);
            //setFocusPainted(false);
        }
        
        public BrowserButton(String text){
            super(text);
            setBorderPainted(false);
            //setFocusPainted(false);
        }
        
        public BrowserButton(Icon icon){
            super(icon);
            setBorderPainted(false);
            //setFocusPainted(false);
        }
        public @Override void setEnabled(boolean b) {
            super.setEnabled(b);
        }
        
        
    }
    private class MouseEventListener extends MouseAdapter {        
        private JButton button;
        
        MouseEventListener(JButton button) {
            this. = button;
        }
        
        public @Override void mouseEntered(MouseEvent ev) {
            if (.isEnabled()){
                .setContentAreaFilled(true);
                .setBorderPainted(true);
            }
        }
        public @Override void mouseExited(MouseEvent ev) {
            .setContentAreaFilled(false);
            .setBorderPainted(false);
        }          
    }
    private class HyperlinkAction implements HyperlinkListener {
        
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (e != null && ...equals(e.getEventType())) {
                final String desc = e.getDescription();
                if (desc != null) {
                    .post(new Runnable() {
                        public @Override void run() {
                            final ElementJavadoc doc = .resolveLink(desc);
                            if (doc != null) {
                                EventQueue.invokeLater(new Runnable() {
                                    public @Override void run() {
                                        setData(docfalse);
                                    }
                                });
                            }
                        }
                    });
                }                    
            }
        }
    }
    private class DocPaneAction extends AbstractAction {
        private int action;
        
        private DocPaneAction(int action) {
            this. = action;
        }
        
        public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
            switch () {
                case :
                    backHistory();
                    break;
                case :
                    forwardHistory();
                    break;
                case :
                    openInExternalBrowser();
                    break;
                case :
                    goToSource();
                    break;
            }
        }
    }