use of org.bndtools.utils.collections.IdentityHashSet in project bndtools by bndtools.
the class RepositoriesView method createActions.
void createActions() {
collapseAllAction = new Action() {
@Override
public void run() {
viewer.collapseAll();
}
};
collapseAllAction.setEnabled(false);
collapseAllAction.setText("Collapse All");
collapseAllAction.setToolTipText("Collapse All");
collapseAllAction.setImageDescriptor(Icons.desc("collapse"));
collapseAllAction.setDisabledImageDescriptor(Icons.desc("collapse.disabled"));
refreshAction = new Action() {
@Override
public void run() {
new WorkspaceJob("Refresh repositories") {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
if (monitor == null)
monitor = new NullProgressMonitor();
monitor.subTask("Refresh all repositories");
try {
refreshAction.setEnabled(false);
Central.refreshPlugins();
} catch (Exception e) {
return new Status(Status.ERROR, Plugin.PLUGIN_ID, "Failed to refresh plugins", e);
} finally {
refreshAction.setEnabled(true);
}
return Status.OK_STATUS;
}
}.schedule();
}
};
refreshAction.setEnabled(false);
refreshAction.setText("Refresh");
refreshAction.setToolTipText("Refresh Repositories Tree");
refreshAction.setImageDescriptor(Icons.desc("refresh"));
refreshAction.setDisabledImageDescriptor(Icons.desc("refresh.disabled"));
addBundlesAction = new Action() {
@Override
public void run() {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
Object element = selection.getFirstElement();
if (element != null && element instanceof RepositoryPlugin) {
RepositoryPlugin repo = (RepositoryPlugin) element;
if (repo.canWrite()) {
AddFilesToRepositoryWizard wizard = new AddFilesToRepositoryWizard(repo, new File[0]);
WizardDialog dialog = new WizardDialog(getViewSite().getShell(), wizard);
dialog.open();
viewer.refresh(repo);
}
}
}
};
addBundlesAction.setEnabled(false);
addBundlesAction.setText("Add");
addBundlesAction.setToolTipText("Add Bundles to Repository");
addBundlesAction.setImageDescriptor(Icons.desc("add"));
addBundlesAction.setDisabledImageDescriptor(Icons.desc("add.disabled"));
advancedSearchAction = new Action("Advanced Search", Action.AS_CHECK_BOX) {
@Override
public void run() {
if (advancedSearchAction.isChecked()) {
AdvancedSearchDialog dialog = new AdvancedSearchDialog(getSite().getShell());
if (advancedSearchState != null) {
try {
XMLMemento memento = XMLMemento.createReadRoot(new StringReader(advancedSearchState));
dialog.restoreState(memento);
} catch (Exception e) {
logger.logError("Failed to load dialog state", e);
}
}
if (Window.OK == dialog.open()) {
Requirement req = dialog.getRequirement();
contentProvider.setRequirementFilter(req);
SWTUtil.recurseEnable(false, filterPanel);
viewer.refresh();
viewer.expandToLevel(2);
} else {
advancedSearchAction.setChecked(false);
}
try {
XMLMemento memento = XMLMemento.createWriteRoot("search");
dialog.saveState(memento);
StringWriter writer = new StringWriter();
memento.save(writer);
advancedSearchState = writer.toString();
} catch (Exception e) {
logger.logError("Failed to save dialog state", e);
}
} else {
contentProvider.setRequirementFilter(null);
SWTUtil.recurseEnable(true, filterPanel);
viewer.refresh();
}
}
};
advancedSearchAction.setEnabled(false);
advancedSearchAction.setText("Advanced Search");
advancedSearchAction.setToolTipText("Toggle Advanced Search");
advancedSearchAction.setImageDescriptor(Icons.desc("search"));
advancedSearchAction.setDisabledImageDescriptor(Icons.desc("search.disabled"));
downloadAction = new Action() {
@Override
public void run() {
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
// The set of Repos included in the selection; they will be completely downloaded.
Set<RemoteRepositoryPlugin> repos = new IdentityHashSet<>(selectionByType(selection, RemoteRepositoryPlugin.class));
// The set of Bundles included in the selection.
Set<RepositoryBundle> bundles = new IdentityHashSet<RepositoryBundle>();
for (RepositoryBundle bundle : selectionByType(selection, RepositoryBundle.class)) {
// filter out bundles that come from already-selected repos.
if (!repos.contains(bundle.getRepo()))
bundles.add(bundle);
}
// The set of Bundle Versions included in the selection
Set<RepositoryBundleVersion> bundleVersions = new IdentityHashSet<RepositoryBundleVersion>();
for (RepositoryBundleVersion bundleVersion : selectionByType(selection, RepositoryBundleVersion.class)) {
// filter out bundles that come from already-selected repos.
if (!repos.contains(bundleVersion.getRepo()))
bundleVersions.add(bundleVersion);
}
RepoDownloadJob downloadJob = new RepoDownloadJob(repos, bundles, bundleVersions);
downloadJob.schedule();
}
private <T> List<T> selectionByType(IStructuredSelection selection, Class<T> type) {
List<T> result = new ArrayList<T>(selection.size());
@SuppressWarnings("unchecked") Iterator<Object> iterator = selection.iterator();
while (iterator.hasNext()) {
Object item = iterator.next();
if (type.isInstance(item)) {
@SuppressWarnings("unchecked") T cast = (T) item;
result.add(cast);
}
}
return result;
}
};
downloadAction.setEnabled(false);
downloadAction.setText("Download Repository Content");
downloadAction.setImageDescriptor(Icons.desc("download"));
downloadAction.setDisabledImageDescriptor(Icons.desc("download.disabled"));
offlineAction = new Action("Online/Offline Mode", Action.AS_CHECK_BOX) {
@Override
public void run() {
Workspace workspace = Central.getWorkspaceIfPresent();
if (workspace != null) {
prefs.setWorkspaceOffline(offlineAction.isChecked());
}
}
};
offlineAction.setEnabled(false);
offlineAction.setToolTipText("Go Offline");
offlineAction.setImageDescriptor(Icons.desc("connected"));
offlineAction.setDisabledImageDescriptor(Icons.desc("connected.disabled"));
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
boolean enable = false;
@SuppressWarnings("unchecked") List<Object> list = selection.toList();
for (Object item : list) {
if (item instanceof RemoteRepositoryPlugin) {
enable = true;
break;
} else if (item instanceof RepositoryEntry) {
if (!((RepositoryEntry) item).isLocal()) {
enable = true;
break;
}
}
}
downloadAction.setEnabled(enable);
}
});
}
Aggregations