Package virtualnewsreader

Source Code of virtualnewsreader.TabBrowserControl$PopupListener

/*
* This class will help to create the JWebBrowser object and return it to the caller.
* should be control all of the Rightclick functionality here.
*/

/**
*
* @author kpham
*/


package virtualnewsreader;


import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
//import chrriis.dj.nativeswing.components.JWebBrowser;
//import chrriis.dj.nativeswing.components.WebBrowserAdapter;
//import chrriis.dj.nativeswing.components.WebBrowserEvent;
import java.awt.Component;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.jdesktop.application.*;

public class TabBrowserControl implements ActionListener{
   
   
    private JWebBrowser webBrowser = null;
    private TabManager  myTabManager = null;
    private static final int NO_SELECTION = 1;
    private static final int NO_EXIST_DOC = 2;
    private static final String docToken = " > ";
   
    public TabBrowserControl(TabManager tManager)
    {
        myTabManager = tManager;
    }

    public JWebBrowser CreateBrowserTab()
    {  
       
        webBrowser = new JWebBrowser();
        webBrowser.setStatusBarVisible(false);
        webBrowser.addWebBrowserListener(new WebBrowserAdapter() { 
            @Override
            public void statusChanged(WebBrowserEvent e) { 

                String status = webBrowser.getStatusText();

                VirtualNewsReaderView frameView =
                        (VirtualNewsReaderView)
                        ((VirtualNewsReaderApp) SingleFrameApplication.getInstance()).getMainView();
               
                frameView.SetStatusText(status.length() == 0? "" : status);
           
        });
        webBrowser.setDefaultPopupMenuRegistered(false);
      
        createPopupMenu();
       
        return webBrowser;
    }
   
    /**
     * Popup menu action handler; for both the Browser poup and Document selection pupup
     * 
     * @param action
     */
    public void actionPerformed(ActionEvent action) {
        if(action.getActionCommand().startsWith("Create"))
        {
            createNewDocumentBasedOnSelection();
        }
       
        if(action.getActionCommand().startsWith("Appending"))
        {
            //popup the document menu
            createDocPopMenu();
        }
       
        if(action.getActionCommand().startsWith("Copy"))
        {
            copyText();
        }
       
        if(action.getActionCommand().startsWith(docToken))
            //If user is selecting from the Document popup menu
        {
            appendingTextToExistingDocument(action);
        }
    }
   
   
    /**
     * Get the selected text from the browser
     *
     * @return Selected text from the browser
     */
    public String getSelectedText()
    {
        String jScript =
//                    "function getTextSelection(w) { " +
//                        "if(w == null) { " +
//                            "w = window; " +
//                        "} " +
//                        "var selection = w.document.getSelection(); " +
//                        "if(selection != null && selection != '') {" +
//                            "return selection; " +
//                         "}" +
//                         "for(var i=w.frames.length-1; i>=0; i--) {" +
//                            "var selection = this.getTextSelection(w.frames[i]);" +
//                            "if(selection != null && selection != '') {" +
//                                "return selection;" +
//                            "}" +
//                         "}" +
//                         "return selection;" +
//                      "}" +
//                      "return getTextSelection();";
               
                "function getTextSelection(w) { " +
                    "if(w == null) {" +
                            "w = window;" +
                    "}" +
                     
                    "var selection = document.all? " +
                        "w.document.selection.createRange().text : " +
                        "w.document.getSelection();" +
                    
                    "if(selection != null && selection != '') {" +
                        "return selection;" +
                    "}" +
                    
                    "for(var i=w.frames.length-1; i>=0; i--) {" +
                        "var selection = this.getTextSelection(w.frames[i]);" +
                        "if(selection != null && selection != '') {" +
                            "return selection;" +
                        "}" +
                    "}" +
                    "return selection;" +
                   
                "}" +
                "return getTextSelection();";
        if(webBrowser.executeJavascriptWithResult(jScript) != null)
            return (String) webBrowser.executeJavascriptWithResult(jScript) + "\n\n";
        else
            return null;
    }
   
    /**
     * Copy the Text
     */
    private void copyText()
    {
        //not implimented.
    }
    /**
     * Create a document with the selected text
     */
    private void createNewDocumentBasedOnSelection() {      
      try {
            String text = getSelectedText();
            if(text != null)
            {
                //first add new document
                myTabManager.addDocumentTab();

                //secondly get the newly added document and set the text to it.
                int tabCount = myTabManager.getTabCount();
                JScrollPane scrPane = (JScrollPane) myTabManager.getComponentAt(tabCount-1);
                JEditorPane myEditor= (JEditorPane) scrPane.getViewport().getComponent(0);
                myEditor.setText(text);
            }
            else
                ShowError(NO_SELECTION);
           
        } catch (IOException ex) {
            Logger.getLogger(TabBrowserControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    private void appendingTextToExistingDocument(ActionEvent action){
        String text = getSelectedText();
       
        if(text != null)
        {
            //first find out which document is the user selected.
            JEditorPane myEditor = null;
            for(int i =0; i<myTabManager.getTabCount(); i++){
                Component comp = myTabManager.getComponentAt(i);
                if(action.getActionCommand().equals(docToken + comp.getName())){
                    try { //if document found appending the text into it.
                        myTabManager.setSelectedComponent(comp);
                        JScrollPane scrPane = (JScrollPane) comp;
                        myEditor = (JEditorPane) scrPane.getViewport().getComponent(0);
                        Document doc = myEditor.getDocument();
                        doc.insertString(doc.getLength(), text, null);
                        return;
                    } catch (BadLocationException ex) {
                        Logger.getLogger(TabBrowserControl.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
           
           
        }
        else
            ShowError(NO_SELECTION);
    }
   
    /**
     * Create a List of Document menu that the user can select when
     * decided to appending text to existing document.
     */
    private void createDocPopMenu()
    {
        JMenuItem menuItem;
        //Create the popup menu.
        int tabCount = myTabManager.getTabCount();
        JPopupMenu popup = new JPopupMenu();
       
        popup.setToolTipText("Please select a document you want to appending the text to.");
        menuItem = new JMenuItem(":: Select a Document below ::");
        menuItem.setEnabled(false);
        popup.add(menuItem);
        popup.addSeparator();
       
               
        for(int i= 0; i<tabCount; i++){
            Component cmp = myTabManager.getComponent(i);
            boolean isDocumentTab = !cmp.getName().startsWith("Web");
            if(isDocumentTab)
            {
                menuItem = new JMenuItem(docToken + cmp.getName());
                menuItem.addActionListener(this);
                menuItem.setMargin(new Insets(0,5,0,0));
                popup.add(menuItem);  
            }
        }
       
        if(popup.getComponentCount() <=2)
            ShowError(NO_EXIST_DOC);
        else{
            Rectangle rec = webBrowser.getVisibleRect();
            popup.show(webBrowser, (int)rec.getWidth()/2, (int)rec.getHeight()/2);
        }
    }
   
    /**
     * Right click menu for the browser
     *
     */
    private void createPopupMenu() {
        JMenuItem menuItem;

        //Create the popup menu.
        JPopupMenu popup = new JPopupMenu();
       
        menuItem = new JMenuItem("Create Document");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem("Appending to Document");
        menuItem.addActionListener(this);
        popup.add(menuItem);
       
        popup.addSeparator();
       
        menuItem = new JMenuItem("cut");
        menuItem.setEnabled(false);
        popup.add(menuItem);
        menuItem = new JMenuItem("Copy");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem("Paste");
        menuItem.setEnabled(false);
        popup.add(menuItem);

        //Add listener to the text area so the popup menu can come up.
        MouseListener popupListener = new PopupListener(popup);
        webBrowser.getNativeComponent().addMouseListener(popupListener);
    }
   
    class PopupListener extends MouseAdapter {
        JPopupMenu popupMenu;

        PopupListener(JPopupMenu Menu) {
            popupMenu = Menu;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            ShowPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            ShowPopup(e);
        }

        private void ShowPopup(MouseEvent e)
        {
            //if (e.isPopupTrigger()) {
            if(SwingUtilities.isRightMouseButton(e)){
                popupMenu.show(e.getComponent(),e.getX(), e.getY());
            }
        }
    }
   
    /**
     * Show Error
     */
    private void ShowError(int Type)
    {
        if(Type == NO_EXIST_DOC)
            JOptionPane.showMessageDialog(null,"You have not create any Document, please create one", "No document exist!", JOptionPane.ERROR_MESSAGE);
        else if(Type == NO_SELECTION)
           JOptionPane.showMessageDialog(null,"Please Select some text from the Browser", "No document exist!", JOptionPane.ERROR_MESSAGE);
    }
   
}
TOP

Related Classes of virtualnewsreader.TabBrowserControl$PopupListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.