The first most important thing is to override fillContentProvider() in order to add model items to dialog’s content provider as shown in the attached saple code.

Then Base API will call the filter that should be provided by extender by overriiding createFilter() method.

Finally we need to override restoreDialog to invoke applyFillter because restoreDialog will be called everytime the dialog is recreated or refreshed.

We should provide a label provider for showing item details in the status bar of dialog.

We can also add a ViewerFilter to the content provider as shown in attached snippet.

public class MyModelSelectionDialog extends FilteredItemsSelectionDialog {

private AbstractContentProvider contentProvider;

private SearchPattern fPatternMatcher;

public MyModelSelectionDialog(Shell shell, IProject project, String title) {

super(shell);

setTitle(title);

MyModelLabelProvider lp = new MyModelLabelProvider();

setListLabelProvider(lp);

setListSelectionLabelDecorator(lp);

setDetailsLabelProvider(new MyModelDetailsLabelProvider());

// setDialogImage();

// setDefaultImage();

}

@Override

protected void restoreDialog(IDialogSettings settings) {

super.restoreDialog(settings);

applyFilter();

}

protected void applyFilter() {

super.applyFilter();

}

private Collection getModelList() {

List list = Collections.EMPTY_LIST;

//

return list;

}

@Override

protected ItemsFilter createFilter() {

return new ItemsFilter() {

@Override

public boolean isConsistentItem(Object item) {

return true;

}

@Override

public boolean matchItem(Object element) {

/*

* if (element instanceof MyModel) { MyModel item = (MyModel)

* element; if (!matches(item.getName())) { return false; } }

*/

return true;

}

@Override

public String getPattern() {

if (fPatternMatcher == null) {

fPatternMatcher = patternMatcher;

}

if (“”.equals(patternMatcher.getPattern())) { //$NON-NLS-1$

return “?”; //$NON-NLS-1$

} else {

return super.getPattern();

}

}

};

}

@Override

protected void fillContentProvider(AbstractContentProvider contentProvider,

ItemsFilter itemsFilter, IProgressMonitor progressMonitor)

throws CoreException {

this.contentProvider = contentProvider;

populateModelList(itemsFilter);

}

/**

*

*/

private void populateModelList(ItemsFilter itemsFilter) {

Iterator iterator = getModelList().iterator();

while (iterator.hasNext()) {

Object item = iterator.next();

removeHistoryItem(item);

contentProvider.add(item, itemsFilter);

}

}

@Override

protected IDialogSettings getDialogSettings() {

/*

* IDialogSettings settings = UIActivator.getDefault()

* .getDialogSettings().getSection(this.getClass().getName()); if

* (settings == null) { settings = UIActivator.getDefault()

* .getDialogSettings().addNewSection( this.getClass().getName()); }

* return settings;

*/

return null;

}

@Override

public String getElementName(Object item) {

// apply some logic

return item.toString();

}

@Override

protected Comparator getItemsComparator() {

return new Comparator() {

public int compare(Object o1, Object o2) {

Collator.getInstance().compare(o1.toString(), o2.toString());

return 0;

}

};

}

@Override

protected IStatus validateItem(Object item) {

return Status.OK_STATUS;

}

/**

* @author kaniska

*/

private class MyModelLabelProvider extends LabelProvider implements

ILabelDecorator {

@Override

public String getText(Object element) {

return element == null ? “- none -” : element.toString();

}

@Override

public Image getImage(Object element) {

return null;

}

public Image decorateImage(Image image, Object element) {

return image;

}

public String decorateText(String text, Object element) {

return text;

}

}

private class MyModelDetailsLabelProvider extends LabelProvider implements

ILabelDecorator {

@Override

public String getText(Object element) {

return “”;

}

@Override

public Image getImage(Object element) {

Image image = null;

return image;

}

public Image decorateImage(Image image, Object element) {

return image;

}

public String decorateText(String text, Object element) {

return text;

}

}

@Override

protected void setResult(List newResult) {

List resultset = new ArrayList(newResult.size());

for (Object object : newResult) {

// add your logic

// resultset.add(object);

}

super.setResult(resultset);

}

@Override

protected Control createExtendedContentArea(Composite parent) {

// TODO Auto-generated method stub

return null;

}

}

Advertisements