Search in sources :

Example 16 with RepositoryPlugin

use of aQute.bnd.service.RepositoryPlugin 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);
        }
    });
}
Also used : RepositoryBundle(bndtools.model.repo.RepositoryBundle) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Action(org.eclipse.jface.action.Action) RepositoryBundleVersion(bndtools.model.repo.RepositoryBundleVersion) Set(java.util.Set) IdentityHashSet(org.bndtools.utils.collections.IdentityHashSet) AddFilesToRepositoryWizard(bndtools.wizards.workspace.AddFilesToRepositoryWizard) RemoteRepositoryPlugin(aQute.bnd.service.RemoteRepositoryPlugin) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) RepositoryEntry(bndtools.model.repo.RepositoryEntry) StringWriter(java.io.StringWriter) SWT(org.eclipse.swt.SWT) StringReader(java.io.StringReader) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) RemoteRepositoryPlugin(aQute.bnd.service.RemoteRepositoryPlugin) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) Requirement(org.osgi.resource.Requirement) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) XMLMemento(org.eclipse.ui.XMLMemento) IFile(org.eclipse.core.resources.IFile) File(java.io.File) JarFile(java.util.jar.JarFile) WizardDialog(org.eclipse.jface.wizard.WizardDialog) Workspace(aQute.bnd.build.Workspace)

Example 17 with RepositoryPlugin

use of aQute.bnd.service.RepositoryPlugin in project bndtools by bndtools.

the class RepositoriesView method createContextMenu.

void createContextMenu() {
    MenuManager mgr = new MenuManager();
    Menu menu = mgr.createContextMenu(viewer.getControl());
    viewer.getControl().setMenu(menu);
    mgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
    getSite().registerContextMenu(mgr, viewer);
    mgr.addMenuListener(new IMenuListener() {

        @Override
        public void menuAboutToShow(IMenuManager manager) {
            try {
                manager.removeAll();
                IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
                if (!selection.isEmpty()) {
                    final Object firstElement = selection.getFirstElement();
                    if (firstElement instanceof Actionable) {
                        final RepositoryPlugin rp = getRepositoryPlugin(firstElement);
                        //
                        // Use the Actionable interface to fill the menu
                        // Should extend this to allow other menu entries
                        // from the view, but currently there are none
                        //
                        final Actionable act = (Actionable) firstElement;
                        Map<String, Runnable> actions = act.actions();
                        if (actions != null) {
                            for (final Entry<String, Runnable> e : actions.entrySet()) {
                                String label = e.getKey();
                                boolean enabled = true;
                                boolean checked = false;
                                String description = null;
                                Matcher m = LABEL_PATTERN.matcher(label);
                                if (m.matches()) {
                                    if (m.group(1) != null)
                                        enabled = false;
                                    if (m.group(2) != null)
                                        checked = true;
                                    label = m.group(3);
                                    description = m.group(4);
                                }
                                Action a = new Action(label.replace("&", "&&")) {

                                    @Override
                                    public void run() {
                                        Job backgroundJob = new Job("Repository Action '" + getText() + "'") {

                                            @Override
                                            protected IStatus run(IProgressMonitor monitor) {
                                                try {
                                                    e.getValue().run();
                                                    if (rp != null && rp instanceof Refreshable)
                                                        Central.refreshPlugin((Refreshable) rp);
                                                } catch (final Exception e) {
                                                    IStatus status = new Status(IStatus.ERROR, Plugin.PLUGIN_ID, "Error executing: " + getName(), e);
                                                    Plugin.getDefault().getLog().log(status);
                                                }
                                                monitor.done();
                                                return Status.OK_STATUS;
                                            }
                                        };
                                        backgroundJob.addJobChangeListener(new JobChangeAdapter() {

                                            @Override
                                            public void done(IJobChangeEvent event) {
                                                if (event.getResult().isOK()) {
                                                    viewer.getTree().getDisplay().asyncExec(new Runnable() {

                                                        @Override
                                                        public void run() {
                                                            viewer.refresh();
                                                        }
                                                    });
                                                }
                                            }
                                        });
                                        backgroundJob.setUser(true);
                                        backgroundJob.setPriority(Job.SHORT);
                                        backgroundJob.schedule();
                                    }
                                };
                                a.setEnabled(enabled);
                                if (description != null)
                                    a.setDescription(description);
                                a.setChecked(checked);
                                manager.add(a);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : Action(org.eclipse.jface.action.Action) IStatus(org.eclipse.core.runtime.IStatus) Matcher(java.util.regex.Matcher) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) Actionable(aQute.bnd.service.Actionable) RemoteRepositoryPlugin(aQute.bnd.service.RemoteRepositoryPlugin) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) RepositoryEntry(bndtools.model.repo.RepositoryEntry) Entry(java.util.Map.Entry) GroupMarker(org.eclipse.jface.action.GroupMarker) Menu(org.eclipse.swt.widgets.Menu) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Job(org.eclipse.core.runtime.jobs.Job) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) Refreshable(aQute.bnd.service.Refreshable) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) IMenuListener(org.eclipse.jface.action.IMenuListener) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) Map(java.util.Map)

Example 18 with RepositoryPlugin

use of aQute.bnd.service.RepositoryPlugin in project bndtools by bndtools.

the class RepositoriesView method performDrop.

/**
     * Try a drop on the target. A drop is allowed if the target implements a {@code dropTarget} method that returns a
     * boolean.
     *
     * @param target
     *            the target being dropped upon
     * @param data
     *            the data
     * @param data2
     * @return true if dropped and processed, false if not
     */
boolean performDrop(Object target, TransferData data, Object dropped) {
    try {
        Object java = toJava(data);
        if (java == null) {
            java = toJava(dropped);
            if (java == null)
                return false;
        }
        try {
            Method m = target.getClass().getMethod(DROP_TARGET, java.getClass());
            Boolean invoke = (Boolean) m.invoke(target, java);
            if (!invoke)
                return false;
        } catch (NoSuchMethodException e) {
            return false;
        }
        RepositoryPlugin repositoryPlugin = getRepositoryPlugin(target);
        if (repositoryPlugin != null && repositoryPlugin instanceof Refreshable)
            Central.refreshPlugin((Refreshable) repositoryPlugin);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : RemoteRepositoryPlugin(aQute.bnd.service.RemoteRepositoryPlugin) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) Method(java.lang.reflect.Method) Refreshable(aQute.bnd.service.Refreshable) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException)

Example 19 with RepositoryPlugin

use of aQute.bnd.service.RepositoryPlugin in project bndtools by bndtools.

the class RepositoriesView method createPartControl.

@Override
public void createPartControl(final Composite parent) {
    // CREATE CONTROLS
    final StackLayout stackLayout = new StackLayout();
    parent.setLayout(stackLayout);
    FormToolkit toolkit = new FormToolkit(parent.getDisplay());
    Composite defaultParent = toolkit.createComposite(parent, SWT.NONE);
    FillLayout fill = new FillLayout();
    fill.marginHeight = 5;
    fill.marginWidth = 5;
    defaultParent.setLayout(fill);
    if (!Central.hasWorkspaceDirectory()) {
        FormText form = toolkit.createFormText(defaultParent, true);
        form.setText("<form><p>No workspace configuration found. <a>Create a new Bnd workspace...</a></p></form>", true, false);
        form.addHyperlinkListener(new HyperlinkAdapter() {

            @Override
            public void linkActivated(HyperlinkEvent e) {
                IWorkbench workbench = PlatformUI.getWorkbench();
                IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
                WorkspaceSetupWizard wizard = new WorkspaceSetupWizard();
                wizard.init(workbench, StructuredSelection.EMPTY);
                WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
                dialog.open();
            }
        });
    } else {
        toolkit.createLabel(defaultParent, "Repositories are loading, please wait...");
    }
    stackLayout.topControl = defaultParent;
    parent.layout();
    final Composite mainPanel = new Composite(parent, SWT.NONE);
    filterPanel = filterPart.createControl(mainPanel, 5, 5);
    Tree tree = new Tree(mainPanel, SWT.FULL_SELECTION | SWT.MULTI);
    filterPanel.setBackground(tree.getBackground());
    viewer = new TreeViewer(tree);
    contentProvider = new SearchableRepositoryTreeContentProvider() {

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            super.inputChanged(viewer, oldInput, newInput);
            if (newInput != null) {
                stackLayout.topControl = mainPanel;
                advancedSearchAction.setEnabled(true);
                refreshAction.setEnabled(true);
                collapseAllAction.setEnabled(true);
                configureOfflineAction();
                parent.layout();
            }
        }
    };
    viewer.setContentProvider(contentProvider);
    ColumnViewerToolTipSupport.enableFor(viewer);
    viewer.setLabelProvider(new RepositoryTreeLabelProvider(false));
    getViewSite().setSelectionProvider(viewer);
    Central.addRepositoriesViewer(viewer, RepositoriesView.this);
    JpmPreferences jpmPrefs = new JpmPreferences();
    final boolean showJpmOnClick = jpmPrefs.getBrowserSelection() != JpmPreferences.PREF_BROWSER_EXTERNAL;
    // LISTENERS
    filterPart.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent event) {
            String filter = (String) event.getNewValue();
            updatedFilter(filter);
        }
    });
    ViewerDropAdapter dropAdapter = new ViewerDropAdapter(viewer) {

        @Override
        public boolean validateDrop(Object target, int operation, TransferData transferType) {
            if (target == null)
                return false;
            if (canDrop(target, transferType))
                return true;
            boolean valid = false;
            if (target instanceof RepositoryPlugin) {
                if (((RepositoryPlugin) target).canWrite()) {
                    if (URLTransfer.getInstance().isSupportedType(transferType))
                        return true;
                    if (LocalSelectionTransfer.getTransfer().isSupportedType(transferType)) {
                        ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
                        if (selection instanceof IStructuredSelection) {
                            for (Iterator<?> iter = ((IStructuredSelection) selection).iterator(); iter.hasNext(); ) {
                                Object element = iter.next();
                                if (element instanceof RepositoryBundle || element instanceof RepositoryBundleVersion) {
                                    valid = true;
                                    break;
                                }
                                if (element instanceof IFile) {
                                    valid = true;
                                    break;
                                }
                                if (element instanceof IAdaptable) {
                                    IFile file = (IFile) ((IAdaptable) element).getAdapter(IFile.class);
                                    if (file != null) {
                                        valid = true;
                                        break;
                                    }
                                }
                            }
                        }
                    } else {
                        valid = true;
                    }
                }
            }
            return valid;
        }

        @Override
        public void dragEnter(DropTargetEvent event) {
            super.dragEnter(event);
            event.detail = DND.DROP_COPY;
        }

        @Override
        public boolean performDrop(Object data) {
            if (RepositoriesView.this.performDrop(getCurrentTarget(), getCurrentEvent().currentDataType, data)) {
                viewer.refresh(getCurrentTarget(), true);
                return true;
            }
            boolean copied = false;
            if (URLTransfer.getInstance().isSupportedType(getCurrentEvent().currentDataType)) {
                try {
                    URL url = new URL((String) URLTransfer.getInstance().nativeToJava(getCurrentEvent().currentDataType));
                    if (!url.getPath().endsWith(".jar")) {
                        String uris = url.toString();
                        if (uris.contains("#!/p/sha/")) {
                            MessageDialog.openWarning(null, "Dropped URL is a JPM Revision Identifier, not a JAR", "The dropped URL is a JPM identifier, can only be dropped on a JPM repository. You can also select the revision on JPM and drag the 'jar' link of the revision to any of the other repositories.");
                            return false;
                        }
                    }
                    File tmp = File.createTempFile("dwnl", ".jar");
                    try (HttpClient client = new HttpClient()) {
                        IO.copy(client.connect(url), tmp);
                    }
                    if (isJarFile(tmp)) {
                        copied = addFilesToRepository((RepositoryPlugin) getCurrentTarget(), new File[] { tmp });
                    } else {
                        tmp.delete();
                        MessageDialog.openWarning(null, "Unrecognized Artifact", "The dropped URL is not recognized as a remote JAR file: " + url.toString());
                    }
                } catch (Exception e) {
                    return false;
                }
            } else if (data instanceof String[]) {
                String[] paths = (String[]) data;
                File[] files = new File[paths.length];
                for (int i = 0; i < paths.length; i++) {
                    files[i] = new File(paths[i]);
                }
                copied = addFilesToRepository((RepositoryPlugin) getCurrentTarget(), files);
            } else if (data instanceof IResource[]) {
                IResource[] resources = (IResource[]) data;
                File[] files = new File[resources.length];
                for (int i = 0; i < resources.length; i++) {
                    files[i] = resources[i].getLocation().toFile();
                }
                copied = addFilesToRepository((RepositoryPlugin) getCurrentTarget(), files);
            } else if (data instanceof IStructuredSelection) {
                File[] files = convertSelectionToFiles((IStructuredSelection) data);
                if (files != null && files.length > 0)
                    copied = addFilesToRepository((RepositoryPlugin) getCurrentTarget(), files);
            }
            return copied;
        }
    };
    dropAdapter.setFeedbackEnabled(false);
    dropAdapter.setExpandEnabled(false);
    viewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { URLTransfer.getInstance(), FileTransfer.getInstance(), ResourceTransfer.getInstance(), LocalSelectionTransfer.getTransfer() }, dropAdapter);
    viewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { LocalSelectionTransfer.getTransfer() }, new SelectionDragAdapter(viewer));
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            boolean writableRepoSelected = false;
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            Object element = selection.getFirstElement();
            if (element instanceof RepositoryPlugin) {
                RepositoryPlugin repo = (RepositoryPlugin) element;
                writableRepoSelected = repo.canWrite();
            }
            addBundlesAction.setEnabled(writableRepoSelected);
        }
    });
    tree.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseUp(MouseEvent ev) {
            Object element = ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            if (element instanceof ContinueSearchElement) {
                try {
                    getViewSite().getPage().showView(Plugin.JPM_BROWSER_VIEW_ID, null, showJpmOnClick ? IWorkbenchPage.VIEW_ACTIVATE : IWorkbenchPage.VIEW_CREATE);
                } catch (PartInitException e) {
                    Plugin.getDefault().getLog().log(e.getStatus());
                }
            }
        }
    });
    viewer.addDoubleClickListener(new IDoubleClickListener() {

        @Override
        public void doubleClick(DoubleClickEvent event) {
            if (!event.getSelection().isEmpty()) {
                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
                final Object element = selection.getFirstElement();
                if (element instanceof IAdaptable) {
                    final URI uri = (URI) ((IAdaptable) element).getAdapter(URI.class);
                    if (uri == null && element instanceof RepositoryEntry) {
                        boolean download = MessageDialog.openQuestion(getSite().getShell(), "Repositories", "This repository entry is unable to be opened because it has not been downloaded. Download and open it now?");
                        if (download) {
                            final RepositoryEntry entry = (RepositoryEntry) element;
                            Job downloadJob = new Job("Downloading repository entry " + entry.getBsn()) {

                                @Override
                                protected IStatus run(IProgressMonitor monitor) {
                                    final File repoFile = entry.getFile(true);
                                    if (repoFile != null && repoFile.exists()) {
                                        getSite().getShell().getDisplay().asyncExec(new Runnable() {

                                            @Override
                                            public void run() {
                                                openURI(repoFile.toURI());
                                            }
                                        });
                                    }
                                    return Status.OK_STATUS;
                                }
                            };
                            downloadJob.setUser(true);
                            downloadJob.schedule();
                        }
                    } else if (uri != null) {
                        openURI(uri);
                    }
                } else if (element instanceof ContinueSearchElement) {
                    ContinueSearchElement searchElement = (ContinueSearchElement) element;
                    try {
                        JpmPreferences jpmPrefs = new JpmPreferences();
                        if (jpmPrefs.getBrowserSelection() == JpmPreferences.PREF_BROWSER_EXTERNAL) {
                            URI browseUrl = searchElement.browse();
                            getViewSite().getWorkbenchWindow().getWorkbench().getBrowserSupport().getExternalBrowser().openURL(browseUrl.toURL());
                        } else
                            getViewSite().getPage().showView(Plugin.JPM_BROWSER_VIEW_ID, null, IWorkbenchPage.VIEW_VISIBLE);
                    } catch (PartInitException e) {
                        Plugin.getDefault().getLog().log(e.getStatus());
                    } catch (Exception e) {
                        Plugin.getDefault().getLog().log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Failed to load repository browser view", e));
                    }
                } else if (element instanceof RepositoryPlugin) {
                    viewer.setExpandedState(element, !viewer.getExpandedState(element));
                }
            }
        }
    });
    createContextMenu();
    // LAYOUT
    GridLayout layout = new GridLayout(1, false);
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    mainPanel.setLayout(layout);
    tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    filterPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    // Toolbar
    createActions();
    fillToolBar(getViewSite().getActionBars().getToolBarManager());
    prefs.addPropertyChangeListener(workspaceOfflineListener);
    // synthenic call to "refresh" so that we can get the repositories to show up in the UI
    new WorkspaceJob("Load repositories") {

        @Override
        public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
            try {
                Central.refreshPlugins();
            } catch (Exception e) {
            // ignore errors there may be no workspace yet
            }
            return Status.OK_STATUS;
        }
    }.schedule();
    IActionBars actionBars = getViewSite().getActionBars();
    actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction);
}
Also used : RepositoryBundle(bndtools.model.repo.RepositoryBundle) IAdaptable(org.eclipse.core.runtime.IAdaptable) RepositoryBundleVersion(bndtools.model.repo.RepositoryBundleVersion) TreeViewer(org.eclipse.jface.viewers.TreeViewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Viewer(org.eclipse.jface.viewers.Viewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) RepositoryEntry(bndtools.model.repo.RepositoryEntry) ContinueSearchElement(bndtools.model.repo.ContinueSearchElement) Tree(org.eclipse.swt.widgets.Tree) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Job(org.eclipse.core.runtime.jobs.Job) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) PropertyChangeEvent(java.beans.PropertyChangeEvent) JpmPreferences(bndtools.preferences.JpmPreferences) FormText(org.eclipse.ui.forms.widgets.FormText) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) FillLayout(org.eclipse.swt.layout.FillLayout) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IFile(org.eclipse.core.resources.IFile) File(java.io.File) JarFile(java.util.jar.JarFile) SelectionDragAdapter(bndtools.utils.SelectionDragAdapter) IStatus(org.eclipse.core.runtime.IStatus) HyperlinkEvent(org.eclipse.ui.forms.events.HyperlinkEvent) IFile(org.eclipse.core.resources.IFile) FormToolkit(org.eclipse.ui.forms.widgets.FormToolkit) PropertyChangeListener(java.beans.PropertyChangeListener) RemoteRepositoryPlugin(aQute.bnd.service.RemoteRepositoryPlugin) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) URI(java.net.URI) URL(java.net.URL) SearchableRepositoryTreeContentProvider(bndtools.model.repo.SearchableRepositoryTreeContentProvider) GridLayout(org.eclipse.swt.layout.GridLayout) TransferData(org.eclipse.swt.dnd.TransferData) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) StackLayout(org.eclipse.swt.custom.StackLayout) ISelection(org.eclipse.jface.viewers.ISelection) PartInitException(org.eclipse.ui.PartInitException) IActionBars(org.eclipse.ui.IActionBars) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) MouseEvent(org.eclipse.swt.events.MouseEvent) Composite(org.eclipse.swt.widgets.Composite) MouseAdapter(org.eclipse.swt.events.MouseAdapter) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) ViewerDropAdapter(org.eclipse.jface.viewers.ViewerDropAdapter) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) IWorkbench(org.eclipse.ui.IWorkbench) WorkspaceSetupWizard(bndtools.wizards.workspace.WorkspaceSetupWizard) RepositoryTreeLabelProvider(bndtools.model.repo.RepositoryTreeLabelProvider) HttpClient(aQute.bnd.http.HttpClient) GridData(org.eclipse.swt.layout.GridData) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IResource(org.eclipse.core.resources.IResource) HyperlinkAdapter(org.eclipse.ui.forms.events.HyperlinkAdapter)

Example 20 with RepositoryPlugin

use of aQute.bnd.service.RepositoryPlugin in project bnd by bndtools.

the class RepoCommand method _copy.

public void _copy(CopyOptions options) throws Exception {
    List<String> args = options._arguments();
    String srcName = args.remove(0);
    String dstName = args.remove(0);
    RepositoryPlugin source = workspace.getRepository(srcName);
    RepositoryPlugin dest = workspace.getRepository(dstName);
    if (source == null) {
        bnd.error("No such source repository: %s, available repos %s", srcName, workspace.getRepositories());
    }
    if (dest == null) {
        bnd.error("No such destination repository: %s, available repos %s", dstName, workspace.getRepositories());
    } else if (!dest.canWrite())
        bnd.error("Destination repository cannot write: %s", dest);
    if (!bnd.isOk() || source == null || dest == null) {
        return;
    }
    logger.debug("src = {} -> {}", srcName, source);
    logger.debug("dst = {} -> {}", dstName, dest);
    @SuppressWarnings("unused")
    class Spec {

        DownloadBlocker src;

        DownloadBlocker dst;

        String bsn;

        Version version;

        public byte[] digest;
    }
    List<Spec> sources = new ArrayList<Spec>();
    for (String bsn : source.list(null)) {
        for (Version version : source.versions(bsn)) {
            logger.debug("src: {} {}", bsn, version);
            Spec spec = new Spec();
            spec.bsn = bsn;
            spec.version = version;
            spec.src = new DownloadBlocker(bnd);
            File src = source.get(bsn, version, null, spec.src);
            if (src == null) {
                bnd.error("No such entry: %s-%s", bsn, version);
            } else {
                spec.dst = findMatchingVersion(dest, bsn, version);
                sources.add(spec);
            }
        }
    }
    for (Spec spec : sources) {
        String reason = spec.src.getReason();
        if (reason != null) {
            bnd.error("Failed to find %s because: %s", spec.src.getFile(), reason);
        }
        File src = spec.src.getFile();
        if (!src.isFile()) {
            bnd.error("Not a valid file %s", spec.src.getFile());
        }
        spec.digest = SHA1.digest(src).digest();
    }
    //
    // See if we can prune the list by diffing
    //
    ResourceRepository resources = null;
    if (dest instanceof ResourceRepository)
        resources = (ResourceRepository) dest;
    nextFile: for (Iterator<Spec> i = sources.iterator(); i.hasNext(); ) {
        Spec spec = i.next();
        if (resources != null) {
            ResourceDescriptor rd = resources.getResourceDescriptor(spec.digest);
            if (rd != null)
                // Already exists
                continue nextFile;
        }
    // TODO Diff
    }
    if (!bnd.isOk())
        return;
    for (Spec spec : sources) {
        File src = spec.src.getFile();
        if (!options.dry()) {
            try (InputStream fin = IO.stream(src)) {
                PutResult put = dest.put(fin, null);
                if (put.digest != null) {
                    if (!Arrays.equals(spec.digest, put.digest)) {
                        bnd.error("Digest error in upload %s", src);
                    }
                }
            } catch (Exception e) {
                bnd.exception(e, "Exception %s in upload %s", e, src);
            }
        }
    }
    for (String bsn : source.list(null)) {
        for (Version version : source.versions(bsn)) {
            System.out.println(bsn + ";version=" + version);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) DownloadBlocker(aQute.bnd.build.DownloadBlocker) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Version(aQute.bnd.version.Version) Iterator(java.util.Iterator) ResourceRepository(aQute.bnd.service.repository.ResourceRepository) File(java.io.File) PutResult(aQute.bnd.service.RepositoryPlugin.PutResult) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)

Aggregations

RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)62 Version (aQute.bnd.version.Version)25 File (java.io.File)24 Workspace (aQute.bnd.build.Workspace)12 IOException (java.io.IOException)10 Project (aQute.bnd.build.Project)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 Jar (aQute.bnd.osgi.Jar)7 CoreException (org.eclipse.core.runtime.CoreException)7 RemoteRepositoryPlugin (aQute.bnd.service.RemoteRepositoryPlugin)5 VersionRange (aQute.bnd.version.VersionRange)5 Description (aQute.lib.getopt.Description)5 Parameters (aQute.bnd.header.Parameters)4 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 IStatus (org.eclipse.core.runtime.IStatus)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 PartInitException (org.eclipse.ui.PartInitException)4 Attrs (aQute.bnd.header.Attrs)3