Search in sources :

Example 1 with Library

use of io.sloeber.ui.preferences.LibrarySelectionPage.LibraryTree.Library in project arduino-eclipse-plugin by Sloeber.

the class LibrarySelectionPage method createTree.

public void createTree(Composite parent) {
    // filtering applied to all columns
    PatternFilter filter = new PatternFilter() {

        @Override
        protected boolean isLeafMatch(final Viewer viewer1, final Object element) {
            int numberOfColumns = ((TreeViewer) viewer1).getTree().getColumnCount();
            boolean isMatch = false;
            for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
                String labelText = LibraryLabelProvider.getColumnText(element, columnIndex);
                isMatch |= wordMatches(labelText);
            }
            return isMatch;
        }
    };
    this.tree = new FilteredTree(parent, SWT.CHECK | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION, filter, true, true) {

        @Override
        protected TreeViewer doCreateTreeViewer(Composite composite, int style) {
            CheckboxTreeViewer viewer1 = new CheckboxTreeViewer(composite);
            viewer1.setCheckStateProvider(new LibraryCheckProvider());
            viewer1.setLabelProvider(new LibraryLabelProvider());
            viewer1.setContentProvider(new LibraryContentProvider());
            return viewer1;
        }
    };
    this.viewer = this.tree.getViewer();
    this.viewer.setInput(this.libs);
    TreeColumn name = new TreeColumn(this.viewer.getTree(), SWT.LEFT);
    name.setWidth(400);
    TreeColumn version = new TreeColumn(this.viewer.getTree(), SWT.LEFT);
    version.setWidth(100);
    // create the editor and set its attributes
    this.editor = new TreeEditor(this.viewer.getTree());
    this.editor.horizontalAlignment = SWT.LEFT;
    this.editor.grabHorizontal = true;
    this.editor.setColumn(1);
    // this ensures the tree labels are displayed correctly
    this.viewer.refresh(true);
    // enable tooltips
    ColumnViewerToolTipSupport.enableFor(this.viewer);
    // tree interactions listener
    this.viewer.getTree().addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            if (LibrarySelectionPage.this.editor.getEditor() != null) {
                LibrarySelectionPage.this.editor.getEditor().dispose();
            }
            final TreeItem item = event.item instanceof TreeItem ? (TreeItem) event.item : null;
            if (item != null && event.detail == SWT.CHECK) {
                if (item.getData() instanceof LibraryTree.Category) {
                    Category category = ((LibraryTree.Category) item.getData());
                    item.setGrayed(false);
                    for (LibraryTree.Library child : category.getLibraries()) {
                        if (item.getChecked()) {
                            child.setVersion(child.getLatest());
                        } else {
                            child.setVersion((ArduinoLibraryVersion) null);
                        }
                    }
                    for (TreeItem child : item.getItems()) {
                        child.setChecked(item.getChecked());
                        child.setText(1, ((LibraryTree.Library) child.getData()).getVersionString());
                    }
                } else {
                    if (item.getData() instanceof LibraryTree.Library) {
                        Library lib = ((LibraryTree.Library) item.getData());
                        if (item.getChecked()) {
                            lib.setVersion(lib.getLatest());
                        } else {
                            lib.setVersion((ArduinoLibraryVersion) null);
                        }
                        item.setText(0, lib.getName());
                        item.setText(1, lib.getVersionString());
                        verifySubtreeCheckStatus(item.getParentItem());
                    }
                }
            }
            if (item != null && item.getItemCount() == 0 && item.getChecked()) {
                // Create the dropdown and add data to it
                final CCombo combo = new CCombo(LibrarySelectionPage.this.viewer.getTree(), SWT.READ_ONLY);
                Library selectedLib = ((LibraryTree.Library) item.getData());
                for (ArduinoLibraryVersion version1 : selectedLib.getVersions()) {
                    combo.add(version1.getVersion().toString());
                }
                ArduinoLibraryVersion displayVersion = selectedLib.getVersion();
                if (displayVersion == null) {
                    displayVersion = selectedLib.getLatest();
                    selectedLib.setVersion(displayVersion);
                    item.setText(0, selectedLib.getName());
                }
                combo.select(combo.indexOf(displayVersion.getVersion().toString()));
                // Compute the width for the editor
                // Also, compute the column width, so that the dropdown fits
                // Set the focus on the dropdown and set into the editor
                combo.setFocus();
                LibrarySelectionPage.this.editor.setEditor(combo, item, 1);
                // Add a listener to set the selected item back into the
                // cell
                combo.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent event1) {
                        LibraryTree.Library lib = (LibraryTree.Library) item.getData();
                        lib.setVersion(new VersionNumber(combo.getText()));
                        item.setText(1, lib.getVersionString());
                        item.setText(0, lib.getName());
                        // Item selected: end the editing session
                        combo.dispose();
                    }
                });
            }
        }
    });
}
Also used : PatternFilter(org.eclipse.ui.dialogs.PatternFilter) Category(io.sloeber.ui.preferences.LibrarySelectionPage.LibraryTree.Category) TreeItem(org.eclipse.swt.widgets.TreeItem) CheckboxTreeViewer(org.eclipse.jface.viewers.CheckboxTreeViewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Viewer(org.eclipse.jface.viewers.Viewer) CheckboxTreeViewer(org.eclipse.jface.viewers.CheckboxTreeViewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) FilteredTree(org.eclipse.ui.dialogs.FilteredTree) TreeColumn(org.eclipse.swt.widgets.TreeColumn) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Category(io.sloeber.ui.preferences.LibrarySelectionPage.LibraryTree.Category) Composite(org.eclipse.swt.widgets.Composite) TreeEditor(org.eclipse.swt.custom.TreeEditor) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Library(io.sloeber.ui.preferences.LibrarySelectionPage.LibraryTree.Library) Point(org.eclipse.swt.graphics.Point) VersionNumber(io.sloeber.core.api.VersionNumber) CheckboxTreeViewer(org.eclipse.jface.viewers.CheckboxTreeViewer) ArduinoLibraryVersion(io.sloeber.core.api.Json.ArduinoLibraryVersion) CCombo(org.eclipse.swt.custom.CCombo) ArduinoLibrary(io.sloeber.core.api.Json.ArduinoLibrary) Library(io.sloeber.ui.preferences.LibrarySelectionPage.LibraryTree.Library)

Example 2 with Library

use of io.sloeber.ui.preferences.LibrarySelectionPage.LibraryTree.Library in project arduino-eclipse-plugin by Sloeber.

the class LibrarySelectionPage method performOk.

@Override
public boolean performOk() {
    if (this.isJobRunning == true) {
        // $NON-NLS-1$
        MessageDialog.openInformation(// $NON-NLS-1$
        getShell(), // $NON-NLS-1$
        "Library Manager", // $NON-NLS-1$
        "Library Manager is busy. Please wait some time...");
        return false;
    }
    this.isJobRunning = true;
    Job job = new Job(Messages.ui_Adopting_arduino_libraries) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            MultiStatus status = new MultiStatus(PLUGIN_ID, 0, Messages.ui_installing_arduino_libraries, null);
            Set<ArduinoLibraryVersion> toRemoveLibs = new HashSet<>();
            Set<ArduinoLibraryVersion> toInstallLibs = new HashSet<>();
            for (Category category : libs.categories.values()) {
                for (Library library : category.libraries.values()) {
                    ArduinoLibraryVersion installedVersion = library.getInstalledVersion();
                    ArduinoLibraryVersion toInstalVersion = library.getVersion();
                    if ((installedVersion != null) && (installedVersion.compareTo(toInstalVersion) != 0)) {
                        toRemoveLibs.add(installedVersion);
                    }
                    if ((toInstalVersion != null) && (toInstalVersion.compareTo(installedVersion) != 0)) {
                        toInstallLibs.add(toInstalVersion);
                    }
                }
            }
            return LibraryManager.updateLibraries(toRemoveLibs, toInstallLibs, monitor, status);
        }
    };
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            LibrarySelectionPage.this.isJobRunning = false;
        }
    });
    job.setUser(true);
    job.schedule();
    return true;
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ArduinoLibraryVersion(io.sloeber.core.api.Json.ArduinoLibraryVersion) Category(io.sloeber.ui.preferences.LibrarySelectionPage.LibraryTree.Category) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) MultiStatus(org.eclipse.core.runtime.MultiStatus) ArduinoLibrary(io.sloeber.core.api.Json.ArduinoLibrary) Library(io.sloeber.ui.preferences.LibrarySelectionPage.LibraryTree.Library) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) Job(org.eclipse.core.runtime.jobs.Job) HashSet(java.util.HashSet)

Aggregations

ArduinoLibrary (io.sloeber.core.api.Json.ArduinoLibrary)2 ArduinoLibraryVersion (io.sloeber.core.api.Json.ArduinoLibraryVersion)2 Category (io.sloeber.ui.preferences.LibrarySelectionPage.LibraryTree.Category)2 Library (io.sloeber.ui.preferences.LibrarySelectionPage.LibraryTree.Library)2 VersionNumber (io.sloeber.core.api.VersionNumber)1 HashSet (java.util.HashSet)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 MultiStatus (org.eclipse.core.runtime.MultiStatus)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1 Job (org.eclipse.core.runtime.jobs.Job)1 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)1 CheckboxTreeViewer (org.eclipse.jface.viewers.CheckboxTreeViewer)1 TreeViewer (org.eclipse.jface.viewers.TreeViewer)1 Viewer (org.eclipse.jface.viewers.Viewer)1 CCombo (org.eclipse.swt.custom.CCombo)1 TreeEditor (org.eclipse.swt.custom.TreeEditor)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 Point (org.eclipse.swt.graphics.Point)1 Composite (org.eclipse.swt.widgets.Composite)1