Skip to content

Commit 18714fd

Browse files
committed
Add filter text support in Templates part in Eclipse preference pages
across Java, Ant, and PDE editors
1 parent ce17c9e commit 18714fd

File tree

3 files changed

+97
-36
lines changed

3 files changed

+97
-36
lines changed

bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/templates/TemplatePreferencePage.java

Lines changed: 94 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import org.eclipse.jface.viewers.ColumnPixelData;
8989
import org.eclipse.jface.viewers.ColumnWeightData;
9090
import org.eclipse.jface.viewers.IBaseLabelProvider;
91+
import org.eclipse.jface.viewers.IStructuredContentProvider;
9192
import org.eclipse.jface.viewers.IStructuredSelection;
9293
import org.eclipse.jface.viewers.ITableLabelProvider;
9394
import org.eclipse.jface.viewers.LabelProvider;
@@ -96,6 +97,7 @@
9697
import org.eclipse.jface.viewers.TableViewer;
9798
import org.eclipse.jface.viewers.Viewer;
9899
import org.eclipse.jface.viewers.ViewerComparator;
100+
import org.eclipse.jface.viewers.ViewerFilter;
99101
import org.eclipse.jface.window.Window;
100102

101103
import org.eclipse.jface.text.Document;
@@ -152,6 +154,8 @@ public abstract class TemplatePreferencePage extends PreferencePage implements I
152154
*
153155
* @since 3.3
154156
*/
157+
private final Map<String, Boolean> checkedStates = new HashMap<>();
158+
155159
protected static class EditTemplateDialog extends StatusDialog {
156160

157161
private final Template fOriginalTemplate;
@@ -796,38 +800,45 @@ public void setContextTypeRegistry(ContextTypeRegistry registry) {
796800
public void init(IWorkbench workbench) {
797801
}
798802

803+
private String getKey(TemplatePersistenceData data) {
804+
return data.getTemplate().getName() + "|" + data.getTemplate().getContextTypeId(); //$NON-NLS-1$
805+
}
806+
799807
@Override
800808
protected Control createContents(Composite ancestor) {
801-
Composite parent= new Composite(ancestor, SWT.NONE);
802-
GridLayout layout= new GridLayout();
803-
layout.numColumns= 2;
804-
layout.marginHeight= 0;
805-
layout.marginWidth= 0;
806-
parent.setLayout(layout);
807-
808-
Composite innerParent= new Composite(parent, SWT.NONE);
809-
GridLayout innerLayout= new GridLayout();
810-
innerLayout.numColumns= 2;
811-
innerLayout.marginHeight= 0;
812-
innerLayout.marginWidth= 0;
813-
innerParent.setLayout(innerLayout);
814-
GridData gd= new GridData(GridData.FILL_BOTH);
815-
gd.horizontalSpan= 2;
816-
innerParent.setLayoutData(gd);
817-
818-
Composite tableComposite= new Composite(innerParent, SWT.NONE);
819-
GridData data= new GridData(GridData.FILL_BOTH);
820-
data.widthHint= 360;
821-
data.heightHint= convertHeightInCharsToPixels(10);
822-
tableComposite.setLayoutData(data);
823-
824-
ColumnLayout columnLayout= new ColumnLayout();
809+
Composite parent = new Composite(ancestor, SWT.NONE);
810+
parent.setLayout(new GridLayout(1, false));
811+
812+
Composite tcomposite = new Composite(parent, SWT.NONE);
813+
tcomposite.setLayout(new GridLayout(2, false));
814+
tcomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
815+
816+
Composite fcomposite = new Composite(tcomposite, SWT.NONE);
817+
fcomposite.setLayout(new GridLayout(1, false));
818+
fcomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
819+
820+
Text filterText = new Text(fcomposite, SWT.BORDER | SWT.SEARCH | SWT.ICON_CANCEL);
821+
filterText.setMessage(TemplatesMessages.TemplatePreferencePage_filterText);
822+
GridData filterData = new GridData(SWT.FILL, SWT.CENTER, true, false);
823+
filterData.widthHint = 360;
824+
filterText.setLayoutData(filterData);
825+
826+
Composite tableComposite = new Composite(fcomposite, SWT.NONE);
827+
GridData tableData = new GridData(SWT.FILL, SWT.FILL, true, true);
828+
tableData.widthHint = 360;
829+
tableData.heightHint = convertHeightInCharsToPixels(10);
830+
tableComposite.setLayoutData(tableData);
831+
832+
ColumnLayout columnLayout = new ColumnLayout();
825833
tableComposite.setLayout(columnLayout);
826-
Table table= new Table(tableComposite, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
834+
Table table = new Table(tableComposite,
835+
SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
827836

828837
table.setHeaderVisible(true);
829838
table.setLinesVisible(true);
830839

840+
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
841+
831842
GC gc= new GC(getShell());
832843
gc.setFont(JFaceResources.getDialogFont());
833844

@@ -877,18 +888,52 @@ protected Control createContents(Composite ancestor) {
877888
fTableViewer.addCheckStateListener(event -> {
878889
TemplatePersistenceData d = (TemplatePersistenceData) event.getElement();
879890
d.setEnabled(event.getChecked());
891+
checkedStates.put(getKey(d), event.getChecked());
880892
});
881893

882894
BidiUtils.applyTextDirection(fTableViewer.getControl(), BidiUtils.BTD_DEFAULT);
883895

884-
Composite buttons= new Composite(innerParent, SWT.NONE);
885-
buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
886-
layout= new GridLayout();
887-
layout.marginHeight= 0;
888-
layout.marginWidth= 0;
889-
buttons.setLayout(layout);
896+
TemplateFilter filter = new TemplateFilter();
897+
fTableViewer.addFilter(filter);
898+
899+
filterText.addModifyListener(e -> {
900+
filter.setSearchText(filterText.getText());
901+
for (Object element : fTableViewer.getCheckedElements()) {
902+
if (element instanceof TemplatePersistenceData data) {
903+
String key = getKey(data);
904+
checkedStates.put(key, true);
905+
}
906+
}
907+
for (Object element : ((IStructuredContentProvider) fTableViewer.getContentProvider())
908+
.getElements(fTableViewer.getInput())) {
909+
if (element instanceof TemplatePersistenceData data) {
910+
String key = getKey(data);
911+
if (!fTableViewer.getChecked(data)) {
912+
checkedStates.putIfAbsent(key, false);
913+
}
914+
}
915+
}
916+
fTableViewer.refresh();
917+
918+
for (Object element : ((IStructuredContentProvider) fTableViewer.getContentProvider())
919+
.getElements(fTableViewer.getInput())) {
920+
if (element instanceof TemplatePersistenceData data) {
921+
Boolean checked = checkedStates.get(getKey(data));
922+
if (checked != null && checked) {
923+
fTableViewer.setChecked(data, true);
924+
}
925+
}
926+
}
927+
});
928+
929+
Composite buttons = new Composite(tcomposite, SWT.NONE);
930+
buttons.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
931+
GridLayout buttonLayout = new GridLayout();
932+
buttonLayout.marginHeight = 0;
933+
buttonLayout.marginWidth = 0;
934+
buttons.setLayout(buttonLayout);
890935

891-
fAddButton= new Button(buttons, SWT.PUSH);
936+
fAddButton = new Button(buttons, SWT.PUSH);
892937
fAddButton.setText(TemplatesMessages.TemplatePreferencePage_new);
893938
fAddButton.setLayoutData(getButtonGridData(fAddButton));
894939
fAddButton.addListener(SWT.Selection, e -> add());
@@ -932,9 +977,7 @@ protected Control createContents(Composite ancestor) {
932977
if (isShowFormatterSetting()) {
933978
fFormatButton= new Button(parent, SWT.CHECK);
934979
fFormatButton.setText(TemplatesMessages.TemplatePreferencePage_use_code_formatter);
935-
GridData gd1= new GridData();
936-
gd1.horizontalSpan= 2;
937-
fFormatButton.setLayoutData(gd1);
980+
fFormatButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
938981
fFormatButton.setSelection(getPreferenceStore().getBoolean(getFormatterPreferenceKey()));
939982
}
940983

@@ -944,11 +987,26 @@ protected Control createContents(Composite ancestor) {
944987

945988
updateButtons();
946989
Dialog.applyDialogFont(parent);
947-
innerParent.layout();
948990

949991
return parent;
950992
}
951993

994+
class TemplateFilter extends ViewerFilter {
995+
private String searchString = ""; //$NON-NLS-1$
996+
public void setSearchText(String s) {
997+
searchString = (s == null) ? "" : s.toLowerCase(); //$NON-NLS-1$
998+
}
999+
@Override
1000+
public boolean select(Viewer viewer, Object parentElement, Object element) {
1001+
if (searchString.isEmpty())
1002+
return true;
1003+
Template template = ((TemplatePersistenceData) element).getTemplate();
1004+
return template.getName().toLowerCase().contains(searchString)
1005+
|| template.getContextTypeId().toLowerCase().contains(searchString)
1006+
|| template.getDescription().toLowerCase().contains(searchString);
1007+
}
1008+
}
1009+
9521010
/*
9531011
* @since 3.2
9541012
*/

bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/templates/TemplatesMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ private TemplatesMessages() {
126126
public static String TemplatesPage_remove_title_single;
127127
public static String TemplatesPage_remove_title_multi;
128128
public static String TemplatesView_no_templates;
129+
public static String TemplatePreferencePage_filterText;
129130

130131
static {
131132
NLS.initializeMessages(BUNDLE_NAME, TemplatesMessages.class);

bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/templates/TemplatesMessages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ TemplatePreferencePage_question_create_new_button_rename= Rename Existing
6464

6565
TemplatePreferencePage_preview=Previe&w:
6666

67+
TemplatePreferencePage_filterText=type filter text
68+
6769
# edit template dialog
6870
EditTemplateDialog_error_noname=Template name cannot be empty.
6971
EditTemplateDialog_error_adjacent_variables=Template pattern has adjacent variables.

0 commit comments

Comments
 (0)