[Jiemamy-notify:2407] commit [3407] [ECL-54] 自動レイアウトの際にIProgressMonitorDialogを表示し、UIスレッドとは別スレッドで処理を行うよう修正した。

アーカイブの一覧に戻る

svnno****@sourc***** svnno****@sourc*****
2009年 5月 20日 (水) 16:22:13 JST


Revision: 3407
          http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=3407
Author:   shin1
Date:     2009-05-20 16:22:13 +0900 (Wed, 20 May 2009)

Log Message:
-----------
[ECL-54]自動レイアウトの際にIProgressMonitorDialogを表示し、UIスレッドとは別スレッドで処理を行うよう修正した。

Modified Paths:
--------------
    hestia/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/action/AutoLayoutAction.java


-------------- next part --------------
Modified: hestia/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/action/AutoLayoutAction.java
===================================================================
--- hestia/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/action/AutoLayoutAction.java	2009-05-18 13:35:51 UTC (rev 3406)
+++ hestia/trunk/org.jiemamy.eclipse.ui/src/main/java/org/jiemamy/eclipse/action/AutoLayoutAction.java	2009-05-20 07:22:13 UTC (rev 3407)
@@ -18,12 +18,14 @@
  */
 package org.jiemamy.eclipse.action;
 
+import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.draw2d.geometry.Insets;
 import org.eclipse.draw2d.graph.DirectedGraph;
 import org.eclipse.draw2d.graph.DirectedGraphLayout;
@@ -35,6 +37,9 @@
 import org.eclipse.gef.GraphicalViewer;
 import org.eclipse.gef.commands.Command;
 import org.eclipse.gef.commands.CompoundCommand;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.ui.PlatformUI;
 
 import org.jiemamy.Migration;
 import org.jiemamy.eclipse.editor.editpart.diagram.AbstractJmNodeEditPart;
@@ -80,67 +85,15 @@
 	
 	@Override
 	public void run() {
-		RootEditPart rootEditPart = (RootEditPart) getViewer().getContents();
-		RootModel rootModel = rootEditPart.getModel();
-		CompoundCommand commands = new CompoundCommand();
-		
-		@SuppressWarnings("unchecked")
-		// TODO キャスト安全性の根拠提示
-		List<EditPart> editParts = rootEditPart.getChildren();
-		
-		@SuppressWarnings("unchecked")
-		// TODO キャスト安全性の根拠提示
-		List<Node> graphNodes = new NodeList();
-		
-		@SuppressWarnings("unchecked")
-		// TODO キャスト安全性の根拠提示
-		List<Edge> graphEdges = new EdgeList();
-		
-		// assemble nodes
-		for (EditPart obj : editParts) {
-			if (obj instanceof AbstractJmNodeEditPart) {
-				AbstractJmNodeEditPart editPart = (AbstractJmNodeEditPart) obj;
-				NodeAdapter model = editPart.getModel();
-				EntityNode node = new EntityNode();
-				node.model = model;
-				node.width = editPart.getFigure().getSize().width;
-				node.height = editPart.getFigure().getSize().height;
-				graphNodes.add(node);
-			}
+		ProgressMonitorDialog dialog =
+				new ProgressMonitorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
+		try {
+			dialog.run(/*fork*/true, /*cancelable*/false, new Operation(getViewer()));
+		} catch (InvocationTargetException e) {
+			e.printStackTrace();
+		} catch (InterruptedException e) {
+			e.printStackTrace();
 		}
-		// assemble edges
-		for (Object obj : graphNodes) {
-			EntityNode node = (EntityNode) obj;
-			Collection<ConnectionAdapter> conns = node.model.getSourceConnections();
-			CONN_LOOP: for (ConnectionAdapter conn : conns) {
-				if (conn.isSelfConnection()) {
-					continue;
-				}
-				
-				// skip if the connection already added
-				for (Object obj2 : graphEdges) {
-					ConnectionEdge edge = (ConnectionEdge) obj2;
-					if (edge.model == conn) {
-						continue CONN_LOOP;
-					}
-				}
-				Node source = getNode(graphNodes, conn.getSource());
-				Node target = getNode(graphNodes, conn.getTarget());
-				if (source != null && target != null) {
-					graphEdges.add(new ConnectionEdge(source, target, conn));
-				}
-			}
-		}
-		DirectedGraph graph = new DirectedGraph();
-		graph.setDefaultPadding(new Insets(PADDING));
-		graph.nodes = (NodeList) graphNodes;
-		graph.edges = (EdgeList) graphEdges;
-		new DirectedGraphLayout().visit(graph);
-		for (Object obj : graph.nodes) {
-			EntityNode node = (EntityNode) obj;
-			commands.add(new LayoutCommand(rootModel, Migration.DIAGRAM_INDEX, node.model, node.x, node.y));
-		}
-		getViewer().getEditDomain().getCommandStack().execute(commands);
 	}
 	
 
@@ -243,4 +196,110 @@
 			PresentationUtil.setBoundary(presentation, target, new JmRectangle(oldX, oldY, -1, -1));
 		}
 	}
+	
+	private static class Operation implements IRunnableWithProgress {
+		
+		final GraphicalViewer viewer;
+		
+
+		public Operation(GraphicalViewer viewer) {
+			super();
+			this.viewer = viewer;
+		}
+		
+		public void run(IProgressMonitor monitor) {
+			
+			int totalWork = 4; //editParts.size() + graphNodes.size() + graphNodes.size(); 
+			int worked = 0;
+			monitor.beginTask(Messages.AutoLayoutAction_name, totalWork);
+			
+			@SuppressWarnings("unchecked")
+			// TODO キャスト安全性の根拠提示
+			final List<EditPart> editParts = ((RootEditPart) viewer.getContents()).getChildren();
+			
+			@SuppressWarnings("unchecked")
+			// TODO キャスト安全性の根拠提示
+			final List<Node> graphNodes = new NodeList();
+			
+			@SuppressWarnings("unchecked")
+			// TODO キャスト安全性の根拠提示
+			final List<Edge> graphEdges = new EdgeList();
+			
+			CompoundCommand commands = new CompoundCommand();
+			RootModel rootModel = ((RootEditPart) viewer.getContents()).getModel();
+			
+			// assemble nodes
+			monitor.setTaskName(Messages.AutoLayoutAction_name + " - assemble nodes.");
+			assembleNodes(editParts, graphNodes);
+			monitor.worked(++worked);
+			
+			// assemble edges
+			monitor.setTaskName(Messages.AutoLayoutAction_name + " - assemble edges.");
+			assembleEdges(graphNodes, graphEdges);
+			monitor.worked(++worked);
+			
+			// amnalyze graph
+			monitor.setTaskName(Messages.AutoLayoutAction_name + " - graph.");
+			analyzeGraph(graphNodes, graphEdges, commands, rootModel);
+			monitor.worked(++worked);
+			
+			monitor.setTaskName(Messages.AutoLayoutAction_name + " - execute command stack.");
+			viewer.getEditDomain().getCommandStack().execute(commands);
+			monitor.worked(++worked);
+			
+			monitor.done();
+		}
+		
+		private void analyzeGraph(final List<Node> graphNodes, final List<Edge> graphEdges, CompoundCommand commands,
+				RootModel rootModel) {
+			DirectedGraph graph = new DirectedGraph();
+			graph.setDefaultPadding(new Insets(PADDING));
+			graph.nodes = (NodeList) graphNodes;
+			graph.edges = (EdgeList) graphEdges;
+			new DirectedGraphLayout().visit(graph);
+			for (Object obj : graph.nodes) {
+				EntityNode node = (EntityNode) obj;
+				commands.add(new LayoutCommand(rootModel, Migration.DIAGRAM_INDEX, node.model, node.x, node.y));
+			}
+		}
+		
+		private void assembleEdges(final List<Node> graphNodes, final List<Edge> graphEdges) {
+			for (Object obj : graphNodes) {
+				EntityNode node = (EntityNode) obj;
+				Collection<ConnectionAdapter> conns = node.model.getSourceConnections();
+				CONN_LOOP: for (ConnectionAdapter conn : conns) {
+					if (conn.isSelfConnection()) {
+						continue;
+					}
+					
+					// skip if the connection already added
+					for (Object obj2 : graphEdges) {
+						ConnectionEdge edge = (ConnectionEdge) obj2;
+						if (edge.model == conn) {
+							continue CONN_LOOP;
+						}
+					}
+					Node source = getNode(graphNodes, conn.getSource());
+					Node target = getNode(graphNodes, conn.getTarget());
+					if (source != null && target != null) {
+						graphEdges.add(new ConnectionEdge(source, target, conn));
+					}
+				}
+			}
+		}
+		
+		private void assembleNodes(final List<EditPart> editParts, final List<Node> graphNodes) {
+			for (EditPart obj : editParts) {
+				if (obj instanceof AbstractJmNodeEditPart) {
+					AbstractJmNodeEditPart editPart = (AbstractJmNodeEditPart) obj;
+					NodeAdapter model = editPart.getModel();
+					EntityNode node = new EntityNode();
+					node.model = model;
+					node.width = editPart.getFigure().getSize().width;
+					node.height = editPart.getFigure().getSize().height;
+					graphNodes.add(node);
+				}
+			}
+		}
+	}
 }



Jiemamy-notify メーリングリストの案内
アーカイブの一覧に戻る