Project

General

Profile

Download (2.53 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.perspective;
10

    
11
import java.io.IOException;
12
import java.net.URL;
13
import java.util.List;
14

    
15
import javax.annotation.PostConstruct;
16

    
17
import org.eclipse.core.runtime.FileLocator;
18
import org.eclipse.e4.ui.model.application.MApplication;
19
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
20
import org.eclipse.e4.ui.workbench.modeling.EModelService;
21
import org.eclipse.e4.ui.workbench.modeling.EPartService;
22
import org.eclipse.jface.resource.ImageDescriptor;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.events.SelectionEvent;
25
import org.eclipse.swt.events.SelectionListener;
26
import org.eclipse.swt.widgets.Composite;
27
import org.eclipse.swt.widgets.ToolBar;
28
import org.eclipse.swt.widgets.ToolItem;
29

    
30

    
31
/**
32
 * @author pplitzner
33
 * @since Nov 16, 2017
34
 *
35
 */
36
public class PerspectiveSwitch {
37

    
38
    @PostConstruct
39
    public void create(Composite parent, MApplication application, EModelService modelService,
40
            EPartService partService) throws IOException {
41
        ToolBar toolbar = new ToolBar(parent, SWT.WRAP);
42

    
43
        List<MPerspective> perspectives = modelService.findElements(application, null, MPerspective.class, null);
44

    
45
        for (MPerspective perspective : perspectives) {
46
            ToolItem item = new ToolItem(toolbar, SWT.NONE);
47
            item.setData(perspective);
48
            item.setToolTipText(perspective.getLabel());
49
            String iconURI = perspective.getIconURI();
50
            if(iconURI!=null){
51
                URL url = FileLocator.find(new URL(iconURI));
52
                URL fileUrl = FileLocator.toFileURL(url);
53
                if(fileUrl!=null){
54
                    item.setImage(ImageDescriptor.createFromURL(fileUrl).createImage());
55
                }
56
            }
57
            if(item.getImage()==null){
58
                item.setText(perspective.getLabel());
59
            }
60

    
61
            item.addSelectionListener(new SelectionListener() {
62
                @Override
63
                public void widgetSelected(SelectionEvent e) {
64
                    MPerspective perspective = (MPerspective) item.getData();
65
                    partService.switchPerspective(perspective);
66
                }
67

    
68
                @Override
69
                public void widgetDefaultSelected(SelectionEvent e) {
70
                }
71
            });
72
        }
73

    
74
    }
75

    
76
}
(4-4/8)