001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package com.artenum.so6.dataflow.graph;
034:
035: import com.artenum.graph.interfaces.Cell;
036: import com.artenum.graph.interfaces.Connection;
037:
038: import com.artenum.so6.dataflow.ActionManager;
039: import com.artenum.so6.dataflow.StyleManager;
040: import com.artenum.so6.dataflow.graph.ui.SynchronizerUI;
041: import com.artenum.so6.dataflow.util.So6PopupMenu;
042:
043: import java.awt.Point;
044: import java.awt.Rectangle;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047:
048: import java.util.ArrayList;
049: import java.util.Hashtable;
050: import java.util.Iterator;
051: import java.util.Properties;
052:
053: import javax.swing.JMenuItem;
054:
055: /**
056: * @author seb
057: */
058: public class SynchronizerNode extends So6Node implements ActionListener {
059: private Hashtable connectionPoint;
060: private ArrayList availablePoints;
061: private ArrayList unavailablePoints;
062: public boolean ONE_CONNECTOR_BY_CONNECTION = false;
063: public So6PopupMenu popupMenu;
064:
065: public SynchronizerNode(Properties externalProps) {
066: super (externalProps);
067: connectionPoint = new Hashtable();
068: availablePoints = new ArrayList();
069: unavailablePoints = new ArrayList();
070: ui = new SynchronizerUI(this );
071: ui.setBounds(new Rectangle(ui.getPreferredSize()));
072:
073: So6PopupMenu popupMenu = new So6PopupMenu(new So6DragListener(
074: this , false));
075: ui.addMouseListener(popupMenu);
076: ui.addMouseMotionListener(popupMenu);
077:
078: JMenuItem cwc = new JMenuItem("Create workspace");
079: cwc.addActionListener(this );
080: popupMenu.add(cwc);
081: setSelectedBorderColor(StyleManager.getInstance()
082: .getWsUpToDateColor());
083: setSelectedBorderSize(1);
084: }
085:
086: public String getName() {
087: return getExternalProperties().getProperty("uri");
088: }
089:
090: public void buildSimpleConnectionAssignation() {
091: availablePoints.clear();
092: unavailablePoints.clear();
093:
094: Point[] pointList = ((SynchronizerUI) ui)
095: .getConnectionPointArray();
096:
097: for (int i = 0; i < pointList.length; i++) {
098: Point p = (Point) pointList[i].clone();
099: p.translate(ui.getBounds().getLocation().x, ui.getBounds()
100: .getLocation().y);
101: availablePoints.add(p);
102: }
103:
104: for (Iterator i = getConnections().iterator(); i.hasNext();) {
105: Point p = (Point) availablePoints.remove(0);
106: unavailablePoints.add(p);
107: connectionPoint.put(i.next(), p);
108: }
109: }
110:
111: public void computeConnectionAssignation() {
112: //buildSimpleConnectionAssignation();
113: availablePoints.clear();
114: unavailablePoints.clear();
115:
116: Point[] pointList = ((SynchronizerUI) ui)
117: .getConnectionPointArray();
118:
119: for (int i = 0; i < pointList.length; i++) {
120: Point p = (Point) pointList[i].clone();
121:
122: //p.translate(ui.getBounds().getLocation().x, ui.getBounds().getLocation().y);
123: availablePoints.add(p);
124: }
125:
126: double dist = -1;
127:
128: for (Iterator connections = getConnections().iterator(); connections
129: .hasNext();) {
130: Connection connection = (Connection) connections.next();
131: Point p1 = connection.getOtherCell(this )
132: .getConnectionPoint(connection);
133: Point selectedPoint = (Point) availablePoints.get(0);
134: Point p2 = selectedPoint;
135: dist = p1.distance(p2);
136:
137: for (Iterator pointIterator = availablePoints.iterator(); pointIterator
138: .hasNext();) {
139: p2 = (Point) pointIterator.next();
140:
141: if (dist > p1.distance(p2)) {
142: selectedPoint = p2;
143: dist = p1.distance(p2);
144: }
145: }
146:
147: connectionPoint.put(connection, selectedPoint);
148: availablePoints.remove(selectedPoint);
149: unavailablePoints.add(selectedPoint);
150: }
151: }
152:
153: public long getTicket() {
154: return Long.parseLong(getExternalProperties().getProperty(
155: "LastTicket"));
156: }
157:
158: public Point getConnectionPoint(Connection connection) {
159: if (!ONE_CONNECTOR_BY_CONNECTION) {
160: Cell connectionCell = connection.getOtherCell(this );
161: Point remoteCellPoint = ((ConnectionNode) connectionCell)
162: .getConnectionPoint(connection);
163: Point p = null;
164: Point currentPoint = null;
165: double dist = -1;
166: Point[] pointList = ((SynchronizerUI) ui)
167: .getConnectionPointArray();
168:
169: return checkDistance(remoteCellPoint, pointList);
170: } else {
171: Point p = (Point) connectionPoint.get(connection);
172:
173: if (p == null) {
174: return new Point(10, 10);
175: }
176:
177: return p;
178: }
179: }
180:
181: private Point checkDistance(Point remoteCell, Point[] pointList) {
182: double newDist;
183: double smallestDist = -1;
184: Point selectedPoint = null;
185:
186: for (int i = 0; i < pointList.length; i++) {
187: newDist = remoteCell.distance(pointList[i]);
188:
189: if (selectedPoint == null) {
190: selectedPoint = pointList[i];
191: smallestDist = newDist;
192: } else if (smallestDist > newDist) {
193: smallestDist = newDist;
194: selectedPoint = pointList[i];
195: }
196: }
197:
198: return selectedPoint;
199: }
200:
201: public void updateNode() {
202: if (ONE_CONNECTOR_BY_CONNECTION) {
203: computeConnectionAssignation();
204:
205: //buildSimpleConnectionAssignation();
206: }
207: }
208:
209: public void actionPerformed(ActionEvent e) {
210: ActionManager.getInstance().connectWorkspace(
211: getExternalProperties().getProperty("uri"));
212: }
213: }
|