Coverage details for net.sourceforge.demetrix.ui.ProcessChainEditorActions

LineHitsSource
1 /*******************************************************************************
2  * Demetrix process modelling system
3  *
4  * Copyright (c) 2003, 2004 Dimitri A. Pissarenko
5  *
6  * This file is part of Demetrix.
7  *
8  * Demetrix is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * Demetrix is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Demetrix; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * For further information you may
23  *
24  * - send an e-mail in Russian, German or English to dimitri.pissarenko@gmx.net
25  * - look at http://sourceforge.net/projects/demetrix/
26  * - look at http://demetrix.sourceforge.net/
27  * - look at http://members.inode.at/d.pissarenko/
28  *
29  *****************************************************************************/
30 package net.sourceforge.demetrix.ui;
31 import java.awt.image.BufferedImage;
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.Date;
36 import java.util.Iterator;
37 import javax.swing.JDialog;
38 import javax.swing.JFileChooser;
39 import javax.swing.JOptionPane;
40 import javax.swing.tree.DefaultMutableTreeNode;
41  
42 import org._3pq.jgrapht.Graph;
43 import org._3pq.jgrapht.ext.JGraphModelAdapter;
44 import org._3pq.jgrapht.graph.ListenableDirectedGraph;
45 import org.apache.log4j.Logger;
46 import org.jgraph.JGraph;
47 import org.jgraph.graph.DefaultGraphCell;
48 import net.sourceforge.demetrix.model.*;
49 import net.sourceforge.demetrix.properties.DemetrixPropertiesHolder;
50 import net.sourceforge.demetrix.ui.balancesheets.*;
51 import net.sourceforge.demetrix.util.ExcelFileFilter;
52 import net.sourceforge.demetrix.util.GraphVertexPositioningHelper;
53 import net.sourceforge.demetrix.util.JGraphExporter;
54 import net.sourceforge.demetrix.util.PNGFileFilter;
55 import net.sourceforge.demetrix.util.SwingWorker;
56 /**
57  * @author Dimitri Pissarenko
58  *
59  */
600public class ProcessChainEditorActions {
61     private Logger logger = Logger.getLogger(getClass());
624    public ProcessChainEditorActions() {
634    }
64     public void newLinkAction(JGraph jgraph, ListenableDirectedGraph grapht) {
650        Object selectedObject1 = null;
660        Object selectedObject2 = null;
670        Object[] selectedCells = null;
680        Link link = null;
690        if (jgraph.getSelectionCount() == 2) {
700            selectedCells = jgraph.getSelectionCells();
710            selectedObject1 =
72                 ((DefaultGraphCell) selectedCells[0]).getUserObject();
730            selectedObject2 =
74                 ((DefaultGraphCell) selectedCells[1]).getUserObject();
750            if ((selectedObject1 instanceof ProcessChainNode)
76                 && (selectedObject2 instanceof ProcessChainNode)) {
770                if (((selectedObject1 instanceof Task)
78                     && (selectedObject2 instanceof Resource))
79                     || ((selectedObject1 instanceof Resource)
80                         && (selectedObject2 instanceof Task))) {
810                    link =
82                         new Link(
83                             (DemetrixPropertiesHolder) selectedObject1,
84                             (DemetrixPropertiesHolder) selectedObject2);
850                    grapht.addEdge(link);
860                    (
87                         (ProcessChain) CurrentProcessChainSingleton
88                             .getInstance()
89                             .getCurrentGraphParent()
90                             .getSubGraph())
91                             .addLink(
92                         link);
93                 } else {
94                     this.logger.debug(
95                         "newLinkAction: either both selected objects are tasks or both are resources, can't create a link between them");
96                 }
97             } else {
98                 this.logger.debug(
99                     "newLinkAction: at least one of the selected objects is not a ProcessChainNode");
100             }
101         } else {
102             this.logger.debug(
103                 "newLinkAction: in order to create a link, exactly two objects must be selected");
104         }
1050    }
106     public void balanceSheetAction() {
1070        TaskBalanceSheetDialog dialog = null;
1080        SwixMLTaskBalanceSheetRenderer balanceSheet = null;
1090        Task task = null;
1100        task =
111             (Task) CurrentlySelectedNodeSingleton
112                 .getInstance()
113                 .getCurrentlySelectedNode()
114                 .getUserObject();
1150        balanceSheet = this.createBalanceSheet(task);
1160        balanceSheet.render();
1170        dialog = new TaskBalanceSheetDialog(balanceSheet, task);
1180        dialog.getDialog().show();
1190    }
120     private SwixMLTaskBalanceSheetRenderer createBalanceSheet(Task task) {
1211        SwixMLTaskBalanceSheetRenderer balanceSheet = null;
1221        ProcessChain processChain = null;
1231        ArrayList inputs = null;
1241        ArrayList outputs = null;
1251        balanceSheet = new SwixMLTaskBalanceSheetRenderer();
1261        balanceSheet.setTask(task);
1271        processChain =
128             (ProcessChain) CurrentProcessChainSingleton
129                 .getInstance()
130                 .getCurrentGraphParent()
131                 .getSubGraph();
1321        if (processChain != null) {
1331            inputs =
134                 new ArrayList(
135                     processChain.getInputs(
136                         (String) task.getProperty("name").getValue()));
1371            outputs =
138                 new ArrayList(
139                     processChain.getOutputs(
140                         (String) task.getProperty("name").getValue()));
1411            balanceSheet.setInputs(inputs);
1421            balanceSheet.setOutputs(outputs);
143         } else {
144             this.logger.error(
145                 "no process chain, couldn't fetch inputs/outputs of the task (CurrentProcessChainSingleton.data==null)");
146         }
1471        return balanceSheet;
148     }
149     public void loadProcessChainActionAction() {
150         final JFileChooser fileChooser;
151  
1520        fileChooser = new JFileChooser();
1530        fileChooser.setFileFilter(new ExcelFileFilter());
1540        if (fileChooser
155             .showOpenDialog(ProcessChainEditor.getInstance().getFrame())
156             == JFileChooser.APPROVE_OPTION) {
1570            SwingWorker worker = new SwingWorker() {
158                 public Object construct() {
159                     File file = null;
160                     Task task = null;
161  
162                     ProcessChainExcelReader reader = null;
163                     ProcessChain processChain = null;
164                     file = fileChooser.getSelectedFile();
165                     CursorManagementSingleton.getInstance().setWaitCursor();
166                     reader = new ProcessChainExcelReader(file);
167                     try {
168                         processChain = reader.read();
169                     } catch (Exception exception) {
170                         logger.error(
171                             "an error occured while trying to read a process chain from file \""
172                                 + file
173                                 + "\"",
174                             exception);
175                         CursorManagementSingleton
176                             .getInstance()
177                             .setDefaultCursor();
178                         JOptionPane.showMessageDialog(
179                             ProcessChainEditor.getInstance().getFrame(),
180                             "An error occured while loading the process chain. See console output for details.",
181                             ProcessChainEditor
182                                 .getInstance()
183                                 .getFrame()
184                                 .getTitle(),
185                             JOptionPane.OK_OPTION);
186                     }
187                     task = new Task();
188                     task.setSubGraph(processChain);
189  
190                     CurrentProcessChainSingleton
191                         .getInstance()
192                         .setCurrentGraphParent(
193                         task);
194                     CurrentProcessChainSingleton
195                         .getInstance()
196                         .setSavingLocation(
197                         file);
198                     CurrentProcessChainSingleton
199                         .getInstance()
200                         .setTopLevelProcessChain(
201                         true);
202                     CurrentProcessChainSingleton
203                         .getInstance()
204                         .informCurrentGraphParentChanged();
205  
206                     return "";
207                 }
208             };
2090            worker.start();
210         }
2110    }
212     public void newProcessChainAction() {
2130        NewProcessChainActionSwingWorker worker = null;
2140        worker = new NewProcessChainActionSwingWorker();
2150        worker.start();
2160    }
217     public Resource newResourceAction(ListenableDirectedGraph grapht) {
2181        Resource resource = null;
2191        resource = new Resource();
2201        resource.getProperty("name").setValue("resource " + (new Date()));
2211        (
222             (ProcessChain) CurrentProcessChainSingleton
223                 .getInstance()
224                 .getCurrentGraphParent()
225                 .getSubGraph())
226                 .addResource(
227             resource);
2281        grapht.addVertex(resource);
2291        return resource;
230     }
231     public void newResourcePopupAction(
232         ListenableDirectedGraph grapht,
233         int x,
234         int y,
235         JGraphModelAdapter jgraphAdapter) {
2360        Object vertex = null;
2370        vertex = this.newResourceAction(grapht);
2380        GraphVertexPositioningHelper.getInstance().positionVertexAt(
239             vertex,
240             x,
241             y,
242             jgraphAdapter);
2430    }
244     public Task newTaskAction(ListenableDirectedGraph grapht) {
2451        Task task = null;
2461        task = new Task();
2471        task.getProperty("name").setValue("task " + (new Date()));
2481        (
249             (ProcessChain) CurrentProcessChainSingleton
250                 .getInstance()
251                 .getCurrentGraphParent()
252                 .getSubGraph())
253                 .addTask(
254             task);
2551        grapht.addVertex(task);
2561        return task;
257     }
258     public void newTaskPopupAction(
259         ListenableDirectedGraph grapht,
260         int x,
261         int y,
262         JGraphModelAdapter jgraphAdapter) {
2630        Object vertex = null;
2640        vertex = this.newTaskAction(grapht);
2650        GraphVertexPositioningHelper.getInstance().positionVertexAt(
266             vertex,
267             x,
268             y,
269             jgraphAdapter);
2700    }
271     public void saveProcessChainAction() {
2720        SwingWorker worker = new SwingWorker() {
273             public Object construct() {
274                 JFileChooser fileChooser = null;
275                 ProcessChainExcelWriter writer = null;
276                 if (CurrentProcessChainSingleton
277                     .getInstance()
278                     .getSavingLocation()
279                     == null) {
280                     fileChooser = new JFileChooser();
281                     fileChooser.setFileFilter(new ExcelFileFilter());
282                     if (fileChooser
283                         .showSaveDialog(
284                             ProcessChainEditor.getInstance().getFrame())
285                         == JFileChooser.APPROVE_OPTION) {
286                         CurrentProcessChainSingleton
287                             .getInstance()
288                             .setSavingLocation(
289                             fileChooser.getSelectedFile());
290                     }
291                 }
292                 if (CurrentProcessChainSingleton
293                     .getInstance()
294                     .getSavingLocation()
295                     != null) {
296                     CursorManagementSingleton.getInstance().setWaitCursor();
297                     writer =
298                         new ProcessChainExcelWriter(
299                             CurrentProcessChainSingleton
300                                 .getInstance()
301                                 .getSavingLocation(),
302                             (ProcessChain) CurrentProcessChainSingleton
303                                 .getInstance()
304                                 .getCurrentGraphParent()
305                                 .getSubGraph());
306                     try {
307                         writer.write();
308                         CursorManagementSingleton
309                             .getInstance()
310                             .setDefaultCursor();
311                     } catch (IOException exception) {
312                         exception.printStackTrace();
313                         logger.error(
314                             "error while saving process chain",
315                             exception);
316                         CursorManagementSingleton
317                             .getInstance()
318                             .setDefaultCursor();
319                         JOptionPane.showMessageDialog(
320                             ProcessChainEditor.getInstance().getFrame(),
321                             "An error occured while saving the process chain. Perhaps the file is open in Microsoft(R) Excel(tm). See console output for details.",
322                             ProcessChainEditor
323                                 .getInstance()
324                                 .getFrame()
325                                 .getTitle(),
326                             JOptionPane.ERROR_MESSAGE);
327                     }
328                 }
329                 return "";
330             }
331         };
3320        worker.start();
3330    }
334     public void systemBalanceSheetAction() {
3350        SystemBalanceSheetDialog dialog = null;
3360        SwixMLBalanceSheetRenderer balanceSheetRenderer = null;
3370        balanceSheetRenderer = new SwixMLSystemBalanceSheetRenderer();
3380        balanceSheetRenderer.render();
3390        dialog = new SystemBalanceSheetDialog(balanceSheetRenderer);
3400        dialog.getDialog().show();
3410    }
342     public void aboutAction() {
3430        SwingWorker worker = new SwingWorker() {
344             public Object construct() {
345                 JDialog aboutDialog = null;
346                 CursorManagementSingleton.getInstance().setWaitCursor();
347                 aboutDialog = (new AboutDialog()).getDialog();
348                 CursorManagementSingleton.getInstance().setDefaultCursor();
349                 aboutDialog.show();
350                 return "";
351             }
352         };
3530        worker.start();
3540    }
355     public void exportToPNGAction(JGraph graph) {
3560        BufferedImage img = null;
3570        String selectedFileName = null;
3580        JFileChooser fileChooser = null;
3590        fileChooser = new JFileChooser();
3600        fileChooser.setFileFilter(new PNGFileFilter());
3610        if (fileChooser
362             .showSaveDialog(ProcessChainEditor.getInstance().getFrame())
363             == JFileChooser.APPROVE_OPTION) {
3640            selectedFileName = fileChooser.getSelectedFile().getAbsolutePath();
3650            if (!selectedFileName.toLowerCase().endsWith(".png")) {
3660                selectedFileName = selectedFileName + ".png";
367             }
3680            JGraphExporter.getInstance().exportJGraph(
369                 graph,
370                 JGraphExporter.PNG_FORMAT,
371                 new File(selectedFileName));
372         };
3730    }
374     public void createTaskSubGraphAction(
375         ListenableDirectedGraph graph,
376         JGraph jgraph) {
3770        SubGraphCreationHelper.getInstance().createSubGraph(
378             (ProcessChain) graph,
379             jgraph);
3800    }
381     public void descendIntoSubGraphAction() {
3820        ProcessChainNode node = null;
3830        DefaultGraphCell cell = null;
3840        Object userObject = null;
3850        Graph subGraph = null;
3860        cell =
387             CurrentlySelectedNodeSingleton
388                 .getInstance()
389                 .getCurrentlySelectedNode();
3900        if (cell != null) {
3910            userObject = cell.getUserObject();
3920            if ((userObject != null)
393                 && (userObject instanceof ProcessChainNode)) {
3940                node = (ProcessChainNode) userObject;
3950                subGraph = node.getSubGraph();
3960                if (subGraph != null) {
3970                    CurrentProcessChainSingleton
398                         .getInstance()
399                         .setCurrentGraphParent(
400                         node);
4010                    CurrentProcessChainSingleton
402                         .getInstance()
403                         .setTopLevelProcessChain(
404                         false);
4050                    CurrentProcessChainSingleton
406                         .getInstance()
407                         .setSavingLocation(
408                         null);
409                 }
410             }
411         }
4120    }
413     public void deleteAction(JGraph jgraph) {
4147        this.deleteSingleCell(
415             jgraph,
416             CurrentlySelectedNodeSingleton
417                 .getInstance()
418                 .getCurrentlySelectedNode());
4197        this.deleteSeveralCells(jgraph);
4207    }
421     private void deleteSingleCell(JGraph jgraph, DefaultGraphCell cell) {
42214        Object[] stuffToDelete = null;
42314        Object userObject = null;
42414        ProcessChain processChain = null;
42514        Iterator edgesIterator = null;
42614        DefaultMutableTreeNode treeNode=null;
427         
42814        if (cell != null) {
42911            processChain =
430                 (ProcessChain) CurrentProcessChainSingleton
431                     .getInstance()
432                     .getCurrentGraphParent()
433                     .getSubGraph();
434  
43511            userObject = cell.getUserObject();
43611            if (userObject != null) {
43711                if ((userObject instanceof Resource)
438                     || (userObject instanceof Task)) {
439  
4409                    edgesIterator = processChain.edgesOf(userObject).iterator();
44111                    while (edgesIterator.hasNext()) {
4422                        processChain.removeLink((Link)edgesIterator.next());
443                     }
4449                    ((ProcessChainNodeTreeModel)SideBar.getInstance().getGraphStructureTree().getModel()).removeTreeNodeCorrespongingToUserObject(userObject);
445                     
4469                    processChain.removeTaskOrResource(userObject);
4472                } else if (userObject instanceof Link) {
4480                    processChain.removeLink((Link) userObject);
449                 }
450                 
451             }
452         }
45314    }
454     private void deleteSeveralCells(JGraph jgraph) {
4557        Object[] selectedCells = null;
456  
4577        selectedCells =
458             CurrentlySelectedNodeSingleton
459                 .getInstance()
460                 .getCurrentlySelectedCells();
4617        if (selectedCells != null) {
46211            for (int i = 0; i < selectedCells.length; i++) {
4637                this.deleteSingleCell(
464                     jgraph,
465                     (DefaultGraphCell) selectedCells[i]);
466             }
467         }
4687    }
469 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.