public static ContentProposalAdapter createContentProposal(
     final Text text, String[] proposedValues) {
   SimpleContentProposalProvider proposalProvider =
       new SimpleContentProposalProvider(proposedValues);
   proposalProvider.setFiltering(true);
   KeyStroke keyStroke = KeyStroke.getInstance(SWT.CONTROL, ' ');
   ContentProposalAdapter proposalAdapter =
       new ContentProposalAdapter(
           text, new TextContentAdapter(), proposalProvider, keyStroke, null);
   proposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
   return proposalAdapter;
 }
Exemplo n.º 2
0
  /**
   * Adds a content proposal for {@link Ref}s (branches, tags...) to a text field
   *
   * @param textField the text field
   * @param repository the repository
   * @param refListProvider provides the {@link Ref}s to show in the proposal
   */
  public static final void addRefContentProposalToText(
      final Text textField, final Repository repository, final IRefListProvider refListProvider) {
    KeyStroke stroke;
    try {
      stroke = KeyStroke.getInstance("M1+SPACE"); // $NON-NLS-1$
      UIUtils.addBulbDecorator(
          textField, NLS.bind(UIText.UIUtils_PressShortcutMessage, stroke.format()));
    } catch (ParseException e1) {
      Activator.handleError(e1.getMessage(), e1, false);
      stroke = null;
      UIUtils.addBulbDecorator(textField, UIText.UIUtils_StartTypingForPreviousValuesMessage);
    }

    IContentProposalProvider cp =
        new IContentProposalProvider() {
          public IContentProposal[] getProposals(String contents, int position) {
            List<IContentProposal> resultList = new ArrayList<IContentProposal>();

            // make the simplest possible pattern check: allow "*"
            // for multiple characters
            String patternString = contents;
            // ignore spaces in the beginning
            while (patternString.length() > 0 && patternString.charAt(0) == ' ') {
              patternString = patternString.substring(1);
            }

            // we quote the string as it may contain spaces
            // and other stuff colliding with the Pattern
            patternString = Pattern.quote(patternString);

            patternString = patternString.replaceAll("\\x2A", ".*"); // $NON-NLS-1$ //$NON-NLS-2$

            // make sure we add a (logical) * at the end
            if (!patternString.endsWith(".*")) { // $NON-NLS-1$
              patternString = patternString + ".*"; // $NON-NLS-1$
            }

            // let's compile a case-insensitive pattern (assumes ASCII only)
            Pattern pattern;
            try {
              pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
            } catch (PatternSyntaxException e) {
              pattern = null;
            }

            List<Ref> proposals = refListProvider.getRefList();

            if (proposals != null)
              for (final Ref ref : proposals) {
                final String shortenedName = Repository.shortenRefName(ref.getName());
                if (pattern != null
                    && !pattern.matcher(ref.getName()).matches()
                    && !pattern.matcher(shortenedName).matches()) continue;

                IContentProposal propsal = new RefContentProposal(repository, ref);
                resultList.add(propsal);
              }

            return resultList.toArray(new IContentProposal[resultList.size()]);
          }
        };

    ContentProposalAdapter adapter =
        new ContentProposalAdapter(
            textField, new TextContentAdapter(), cp, stroke, UIUtils.VALUE_HELP_ACTIVATIONCHARS);
    // set the acceptance style to always replace the complete content
    adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
  }
Exemplo n.º 3
0
  /**
   * Adds a "previously used values" content proposal handler to a text field.
   *
   * <p>The keyboard shortcut will be "M1+SPACE" and the list will be limited to 10 values.
   *
   * @param textField the text field
   * @param preferenceKey the key under which to store the "previously used values" in the dialog
   *     settings
   * @return the handler the proposal handler
   */
  public static IPreviousValueProposalHandler addPreviousValuesContentProposalToText(
      final Text textField, final String preferenceKey) {
    KeyStroke stroke;
    try {
      stroke = KeyStroke.getInstance("M1+SPACE"); // $NON-NLS-1$
      addBulbDecorator(textField, NLS.bind(UIText.UIUtils_PressShortcutMessage, stroke.format()));
    } catch (ParseException e1) {
      Activator.handleError(e1.getMessage(), e1, false);
      stroke = null;
      addBulbDecorator(textField, UIText.UIUtils_StartTypingForPreviousValuesMessage);
    }

    IContentProposalProvider cp =
        new IContentProposalProvider() {

          public IContentProposal[] getProposals(String contents, int position) {

            List<IContentProposal> resultList = new ArrayList<IContentProposal>();

            // make the simplest possible pattern check: allow "*"
            // for multiple characters
            String patternString = contents;
            // ignore spaces in the beginning
            while (patternString.length() > 0 && patternString.charAt(0) == ' ') {
              patternString = patternString.substring(1);
            }

            // we quote the string as it may contain spaces
            // and other stuff colliding with the Pattern
            patternString = Pattern.quote(patternString);

            patternString = patternString.replaceAll("\\x2A", ".*"); // $NON-NLS-1$ //$NON-NLS-2$

            // make sure we add a (logical) * at the end
            if (!patternString.endsWith(".*")) { // $NON-NLS-1$
              patternString = patternString + ".*"; // $NON-NLS-1$
            }

            // let's compile a case-insensitive pattern (assumes ASCII only)
            Pattern pattern;
            try {
              pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
            } catch (PatternSyntaxException e) {
              pattern = null;
            }

            String[] proposals =
                org.eclipse.egit.ui.Activator.getDefault()
                    .getDialogSettings()
                    .getArray(preferenceKey);

            if (proposals != null)
              for (final String uriString : proposals) {

                if (pattern != null && !pattern.matcher(uriString).matches()) continue;

                IContentProposal propsal =
                    new IContentProposal() {

                      public String getLabel() {
                        return null;
                      }

                      public String getDescription() {
                        return null;
                      }

                      public int getCursorPosition() {
                        return 0;
                      }

                      public String getContent() {
                        return uriString;
                      }
                    };
                resultList.add(propsal);
              }

            return resultList.toArray(new IContentProposal[resultList.size()]);
          }
        };

    ContentProposalAdapter adapter =
        new ContentProposalAdapter(
            textField, new TextContentAdapter(), cp, stroke, VALUE_HELP_ACTIVATIONCHARS);
    // set the acceptance style to always replace the complete content
    adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

    return new IPreviousValueProposalHandler() {
      public void updateProposals() {
        String value = textField.getText();
        // don't store empty values
        if (value.length() > 0) {
          // we don't want to save too much in the preferences
          if (value.length() > 2000) {
            value = value.substring(0, 1999);
          }
          // now we need to mix the value into the list
          IDialogSettings settings = org.eclipse.egit.ui.Activator.getDefault().getDialogSettings();
          String[] existingValues = settings.getArray(preferenceKey);
          if (existingValues == null) {
            existingValues = new String[] {value};
            settings.put(preferenceKey, existingValues);
          } else {

            List<String> values = new ArrayList<String>(existingValues.length + 1);

            for (String existingValue : existingValues) values.add(existingValue);
            // if it is already the first value, we don't need to do
            // anything
            if (values.indexOf(value) == 0) return;

            values.remove(value);
            // we insert at the top
            values.add(0, value);
            // make sure to not store more than the maximum number
            // of values
            while (values.size() > 10) values.remove(values.size() - 1);

            settings.put(preferenceKey, values.toArray(new String[values.size()]));
          }
        }
      }
    };
  }
  private void addRefContentProposalToText(final Text textField) {
    KeyStroke stroke =
        UIUtils.getKeystrokeOfBestActiveBindingFor(IWorkbenchCommandConstants.EDIT_CONTENT_ASSIST);
    if (stroke != null)
      UIUtils.addBulbDecorator(
          textField, NLS.bind(UIText.GerritConfigurationPage_BranchTooltipHover, stroke.format()));

    IContentProposalProvider cp =
        new IContentProposalProvider() {
          public IContentProposal[] getProposals(String contents, int position) {
            List<IContentProposal> resultList = new ArrayList<IContentProposal>();

            // make the simplest possible pattern check: allow "*"
            // for multiple characters
            String patternString = contents;
            // ignore spaces in the beginning
            while (patternString.length() > 0 && patternString.charAt(0) == ' ') {
              patternString = patternString.substring(1);
            }

            // we quote the string as it may contain spaces
            // and other stuff colliding with the Pattern
            patternString = Pattern.quote(patternString);

            patternString = patternString.replaceAll("\\x2A", ".*"); // $NON-NLS-1$ //$NON-NLS-2$

            // make sure we add a (logical) * at the end
            if (!patternString.endsWith(".*")) // $NON-NLS-1$
            patternString = patternString + ".*"; // $NON-NLS-1$

            // let's compile a case-insensitive pattern (assumes ASCII only)
            Pattern pattern;
            try {
              pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
            } catch (PatternSyntaxException e) {
              pattern = null;
            }

            Set<String> proposals = new TreeSet<String>();

            try {
              // propose the names of the remote tracking
              // branches for the given remote
              Set<String> remotes =
                  repository
                      .getRefDatabase()
                      .getRefs(Constants.R_REMOTES + remoteName + "/")
                      .keySet(); //$NON-NLS-1$
              proposals.addAll(remotes);
            } catch (IOException e) {
              // simply ignore, no proposals then
            }

            for (final String proposal : proposals) {
              if (pattern != null && !pattern.matcher(proposal).matches()) continue;
              IContentProposal propsal = new BranchContentProposal(proposal);
              resultList.add(propsal);
            }

            return resultList.toArray(new IContentProposal[resultList.size()]);
          }
        };

    ContentProposalAdapter adapter =
        new ContentProposalAdapter(textField, new TextContentAdapter(), cp, stroke, null);
    // set the acceptance style to always replace the complete content
    adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
  }
Exemplo n.º 5
0
  private void addRefContentProposalToText(final Text textField) {
    KeyStroke stroke;
    try {
      stroke = KeyStroke.getInstance("CTRL+SPACE"); // $NON-NLS-1$
      UIUtils.addBulbDecorator(
          textField, NLS.bind(UIText.FetchGerritChangePage_ContentAssistTooltip, stroke.format()));
    } catch (ParseException e1) {
      Activator.handleError(e1.getMessage(), e1, false);
      stroke = null;
    }

    IContentProposalProvider cp =
        new IContentProposalProvider() {
          public IContentProposal[] getProposals(String contents, int position) {
            List<IContentProposal> resultList = new ArrayList<IContentProposal>();

            // make the simplest possible pattern check: allow "*"
            // for multiple characters
            String patternString = contents;
            // ignore spaces in the beginning
            while (patternString.length() > 0 && patternString.charAt(0) == ' ')
              patternString = patternString.substring(1);

            // we quote the string as it may contain spaces
            // and other stuff colliding with the Pattern
            patternString = Pattern.quote(patternString);

            patternString = patternString.replaceAll("\\x2A", ".*"); // $NON-NLS-1$ //$NON-NLS-2$

            // make sure we add a (logical) * at the end
            if (!patternString.endsWith(".*")) // $NON-NLS-1$
            patternString = patternString + ".*"; // $NON-NLS-1$

            // let's compile a case-insensitive pattern (assumes ASCII only)
            Pattern pattern;
            try {
              pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
            } catch (PatternSyntaxException e) {
              pattern = null;
            }

            List<Change> proposals;
            try {
              proposals = getRefsForContentAssist();
            } catch (InvocationTargetException e) {
              Activator.handleError(e.getMessage(), e, false);
              return null;
            } catch (InterruptedException e) {
              return null;
            }

            if (proposals != null)
              for (final Change ref : proposals) {
                if (pattern != null && !pattern.matcher(ref.getChangeNumber().toString()).matches())
                  continue;
                IContentProposal propsal = new ChangeContentProposal(ref);
                resultList.add(propsal);
              }

            return resultList.toArray(new IContentProposal[resultList.size()]);
          }
        };

    ContentProposalAdapter adapter =
        new ContentProposalAdapter(textField, new TextContentAdapter(), cp, stroke, null);
    // set the acceptance style to always replace the complete content
    adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
  }