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:
020: package org.netbeans.modules.xslt.mapper.view;
021:
022: import java.awt.event.KeyAdapter;
023: import java.awt.event.KeyEvent;
024: import java.awt.event.MouseAdapter;
025: import java.awt.event.MouseEvent;
026: import java.awt.event.MouseListener;
027: import javax.swing.JPopupMenu;
028: import javax.swing.JTree;
029: import javax.swing.tree.TreePath;
030: import javax.swing.tree.TreeSelectionModel;
031: import org.netbeans.modules.soa.mapper.basicmapper.BasicMapper;
032: import org.netbeans.modules.soa.mapper.basicmapper.util.MapperUtilities;
033: import org.netbeans.modules.soa.mapper.common.IMapperEvent;
034: import org.netbeans.modules.soa.mapper.common.IMapperLink;
035: import org.netbeans.modules.soa.mapper.common.IMapperNode;
036: import org.netbeans.modules.soa.mapper.common.IMapperViewModel;
037: import org.netbeans.modules.soa.mapper.common.basicmapper.canvas.IMapperCanvasView;
038: import org.netbeans.modules.soa.mapper.common.basicmapper.literal.ILiteralUpdaterFactory;
039: import org.netbeans.modules.soa.mapper.common.basicmapper.tree.IMapperTreeNode;
040: import org.netbeans.modules.soa.mapper.common.basicmapper.tree.IMapperTreeView;
041: import org.netbeans.modules.xslt.mapper.methoid.LiteralUpdaterFactory;
042: import org.netbeans.modules.xslt.mapper.model.MapperContext;
043: import org.netbeans.modules.xslt.mapper.model.MapperContextChangeListenerAdapter;
044: import org.netbeans.modules.xslt.mapper.model.SchemaModelBridge;
045: import org.netbeans.modules.xslt.mapper.model.XsltModelBridge;
046: import org.netbeans.modules.xslt.mapper.model.SourceTreeModel;
047: import org.netbeans.modules.xslt.mapper.model.XsltNodesTreeRenderer;
048: import org.netbeans.modules.xslt.mapper.model.nodes.Node;
049: import org.netbeans.modules.xslt.mapper.model.nodes.TreeNode;
050: import org.netbeans.modules.xslt.mapper.model.nodes.actions.DeleteAction;
051: import org.netbeans.modules.xslt.mapper.model.targettree.TargetTreeModel;
052: import org.openide.util.HelpCtx;
053: import org.openide.util.Lookup;
054:
055: /**
056: *
057: * @author Alexey
058: */
059: public class XsltMapper extends BasicMapper implements HelpCtx.Provider {
060:
061: private MapperContext context;
062:
063: private Lookup lookup;
064:
065: private ILiteralUpdaterFactory myLiteralUpdaterFactory;
066: private LiteralEditListener myLiteralEditListener;
067:
068: private XsltModelBridge xslModelBridge;
069: private SchemaModelBridge schemaModelBridge;
070: private DiagramBuilder diagramBuilder;
071:
072: private ErrorPanel errorPanel;
073:
074: private PredicateManager myPredicateManager;
075:
076: public XsltMapper(Lookup lookup) {
077: super ();
078: this .lookup = lookup;
079: this .context = (MapperContext) lookup
080: .lookup(MapperContext.class);// TODO r new MapperContext(lookup);
081:
082: myPredicateManager = new PredicateManager(this );
083: errorPanel = new ErrorPanel(this );
084:
085: initializeTrees();
086:
087: xslModelBridge = new XsltModelBridge(this );
088: schemaModelBridge = new SchemaModelBridge(this );
089: diagramBuilder = new DiagramBuilder(this );
090: super .addMapperListener(xslModelBridge);
091: super .setMapperRule(new XsltMapperRule(this ));
092:
093: if (context != null) {
094: context
095: .addMapperContextChangeListener(new MapperContextChangeListenerAdapter() {
096: public void mapperContextChanged(
097: Object oldValue, Object newValue) {
098: schemaModelBridge.updateDiagram();
099: }
100: });
101: }
102: //
103: this .setLiteralUpdaterFactory(new LiteralUpdaterFactory(this ));
104:
105: xslModelBridge.updateDiagram();
106: }
107:
108: public MapperContext getContext() {
109: return context;
110: }
111:
112: public Lookup getLookup() {
113: return lookup;
114: }
115:
116: public DiagramBuilder getBuilder() {
117: return this .diagramBuilder;
118: }
119:
120: protected void initializeTrees() {
121:
122: JTree sourceTree = getMapperViewManager().getSourceView()
123: .getTree();
124: sourceTree.setModel(new SourceTreeModel(this ));
125: sourceTree.setRootVisible(true);
126: sourceTree.setShowsRootHandles(true);
127: sourceTree.getSelectionModel().setSelectionMode(
128: TreeSelectionModel.SINGLE_TREE_SELECTION);
129: sourceTree.expandRow(0);
130: sourceTree.setCellRenderer(new XsltNodesTreeRenderer());
131: //
132: final JTree targetTree = getMapperViewManager().getDestView()
133: .getTree();
134: targetTree.setModel(new TargetTreeModel(this ));
135: targetTree.setRootVisible(false);
136: targetTree.setShowsRootHandles(true);
137: targetTree.getSelectionModel().setSelectionMode(
138: TreeSelectionModel.SINGLE_TREE_SELECTION);
139: targetTree.expandRow(0);
140: targetTree.setCellRenderer(new XsltNodesTreeRenderer());
141: //
142: // Add the mouse listener for popup menu
143: MouseListener pupupMouseListener = new MouseAdapter() {
144: public void mousePressed(MouseEvent e) {
145: maybeShowPopup(e);
146: }
147:
148: public void mouseReleased(MouseEvent e) {
149: maybeShowPopup(e);
150: }
151:
152: private void maybeShowPopup(MouseEvent e) {
153: if (e.isPopupTrigger()) {
154: Object source = e.getSource();
155: assert source instanceof JTree;
156: TreePath path = ((JTree) source)
157: .getPathForLocation(e.getX(), e.getY());
158: if (path != null) {
159: Object lastComp = path.getLastPathComponent();
160: if (lastComp instanceof TreeNode) {
161: TreeNode node = (TreeNode) lastComp;
162: JPopupMenu popup = node
163: .constructPopupMenu();
164: //
165: if (popup != null) {
166: popup.show(e.getComponent(), e.getX(),
167: e.getY());
168: }
169: }
170: }
171: }
172: }
173: };
174: //
175: targetTree.addMouseListener(pupupMouseListener);
176: sourceTree.addMouseListener(pupupMouseListener);
177: //
178: targetTree.addKeyListener(new KeyAdapter() {
179: public void keyReleased(KeyEvent event) {
180: if (event.getKeyCode() == KeyEvent.VK_DELETE) {
181: TreeNode selectedNode = (TreeNode) targetTree
182: .getSelectionPath().getLastPathComponent();
183: DeleteAction da = new DeleteAction(XsltMapper.this ,
184: selectedNode);
185: da.actionPerformed(null);
186: }
187: }
188: });
189:
190: }
191:
192: public void setLiteralUpdaterFactory(
193: ILiteralUpdaterFactory literalUpdaterFactory) {
194: super .setLiteralUpdaterFactory(literalUpdaterFactory);
195: //
196: myLiteralUpdaterFactory = literalUpdaterFactory;
197: //
198: // also set up listener on literal edit events
199: if (myLiteralEditListener == null) {
200: IMapperCanvasView canvasView = getMapperViewManager()
201: .getCanvasView();
202: myLiteralEditListener = new LiteralEditListener(this );
203: canvasView.getCanvas().addCanvasMouseListener(
204: myLiteralEditListener);
205: getMapperController().addMapperListener(
206: myLiteralEditListener);
207: }
208: }
209:
210: public ILiteralUpdaterFactory getLiteralUpdaterFactory() {
211: return myLiteralUpdaterFactory;
212: }
213:
214: public IMapperNode getMapperNode(TreeNode node) {
215:
216: TreePath path = TreeNode.getTreePath(node);
217:
218: IMapperNode result = findNodeInTree(path,
219: getMapperViewManager().getSourceView());
220:
221: if (result == null) {
222: return findNodeInTree(path, getMapperViewManager()
223: .getDestView());
224: }
225:
226: return result;
227:
228: }
229:
230: private IMapperNode findNodeInTree(TreePath path,
231: IMapperTreeView view) {
232: if (view.getTree().getModel().getRoot() == path
233: .getPathComponent(0)) {
234: return view.getMapperTreeNode(path);
235: }
236:
237: return null;
238: }
239:
240: public void addLink(Node node_from, Node node_to) {
241: IMapperNode startNode = node_from.getOutputNode();
242: IMapperNode endNode = node_to.getInputNode(node_from);
243: if (startNode != null & endNode != null) {
244: IMapperLink link = super .createLink(startNode, endNode);
245: addLink(link);
246: }
247: }
248:
249: /**
250: * Remove a link from this mapper. If any link end point nodes are tree
251: * node, and no other links connect to the node. The tree node will also be
252: * removed.
253: *
254: * @param link the link to be remove from this mapper.
255: */
256: public void removeLink(IMapperLink link) {
257: IMapperNode startNode = link.getStartNode();
258: IMapperNode endNode = link.getEndNode();
259: if (startNode != null) {
260: startNode.removeLink(link);
261: }
262: if (endNode != null) {
263: endNode.removeLink(link);
264: }
265:
266: if ((getMapperViewManager().getMapperModel() != null)
267: && (getMapperViewManager().getMapperModel()
268: .getSelectedViewModel() != null)) {
269: IMapperViewModel viewModel = getMapperViewManager()
270: .getMapperModel().getSelectedViewModel();
271:
272: if (startNode instanceof IMapperTreeNode
273: && (startNode.getLinkCount() == 0)) {
274: viewModel.removeNode(startNode);
275: }
276:
277: if (endNode instanceof IMapperTreeNode
278: && (endNode.getLinkCount() == 0)) {
279: viewModel.removeNode(endNode);
280: }
281: }
282: if (!diagramBuilder.isUpdating()) {
283: getMapperViewManager().postMapperEvent(
284: MapperUtilities.getMapperEvent(this , link,
285: IMapperEvent.LINK_DEL,
286: "Link removed from mapper"));
287: }
288: }
289:
290: /**
291: * Add a link to this mapper. This method add the link to the start node and
292: * end node if the link is not in the nodes yet. Next, add the start and end
293: * node to the model, if any of the two node is a group node, it only adds
294: * the root node to the model. Finally, it posts a IMapperEvent.NEW_LINK
295: * event.
296: *
297: * @param link the link to be added.
298: */
299: public void addLink(IMapperLink link) {
300: IMapperNode startNode = link.getStartNode();
301: IMapperNode endNode = link.getEndNode();
302: connectLink(link);
303: if ((getMapperViewManager().getMapperModel() != null)
304: && (getMapperViewManager().getMapperModel()
305: .getSelectedViewModel() != null)) {
306: IMapperViewModel viewModel = getMapperViewManager()
307: .getMapperModel().getSelectedViewModel();
308:
309: if (startNode != null) {
310: IMapperNode groupNode = startNode;
311:
312: while (groupNode.getGroupNode() != null) {
313: groupNode = groupNode.getGroupNode();
314: }
315:
316: if (!(viewModel.containsNode(groupNode))) {
317: addNode(groupNode);
318: }
319: }
320:
321: if (endNode != null) {
322: IMapperNode groupNode = endNode;
323:
324: while (groupNode.getGroupNode() != null) {
325: groupNode = groupNode.getGroupNode();
326: }
327:
328: if (!(viewModel.containsNode(groupNode))) {
329: addNode(groupNode);
330: }
331: }
332: }
333:
334: if (!diagramBuilder.isUpdating()) {
335: getMapperViewManager().postMapperEvent(
336: MapperUtilities.getMapperEvent(this , link,
337: IMapperEvent.LINK_ADDED,
338: "New Link added to mapper")); //NOI18N
339: }
340: }
341:
342: /**
343: * Add a node to this mapper. Node is added (to fire event) only if there is
344: * a selected view model in the mapper model. Then a IMapperEvent.NODE_ADDED
345: * event is posted.
346: *
347: * @param node the group node to be added.
348: */
349: public void addNode(IMapperNode node) {
350: if ((getMapperViewManager().getMapperModel() != null)
351: && (getMapperViewManager().getMapperModel()
352: .getSelectedViewModel() != null)) {
353:
354: IMapperViewModel viewModel = getMapperViewManager()
355: .getMapperModel().getSelectedViewModel();
356: viewModel.addNode(node);
357: if (!diagramBuilder.isUpdating()) {
358: getMapperViewManager().postMapperEvent(
359: MapperUtilities.getMapperEvent(this , node,
360: IMapperEvent.NODE_ADDED,
361: "New Node added to mapper")); //NOI18N
362: }
363: }
364: }
365:
366: public void setError(String message) {
367: if (message == null) {
368: errorPanel.uninstall();
369: } else {
370: errorPanel.setMessage(message);
371: errorPanel.install();
372:
373: }
374: }
375:
376: public PredicateManager getPredicateManager() {
377: return myPredicateManager;
378: }
379:
380: public HelpCtx getHelpCtx() {
381: return new HelpCtx("xslt_editor_design_about"); //NOI18N
382: }
383:
384: }
|