001    package nl.cwi.sen1.visplugin;
002    
003    import java.util.ArrayList;
004    
005    import nl.cwi.sen1.gui.Studio;
006    import nl.cwi.sen1.gui.plugin.DefaultStudioPlugin;
007    import nl.cwi.sen1.relationstores.Factory;
008    import nl.cwi.sen1.relationstores.types.Location;
009    import aterm.ATerm;
010    import aterm.pure.PureFactory;
011    
012    /**
013     * The VisualizationPluginController class represents the base class for the
014     * different plugin controllers.
015     *
016     * @author Aldert Boerhoop
017     * @date 20-2-2007
018     */
019    public abstract class VisualizationPluginController extends DefaultStudioPlugin
020            implements VisualizationPluginTif {
021    
022        public enum requestType {
023            image, csv, rStore
024        };
025    
026        private static final String TOOL_NAME = "visualizationPlugin";
027    
028        private PureFactory m_pureFactory;
029        private Studio m_studio;
030        private String m_pluginName;
031        private ATerm[] m_supportedTypes;
032        private ArrayList<WindowProperties> m_openWindows;
033        private VisualizationPluginBridge m_bridge;
034            
035        protected Factory m_factory;
036    
037        /**
038         * Returns the bridge of the visualization plugin controller.
039         *
040         * @return The bridge object.
041         *
042         * @author Arend van Beelen
043         * @date 13-3-2007
044         */
045        public VisualizationPluginBridge getBridge() {
046            return m_bridge;
047        }
048    
049        /**
050         * Initialize the plugin
051         *
052         * @author Aldert Boerhoop
053         * @date 20-2-2007
054         * @param Studio studioArgument
055         */
056        public void initStudioPlugin(Studio studioArgument) {
057            m_studio = studioArgument;
058            m_bridge = new VisualizationPluginBridge(m_studio.getATermFactory(), this);
059    
060            initPluginController((aterm.pure.PureFactory) m_studio.getATermFactory());
061    
062            m_studio.connect(TOOL_NAME, m_bridge);
063        }
064    
065        /**
066         * Initialize the plugin controller
067         *
068         * @author Aldert Boerhoop
069         * @date 21-2-2007
070         * @param PureFactory pureFactory that should be used by the controller
071         */
072        public final void initPluginController(PureFactory pureFactory) {
073            m_pureFactory = pureFactory;
074            m_factory = Factory.getInstance(m_pureFactory);
075    
076            VisualizationFactorySingleton.initInstances(m_pureFactory, m_factory);
077    
078            m_openWindows = new ArrayList<WindowProperties>();
079            m_pluginName = getPluginName();
080            m_supportedTypes = getSupportedTypes();
081        }
082    
083        /**
084         * Visualize the fact (Incomming call from the toolbus)
085         *
086         * @author Aldert Boerhoop
087         * @date 20-2-2007
088         * @param Rstore id, Fact id and the fact to visualize
089         */
090        public final void vpVisualizeFact(int storeId, int factId, ATerm fact) {
091    
092            WindowProperties newWindowProperties = new WindowProperties();
093            VisualizationPluginWindow newWindow = createWindow();
094    
095            newWindow.initializeWindow(m_studio, storeId, factId, m_factory.RTupleFromTerm(fact), m_factory, m_pluginName, this, m_openWindows.size());
096    
097            newWindow.displayVisualization();
098    
099            newWindowProperties.window = newWindow;
100            m_openWindows.add(newWindowProperties);
101    
102            newWindow.executeOnLoad();
103    
104        }
105    
106        /**
107         * Had to be implemented (studioplugin.getName)
108         *
109         * @author Aldert Boerhoop
110         * @date 20-2-2007
111         * @return The plugin name
112         */
113        public final String getName() {
114            return getPluginName();
115        }
116    
117        /**
118         * Sends an out of date ack. to the asigned plugin window
119         *
120         * @author Aldert Boerhoop
121         * @date 20-2-2007
122         * @param Store id and the Fact id
123         */
124        public final void vpFactOutOfDate(int storeId, int factId) {
125            if (m_openWindows != null) {
126                for (int i = 0; i < m_openWindows.size(); i++) {
127    
128                    WindowProperties windowProperties = m_openWindows.get(i);
129                    VisualizationPluginWindow window = windowProperties.window;
130    
131                    if (window.getFactId() == factId && window.getStoreId() == storeId) {
132                        window.factOutOfDate();
133                    }
134                }
135            }
136        }
137    
138        /**
139         * The VPI will call this method when an rstore has been unloaded.
140         * The VisualizationController will check which VisualizationPluginWindows
141         * correspond with the specified rstoreId and these windows will be notified.
142         *
143         * @author Bas Basten
144         * @author Anton Lycklama a Nijeholt
145         * @date 14-03-2007
146         *
147         * @param rstoreId The rstoreId of the unloaded rstore.
148         */
149        public final void vpRstoreUnloaded(int rstoreId) {
150            if (m_openWindows != null) {
151                for (int i = 0; i < m_openWindows.size(); i++) {
152    
153                    WindowProperties windowProperties = m_openWindows.get(i);
154                    VisualizationPluginWindow window = windowProperties.window;
155    
156                    if (window.getStoreId() == rstoreId) {
157                        window.rstoreUnloaded();
158                    }
159                }
160            }
161        }
162    
163        /**
164         * Get the name of the plugin (Incomming call from the toolbus)
165         *
166         * @author Aldert Boerhoop
167         * @date 20-2-2007 return The plugin name as an ATerm
168         */
169        public final ATerm vpGetName() {
170            return m_pureFactory.make("snd-value(vp-name(<str>))", m_pluginName);
171        }
172    
173        /**
174         * Checks if the type is supported (Incomming call from the toolbus)
175         *
176         * @author Aldert Boerhoop
177         * @date 20-2-2007 return true if the type is supported else false
178         */
179        public final ATerm vpIsTypeSupported(ATerm type) {
180            boolean typeSupported = isTypeSupported(type);
181            return m_pureFactory.make("snd-value(vp-type-supported(<bool>))", typeSupported);
182        }
183    
184        /**
185         * Checks if a type is supported. Is overridable by children to support
186         * alternative ways to check for a type. For instance checking for n-size
187         * relations.
188         *
189         * @author Aldert Boerhoop
190         * @author Anton Lycklama
191         * @author Bas Basten
192         * @date 09-03-2007
193         */
194        protected boolean isTypeSupported(ATerm type) {
195            if (m_supportedTypes != null) {
196                for (int i = 0; i < m_supportedTypes.length; i++) {
197                    if (m_supportedTypes[i].toString().equals(type.toString())) {
198                        return true;
199                    }
200                }
201            }
202            return false;
203        }
204    
205        /**
206         * Close the plugin
207         *
208         * @author Aldert Boerhoop
209         * @date 20-2-2007
210         * @param ATerm event
211         */
212        public void recTerminate(ATerm event) {
213            fireStudioPluginClosed();
214        }
215    
216        public void recAckEvent(ATerm t0) {
217        }
218    
219        /**
220         * Create a new instance of the plugin window
221         *
222         * @author Aldert Boerhoop
223         * @date 20-2-2007
224         * @return The instance of the plugin window
225         */
226        public abstract VisualizationPluginWindow createWindow();
227    
228        /**
229         * Get the name of the plugin
230         *
231         * @author Aldert Boerhoop
232         * @date 20-2-2007
233         * @return The plugin name
234         */
235        public abstract String getPluginName();
236    
237        /**
238         * Get the supported types of the plugin
239         *
240         * @author Aldert Boerhoop
241         * @date 20-2-2007
242         * @return The supported types
243         */
244        public abstract ATerm[] getSupportedTypes();
245    
246        /**
247         * Opens a file dialog with the correct properties
248         *
249         * @author Aldert Boerhoop
250         * @date 15-3-2007
251         * @param requestType exportRequestType type of export, int windowId of the window who requested the export
252         */
253        public void exportToClicked(requestType exportRequestType, int windowId) {
254    
255            WindowProperties windowProperties = m_openWindows.get(windowId);
256            windowProperties.exportRequestType = exportRequestType;
257    
258            String dialogTitle = "Export as";
259            String paths = "[]";
260            String extensions = "";
261    
262            System.out.println(exportRequestType);
263    
264            switch (exportRequestType) {
265    
266            case image:
267                extensions = ".png";
268                break;
269    
270            case csv:
271                extensions = ".csv";
272                break;
273    
274            case rStore:
275                extensions = ".rstore";
276                break;
277            }
278    
279            ATerm term = m_studio.getATermFactory().make(
280                    "vp-open-file-dialog(<int>,<str>,<str>,<str>)", windowId,
281                    dialogTitle, paths, extensions);
282    
283            m_bridge.sendEvent(term);
284        }
285    
286        /**
287         * Calls the correct function to handle the export 
288         *
289         * @author Aldert Boerhoop
290         * @date 15-3-2007
291         * @param int windowId of the window who requested the export, String fileName thats entered in the file dialog
292         */
293        public void vpFileDialogResult(int windowId, String fileName) {
294    
295            VisualizationPluginWindow window = getWindowById(windowId);
296    
297            switch (getWindowExportRequestType(windowId)) {
298            case image:
299                window.exportToImage(fileName);
300                break;
301    
302            case csv:
303                window.exportToCsv(fileName);
304                break;
305                
306            default:
307            }
308        }
309    
310        /**
311         * Returns a plugin window given by its ID.
312         *
313         * @param windowId ID of the window to return.
314         * @return The window object.
315         *
316         * @author Aldert Boerhoop
317         * @author Arend van Beelen
318         * @date 19-03-2007
319         */
320        public VisualizationPluginWindow getWindowById(int windowId) {
321            return m_openWindows.get(windowId).window;
322        }
323    
324        /**
325         * Gets the export request type.
326         *
327         * @param windowId ID of the window to get the export request type of.
328         * @return The export request type of the window.
329         *
330         * TODO: Should be handled through proper export handlers, but the current
331         *       architecture doesn't allow this without extensive refactoring.
332         *
333         * @author Arend van Beelen
334         * @date 13-03-2007
335         */
336        protected requestType getWindowExportRequestType(int windowId) {
337            WindowProperties windowProperties = m_openWindows.get(windowId);
338    
339            return windowProperties.exportRequestType;
340        }
341        
342        /**
343         * Calls the correct function to handle the user cancel of the file dialog 
344         *
345         * @author Aldert Boerhoop
346         * @date 15-3-2007
347         * @param int windowId of the window who requested the export
348         */
349        public void vpFileDialogCancel(int windowId) {
350            System.out.println("User cancel!! windowId => " + windowId);
351        }
352    
353        public void openLocationInEditor(Location loc) {
354            ATerm event = m_studio.getATermFactory().make("vp-link-clicked(<term>)", loc.toTerm());
355            m_bridge.sendEvent(event);
356        }
357    
358        public class WindowProperties {
359            public VisualizationPluginWindow window;
360            public VisualizationPluginController.requestType exportRequestType;
361        }
362    }