001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.bpel.mapper.multiview;
020:
021: import java.util.LinkedHashMap;
022: import java.util.Map;
023: import java.util.Map.Entry;
024: import javax.swing.JTree;
025: import javax.swing.SwingUtilities;
026: import org.netbeans.modules.bpel.mapper.tree.GraphExpandedState;
027: import org.netbeans.modules.bpel.mapper.tree.RightTreeExpandedState;
028: import org.netbeans.modules.bpel.mapper.tree.TreeExpandedState;
029: import org.netbeans.modules.bpel.mapper.tree.TreeExpandedStateImpl;
030: import org.netbeans.modules.bpel.mapper.tree.spi.MapperTcContext;
031: import org.netbeans.modules.bpel.model.api.BpelEntity;
032: import org.netbeans.modules.bpel.model.api.support.UniqueId;
033: import org.netbeans.modules.soa.mappercore.Mapper;
034: import org.netbeans.modules.soa.mappercore.RightTree;
035:
036: /**
037: * Store mapper state for context entity which is the unique id of mapper
038: * @author Vitaly Bychkov
039: * @version 1.0
040: */
041: public class MapperStateManager {
042: private Map<UniqueId, MapperState> myEntitiesContext = new ContextCache<UniqueId, MapperState>();
043:
044: private MapperTcContext myMapperTcContext;
045:
046: public MapperStateManager(MapperTcContext mapperTcContext) {
047: myMapperTcContext = mapperTcContext;
048: }
049:
050: public void storeOldEntityContext(BpelDesignContext oldContext) {
051: if (oldContext == null) {
052: return;
053: }
054:
055: TreeExpandedState leftTreeState = getLeftTreeExpandedState();
056: TreeExpandedState rightTreeState = getRightTreeExpandedState();
057: TreeExpandedState graphState = getGraphState();
058:
059: BpelEntity entity = null;
060:
061: entity = oldContext.getContextEntity();
062:
063: UniqueId entityUID = entity != null ? entity.getUID() : null;
064: if (entityUID != null) {
065: MapperState mapperEntityState = new MapperState(entityUID);
066: mapperEntityState.setLeftTreeExpandedState(leftTreeState);
067: mapperEntityState.setRightTreeExpandedState(rightTreeState);
068: mapperEntityState.setGraphExpandedState(graphState);
069: myEntitiesContext.put(entityUID, mapperEntityState);
070: }
071:
072: }
073:
074: public void restoreOldEntityContext(BpelDesignContext context) {
075: if (context == null) {
076: return;
077: }
078:
079: BpelEntity entity = context.getContextEntity();
080: UniqueId uid = entity != null ? entity.getUID() : null;
081: if (uid == null) {
082: return;
083: }
084:
085: final MapperState state = myEntitiesContext.get(uid);
086: if (state != null) {
087: SwingUtilities.invokeLater(new Runnable() {
088: public void run() {
089: TreeExpandedState leftTreeState = state
090: .getLeftTreeExpandedState();
091: if (leftTreeState != null) {
092: leftTreeState.restore();
093: }
094:
095: TreeExpandedState rightTreeState = state
096: .getRightTreeExpandedState();
097: if (rightTreeState != null) {
098: rightTreeState.restore();
099: }
100:
101: TreeExpandedState graphState = state
102: .getGraphEXpandedState();
103: if (graphState != null) {
104: graphState.restore();
105: }
106: }
107: });
108: // TODO: restore tree selection, expanded graph
109: }
110: }
111:
112: private TreeExpandedState getLeftTreeExpandedState() {
113: TreeExpandedState leftTreeState = null;
114:
115: Mapper mapper = myMapperTcContext.getMapper();
116: if (mapper != null) {
117: // JTree leftTree = mapper.getLeftTree();
118: // if (leftTree != null) {
119: leftTreeState = new TreeExpandedStateImpl(mapper);
120: leftTreeState.save();
121: // }
122: }
123: return leftTreeState;
124: }
125:
126: private TreeExpandedState getGraphState() {
127: TreeExpandedState graphState = null;
128:
129: Mapper mapper = myMapperTcContext.getMapper();
130: if (mapper != null) {
131: graphState = new GraphExpandedState(mapper);
132: graphState.save();
133: }
134: return graphState;
135: }
136:
137: // TODO a
138: private TreeExpandedState getRightTreeExpandedState() {
139: TreeExpandedState rightTreeState = null;
140:
141: Mapper mapper = myMapperTcContext.getMapper();
142: if (mapper != null) {
143: // RightTree rightTree = mapper.getRightTree();
144: // if (rightTree != null) {
145: rightTreeState = new RightTreeExpandedState(mapper);
146: rightTreeState.save();
147: // }
148: }
149: return rightTreeState;
150: }
151:
152: private static class ContextCache<K, V> extends LinkedHashMap<K, V> {
153: private static final long serialVersionUID = 1L;
154: private static final int MAX_ENTRIES = 50; // we aren't going to store more then last 50 entities
155:
156: @Override
157: protected boolean removeEldestEntry(Entry<K, V> eldest) {
158: return size() > MAX_ENTRIES;
159: }
160: }
161: }
|