Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 54fa901b9a713d217a034c27dd989e0e9af00a34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
 * Copyright (c) 2008 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.m2e.core.ui.internal.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.fieldassist.IContentProposal;
import org.eclipse.jface.fieldassist.IContentProposalProvider;
import org.eclipse.jface.fieldassist.IControlContentAdapter;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.fieldassist.ContentAssistCommandAdapter;

import org.apache.lucene.queryParser.QueryParser;

import org.apache.maven.project.MavenProject;

import org.eclipse.m2e.core.ui.internal.M2EUIPluginActivator;
import org.eclipse.m2e.core.ui.internal.search.util.CComboContentAdapter;
import org.eclipse.m2e.core.ui.internal.search.util.ControlDecoration;
import org.eclipse.m2e.core.ui.internal.search.util.Packaging;
import org.eclipse.m2e.core.ui.internal.search.util.SearchEngine;


/**
 * Holds the proposal utility code, previously in the editor.xml plug-in. Provides proposal suggestions for text and
 * combo widgets for various metadata (group, artifact, etc.)
 * 
 * @author rgould
 */
public class ProposalUtil {
  private static final Logger log = LoggerFactory.getLogger(ProposalUtil.class);

  public static abstract class Searcher {
    public abstract Collection<String> search() throws CoreException;
  }

  public static final class TextProposal implements IContentProposal {
    private final String text;

    public TextProposal(String text) {
      this.text = text;
    }

    public int getCursorPosition() {
      return text.length();
    }

    public String getContent() {
      return text;
    }

    public String getLabel() {
      return text;
    }

    public String getDescription() {
      return null;
    }
  }

  public static void addCompletionProposal(final Control control, final Searcher searcher) {
    FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
        FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
    ControlDecoration decoration = new ControlDecoration(control, SWT.LEFT | SWT.TOP);
    decoration.setShowOnlyOnFocus(true);
    decoration.setDescriptionText(fieldDecoration.getDescription());
    decoration.setImage(fieldDecoration.getImage());

    IContentProposalProvider proposalProvider = new IContentProposalProvider() {
      public IContentProposal[] getProposals(String contents, int position) {
        final String start = contents.length() > position ? contents.substring(0, position) : contents;
        ArrayList<IContentProposal> proposals = new ArrayList<IContentProposal>();
        try {
          for(final String text : searcher.search()) {
            if(text.startsWith(start)) {
              proposals.add(new TextProposal(text));
            }
          }
        } catch(CoreException e) {
          log.error(e.getMessage(), e);
        }
        return proposals.toArray(new IContentProposal[proposals.size()]);
      }
    };

    IControlContentAdapter contentAdapter;
    if(control instanceof Text) {
      contentAdapter = new TextContentAdapter();
    } else {
      contentAdapter = new CComboContentAdapter();
    }

    ContentAssistCommandAdapter adapter = new ContentAssistCommandAdapter( //
        control, contentAdapter, proposalProvider, //
        IWorkbenchCommandConstants.EDIT_CONTENT_ASSIST, null);
    // ContentProposalAdapter adapter = new ContentProposalAdapter(control, contentAdapter, //
    //     proposalProvider, KeyStroke.getInstance(SWT.MOD1, ' '), null);
    adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
    adapter.setPopupSize(new Point(250, 120));
    adapter.setPopupSize(new Point(250, 120));
  }

  public static void addClassifierProposal(final IProject project, final Text groupIdText, final Text artifactIdText,
      final Text versionText, final Text classifierText, final Packaging packaging) {
    addCompletionProposal(classifierText, new Searcher() {
      public Collection<String> search() throws CoreException {
        return getSearchEngine(project).findClassifiers(
            escapeQuerySpecialCharacters(groupIdText.getText()), //
            escapeQuerySpecialCharacters(artifactIdText.getText()),
            escapeQuerySpecialCharacters(versionText.getText()), "", packaging);
      }
    });
  }

  public static void addVersionProposal(final IProject project, final MavenProject mp, final Text groupIdText,
      final Text artifactIdText, final Text versionText, final Packaging packaging) {
    addCompletionProposal(versionText, new Searcher() {
      public Collection<String> search() throws CoreException {
        Collection<String> toRet = new ArrayList<String>();
        toRet.addAll(getSearchEngine(project).findVersions(escapeQuerySpecialCharacters(groupIdText.getText()), //
            escapeQuerySpecialCharacters(artifactIdText.getText()), "", packaging));
        if(mp != null) {
          //add version props now..
          Properties props = mp.getProperties();
          ArrayList<String> list = new ArrayList<String>();
          if(props != null) {
            for(Object prop : props.keySet()) {
              String propString = prop.toString();
              if(propString.endsWith("Version") || propString.endsWith(".version")) { //$NON-NLS-1$//$NON-NLS-2$
                list.add("${" + propString + "}"); //$NON-NLS-1$//$NON-NLS-2$
              }
            }
          }
          Collections.sort(list);
          toRet.addAll(list);
        }
        return toRet;
      }
    });
  }

  public static void addArtifactIdProposal(final IProject project, final Text groupIdText, final Text artifactIdText,
      final Packaging packaging) {
    addCompletionProposal(artifactIdText, new Searcher() {
      public Collection<String> search() throws CoreException {
        // TODO handle artifact info
        return getSearchEngine(project).findArtifactIds(escapeQuerySpecialCharacters(groupIdText.getText()), "",
            packaging, null);
      }
    });
  }

  public static void addGroupIdProposal(final IProject project, final Text groupIdText, final Packaging packaging) {
    addCompletionProposal(groupIdText, new Searcher() {
      public Collection<String> search() throws CoreException {
        // TODO handle artifact info
        return getSearchEngine(project).findGroupIds(escapeQuerySpecialCharacters(groupIdText.getText()), packaging,
            null);
      }
    });
  }

  //issue 350271
  //http://lucene.apache.org/java/3_2_0/queryparsersyntax.html#Escaping Special Characters
  //for proposal queries, any special chars shall be escaped
  //    + - && || ! ( ) { } [ ] ^ " ~ * ? : \    
  private static String escapeQuerySpecialCharacters(String raw) {
    return QueryParser.escape(raw);
  }

  public static SearchEngine getSearchEngine(final IProject project) throws CoreException {
    return M2EUIPluginActivator.getDefault().getSearchEngine(project);
  }

}

Back to the top