KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > operations > GraphEdit


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.whiteboard.operations;
20
21 import org.lucane.applications.whiteboard.graph.GraphUtils;
22 import org.lucane.applications.whiteboard.graph.MyGraph;
23 import org.lucane.applications.whiteboard.operations.changers.*;
24
25 import java.util.*;
26 import org.jgraph.event.GraphModelEvent;
27 import org.jgraph.graph.*;
28
29 public class GraphEdit implements GraphOperation
30 {
31     private Object JavaDoc[] changed;
32     private Map oldAttributes;
33     private Map newAttributes;
34     
35     private ConnectionSet connectionSet;
36     private ParentMap parentMap;
37         
38     public void init(GraphModelEvent gme)
39     {
40         GraphModel model = (GraphModel)gme.getSource();
41         GraphModelEvent.GraphModelChange change = (GraphModelEvent.GraphModelChange)gme.getChange();
42                 
43         this.oldAttributes = change.getAttributes();
44         this.newAttributes = change.getPreviousAttributes();
45         this.changed = change.getChanged();
46         
47         this.connectionSet = change.getPreviousConnectionSet();
48         this.parentMap = change.getPreviousParentMap();
49     }
50
51     public void apply(MyGraph graph)
52     {
53         GraphModel model = graph.getModel();
54         HashMap attributes = new HashMap();
55             
56         for(int i=0;i<changed.length;i++)
57         {
58             DefaultGraphCell cell = (DefaultGraphCell)changed[i];
59         
60             AttributeMap allCellAttributes = cell.getAttributes();
61             Map oldCellAttributes = (Map)oldAttributes.get(cell);
62             if(oldCellAttributes != null)
63                 allCellAttributes.putAll(oldCellAttributes);
64             cell.setAttributes(allCellAttributes);
65             cell.setUserObject(allCellAttributes.get("value"));
66             
67             DefaultGraphCell correspondingCell = GraphUtils.findCorrespondingCell(graph, cell);
68             if(correspondingCell == null)
69                 correspondingCell = cell;
70             else
71             {
72                 Changer changer = ChangerFactory.getChanger(cell);
73                 changer.change(graph, correspondingCell, cell);
74             }
75             
76             attributes.put(correspondingCell, newAttributes.get(cell));
77         }
78         
79         model.edit(attributes, connectionSet, parentMap, null);
80     }
81 }
Popular Tags