/deprecated/org.eclipse.zest.core/src/org/eclipse/zest/core/viewers/internal/GraphModelEntityRelationshipFactory.java

https://github.com/uci-sdcl/lighthouse · Java · 137 lines · 67 code · 14 blank · 56 comment · 10 complexity · eafcbb83280fe22885a626dc6607df4c MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright 2005-2006, CHISEL Group, University of Victoria, Victoria, BC,
  3. * Canada. All rights reserved. This program and the accompanying materials are
  4. * made available under the terms of the Eclipse Public License v1.0 which
  5. * accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: The Chisel Group, University of Victoria
  9. ******************************************************************************/
  10. package org.eclipse.zest.core.viewers.internal;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import org.eclipse.zest.core.viewers.IGraphEntityRelationshipContentProvider;
  14. import org.eclipse.zest.core.widgets.Graph;
  15. import org.eclipse.zest.core.widgets.GraphContainer;
  16. import org.eclipse.zest.core.widgets.GraphNode;
  17. /*
  18. * A factory for the IGraphEntityRelationshipContentProvider.
  19. *
  20. * @author Del Myers
  21. */
  22. // @tag bug.154580-Content.fix
  23. // @tag bug.160367-Refreshing.fix : updated to use new
  24. // AbstractStylingModelFactory
  25. public class GraphModelEntityRelationshipFactory extends AbstractStylingModelFactory {
  26. public GraphModelEntityRelationshipFactory(AbstractStructuredGraphViewer viewer) {
  27. super(viewer);
  28. if (!(viewer.getContentProvider() instanceof IGraphEntityRelationshipContentProvider)) {
  29. throw new IllegalArgumentException("Expected IGraphEntityRelationshipContentProvider");
  30. }
  31. }
  32. /*
  33. * (non-Javadoc)
  34. *
  35. * @see org.eclipse.zest.core.internal.graphmodel.AbstractStylingModelFactory#createGraphModel()
  36. */
  37. public Graph createGraphModel(Graph model) {
  38. doBuildGraph(model);
  39. return model;
  40. }
  41. /*
  42. * (non-Javadoc)
  43. *
  44. * @see org.eclipse.zest.core.internal.graphmodel.AbstractStylingModelFactory#doBuildGraph(org.eclipse.zest.core.internal.graphmodel.GraphModel)
  45. */
  46. protected void doBuildGraph(Graph model) {
  47. super.doBuildGraph(model);
  48. Object[] nodes = getContentProvider().getElements(getViewer().getInput());
  49. nodes = filter(getViewer().getInput(), nodes);
  50. createModelNodes(model, nodes);
  51. createModelRelationships(model);
  52. }
  53. /**
  54. * Creates all the model relationships. Assumes that all of the model nodes
  55. * have been created in the graph model already. Runtime O(n^2) + O(r).
  56. *
  57. * @param model
  58. * the model to create the relationship on.
  59. */
  60. private void createModelRelationships(Graph model) {
  61. GraphNode[] modelNodes = getNodesArray(model);
  62. List listOfNodes = new ArrayList();
  63. for (int i = 0; i < modelNodes.length; i++) {
  64. listOfNodes.add(modelNodes[i]);
  65. }
  66. for (int i = 0; i < listOfNodes.size(); i++) {
  67. GraphNode node = (GraphNode) listOfNodes.get(i);
  68. if (node instanceof GraphContainer) {
  69. List childNodes = ((GraphContainer) node).getNodes();
  70. listOfNodes.addAll(childNodes);
  71. }
  72. }
  73. modelNodes = (GraphNode[]) listOfNodes.toArray(new GraphNode[listOfNodes.size()]);
  74. IGraphEntityRelationshipContentProvider content = getCastedContent();
  75. for (int i = 0; i < modelNodes.length; i++) {
  76. for (int j = 0; j < modelNodes.length; j++) {
  77. Object[] rels = content.getRelationships(modelNodes[i].getData(), modelNodes[j].getData());
  78. if (rels != null) {
  79. rels = filter(getViewer().getInput(), rels);
  80. for (int r = 0; r < rels.length; r++) {
  81. createConnection(model, rels[r], modelNodes[i].getData(), modelNodes[j].getData());
  82. }
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * Creates the model nodes for the given external nodes.
  89. *
  90. * @param model
  91. * the graph model.
  92. * @param nodes
  93. * the external nodes.
  94. */
  95. private void createModelNodes(Graph model, Object[] nodes) {
  96. for (int i = 0; i < nodes.length; i++) {
  97. createNode(model, nodes[i]);
  98. }
  99. }
  100. /*
  101. * (non-Javadoc)
  102. *
  103. * @see org.eclipse.zest.core.internal.graphmodel.IStylingGraphModelFactory#refresh(org.eclipse.zest.core.internal.graphmodel.GraphModel,
  104. * java.lang.Object)
  105. */
  106. public void refresh(Graph graph, Object element) {
  107. refresh(graph, element, false);
  108. }
  109. /*
  110. * (non-Javadoc)
  111. *
  112. * @see org.eclipse.zest.core.internal.graphmodel.IStylingGraphModelFactory#refresh(org.eclipse.zest.core.internal.graphmodel.GraphModel,
  113. * java.lang.Object, boolean)
  114. */
  115. public void refresh(Graph graph, Object element, boolean updateLabels) {
  116. // with this kind of graph, it is just as easy and cost-effective to
  117. // rebuild the whole thing.
  118. refreshGraph(graph);
  119. }
  120. private IGraphEntityRelationshipContentProvider getCastedContent() {
  121. return (IGraphEntityRelationshipContentProvider) getContentProvider();
  122. }
  123. }