Eclipseplugins
GitGrepSearchPage.java
1 package com.proalpha.pds.gitutils.search;
2 
3 import java.awt.Desktop;
4 import java.io.File;
5 import java.io.IOException;
6 import java.net.URI;
7 import java.net.URISyntaxException;
8 import java.util.ArrayList;
9 import java.util.List;
10 
11 import org.eclipse.core.resources.ResourcesPlugin;
12 import org.eclipse.egit.ui.internal.repository.RepositoriesViewContentProvider;
13 import org.eclipse.egit.ui.internal.repository.RepositoryTreeNodeLabelProvider;
14 import org.eclipse.egit.ui.internal.repository.tree.RepositoryNode;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.DialogPage;
17 import org.eclipse.jface.dialogs.IDialogSettings;
18 import org.eclipse.jface.layout.GridDataFactory;
19 import org.eclipse.jface.layout.GridLayoutFactory;
20 import org.eclipse.jface.viewers.CheckboxTableViewer;
21 import org.eclipse.search.ui.ISearchPage;
22 import org.eclipse.search.ui.ISearchPageContainer;
23 import org.eclipse.search.ui.NewSearchUI;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Combo;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Group;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Spinner;
33 import org.eclipse.swt.widgets.Text;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 
37 import com.proalpha.pds.gitutils.Activator;
38 
39 public class GitGrepSearchPage extends DialogPage implements ISearchPage {
40 
41  private final Logger logger = LoggerFactory.getLogger(GitGrepSearchPage.class);
42 
43  private static final String PAGE_NAME = "pAGitGrepSearchPage";
44  private static final String HISTORY_STORE = "HISTORY";
45  private static final int HISTORY_SIZE = 1;
46 
47  private Text txtMatchesFile;
48  private Spinner txtMaxFinds;
49  private Button btnCaseSensitive;
50  private Combo cmbObjectType;
51  private CheckboxTableViewer repositoryViewer;
52  private ISearchPageContainer container;
53 
54  private List<GitGrepSearchSettings> savedSettings = new ArrayList<>(HISTORY_SIZE);
55  private Combo cmbSearchPhrase;
56 
57  @SuppressWarnings("restriction")
58  @Override
59  public void createControl(Composite parent) {
60  loadSettings();
61 
62  Composite main = new Composite(parent, SWT.NONE);
63  main.setLayout(new GridLayout(2, false));
64 
65  Label lblContainingText = new Label(main, SWT.NONE);
66  lblContainingText.setText("Containing text:");
67  new Label(main, SWT.NONE);
68  cmbSearchPhrase = new Combo(main, SWT.SINGLE | SWT.BORDER);
69  GridData gdCmbSearchPhrase = new GridData(GridData.FILL, GridData.FILL, true, false, 1, 1);
70  gdCmbSearchPhrase.widthHint = convertWidthInCharsToPixels(10);
71  cmbSearchPhrase.setLayoutData(gdCmbSearchPhrase);
72  cmbSearchPhrase.setFocus();
73 
74  cmbSearchPhrase.addModifyListener(event ->
75  checkOK()
76  );
77 
78  btnCaseSensitive = new Button(main, SWT.CHECK);
79  btnCaseSensitive.setText("Case sensitive");
80 
81  Label lblHint = new Label(main, SWT.NONE);
82  lblHint.setText("(* = any string, ? = any character, \\\\ = escape for literals: * ? \\\\)");
83 
84  Button help = new Button(main, SWT.NONE);
85  help.setText("Git Search Help");
86  help.addListener(SWT.Selection, event -> {
87  if (event.type == SWT.Selection && Desktop.isDesktopSupported()) {
88  try {
89  Desktop.getDesktop().browse(new URI("https://git-scm.com/docs/git-grep"));
90  } catch (IOException | URISyntaxException e) {
91  logger.error("Error occured while opening git grep help", e);
92  }
93  }
94  });
95 
96  Group grpScope = new Group(main, SWT.NONE);
97  GridData gdGrpScope = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
98  gdGrpScope.heightHint = 77;
99  gdGrpScope.widthHint = 475;
100  grpScope.setLayoutData(gdGrpScope);
101  grpScope.setText("Scope");
102 
103  Label lblMatchesFile = new Label(grpScope, SWT.NONE);
104  lblMatchesFile.setBounds(10, 18, 122, 15);
105  lblMatchesFile.setText("Objectpath:");
106 
107  txtMatchesFile = new Text(grpScope, SWT.BORDER);
108  txtMatchesFile.setBounds(139, 15, 150, 21);
109  txtMatchesFile.setText("**/*.*");
110 
111  Label lblObjecttype = new Label(grpScope, SWT.NONE);
112  lblObjecttype.setBounds(10, 44, 70, 15);
113  lblObjecttype.setText("Objecttype:");
114 
115  cmbObjectType = new Combo(grpScope, SWT.NONE);
116  cmbObjectType.setItems(new String[] { "ALL", "PDI", "SRC", "SRC_PDI" });
117  cmbObjectType.setBounds(139, 41, 60, 23);
118  cmbObjectType.setText("ALL");
119 
120  Label lblMaxfinds = new Label(grpScope, SWT.NONE);
121  lblMaxfinds.setBounds(10, 70, 100, 15);
122  lblMaxfinds.setText("Maxfinds (1 - 999):");
123 
124  txtMaxFinds = new Spinner(grpScope, SWT.BORDER);
125  txtMaxFinds.setBounds(139, 67, 40, 21);
126  txtMaxFinds.setSize(50, 20);
127  txtMaxFinds.setValues(100, 1,999,0,50, 100);
128  txtMaxFinds.addListener(SWT.Modify, event -> {
129  try {
130  if (Integer.parseInt(txtMaxFinds.getText()) > txtMaxFinds.getMaximum())
131  txtMaxFinds.setSelection(txtMaxFinds.getMaximum());
132  else if (Integer.parseInt(txtMaxFinds.getText()) < txtMaxFinds.getMinimum())
133  txtMaxFinds.setSelection(txtMaxFinds.getMinimum());
134  }
135  catch (NumberFormatException e) {
136  txtMaxFinds.setSelection(txtMaxFinds.getMinimum());
137  }
138  });
139  new Label(main, SWT.NONE);
140 
141  Group repositoryGroup;
142  repositoryGroup = new Group(main, SWT.NONE);
143  repositoryGroup.setBackgroundMode(SWT.INHERIT_DEFAULT);
144  GridDataFactory.fillDefaults().grab(true, true).span(2, 1).applyTo(repositoryGroup);
145  GridLayoutFactory.swtDefaults().numColumns(2).applyTo(repositoryGroup);
146 
147  this.repositoryViewer = CheckboxTableViewer.newCheckList(repositoryGroup,
148  SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
149  this.repositoryViewer.setLabelProvider(new RepositoryTreeNodeLabelProvider());
150  this.repositoryViewer.setContentProvider(new RepositoriesViewContentProvider());
151  this.repositoryViewer.setInput(ResourcesPlugin.getWorkspace().getRoot());
152  this.repositoryViewer.addCheckStateListener(event -> {
153  repositoryViewer.setAllChecked(false);
154  repositoryViewer.setChecked(event.getElement(), true);
155  checkOK();
156  });
157 
158  GridDataFactory.fillDefaults().grab(true, true).hint(SWT.DEFAULT, 135)
159  .applyTo(this.repositoryViewer.getControl());
160 
161  repositoryGroup.setText("Repositories:");
162 
163  setControl(main);
164  Dialog.applyDialogFont(main);
165 
166  }
167 
168  @SuppressWarnings("restriction")
169  @Override
170  public boolean performAction() {
171 
172  GitGrepSearchSettings searchSettings = new GitGrepSearchSettings();
173 
174  if (txtMatchesFile.getText().length() > 0)
175  searchSettings.setFileNamePhrase(txtMatchesFile.getText());
176 
177  searchSettings.setTextPhrase(cmbSearchPhrase.getText());
178  searchSettings.setFileType(cmbObjectType.getText());
179  searchSettings.setCaseSensitive(btnCaseSensitive.getSelection());
180  searchSettings.setMaxFinds(Integer.parseInt(txtMaxFinds.getText()));
181 
182  for (Object checkedElement : repositoryViewer.getCheckedElements()) {
183  try {
184  searchSettings.setRepositoryPath(
185  ((RepositoryNode) checkedElement).getRepository().getDirectory().getAbsolutePath());
186  searchSettings.setBranchName(((RepositoryNode) checkedElement).getRepository().getBranch());
187  } catch (IOException e) {
188  logger.error(e.getMessage(), e);
189  }
190  }
191 
192  // Add settings to History
193  savedSettings.add(0, searchSettings);
194 
195  GitGrepSearchQuery newQuery = new GitGrepSearchQuery(searchSettings);
196  NewSearchUI.runQueryInBackground(newQuery);
197 
198  return true;
199  }
200 
206  @Override
207  public void setContainer(ISearchPageContainer container) {
208  this.container = container;
209  }
210 
211  private ISearchPageContainer getContainer() {
212  return this.container;
213  }
214 
215  @Override
216  public void dispose() {
217  storeSettings();
218  super.dispose();
219  }
220 
221  @Override
222  public void setVisible(boolean visible) {
223  if (visible) {
224  cmbSearchPhrase.setItems(getSavedSearchPhrases());
225  cmbSearchPhrase.setText(savedSettings.get(0).getTextPhrase());
226  txtMatchesFile.setText(savedSettings.get(0).getFileNamePhrase());
227  txtMaxFinds.setSelection(savedSettings.get(0).getMaxFinds());
228  btnCaseSensitive.setSelection(savedSettings.get(0).isCaseSensitive());
229  cmbObjectType.setText(savedSettings.get(0).getFileType());
230  File file = new File(savedSettings.get(0).getRepositoryPath());
231  if (file.exists())
232  try {
233  @SuppressWarnings("restriction")
234  RepositoryNode node = new RepositoryNode(null,
235  org.eclipse.egit.core.Activator.getDefault().getRepositoryCache().lookupRepository(file));
236  repositoryViewer.setChecked(node, true);
237  } catch (IOException ignore) {
238  // Ignore and don't check
239  }
240 
241  checkOK();
242  }
243 
244  super.setVisible(visible);
245  }
246 
252  private String[] getSavedSearchPhrases() {
253 
254  int size = savedSettings.size();
255  String[] testPhrases = new String[size];
256  for (int iCount = 0; iCount < size; iCount++)
257  testPhrases[iCount] = savedSettings.get(iCount).getTextPhrase();
258 
259  return testPhrases;
260 
261  }
262 
266  private void checkOK() {
267 
268  boolean isOk = false;
269  if (cmbSearchPhrase.getText().length() > 0 && repositoryViewer.getCheckedElements().length > 0)
270  isOk = true;
271 
272  getContainer().setPerformActionEnabled(isOk);
273 
274  }
275 
281  private IDialogSettings getDialogSettings() {
282 
283  IDialogSettings dialogSettings = Activator.getDefault().getDialogSettings();
284  IDialogSettings section = dialogSettings.getSection(PAGE_NAME);
285  if (section == null)
286  section = dialogSettings.addNewSection(PAGE_NAME);
287  return section;
288 
289  }
290 
291  private void storeSettings() {
292 
293  int minSize = Math.min(savedSettings.size(), HISTORY_SIZE);
294 
295  IDialogSettings ds = getDialogSettings();
296  ds.put("HISTORY_SIZE", minSize);
297 
298  for (int iCount = 0; iCount < minSize; iCount++)
299  savedSettings.get(iCount).save(ds.addNewSection(HISTORY_STORE + iCount));
300 
301  }
302 
303  private void loadSettings() {
304  IDialogSettings ds = getDialogSettings();
305  try {
306  int histSize = ds.getInt("HISTORY_SIZE");
307  for (int iCount = 0; iCount < histSize; iCount++) {
308  IDialogSettings histSettings = ds.getSection(HISTORY_STORE + iCount);
309  if (histSettings != null)
310  savedSettings.add(GitGrepSearchSettings.load(histSettings));
311  }
312  } catch (Exception ignored) {
313  //ignored
314  savedSettings.add(GitGrepSearchSettings.init());
315  }
316  }
317 
318 }
void setContainer(ISearchPageContainer container)