1 package com.proalpha.pds.gitutils.search;
3 import java.awt.Desktop;
5 import java.io.IOException;
7 import java.net.URISyntaxException;
8 import java.util.ArrayList;
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;
37 import com.proalpha.pds.gitutils.Activator;
43 private static final String PAGE_NAME =
"pAGitGrepSearchPage";
44 private static final String HISTORY_STORE =
"HISTORY";
45 private static final int HISTORY_SIZE = 1;
47 private Text txtMatchesFile;
48 private Spinner txtMaxFinds;
49 private Button btnCaseSensitive;
50 private Combo cmbObjectType;
51 private CheckboxTableViewer repositoryViewer;
52 private ISearchPageContainer container;
54 private List<GitGrepSearchSettings> savedSettings =
new ArrayList<>(HISTORY_SIZE);
55 private Combo cmbSearchPhrase;
57 @SuppressWarnings(
"restriction")
59 public void createControl(Composite parent) {
62 Composite main =
new Composite(parent, SWT.NONE);
63 main.setLayout(
new GridLayout(2,
false));
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();
74 cmbSearchPhrase.addModifyListener(event ->
78 btnCaseSensitive =
new Button(main, SWT.CHECK);
79 btnCaseSensitive.setText(
"Case sensitive");
81 Label lblHint =
new Label(main, SWT.NONE);
82 lblHint.setText(
"(* = any string, ? = any character, \\\\ = escape for literals: * ? \\\\)");
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()) {
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);
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");
103 Label lblMatchesFile =
new Label(grpScope, SWT.NONE);
104 lblMatchesFile.setBounds(10, 18, 122, 15);
105 lblMatchesFile.setText(
"Objectpath:");
107 txtMatchesFile =
new Text(grpScope, SWT.BORDER);
108 txtMatchesFile.setBounds(139, 15, 150, 21);
109 txtMatchesFile.setText(
"**/*.*");
111 Label lblObjecttype =
new Label(grpScope, SWT.NONE);
112 lblObjecttype.setBounds(10, 44, 70, 15);
113 lblObjecttype.setText(
"Objecttype:");
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");
120 Label lblMaxfinds =
new Label(grpScope, SWT.NONE);
121 lblMaxfinds.setBounds(10, 70, 100, 15);
122 lblMaxfinds.setText(
"Maxfinds (1 - 999):");
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 -> {
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());
135 catch (NumberFormatException e) {
136 txtMaxFinds.setSelection(txtMaxFinds.getMinimum());
139 new Label(main, SWT.NONE);
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);
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);
158 GridDataFactory.fillDefaults().grab(
true,
true).hint(SWT.DEFAULT, 135)
159 .applyTo(this.repositoryViewer.getControl());
161 repositoryGroup.setText(
"Repositories:");
164 Dialog.applyDialogFont(main);
168 @SuppressWarnings(
"restriction")
170 public boolean performAction() {
172 GitGrepSearchSettings searchSettings =
new GitGrepSearchSettings();
174 if (txtMatchesFile.getText().length() > 0)
175 searchSettings.setFileNamePhrase(txtMatchesFile.getText());
177 searchSettings.setTextPhrase(cmbSearchPhrase.getText());
178 searchSettings.setFileType(cmbObjectType.getText());
179 searchSettings.setCaseSensitive(btnCaseSensitive.getSelection());
180 searchSettings.setMaxFinds(Integer.parseInt(txtMaxFinds.getText()));
182 for (Object checkedElement : repositoryViewer.getCheckedElements()) {
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);
193 savedSettings.add(0, searchSettings);
195 GitGrepSearchQuery newQuery =
new GitGrepSearchQuery(searchSettings);
196 NewSearchUI.runQueryInBackground(newQuery);
208 this.container = container;
211 private ISearchPageContainer getContainer() {
212 return this.container;
216 public void dispose() {
222 public void setVisible(
boolean 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());
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) {
244 super.setVisible(visible);
252 private String[] getSavedSearchPhrases() {
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();
266 private void checkOK() {
268 boolean isOk =
false;
269 if (cmbSearchPhrase.getText().length() > 0 && repositoryViewer.getCheckedElements().length > 0)
272 getContainer().setPerformActionEnabled(isOk);
281 private IDialogSettings getDialogSettings() {
283 IDialogSettings dialogSettings = Activator.getDefault().getDialogSettings();
284 IDialogSettings section = dialogSettings.getSection(PAGE_NAME);
286 section = dialogSettings.addNewSection(PAGE_NAME);
291 private void storeSettings() {
293 int minSize = Math.min(savedSettings.size(), HISTORY_SIZE);
295 IDialogSettings ds = getDialogSettings();
296 ds.put(
"HISTORY_SIZE", minSize);
298 for (
int iCount = 0; iCount < minSize; iCount++)
299 savedSettings.get(iCount).save(ds.addNewSection(HISTORY_STORE + iCount));
303 private void loadSettings() {
304 IDialogSettings ds = getDialogSettings();
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));
312 }
catch (Exception ignored) {
314 savedSettings.add(GitGrepSearchSettings.init());
void setContainer(ISearchPageContainer container)