-
Notifications
You must be signed in to change notification settings - Fork 4
/
WrappedFile.java
203 lines (176 loc) · 5.62 KB
/
WrappedFile.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*******************************************************************************************************
*
* WrappedFile.java, in ummisco.gama.ui.navigator, is part of the source code of the GAMA modeling and simulation
* platform (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package ummisco.gama.ui.navigator.contents;
import static ummisco.gama.ui.metadata.FileMetaDataProvider.SHAPEFILE_CT_ID;
import static ummisco.gama.ui.metadata.FileMetaDataProvider.SHAPEFILE_SUPPORT_CT_ID;
import static ummisco.gama.ui.metadata.FileMetaDataProvider.getContentTypeId;
import static ummisco.gama.ui.metadata.FileMetaDataProvider.isSupport;
import static ummisco.gama.ui.metadata.FileMetaDataProvider.shapeFileSupportedBy;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import msi.gama.runtime.GAMA;
import msi.gama.util.file.GamaShapeFile.ShapeInfo;
import msi.gama.util.file.IGamaFileMetaData;
import msi.gaml.compilation.kernel.GamaBundleLoader;
import msi.gaml.types.Types;
import ummisco.gama.ui.navigator.NavigatorContentProvider;
import ummisco.gama.ui.resources.GamaColors;
import ummisco.gama.ui.resources.GamaIcons;
import ummisco.gama.ui.utils.PreferencesHelper;
/**
* The Class WrappedFile.
*/
public class WrappedFile extends WrappedResource<WrappedResource<?, ?>, IFile> {
/** The file parent. */
WrappedFile fileParent;
/** The is shape file. */
boolean isShapeFile;
/** The is shape file support. */
boolean isShapeFileSupport;
/** The image. */
Image image;
Color color;
/**
* Instantiates a new wrapped file.
*
* @param root
* the root
* @param wrapped
* the wrapped
*/
public WrappedFile(final WrappedContainer<?> root, final IFile wrapped) {
super(root, wrapped);
computeFileType();
computeFileParent();
}
/**
* Compute file image.
*/
protected void computeFileImage() {
final IFile f = getResource();
if (GamaBundleLoader.HANDLED_FILE_EXTENSIONS.contains(f.getFileExtension())) {
if (isShapeFileSupport) {
image = GamaIcons.create("navigator/file.shapesupport2").image();
} else {
image = DEFAULT_LABEL_PROVIDER.getImage(f);
}
} else {
image = GamaIcons.create("navigator/file.text2").image();
}
}
/**
* Compute file type.
*/
protected void computeFileType() {
final IFile f = getResource();
isShapeFile = SHAPEFILE_CT_ID.equals(getContentTypeId(f));
isShapeFileSupport = SHAPEFILE_SUPPORT_CT_ID.equals(getContentTypeId(f));
}
/**
* Compute file parent.
*/
private void computeFileParent() {
if (isShapeFileSupport) {
final IResource shape = shapeFileSupportedBy(getResource());
if (shape != null) { fileParent = (WrappedFile) getManager().findWrappedInstanceOf(shape); }
}
}
@Override
public WrappedResource<?, ?> getParent() {
if (fileParent != null) return fileParent;
return super.getParent();
}
@Override
public boolean canBeDecorated() {
return false;
}
@Override
public boolean hasChildren() {
return isShapeFile;
}
@Override
public Object[] getNavigatorChildren() {
if (NavigatorContentProvider.FILE_CHILDREN_ENABLED && (isGamaFile() || isShapeFile)) return getFileChildren();
return EMPTY;
}
/**
* Gets the file children.
*
* @return the file children
*/
public Object[] getFileChildren() {
final IFile p = getResource();
try {
final IContainer folder = p.getParent();
final List<VirtualContent> sub = new ArrayList<>();
for (final IResource r : folder.members()) {
if (r instanceof IFile && isSupport(p, (IFile) r)) { sub.add(getManager().findWrappedInstanceOf(r)); }
}
final IGamaFileMetaData metaData = GAMA.getGui().getMetaDataProvider().getMetaData(p, false, false);
Map<String, String> attributes;
if (metaData instanceof ShapeInfo info && !(attributes = info.getAttributes()).isEmpty()) {
Map<String, String> tags = new LinkedHashMap<>(attributes);
attributes.forEach((k, v) -> {
if (Types.AGENT.getSpecies().hasAttribute(k)) {
tags.put(k, tags.get(k) + " <- built-in attribute of agents");
color = GamaColors.system(SWT.COLOR_DARK_RED);
}
});
final Tags wf = new Tags(this, tags, "Attributes", false);
if (wf.getNavigatorChildren().length > 0) { sub.add(wf); }
}
return sub.toArray();
} catch (final CoreException e) {
e.printStackTrace();
}
return VirtualContent.EMPTY;
}
//
// @Override
// public Font getFont() {
// return GamaFonts.getNavigFileFont();
// }
@Override
public Image getImage() {
if (image == null) { computeFileImage(); }
return image;
}
@Override
public Color getColor() { return color; }
@Override
public void getSuffix(final StringBuilder sb) {
if (PreferencesHelper.NAVIGATOR_METADATA.getValue()) {
final IGamaFileMetaData data = GAMA.getGui().getMetaDataProvider().getMetaData(getResource(), false, true);
if (data != null) { data.appendSuffix(sb); }
}
}
@Override
public int countModels() {
return 0;
}
/**
* Checks if is gama file.
*
* @return true, if is gama file
*/
public boolean isGamaFile() { return false; }
@Override
public VirtualContentType getType() { return VirtualContentType.FILE; }
}