Eclipse RCP : fenêtre de sélection de ressource

Petit mémo pour ne pas ré-inventer la roue : voici un lien très utile qui recense les fenêtres de dialogue facilement implémentables disponibles dans la plateforme RCP : EclipseTips - Selection Dialogs in Eclipse

Et un petit exemple, avec un bonus. Ici on crée une fenêtre permettant de sélectionner uniquement un fichier XML présent dans le workspace :


final ElementTreeSelectionDialog selectDialog = new ElementTreeSelectionDialog(Display.getDefault().getActiveShell(), 
  new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());
selectDialog.setTitle("Sélection de ressource");
selectDialog.setMessage("Sélectionner le fichier XML :");
selectDialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
selectDialog.setAllowMultiple(false);
selectDialog.setHelpAvailable(false);
selectDialog.setValidator(new ISelectionStatusValidator() {
 @Override
 public IStatus validate(final Object[] arg0) {
  boolean isFileOK = arg0.length>0 && (arg0[0] instanceof IFile) && ((IFile)arg0[0]).getName().endsWith(".xml");
  selectDialog.getOkButton().setVisible(isFileOK);
  return isFileOK ? Status.OK_STATUS : Status.CANCEL_STATUS;
 }
});
selectDialog.open();

String xmlPath = ((IFile)selectDialog.getFirstResult()).getRawLocationURI().getPath().substring(1);

On notera l'utilisation du ISelectionStatusValidator pour valider l'objet sélectionné.

Hope this helps!


Fichier(s) joint(s) :

0 commentaires: