|
| 1 | +package org.insilico.jsbml.core.services.provider; |
| 2 | + |
| 3 | +import static org.eclipse.fx.code.editor.Constants.DOCUMENT_URL; |
| 4 | + |
| 5 | +import java.net.URI; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.WeakHashMap; |
| 8 | + |
| 9 | +import org.eclipse.core.runtime.URIUtil; |
| 10 | +import org.eclipse.e4.core.contexts.ContextFunction; |
| 11 | +import org.eclipse.e4.core.contexts.IContextFunction; |
| 12 | +import org.eclipse.e4.core.contexts.IEclipseContext; |
| 13 | +import org.eclipse.e4.core.di.IInjector; |
| 14 | +import org.eclipse.e4.ui.model.application.ui.basic.MPart; |
| 15 | +import org.insilico.jsbml.core.CSVUtils; |
| 16 | +import org.osgi.service.component.annotations.Component; |
| 17 | +import javax.swing.text.PlainDocument; |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * The {@link SBMLDocumentLoader} provides a {@link SBMLDocument} via dependency injection if the |
| 23 | + * current document is a sbml file. |
| 24 | + * |
| 25 | + * In oder to use this {@link IContextFunction} the context must store the location of a sbml file |
| 26 | + * with the key {@link org.eclipse.fx.code.editor.Constants#DOCUMENT_URL}. This context function |
| 27 | + * will only compute values for the contextKey |
| 28 | + * {@link org.insilico.jsbml.core.Constants#KEY_SBML_DOCUMENT} |
| 29 | + * |
| 30 | + * @author roman |
| 31 | + * |
| 32 | + */ |
| 33 | +@SuppressWarnings("restriction") |
| 34 | +@Component(service = IContextFunction.class, |
| 35 | + property = {"service.context.key=javax.swing.text.PlainDocument"}) |
| 36 | +public class CSVDocumentLoader extends ContextFunction { |
| 37 | + // Stores weak reference to already loaded documents. |
| 38 | + Map<String, PlainDocument> cache = new WeakHashMap<>(); |
| 39 | + |
| 40 | + @Override |
| 41 | + public Object compute(IEclipseContext context, String contextKey) { |
| 42 | + System.out.println("Compute..."); |
| 43 | + Object urlVal = context.get(DOCUMENT_URL); |
| 44 | + |
| 45 | + if (urlVal == null) { |
| 46 | + Object partVal = context.get(MPart.class); |
| 47 | + if (partVal != null && partVal instanceof MPart) { |
| 48 | + MPart part = (MPart) partVal; |
| 49 | + urlVal = part.getPersistedState().get(DOCUMENT_URL); |
| 50 | + context.set(DOCUMENT_URL, urlVal); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Check if a input file exists |
| 55 | + if (urlVal != null && urlVal instanceof String) { |
| 56 | + String urlString = (String) urlVal; |
| 57 | + urlString = urlString.replace("%20", " "); |
| 58 | + // Check cache |
| 59 | + PlainDocument doc = cache.get(urlString); |
| 60 | + |
| 61 | + if (doc == null) { |
| 62 | + // Check if the document is a sbml file. |
| 63 | + if (!CSVUtils.isCSVFile(urlString)) { |
| 64 | + System.out.println("Not a csv file"); |
| 65 | + return IInjector.NOT_A_VALUE; |
| 66 | + } |
| 67 | + |
| 68 | + // Load if needed |
| 69 | + try { |
| 70 | + System.out.println("Try to read CSV file"); |
| 71 | + //URI url = URIUtil.fromString(urlString); |
| 72 | + //doc = JSBML.readSBMLFromFile(url.getPath()); |
| 73 | + // doc = SBMLReader.read(new File(url)); |
| 74 | + //cache.put(urlString, doc); |
| 75 | + } |
| 76 | + catch (Exception e) { |
| 77 | + // Not a sbml file? |
| 78 | + e.printStackTrace(); |
| 79 | + System.out.println("Reading failed"); |
| 80 | + return IInjector.NOT_A_VALUE; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return doc; |
| 85 | + } |
| 86 | + |
| 87 | + System.out.println("No doc selected"); |
| 88 | + return IInjector.NOT_A_VALUE; |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | + |
0 commit comments