-
Notifications
You must be signed in to change notification settings - Fork 24
/
DeclarationsContentProvider.java
110 lines (96 loc) · 3.54 KB
/
DeclarationsContentProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* (c) 2011, Alejandro Serrano
* Released under the terms of the EPL.
*/
package net.sf.eclipsefp.haskell.browser.views.declarations;
import java.util.ArrayList;
import net.sf.eclipsefp.haskell.browser.BrowserPlugin;
import net.sf.eclipsefp.haskell.browser.DatabaseType;
import net.sf.eclipsefp.haskell.browser.items.Constructor;
import net.sf.eclipsefp.haskell.browser.items.Declaration;
import net.sf.eclipsefp.haskell.browser.items.DeclarationType;
import net.sf.eclipsefp.haskell.browser.items.Gadt;
import net.sf.eclipsefp.haskell.browser.items.Packaged;
import net.sf.eclipsefp.haskell.browser.items.QueryItem;
import net.sf.eclipsefp.haskell.browser.views.modules.ModulesItem;
import net.sf.eclipsefp.haskell.browser.views.packages.PackagesItem;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
/**
* Content provider for declarations.
* Implements the nesting of instances in their parent elements.
* @author Alejandro Serrano
*
*/
public class DeclarationsContentProvider implements ITreeContentProvider {
boolean isTypes;
ArrayList<QueryItem> cache = null;
public DeclarationsContentProvider( final boolean isTypes ) {
this.isTypes = isTypes;
}
public void inputChanged( final Viewer viewer, final Object oldInput,
final Object newInput ) {
if( newInput == null || !( newInput instanceof ModulesItem ) ) {
cache = new ArrayList<QueryItem>();
} else {
try {
ModulesItem mitem = ( ModulesItem )newInput;
// The module is a fake module from hierarchical view
if( mitem.getModule() == null ) {
cache = new ArrayList<QueryItem>();
return;
}
Object o = mitem.getDatabaseInfo();
if( o instanceof DatabaseType ) {
BrowserPlugin.getSharedInstance().setCurrentDatabase(
( DatabaseType )o, null );
} else {
PackagesItem item = ( PackagesItem )o;
BrowserPlugin.getSharedInstance().setCurrentDatabase(
DatabaseType.PACKAGE, item.getPackage().getIdentifier() );
}
cache = new ArrayList<QueryItem>();
Packaged<Declaration>[] decls = BrowserPlugin.getSharedInstance()
.getDeclarations( mitem.getModule().getName() );
for( QueryItem decl: QueryItem.convertToQueryItem( decls ) ) {
if( decl.getType() == DeclarationType.FUNCTION && !isTypes ) {
cache.add( decl );
} else if( decl.getType() != DeclarationType.FUNCTION && isTypes ) {
cache.add( decl );
}
}
} catch( Throwable ex ) {
cache = new ArrayList<QueryItem>();
}
}
}
public Object[] getElements( final Object inputElement ) {
return cache.toArray();
}
public Object[] getChildren( final Object parentElement ) {
if( parentElement instanceof QueryItem ) {
QueryItem item = ( QueryItem )parentElement;
ArrayList<Object> elements = new ArrayList<Object>();
// If a GADT, add constructors
if( item.getDeclaration() instanceof Gadt ) {
for( Constructor c: ( ( Gadt )item.getDeclaration() ).getConstructors() ) {
elements.add( c );
}
}
// Add other inner items
elements.addAll( item.getInnerItems() );
return elements.toArray();
} else {
return new Object[ 0 ];
}
}
public Object getParent( final Object element ) {
return null;
}
public boolean hasChildren( final Object element ) {
return this.getChildren( element ).length > 0;
}
public void dispose() {
// Do nothing
}
}