Search in sources :

Example 1 with RecentResources

use of eu.esdihumboldt.hale.ui.service.project.RecentResources in project hale by halestudio.

the class FileSourceFileFieldEditor method setContentTypes.

@Override
public void setContentTypes(Set<IContentType> types) {
    super.setContentTypes(types);
    RecentResources rr = PlatformUI.getWorkbench().getService(RecentResources.class);
    if (rr != null) {
        final List<Pair<URI, IContentType>> files = rr.getRecent(types, true);
        if (!files.isEmpty()) {
            historyButton.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    Menu filesMenu = new Menu(historyButton);
                    for (Pair<URI, IContentType> pair : files) {
                        final File file;
                        try {
                            file = new File(pair.getFirst());
                            if (file.exists()) {
                                // only offer existing files
                                MenuItem item = new MenuItem(filesMenu, SWT.PUSH);
                                item.setText(RecentProjectsMenu.shorten(file.toString(), 80, file.getName().length()));
                                item.addSelectionListener(new SelectionAdapter() {

                                    @Override
                                    public void widgetSelected(SelectionEvent e) {
                                        String text = processFile(file);
                                        if (text != null) {
                                            getTextControl().setText(text);
                                            getTextControl().setFocus();
                                            valueChanged();
                                        }
                                    }
                                });
                            }
                        } catch (Exception e1) {
                        // ignore
                        }
                    }
                    Point histLoc = historyButton.getParent().toDisplay(historyButton.getLocation());
                    filesMenu.setLocation(histLoc.x, histLoc.y + historyButton.getSize().y);
                    filesMenu.setVisible(true);
                }
            });
            historyButton.setEnabled(true);
        }
    }
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MenuItem(org.eclipse.swt.widgets.MenuItem) RecentProjectsMenu(eu.esdihumboldt.hale.ui.service.project.RecentProjectsMenu) Menu(org.eclipse.swt.widgets.Menu) Point(org.eclipse.swt.graphics.Point) File(java.io.File) RecentResources(eu.esdihumboldt.hale.ui.service.project.RecentResources) Pair(eu.esdihumboldt.util.Pair)

Example 2 with RecentResources

use of eu.esdihumboldt.hale.ui.service.project.RecentResources in project hale by halestudio.

the class URLSourceURIFieldEditor method setContentTypes.

/**
 * Set the allowed content types and enable the history button if
 * applicable.
 *
 * @param types the supported content types
 */
public void setContentTypes(Set<IContentType> types) {
    RecentResources rr = PlatformUI.getWorkbench().getService(RecentResources.class);
    if (rr != null) {
        Predicate<URI> selectUris = new Predicate<URI>() {

            @Override
            public boolean apply(URI uri) {
                // exclude files
                return !"file".equals(uri.getScheme());
            }
        };
        if (filter != null) {
            selectUris = Predicates.and(selectUris, filter);
        }
        final List<Pair<URI, IContentType>> locations = rr.getRecent(types, selectUris);
        if (!locations.isEmpty()) {
            historyButton.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    Menu filesMenu = new Menu(historyButton);
                    for (Pair<URI, IContentType> pair : locations) {
                        final URI location = pair.getFirst();
                        final IContentType contentType = pair.getSecond();
                        try {
                            MenuItem item = new MenuItem(filesMenu, SWT.PUSH);
                            item.setText(RecentProjectsMenu.shorten(location.toString(), 80, 20));
                            item.addSelectionListener(new SelectionAdapter() {

                                @Override
                                public void widgetSelected(SelectionEvent e) {
                                    getTextControl().setText(location.toString());
                                    getTextControl().setFocus();
                                    valueChanged();
                                    onHistorySelected(location, contentType);
                                }
                            });
                        } catch (Exception e1) {
                        // ignore
                        }
                    }
                    Point histLoc = historyButton.getParent().toDisplay(historyButton.getLocation());
                    filesMenu.setLocation(histLoc.x, histLoc.y + historyButton.getSize().y);
                    filesMenu.setVisible(true);
                }
            });
            historyButton.setEnabled(true);
        }
    }
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) IContentType(org.eclipse.core.runtime.content.IContentType) MenuItem(org.eclipse.swt.widgets.MenuItem) Point(org.eclipse.swt.graphics.Point) URI(java.net.URI) Predicate(com.google.common.base.Predicate) SelectionEvent(org.eclipse.swt.events.SelectionEvent) RecentProjectsMenu(eu.esdihumboldt.hale.ui.service.project.RecentProjectsMenu) Menu(org.eclipse.swt.widgets.Menu) RecentResources(eu.esdihumboldt.hale.ui.service.project.RecentResources) Pair(eu.esdihumboldt.util.Pair)

Aggregations

RecentProjectsMenu (eu.esdihumboldt.hale.ui.service.project.RecentProjectsMenu)2 RecentResources (eu.esdihumboldt.hale.ui.service.project.RecentResources)2 Pair (eu.esdihumboldt.util.Pair)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Point (org.eclipse.swt.graphics.Point)2 Menu (org.eclipse.swt.widgets.Menu)2 MenuItem (org.eclipse.swt.widgets.MenuItem)2 Predicate (com.google.common.base.Predicate)1 File (java.io.File)1 URI (java.net.URI)1 IContentType (org.eclipse.core.runtime.content.IContentType)1