clipboard -text selection
[Logo]
ICEsoft.org Forums: ICEfaces, ICEmobile, ICEpdf
[Search] Search   [Recent Topics] Recent Topics   [Groups] Home Page | www.icesoft.org  [Login] Login 
clipboard -text selection  XML
Forum Index -> ICEpdf General
Author Message
montblack

Joined: 13/09/2010 11:21:04
Messages: 7
Offline


i need to know where i can find an example of text selection and copi to clipboard for ctrl+c from document and ctrl+v to my webpage.

i´m using applet for my PDF document view
patrick.corless

Joined: 26/10/2004 00:00:00
Messages: 1150
Offline


I can give you a hint on how to to this. Basically you need to add the correct key listeners the JApplet container. The regular AI uses the menu key listeners to get the job done.

When you setup your applet add the SwingController's key listener. Using the Applet example it would look like this.

Code:
this.addKeyListener(controller);



Next you need to update the SingController key listener to look like this:

Code:
public void keyPressed(KeyEvent e) {
         if (document == null)
             return;
         int c = e.getKeyCode();
         int m = e.getModifiers();
         if ((c == KeyEventConstants.KEY_CODE_SAVE_AS && m == KeyEventConstants.MODIFIER_SAVE_AS) ||
                 (c == KeyEventConstants.KEY_CODE_PRINT_SETUP && m == KeyEventConstants.MODIFIER_PRINT_SETUP) ||
                 (c == KeyEventConstants.KEY_CODE_PRINT && m == KeyEventConstants.MODIFIER_PRINT) ||
                 (c == KeyEventConstants.KEY_CODE_FIT_ACTUAL && m == KeyEventConstants.MODIFIER_FIT_ACTUAL) ||
                 (c == KeyEventConstants.KEY_CODE_FIT_PAGE && m == KeyEventConstants.MODIFIER_FIT_PAGE) ||
                 (c == KeyEventConstants.KEY_CODE_FIT_WIDTH && m == KeyEventConstants.MODIFIER_FIT_WIDTH) ||
                 (c == KeyEventConstants.KEY_CODE_ZOOM_IN && m == KeyEventConstants.MODIFIER_ZOOM_IN) ||
                 (c == KeyEventConstants.KEY_CODE_ZOOM_OUT && m == KeyEventConstants.MODIFIER_ZOOM_OUT) ||
                 (c == KeyEventConstants.KEY_CODE_ROTATE_LEFT && m == KeyEventConstants.MODIFIER_ROTATE_LEFT) ||
                 (c == KeyEventConstants.KEY_CODE_ROTATE_RIGHT && m == KeyEventConstants.MODIFIER_ROTATE_RIGHT) ||
                 (c == KeyEventConstants.KEY_CODE_FIRST_PAGE && m == KeyEventConstants.MODIFIER_FIRST_PAGE) ||
                 (c == KeyEventConstants.KEY_CODE_PREVIOUS_PAGE && m == KeyEventConstants.MODIFIER_PREVIOUS_PAGE) ||
                 (c == KeyEventConstants.KEY_CODE_NEXT_PAGE && m == KeyEventConstants.MODIFIER_NEXT_PAGE) ||
                 (c == KeyEventConstants.KEY_CODE_LAST_PAGE && m == KeyEventConstants.MODIFIER_LAST_PAGE) ||
                 (c == KeyEventConstants.KEY_CODE_SEARCH && m == KeyEventConstants.MODIFIER_SEARCH) ||
                 (c == KeyEventConstants.KEY_CODE_GOTO && m == KeyEventConstants.MODIFIER_GOTO)||
                 (c == KeyEventConstants.KEY_CODE_COPY && m == KeyEventConstants.MODIFIER_COPY)
                 ) {
             // get document previous icon
             int documentIcon = getDocumentViewToolMode();
             try {
                 // set cursor for document view
                 setDisplayTool(DocumentViewModelImpl.DISPLAY_TOOL_WAIT);
 
                 if (c == KeyEventConstants.KEY_CODE_SAVE_AS &&
                         m == KeyEventConstants.MODIFIER_SAVE_AS) {
                     saveFile();
                 } else if (c == KeyEventConstants.KEY_CODE_PRINT_SETUP &&
                         m == KeyEventConstants.MODIFIER_PRINT_SETUP) {
                     showPrintSetupDialog();
                 } else if (c == KeyEventConstants.KEY_CODE_PRINT &&
                         m == KeyEventConstants.MODIFIER_PRINT) {
                     print(true);
                 } else if (c == KeyEventConstants.KEY_CODE_FIT_ACTUAL &&
                         m == KeyEventConstants.MODIFIER_FIT_ACTUAL) {
                     setPageFitMode(org.icepdf.core.views.DocumentViewController.PAGE_FIT_ACTUAL_SIZE, false);
                 } else if (c == KeyEventConstants.KEY_CODE_FIT_PAGE &&
                         m == KeyEventConstants.MODIFIER_FIT_PAGE) {
                     setPageFitMode(org.icepdf.core.views.DocumentViewController.PAGE_FIT_WINDOW_HEIGHT, false);
                 } else if (c == KeyEventConstants.KEY_CODE_FIT_WIDTH &&
                         m == KeyEventConstants.MODIFIER_FIT_WIDTH) {
                     setPageFitMode(org.icepdf.core.views.DocumentViewController.PAGE_FIT_WINDOW_WIDTH, false);
                 } else if (c == KeyEventConstants.KEY_CODE_ZOOM_IN &&
                         m == KeyEventConstants.MODIFIER_ZOOM_IN) {
                     zoomIn();
                 } else if (c == KeyEventConstants.KEY_CODE_ZOOM_OUT &&
                         m == KeyEventConstants.MODIFIER_ZOOM_OUT) {
                     zoomOut();
                 } else if (c == KeyEventConstants.KEY_CODE_ROTATE_LEFT &&
                         m == KeyEventConstants.MODIFIER_ROTATE_LEFT) {
                     rotateLeft();
                 } else if (c == KeyEventConstants.KEY_CODE_ROTATE_RIGHT &&
                         m == KeyEventConstants.MODIFIER_ROTATE_RIGHT) {
                     rotateRight();
                 } else if (c == KeyEventConstants.KEY_CODE_FIRST_PAGE &&
                         m == KeyEventConstants.MODIFIER_FIRST_PAGE) {
                     showPage(0);
                 } else if (c == KeyEventConstants.KEY_CODE_PREVIOUS_PAGE &&
                         m == KeyEventConstants.MODIFIER_PREVIOUS_PAGE) {
                     DocumentView documentView = documentViewController.getDocumentView();
                     goToDeltaPage(-documentView.getPreviousPageIncrement());
                 } else if (c == KeyEventConstants.KEY_CODE_NEXT_PAGE &&
                         m == KeyEventConstants.MODIFIER_NEXT_PAGE) {
                     DocumentView documentView = documentViewController.getDocumentView();
                     goToDeltaPage(documentView.getNextPageIncrement());
                 } else if (c == KeyEventConstants.KEY_CODE_LAST_PAGE &&
                         m == KeyEventConstants.MODIFIER_LAST_PAGE) {
                     showPage(getPageTree().getNumberOfPages() - 1);
                 } else if (c == KeyEventConstants.KEY_CODE_SEARCH &&
                         m == KeyEventConstants.MODIFIER_SEARCH) {
                     showSearchPanel();
                 } else if (c == KeyEventConstants.KEY_CODE_GOTO &&
                         m == KeyEventConstants.MODIFIER_GOTO) {
                     showPageSelectionDialog();
                 } else if (c == KeyEventConstants.KEY_CODE_COPY &&
                         m == KeyEventConstants.MODIFIER_COPY){
                     if (document != null &&
                             havePermissionToExtractContent() &&
                             !(documentViewController.getDocumentViewModel().isSelectAll() &&
                             document.getNumberOfPages() > MAX_SELECT_ALL_PAGE_COUNT)) {
                         // get the text.
                         StringSelection stringSelection = new StringSelection(
                             documentViewController.getSelectedText());
                         Toolkit.getDefaultToolkit().getSystemClipboard().
                                 setContents(stringSelection, null);
                     } else {
                         Runnable doSwingWork = new Runnable() {
                             public void run() {
                                 org.icepdf.ri.util.Resources.showMessageDialog(
                                         viewer,
                                         JOptionPane.INFORMATION_MESSAGE,
                                         messageBundle,
                                         "viewer.dialog.information.copyAll.title",
                                         "viewer.dialog.information.copyAll.msg",
                                         MAX_SELECT_ALL_PAGE_COUNT);
                             }
                         };
                         SwingUtilities.invokeLater(doSwingWork);
                     }
                 }
             }
             finally {
                 // set view pain back to previous icon
                 setDisplayTool(documentIcon);
             }
         }
     }
 
[Email]
montblack

Joined: 13/09/2010 11:21:04
Messages: 7
Offline


how i can update the SingController key listener?
patrick.corless

Joined: 26/10/2004 00:00:00
Messages: 1150
Offline


You'll have to download a src version of the ICEpdf. Once you've made your changes you can use the provided Ant scripts to rebuild the icepdf-viewer.jar.

[Email]
snacker

Joined: 30/11/2010 10:03:59
Messages: 3
Offline


Thanks patrick for the tip!

You can also either subclass the SwingController and use the "get" methods instead of having to use the members variables directly.

An anonymous class works fine as well.

I'm kinda surprised that this is not something that is available automatically (unless you include the toolbar)...?
snacker

Joined: 30/11/2010 10:03:59
Messages: 3
Offline


I have a standalone java swing app with an JInternalFrame which is divided into two areas (left & right) via a split pane.

This solution works only on the first page of the PDF.

If I go to the next page the key listener doesn't fire.

If I go back to the first page, the key listener does fire though.

Any idea why this is happening?

I've also tried DocumentViewController::setViewKeyListener( myListener ), but I see the same behavior.

Do the icepdf/icefaces api's override the java.awt.Component handling?

I thought java.awt.Component would search the parent(s) for a KeyListener if one wasn't set....?
snacker

Joined: 30/11/2010 10:03:59
Messages: 3
Offline


Simply adding the "modified" swing controller doesn't appear to setup the key listener for the individual pages.

The only way I can get this to work for a document is by doing the following (IN ORDER):

Code:
         // 1 : create the objects //
         MyIcePdfSwingController         controller = new MyIcePdfSwingController();
         SwingViewBuilder                view       = new SwingViewBuilder( controller );
         
         // 2 : open the document //
         controller.openDocument( new ByteArrayInputStream( myPdfByteArray ), "", null );
         
         // 3 : add the listeners //
         KeyListener                     kl = controller;
         DocumentViewController          dvc;
         DocumentViewModel               dvm;
         List<AbstractPageViewComponent> lst;
         AbstractPageViewComponent       apvc;
         dvc = controller.getDocumentViewController();
         dvm = null == dvc ? null : dvc.getDocumentViewModel();
         lst = null == dvm ? null : dvm.getPageComponents();
         for( int i=0,ilen=sizeof( lst ); i<ilen; ++ i ){
             apvc = lst.get( i );
             apvc.addKeyListener( kl )
         }
 



Where "MyIcePdfSwingController" is:
Code:
     private static class MyIcePdfSwingController extends SwingController{
         public void keyPressed( KeyEvent e ){
             final Document document = getDocument();
             if( document == null ){
                 super.keyPressed( e );
                 return;
             }
             int code = e.getKeyCode();
             int mod  = e.getModifiers();
             if( code == KeyEventConstants.KEY_CODE_SELECT_ALL && mod == KeyEvent.CTRL_MASK ){
                 DocumentViewController docViewCntl  = getDocumentViewController();
                 docViewCntl.selectAllText();
                 e.consume();
             }else if( code == KeyEventConstants.KEY_CODE_COPY && mod == KeyEventConstants.MODIFIER_COPY ){
                 // get document previous icon
                 int documentIcon = getDocumentViewToolMode();
                 try {
                     // set cursor for document view
                     setDisplayTool( DocumentViewModelImpl.DISPLAY_TOOL_WAIT );
                     DocumentViewController docViewCntl  = getDocumentViewController();
                     if( havePermissionToExtractContent()
                         && ! ( docViewCntl.getDocumentViewModel().isSelectAll()
                             && document.getNumberOfPages() > MAX_SELECT_ALL_PAGE_COUNT ) )
                     {
                         // get the text.
                         StringSelection stringSelection = new StringSelection( docViewCntl.getSelectedText() );
                         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
                         e.consume();
                     }else{
                         Runnable doSwingWork = new Runnable() {
                                 public void run() {
                                     JFrame           viewer       = getViewerFrame();
                                     ResourceBundle   messageBundle= getMessageBundle();
                                     org.icepdf.ri.util.Resources.showMessageDialog(
                                             viewer,
                                             JOptionPane.INFORMATION_MESSAGE,
                                             messageBundle,
                                             "viewer.dialog.information.copyAll.title",
                                             "viewer.dialog.information.copyAll.msg",
                                             MAX_SELECT_ALL_PAGE_COUNT
                                         )
                                     ;
                                 }
                             }
                         ;
                         SwingUtilities.invokeLater(doSwingWork);
                         e.consume();
                     }
                 }finally{
                     // set view pain back to previous icon
                     setDisplayTool(documentIcon);
                 }
             }else{
                 super.keyPressed( e );
             }
         }
     }
 



BTW, I also tried adding the "modified" SwingController to the parent container, it didn't work.
patrick.corless

Joined: 26/10/2004 00:00:00
Messages: 1150
Offline


I've given this another look. I agree with you that there should be a default or at least option to add the default key listeners. I'll create an enhancement but so the feature isn't lost.

Instead of a key listener I did some experimenting with key bindings instead. Here's how I added the copy functionality

Code:
 public class ComponentKeyBinding {
 
     public static void install(final SwingController controller, final JComponent viewerContainer){
         Action copyText = new AbstractAction() {
             public void actionPerformed(ActionEvent e) {
                 Document document = controller.getDocument();
                 DocumentViewController documentViewController =
                         controller.getDocumentViewController();
                 if (document != null &&
                          controller.havePermissionToExtractContent() &&
                          !(documentViewController.getDocumentViewModel().isSelectAll() &&
                          document.getNumberOfPages() > 250)) {
                      // get the text.
                      StringSelection stringSelection = new StringSelection(
                          documentViewController.getSelectedText());
                      Toolkit.getDefaultToolkit().getSystemClipboard().
                              setContents(stringSelection, null);
                  } else {
                      Runnable doSwingWork = new Runnable() {
                          public void run() {
                              org.icepdf.ri.util.Resources.showMessageDialog(
                                      viewerContainer,
                                      JOptionPane.INFORMATION_MESSAGE,
                                      controller.getMessageBundle(),
                                      "viewer.dialog.information.copyAll.title",
                                      "viewer.dialog.information.copyAll.msg",
                                      250);
                          }
                      };
                      SwingUtilities.invokeLater(doSwingWork);
                  }
             }
         };
 
         InputMap inputMap = viewerContainer.getInputMap(
                 JComponent.WHEN_IN_FOCUSED_WINDOW);
         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK),
                                     "copyText");
         viewerContainer.getActionMap().put("copyText",
                                      copyText);
     }
 }
 


In the applet example code I hooked it up like this.

Code:
         // create a controller and a swing factory
         controller = new SwingController();
         SwingViewBuilder factory = new SwingViewBuilder( controller );
         // add interactive mouse link annotation support via callback
         controller.getDocumentViewController().setAnnotationCallback(
                 new org.icepdf.ri.common.MyAnnotationCallback(
                         controller.getDocumentViewController()));
 
         // add copy keyboard command
         JPanel viewerComponentPanel = factory.buildViewerPanel();
         ComponentKeyBinding.install(controller, viewerComponentPanel);
 
         // build viewer component and add it to the applet content pane.
         getContentPane().add( viewerComponentPanel );
 
         // Now that the GUI is all in place, we can try openning a PDF
         if (documentURL != null)
             controller.openDocument( documentURL );
 
[Email]
patrick.corless

Joined: 26/10/2004 00:00:00
Messages: 1150
Offline


Created to http://jira.icefaces.org/browse/PDF-292 to track this enhancement.
[Email]
 
Forum Index -> ICEpdf General
Go to:   
Powered by JForum 2.1.7ice © JForum Team