Examples of IPasteProvider


Examples of net.sourceforge.eclipastie.core.pasteprovider.IPasteProvider

    // some tests for handler configuration in plugin.xml
    //handlerDevelopmentTests(event);

    IPreferenceStore preferenceStore = EclipastiePlugin.getDefault().getPreferenceStore();
    IPasteProvider paster = factory.createInstance();
    Long fileSizeLimit = -1L;
    if (preferenceStore.getBoolean(IEclipastiePreferenceConstants.MENU_HANDLER_ENABLE_MAXFILESIZE_PREFERENCE)) {
      fileSizeLimit = preferenceStore.getLong(IEclipastiePreferenceConstants.MENU_HANDLER_MAXFILESIZE_PREFERENCE);
    }

    boolean pasteWasHandled = false;
    String pasteResultURL = null;
    if (selection instanceof IFile ||
      selection instanceof IStructuredSelection ||
      selection instanceof IAdaptable) {
      // this selection apparently comes from some space where resources can be selected
      IFile file = null;
      if (selection instanceof IFile) {
        file = (IFile)selection;
      } else if (selection instanceof IStructuredSelection) {
        Object selectedElement = ((IStructuredSelection)selection).getFirstElement();
        if (selectedElement instanceof IFile) {
          file = (IFile)selectedElement;
        } else if (selectedElement instanceof IAdaptable) {
          Object adapter = ((IAdaptable)selectedElement).getAdapter(IResource.class);
          if (adapter != null && adapter instanceof IFile) {
            file = (IFile)adapter;
          }
        }
      } else if (selection instanceof IAdaptable) {
        Object adapter = ((IAdaptable)selection).getAdapter(IResource.class);
        if (adapter != null && adapter instanceof IFile) {
          file = (IFile)adapter;
        }
      }

      if (file != null) {
        // detect language
        ELanguageTypes language = ELanguageTypes.PlainText;
        language = detectContentType(file);

        String fileName = file.getName();

        // check compatibility of file contents
        boolean canPaste = true;
        try {
          if (file.getContentDescription() == null ||
            file.getContentDescription().getContentType() == null ||
            file.getContentDescription().getContentType().getId() == null) {
            canPaste = false;
          }
        } catch (CoreException e) {
          canPaste = false;
        }

        if (!canPaste) {
          MessageDialog.openError(window.getShell(),
              String.format(Messages.Message_Error, EclipastiePlugin.PLUGINNAME),
              String.format(Messages.Message_CannotPasteFile)
              );
          return null;
        }

        // check file size
        long fileSize = 0;

        IFileStore store = null;
        try {
          store = EFS.getStore(file.getLocationURI());
        } catch (CoreException e) {
          // ignore - result will be that fileSize is 0 which will be handled below when reading from the file.
        }

        if (store != null) {
          fileSize = store.fetchInfo().getLength();
        }

        if (fileSizeLimit > 0 && fileSize > fileSizeLimit) {
          String niceFileSize = computeNiceFileSize(fileSize);
          String niceFileSizeLimit = computeNiceFileSize(fileSizeLimit);

          MessageDialog.openError(window.getShell(),
              String.format(Messages.Message_Error, EclipastiePlugin.PLUGINNAME),
              String.format(Messages.Message_FileSizeTooBig, niceFileSize, niceFileSizeLimit)
              );
          return null;
        }

        String niceFileSizeForMessageBox = Messages.Message_Unknown;
        if (fileSize > 0) {
          niceFileSizeForMessageBox = computeNiceFileSize(fileSize);
        }

        boolean doPasteFile = false;
        if (fileSize == 0 || fileSize > 1024) {
          if (preferenceStore.getString(IEclipastiePreferenceConstants.MENU_HANDLER_OPENFILEQUESTION_TOGGLE_PREFERENCE).equals(MessageDialogWithToggle.PROMPT)) {
            MessageDialogWithToggle openFileQuestion = MessageDialogWithToggle.openYesNoCancelQuestion(
                window.getShell(),
                EclipastiePlugin.PLUGINNAME,
                String.format(Messages.Message_ReallyPaste, fileName, niceFileSizeForMessageBox),
                Messages.Message_RememberDecision,
                false,
                preferenceStore,
                IEclipastiePreferenceConstants.MENU_HANDLER_OPENFILEQUESTION_TOGGLE_PREFERENCE);

            if (openFileQuestion.getReturnCode() == IDialogConstants.YES_ID) {
              doPasteFile = true;
            }
          } else {
            if (preferenceStore.getString(IEclipastiePreferenceConstants.MENU_HANDLER_OPENFILEQUESTION_TOGGLE_PREFERENCE).equals(MessageDialogWithToggle.ALWAYS)) {
              doPasteFile = true;
            }
          }
        } else {
          doPasteFile = true;
        }

        if (doPasteFile) {
          // read file content
          String text = ""; //$NON-NLS-1$
          Reader fileReader = null;
          InputStream fileContent = null;
          CharBuffer charBuffer = CharBuffer.allocate(1024);

          try {
            fileContent = new BufferedInputStream(file.getContents());
            fileReader = new BufferedReader(new InputStreamReader(fileContent));

            int offset = 0;
            int sizeRead = 0;
            do {
              sizeRead = fileReader.read(charBuffer.array(), 0, charBuffer.length());
              if (sizeRead != -1) {
                offset += sizeRead;
                text += charBuffer.subSequence(0, sizeRead);
                charBuffer.rewind();
              }
            } while (sizeRead != -1 && offset < fileSizeLimit);

            // complain if file size was too big after all - can happen if file size was impossible to be retrieved above
            if (offset >= fileSizeLimit) {
              String niceFileSize = computeNiceFileSize(offset);
              String niceFileSizeLimit = computeNiceFileSize(fileSizeLimit);

              MessageDialog.openError(window.getShell(),
                  String.format(Messages.Message_Error, EclipastiePlugin.PLUGINNAME),
                  String.format(Messages.Message_FileSizeTooBig, niceFileSize, niceFileSizeLimit)
                  );
              return null;
            }
          } catch (CoreException e) {
            e.printStackTrace();
            String pluginID = EclipastiePlugin.PLUGINNAME;
            IStatus status = new Status(IStatus.ERROR, pluginID, String.format(Messages.Message_ErrorReported, e.getLocalizedMessage()));
            ErrorDialog.openError(window.getShell(),
                String.format(Messages.Message_Error, EclipastiePlugin.PLUGINNAME),
                Messages.Message_ErrorWhileReadingFile,
                status);
            return null;
          } catch (IOException e) {
            e.printStackTrace();
            String pluginID = EclipastiePlugin.PLUGINNAME;
            IStatus status = new Status(IStatus.ERROR, pluginID, String.format(Messages.Message_ErrorReported, e.getLocalizedMessage()));
            ErrorDialog.openError(window.getShell(),
                String.format(Messages.Message_Error, EclipastiePlugin.PLUGINNAME),
                Messages.Message_ErrorWhileReadingFile,
                status);
            return null;
          } finally {
            if (fileReader != null) {
              try {
                fileReader.close();
              } catch (IOException e) {
                // ignore
              }

              fileReader = null;
            }
            if (fileContent != null) {
              try {
                fileContent.close();
              } catch (IOException e) {
                // ignore
              }

              fileContent = null;
            }
          }

          // user wants to paste
          pasteResultURL = doPastiePaste(event, language, paster, text, fileName);
          pasteWasHandled = true;
        }
      }
    }

    if (!pasteWasHandled) {
      if (!(selection instanceof ITextSelection) &&
         activePart instanceof IEditorPart) {
        // we are looking at a selection from an editor
        // next: check if selection is already a valid "text" selection - if not, try to get one
        // directly out of the ITextEditor interface (that helps e.g. for structured documents such as
        // xml code etc.)
        if (!(selection instanceof ITextSelection) &&
           (activePart instanceof IAdaptable)) {
          ITextEditor textEditor = (ITextEditor) ((IAdaptable)activePart).getAdapter(ITextEditor.class);
          if (textEditor != null && textEditor.getSelectionProvider() != null) {
            ISelection textEditorSelection = textEditor.getSelectionProvider().getSelection();
            if (textEditorSelection instanceof ITextSelection) {
              selection = textEditorSelection;
            }
          }
        }
      }

      if (selection instanceof ITextSelection) {
        // a simple text selection
        String text = ((ITextSelection)selection).getText();
        pasteResultURL = checkAndPasteText(event, paster, text);
        pasteWasHandled = true;
      }
    }

    if (!pasteWasHandled) {
      MessageDialog.openError(window.getShell(),
          String.format(Messages.Message_Error, EclipastiePlugin.PLUGINNAME),
          Messages.Message_CannotPasteSelection
          );
      return null;
    }

    if (pasteResultURL != null) {
      // copy result to clipboard
       boolean copySuccessful = copyTextToClipboard(window, pasteResultURL);

      // display result in browser window
       if (preferenceStore.getBoolean(IEclipastiePreferenceConstants.MENU_HANDLER_OPENBROWSER_PREFERENCE)) {
         openWebBrowser(window, pasteResultURL, paster.getPasteBinProviderName());
       }

      // display information about result url in clipboard
      if (copySuccessful) {
        if (preferenceStore.getString(IEclipastiePreferenceConstants.MENU_HANDLER_CLIPBOARDQUESTION_TOGGLE_PREFERENCE).equals(MessageDialogWithToggle.PROMPT)) {
View Full Code Here
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.