Search in sources :

Example 31 with PatternFilter

use of org.eclipse.ui.dialogs.PatternFilter in project arduino-eclipse-plugin by Sloeber.

the class PlatformSelectionPage method createContents.

@Override
protected Control createContents(Composite parent) {
    Composite control = new Composite(parent, SWT.NONE);
    control.setLayout(new GridLayout());
    Button btnCheckButton = new Button(control, SWT.CHECK);
    btnCheckButton.setText(Messages.PlatformSelectionPage_hide_third_party_url);
    btnCheckButton.setSelection(this.myHideJson);
    btnCheckButton.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            PlatformSelectionPage.this.myHideJson = btnCheckButton.getSelection();
            MyPreferences.setHideJson(PlatformSelectionPage.this.myHideJson);
            PlatformSelectionPage.this.viewer.setInput(EMPTY_STRING);
        }
    });
    Text desc = new Text(control, SWT.READ_ONLY);
    desc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    desc.setBackground(parent.getBackground());
    desc.setText(Messages.platformSelectionTip);
    PatternFilter filter = new PatternFilter() {

        @Override
        protected boolean isLeafMatch(final Viewer viewer1, final Object element) {
            boolean isMatch = false;
            if (element instanceof InstallableVersion) {
                InstallableVersion ver = (InstallableVersion) element;
                isMatch |= wordMatches(ver.getVersion().toString());
                isMatch |= myWordMatches(ver.getPlatform());
            }
            if (element instanceof ArduinoPlatformPackageIndex) {
                ArduinoPlatformPackageIndex indexFile = (ArduinoPlatformPackageIndex) element;
                isMatch |= myWordMatches(indexFile);
            }
            if (element instanceof ArduinoPackage) {
                ArduinoPackage pac = (ArduinoPackage) element;
                isMatch |= myWordMatches(pac);
            }
            if (element instanceof ArduinoPlatformVersion) {
                ArduinoPlatformVersion platform = (ArduinoPlatformVersion) element;
                isMatch |= myWordMatches(platform);
            }
            return isMatch;
        }

        private boolean myWordMatches(ArduinoPlatformVersion arduinoPlatformVersion) {
            boolean ret = wordMatches(arduinoPlatformVersion.getName());
            ret |= wordMatches(arduinoPlatformVersion.getArchitecture());
            ret |= wordMatches(arduinoPlatformVersion.getConcattenatedBoardNames());
            ret |= myWordMatches(arduinoPlatformVersion.getParent().getParent());
            return ret;
        }

        private boolean myWordMatches(ArduinoPackage pac) {
            boolean ret = wordMatches(pac.getEmail());
            ret |= wordMatches(pac.getMaintainer());
            ret |= wordMatches(pac.getName());
            ret |= wordMatches(pac.getWebsiteURL().toString());
            ret |= wordMatches(pac.getPackageIndex().getJsonFile().toString());
            return ret;
        }

        private boolean myWordMatches(ArduinoPlatformPackageIndex indexFile) {
            return wordMatches(indexFile.getID());
        }
    };
    this.myGuiplatformTree = new FilteredTree(control, 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 ICheckStateProvider() {

                @Override
                public boolean isChecked(Object element) {
                    if (element instanceof InstallableVersion) {
                        return ((InstallableVersion) element).mustBeInstalled();
                    }
                    if (element instanceof ArduinoPlatformPackageIndex) {
                        return mustBeInstalled((ArduinoPlatformPackageIndex) element);
                    }
                    if (element instanceof ArduinoPackage) {
                        return mustBeInstalled((ArduinoPackage) element);
                    }
                    if (element instanceof ArduinoPlatform) {
                        return mustBeInstalled((ArduinoPlatform) element);
                    }
                    return false;
                }

                @Override
                public boolean isGrayed(Object element) {
                    if (element instanceof InstallableVersion) {
                        return false;
                    }
                    if (element instanceof ArduinoPlatformPackageIndex) {
                        return mustBeInstalled((ArduinoPlatformPackageIndex) element);
                    }
                    if (element instanceof ArduinoPackage) {
                        return mustBeInstalled((ArduinoPackage) element);
                    }
                    if (element instanceof ArduinoPlatform) {
                        return mustBeInstalled((ArduinoPlatform) element);
                    }
                    return false;
                }
            });
            viewer1.addCheckStateListener(new ICheckStateListener() {

                @Override
                public void checkStateChanged(CheckStateChangedEvent event) {
                    Object element = event.getElement();
                    if (element instanceof InstallableVersion) {
                        InstallableVersion cur = (InstallableVersion) element;
                        cur.setMustBeInstalled(event.getChecked());
                    }
                    PlatformSelectionPage.this.viewer.refresh();
                }
            });
            viewer1.setContentProvider(new ITreeContentProvider() {

                @Override
                public Object[] getElements(Object inputElement) {
                    if (PlatformSelectionPage.this.myHideJson) {
                        List<ArduinoPackage> packages = BoardsManager.getPackages();
                        Collections.sort(packages);
                        return packages.toArray(new Object[packages.size()]);
                    }
                    List<ArduinoPlatformPackageIndex> indexFiles = BoardsManager.getPackageIndices();
                    return indexFiles.toArray(new Object[indexFiles.size()]);
                }

                @Override
                public void dispose() {
                // nothing to do
                }

                @Override
                public void inputChanged(Viewer viewer11, Object oldInput, Object newInput) {
                // nothing to do
                }

                @Override
                public Object[] getChildren(Object parentElement) {
                    if (parentElement instanceof ArduinoPlatformPackageIndex) {
                        Collection<ArduinoPackage> packages = ((ArduinoPlatformPackageIndex) parentElement).getPackages();
                        return packages.toArray(new Object[packages.size()]);
                    }
                    if (parentElement instanceof ArduinoPackage) {
                        Collection<ArduinoPlatform> platforms = ((ArduinoPackage) parentElement).getPlatforms();
                        ArduinoPlatform[] platformArray = platforms.toArray(new ArduinoPlatform[platforms.size()]);
                        Arrays.sort(platformArray);
                        return platformArray;
                    }
                    if (parentElement instanceof ArduinoPlatform) {
                        ArduinoPlatform platform = (ArduinoPlatform) parentElement;
                        ArduinoPackage parentPackage = platform.getParent();
                        ArduinoPlatformPackageIndex parentIndex = parentPackage.getPackageIndex();
                        return myShownPlatforms.get(parentIndex.getID()).get(parentPackage.getID()).get(platform.getID());
                    }
                    return null;
                }

                @Override
                public Object getParent(Object element) {
                    return null;
                }

                @Override
                public boolean hasChildren(Object element) {
                    return !(element instanceof InstallableVersion);
                }
            });
            viewer1.setLabelProvider(new CellLabelProvider() {

                @Override
                public String getToolTipText(Object element) {
                    if (element instanceof ArduinoPlatformPackageIndex) {
                        return ((ArduinoPlatformPackageIndex) element).getID();
                    }
                    if (element instanceof ArduinoPackage) {
                        // $NON-NLS-1$
                        String NULL = "NULL";
                        ArduinoPackage pkg = (ArduinoPackage) element;
                        String maintainer = pkg.getMaintainer();
                        String email = pkg.getEmail();
                        String weburlString = pkg.getWebsiteURL();
                        if (maintainer == null)
                            maintainer = NULL;
                        if (email == null)
                            email = NULL;
                        if (weburlString == null)
                            weburlString = NULL;
                        return Messages.packageTooltip.replace(Messages.MAINTAINER, maintainer).replace(Messages.EMAIL, email).replace(Messages.URL, weburlString);
                    }
                    if (element instanceof ArduinoPlatformVersion) {
                        return ((ArduinoPlatformVersion) element).getConcattenatedBoardNames();
                    }
                    if (element instanceof InstallableVersion) {
                        return null;
                    }
                    return null;
                }

                @Override
                public org.eclipse.swt.graphics.Point getToolTipShift(Object object) {
                    return new org.eclipse.swt.graphics.Point(5, 5);
                }

                @Override
                public int getToolTipDisplayDelayTime(Object object) {
                    return 1000;
                }

                @Override
                public int getToolTipTimeDisplayed(Object object) {
                    return 5000;
                }

                @Override
                public void update(ViewerCell cell) {
                    if (cell.getElement() instanceof ArduinoPlatformPackageIndex) {
                        cell.setText(((ArduinoPlatformPackageIndex) cell.getElement()).getName());
                    }
                    if (cell.getElement() instanceof ArduinoPackage) {
                        cell.setText(((ArduinoPackage) cell.getElement()).getName());
                    }
                    if (cell.getElement() instanceof ArduinoPlatform) {
                        cell.setText(((ArduinoPlatform) cell.getElement()).getName());
                    }
                    if (cell.getElement() instanceof InstallableVersion) {
                        cell.setText(((InstallableVersion) cell.getElement()).getVersion().toString());
                    }
                }
            });
            return viewer1;
        }
    };
    this.viewer = this.myGuiplatformTree.getViewer();
    ColumnViewerToolTipSupport.enableFor(this.viewer);
    this.myGuiplatformTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    this.viewer.setInput(EMPTY_STRING);
    return control;
}
Also used : ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) PatternFilter(org.eclipse.ui.dialogs.PatternFilter) ArduinoPlatformVersion(io.sloeber.core.api.Json.ArduinoPlatformVersion) ICheckStateListener(org.eclipse.jface.viewers.ICheckStateListener) Listener(org.eclipse.swt.widgets.Listener) ArduinoPackage(io.sloeber.core.api.Json.ArduinoPackage) CheckboxTreeViewer(org.eclipse.jface.viewers.CheckboxTreeViewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) ICheckStateListener(org.eclipse.jface.viewers.ICheckStateListener) Viewer(org.eclipse.jface.viewers.Viewer) CheckboxTreeViewer(org.eclipse.jface.viewers.CheckboxTreeViewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) FilteredTree(org.eclipse.ui.dialogs.FilteredTree) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) CellLabelProvider(org.eclipse.jface.viewers.CellLabelProvider) ArduinoPlatformPackageIndex(io.sloeber.core.api.Json.ArduinoPlatformPackageIndex) ArduinoPlatform(io.sloeber.core.api.Json.ArduinoPlatform) Composite(org.eclipse.swt.widgets.Composite) ICheckStateProvider(org.eclipse.jface.viewers.ICheckStateProvider) Text(org.eclipse.swt.widgets.Text) ViewerCell(org.eclipse.jface.viewers.ViewerCell) CheckboxTreeViewer(org.eclipse.jface.viewers.CheckboxTreeViewer) GridData(org.eclipse.swt.layout.GridData) Event(org.eclipse.swt.widgets.Event) CheckStateChangedEvent(org.eclipse.jface.viewers.CheckStateChangedEvent) CheckStateChangedEvent(org.eclipse.jface.viewers.CheckStateChangedEvent)

Aggregations

PatternFilter (org.eclipse.ui.dialogs.PatternFilter)31 FilteredTree (org.eclipse.ui.dialogs.FilteredTree)23 Composite (org.eclipse.swt.widgets.Composite)20 GridLayout (org.eclipse.swt.layout.GridLayout)12 TreeViewer (org.eclipse.jface.viewers.TreeViewer)11 Viewer (org.eclipse.jface.viewers.Viewer)11 DoubleClickEvent (org.eclipse.jface.viewers.DoubleClickEvent)9 IDoubleClickListener (org.eclipse.jface.viewers.IDoubleClickListener)9 CheckStateChangedEvent (org.eclipse.jface.viewers.CheckStateChangedEvent)8 ICheckStateListener (org.eclipse.jface.viewers.ICheckStateListener)8 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)7 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)7 SelectionEvent (org.eclipse.swt.events.SelectionEvent)7 GridData (org.eclipse.swt.layout.GridData)7 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)6 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)6 Label (org.eclipse.swt.widgets.Label)6 Tree (org.eclipse.swt.widgets.Tree)6 CachedCheckboxTreeViewer (org.eclipse.egit.ui.internal.components.CachedCheckboxTreeViewer)5 FilteredCheckboxTree (org.eclipse.egit.ui.internal.components.FilteredCheckboxTree)5