001    package nl.cwi.sen1.visplugin;
002    
003    import java.awt.event.ActionEvent;
004    
005    import javax.swing.AbstractAction;
006    import javax.swing.JMenu;
007    import javax.swing.JMenuItem;
008    import javax.swing.JOptionPane;
009    import javax.swing.JPanel;
010    
011    import nl.cwi.sen1.gui.Studio;
012    import nl.cwi.sen1.gui.StudioImplWithPredefinedLayout;
013    import nl.cwi.sen1.gui.StudioWithPredefinedLayout;
014    import nl.cwi.sen1.gui.component.StudioComponent;
015    import nl.cwi.sen1.gui.component.StudioComponentImpl;
016    import nl.cwi.sen1.relationstores.Factory;
017    import nl.cwi.sen1.relationstores.types.Location;
018    import nl.cwi.sen1.relationstores.types.RTuple;
019    import nl.cwi.sen1.visplugin.screenshotmaker.ScreenshotMaker;
020    
021    /**
022     * The VisualizationPluginWindow class represents the base class for the
023     * different plugin windows.
024     *
025     * @author Aldert Boerhoop
026     * @date 20-2-2007
027     */
028    public abstract class VisualizationPluginWindow {
029    
030        protected Factory m_factory;
031    
032        private Studio m_studio;
033        private int m_storeId;
034        private int m_factId;
035        private RTuple m_fact;
036        private String m_title;
037        private VisualizationPluginController m_controller;
038        private int m_windowId;
039        private ContainerPanel m_containerPanel=null;
040    
041    
042        /**
043         * Default constructor
044         *
045         * @author Aldert Boerhoop
046         * @date 20-2-2007
047         */
048        public VisualizationPluginWindow() {
049        }
050    
051        /**
052         * Initialize the window properties
053         *
054         * @author Aldert Boerhoop
055         * @date 20-2-2007
056         * @param Studio studio, int storeId, int factId, RTuple fact
057         */
058        public final void initializeWindow(Studio studio, int storeId, int factId,
059                RTuple fact, Factory factory, String pluginName,
060                VisualizationPluginController controller, int windowId) {
061            m_studio = studio;
062            m_storeId = storeId;
063            m_factId = factId;
064            m_fact = fact;
065            m_factory = factory;
066            m_title = fact.getVariable().getString();
067            m_controller = controller;
068            m_windowId = windowId;
069        }
070    
071        /**
072         * Returns the studio of the visualization plugin window.
073         *
074         * @return The studio object.
075         *
076         * @author Arend van Beelen
077         * @date 13-3-2007
078         */
079        public Studio getStudio() {
080            return m_studio;
081        }
082    
083        /**
084         * method is called on load of the window overridable 
085         *
086         * @author Aldert Boerhoop
087         * @date 16-3-2007
088         * @param the filename to store the export in
089         */
090        public void executeOnLoad() {
091    
092        }
093    
094    
095        /**
096         * Returns the controller of the visualization plugin window.
097         *
098         * @return The controller object.
099         * @author Arend van Beelen
100         * @date 13-3-2007
101         */
102        public VisualizationPluginController getController() {
103            return m_controller;
104        }
105    
106        /**
107         * Returns the window ID of the visualization plugin window.
108         *
109         * @return The window ID.
110         * @author Arend van Beelen
111         * @date 13-3-2007
112         */
113        public int getWindowId() {
114            return m_windowId;
115        }
116    
117        /**
118         * Display the plugin window
119         *
120         * @author Aldert Boerhoop
121         * @date 20-2-2007
122         */
123        public final void displayVisualization() {
124            // create a content panel on the right with menu bar
125            JPanel contentPanel = render(m_fact);
126            JMenu extensionMenu = createExtensionMenu();
127    
128            connectPanelWithMenu(extensionMenu, contentPanel, m_title,
129                    StudioImplWithPredefinedLayout.TOP_RIGHT);
130        }
131    
132        /**
133         * Create a menu
134         *
135         * @author Arjen van Schie & Michel Rijnders
136         * @date 20-2-2007
137         * @return The JMenu
138         */
139        protected JMenu createExtensionMenu() {
140            // create the menu item
141            JMenu extensionMenu = new JMenu("Visualization");
142    
143            // create the sub-item Export
144            JMenuItem export = new JMenuItem("Export to image");
145            export.addActionListener(new AbstractAction() {
146                public void actionPerformed(ActionEvent e) {
147                    m_controller.exportToClicked(
148                            VisualizationPluginController.requestType.image,
149                            m_windowId);
150                }
151    
152                private final static long serialVersionUID = 1;
153            });
154            extensionMenu.add(export);
155    
156            return extensionMenu;
157        }
158    
159    
160        /**
161         * calls the exportToClicked method on the controller 
162         *
163         * @author Aldert Boerhoop
164         * @date 16-3-2007
165         */
166        public final void exportToCsvClicked(){
167    
168            m_controller.exportToClicked(VisualizationPluginController.requestType.csv, m_windowId);
169        }
170    
171        /**
172         * method is called from the toolbus
173         *
174         * @author Aldert Boerhoop
175         * @date 16-3-2007
176         * @param the filename to store the export in
177         */
178        protected void exportToCsv(String fileName){}
179    
180        /**
181         * Show a popup
182         *
183         * @author Arjen van Schie & Michel Rijnders
184         * @date 20-2-2007
185         * @param Message
186         *            to display
187         */
188        public final void showPopUp(String msg) {
189            JOptionPane.showMessageDialog(m_containerPanel, msg);
190        }
191    
192        /**
193         * connects a given JPanel and JMenu to the meta-environment
194         *
195         * @author Arjen van Schie & Michel Rijnders
196         * @date 20-2-2007
197         * @param JMenu menu,JPanel contentPanel,String title,String orientation
198         */
199        private final void connectPanelWithMenu(JMenu menu, JPanel contentPanel,
200                String title, String orientation) {
201            if (contentPanel != null) {
202                // generate a container panel ( this is a panel with a
203                // hidden warningpanel and the given content panel)
204                m_containerPanel = new ContainerPanel(contentPanel);
205    
206                // generate the meta-studio component from the default
207                // implementation
208                StudioComponent component = new StudioComponentImpl(title, m_containerPanel);
209    
210                VisualizationWindowListener tempListener = new VisualizationWindowListener(this, component);
211                component.addStudioComponentListener(tempListener);
212    
213                // add the component to the studio
214                ((StudioWithPredefinedLayout) m_studio).addComponent(component,
215                        orientation);
216    
217                // add the menu (if there is any)
218                if (menu != null) {
219                    m_studio.addComponentMenu(component, menu);
220                }
221            }
222        }
223        
224        /**
225         * Removes a component from the Meta-Environment so it is removed from the
226         * views menu as well.
227         * 
228         * @param component The component to remove.
229         * 
230         * @author Arend van Beelen
231         * @author Anton Gerdesse
232         * @date 19-03-2007
233         */
234        public void disconnectComponent(StudioComponent component) {
235            m_studio.removeComponent(component);
236        }
237    
238        /**
239         * Show out of date message
240         *
241         * @author Aldert Boerhoop
242         * @author Arjen van Schie
243         * @date 20-2-2007
244         */
245        public final void factOutOfDate() {
246            m_containerPanel.showRstoreChangedWarning();
247        }
248    
249        /**
250         * The rstore of this fact has been unloaded.
251         *
252         * @author Bas Basten
253         * @author Anton Lycklama a Nijeholt
254         * @author Arjen van Schie
255         * @date 14-03-2007
256         */
257        public final void rstoreUnloaded() {
258            m_containerPanel.showRstoreUnloadedWarning();
259        }
260    
261        /**
262         * get the Store id
263         *
264         * @author Aldert Boerhoop
265         * @date 20-2-2007
266         * @return The Store id
267         */
268        public final int getStoreId() {
269            return m_storeId;
270        }
271    
272        /**
273         * get the Fact id
274         *
275         * @author Aldert Boerhoop
276         * @date 20-2-2007
277         * @return The Fact id
278         */
279        public final int getFactId() {
280            return m_factId;
281        }
282    
283        /**
284         * Render the plugin window
285         *
286         * @author Aldert Boerhoop
287         * @date 20-2-2007
288         * @param Fact
289         *            to display
290         * @return The JPanel
291         */
292        public abstract JPanel render(RTuple fact);
293    
294        /**
295         * Functionality that's triggered by the export menu
296         *
297         * @author Aldert Boerhoop
298         * @date 9-3-2007
299         */
300        public void exportToImage(String fileName) {
301            ScreenshotMaker.saveScreenshot(m_containerPanel.getContentPanel(), fileName,
302                    ScreenshotMaker.DEFAULT_EXTENSION);
303        }
304    
305        public void openLocationInEditor(Location loc) {
306            m_controller.openLocationInEditor(loc);
307        }
308    }