public void TestMoleUnitChange() { MainPage mp1 = new MainPage(); Workspace w = mp1.GetLogicalWorkspace(); // Add a new stream with a table to the workspace ChemicalStream cs1 = new ChemicalStream(1); cs1.PropertiesTable = new StreamPropertiesTable(cs1); w.AddStream(cs1); // The DrawingCanvas control should have added a stream control ChemProV.PFD.Streams.StreamControl streamControl = mp1.WorkspaceReference.DrawingCanvasReference.GetStreamControl(cs1); Assert.IsNotNull(streamControl, "DrawingCanvas control did not correctly create a " + "stream control for a stream that was added to the workspace."); // From the stream control we can get the properties window control UI.StreamTableControl props = streamControl.Table; Assert.IsNotNull(props, "Stream control had a null table control."); // Add a default data row with label "M1" if it's not already there if (0 == cs1.PropertiesTable.RowCount) { ChemicalStreamData csd = cs1.PropertiesTable.AddNewRow() as ChemicalStreamData; csd.SelectedCompound = "Overall"; csd.Label = "M1"; csd.UserHasRenamed = false; } // Find the text box in the properties window with the text "M1" TextBox tbLabel = props.GetControl(cs1.PropertiesTable.Rows[0], "Label") as TextBox; Assert.IsNotNull(tbLabel, "Could not find text box for row label. If the code has changed " + "such that there is no longer a default row in chemical streams tables or the default " + "row has different units, then this test needs to be altered."); Assert.IsTrue(tbLabel.Text.Equals("M1"), "Default label was not M1"); // Find the combo box for the units ComboBox cbUnits = props.GetControl(cs1.PropertiesTable.Rows[0], "SelectedUnits") as ComboBox; Assert.IsNotNull(cbUnits, "Could not find combo box control for selected units"); // Select mole %, which should change the label from M1 to N1 cbUnits.SelectedItem = "mol %"; // Verify that the label changed to "N1" Assert.IsTrue(tbLabel.Text.Equals("N1"), "Test Failed: After unit change, the label " + "did not change from M1 to N1"); // What would be nice to add to this test in the future: // Change the text in tbLabel which simulates the user manually renaming the row. Change it to // something like "nn1". Then change the units again to something like fractions, which would // normally change 'n' to 'x', but shouldn't after a manual rename. }
public IUndoRedoAction Execute(Workspace sender) { if (m_toAdd is AbstractProcessUnit) { sender.AddProcessUnit(m_toAdd as AbstractProcessUnit); return(new RemoveProcessUnit(m_toAdd as AbstractProcessUnit)); } else if (m_toAdd is AbstractStream) { sender.AddStream(m_toAdd as AbstractStream); return(new RemoveStream(m_toAdd as AbstractStream)); } else { sender.StickyNotes.Add(m_toAdd as ChemProV.Logic.StickyNote); return(new RemoveStickyNote(m_toAdd as ChemProV.Logic.StickyNote)); } }
public void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // If the canvas reference is null then we ignore mouse events if (null == m_canvas) { return; } // Here's where we need to flip over to the MovingStreamEndpoint state. We start by // finding out if we clicked on a process unit or not Point location = e.GetPosition(m_canvas); // Clear the border if we'd previously set one if (null != m_puWithAlteredBorderColor) { m_puWithAlteredBorderColor.SetBorderColor(ProcessUnitBorderColor.NoBorder); m_puWithAlteredBorderColor = null; } // Remove the floating icon m_canvas.RemoveChild(m_sourcePlacementIcon); m_sourcePlacementIcon = null; // Get a reference to the workspace Workspace ws = m_canvas.GetWorkspace(); // See if we clicked on a valid destination object ProcessUnitControl gpu = m_canvas.GetChildAt(location, m_sourcePlacementIcon) as ProcessUnitControl; if (null == gpu) { // This means that we clicked in an area where there's no process unit that we have // to attach to. m_stream.SourceLocation = new MathCore.Vector(location.X, location.Y); m_stream.DestinationLocation = m_stream.SourceLocation + (new MathCore.Vector(0, 20.0)); ws.AddStream(m_stream); // We need an undo to remove the stream that we just added. Note that we're going to switch // over to a different state below and that creates and undo as well. This is fine since // it allows you to undo the destination placement and stream creation separately. ws.AddUndo(new UndoRedoCollection("Undo stream creation", new Logic.Undos.RemoveStream(m_stream))); } else if (gpu.ProcessUnit.CanAcceptOutgoingStream(m_stream)) { // Setup the stream and add it to the workspace m_stream.SourceLocation = new MathCore.Vector(location.X, location.Y); m_stream.DestinationLocation = m_stream.SourceLocation + (new MathCore.Vector(0, 35.0)); m_stream.Source = gpu.ProcessUnit; ws.AddStream(m_stream); // Attach the outgoing stream to the process unit after we've added the stream to // the workspace gpu.ProcessUnit.AttachOutgoingStream(m_stream); // We've linked up the stream and added it to the workspace. Now create an undo that // does the opposite ws.AddUndo(new UndoRedoCollection("Undo stream creation", new Logic.Undos.DetachOutgoingStream(gpu.ProcessUnit, m_stream), new Logic.Undos.RemoveStream(m_stream))); } else { // This means we clicked on a process unit that we cannot connect to. In this case // we want to cancel everything. This is easy since we haven't modified the workspace, // so all we have to do it switch back to select mode. m_palette.SwitchToSelect(); return; } // Get the stream control that the DrawingCanvas should have added PFD.Streams.StreamControl streamControl = m_canvas.GetStreamControl(m_stream); // Tell it to hide the table streamControl.HideTable(); // Set references to null to indicate that this state should no longer process mouse input // messages after this. We are finishing up right here. DrawingCanvas c = m_canvas; m_canvas = null; m_stream = null; // Switch back to select mode m_palette.SwitchToSelect(); // Flip over to the state where we position the stream destination c.CurrentState = new MovingStreamEndpoint(streamControl.DestinationDragIcon, c, true); }
/// <summary> /// Handles mouse-left-button-down event for placing a heat exchanger with utility /// </summary> public void MLBD_HEWU(object sender, MouseButtonEventArgs e) { // Start by getting the mouse position Point pos = e.GetPosition(m_canvas); // See if we have a DraggableStreamEndpoint where we clicked DraggableStreamEndpoint endpoint = m_canvas.GetChildAt(pos, m_placementIcon) as DraggableStreamEndpoint; // Remove the placement icon and then set it to null to indicate state completion m_canvas.RemoveChild(m_placementIcon); m_placementIcon = null; // If there's not an endpoint: // 1. Create and attach an incoming heat stream. Position the source endpoint just // below the process unit // 2. Create an undo that will remove both // 3. Switch to the appropriate state for moving the source endpoint if (null == endpoint) { // Create the actual process unit AbstractProcessUnit apu = (AbstractProcessUnit)Activator.CreateInstance( m_type, AbstractProcessUnit.GetNextUID()); apu.Location = new MathCore.Vector(pos.X, pos.Y); HeatStream s = new HeatStream(); apu.AttachIncomingStream(s); s.Destination = apu; s.PropertiesTable = new StreamPropertiesTable(s); // Position the source of the heat stream. It's above the process unit by default, unless // that would make it go off the screen if (apu.Location.Y < 150.0) { s.SourceLocation = new MathCore.Vector( apu.Location.X - 10.0, apu.Location.Y + 80.0); s.PropertiesTable.Location = new MathCore.Vector( apu.Location.X + 10.0, apu.Location.Y + 140.0); } else { s.SourceLocation = new MathCore.Vector( apu.Location.X - 10.0, apu.Location.Y - 80.0); s.PropertiesTable.Location = new MathCore.Vector( apu.Location.X + 10.0, apu.Location.Y - 100.0); } // Add the stream and the process unit to the workspace. Event handlers will update // the UI appropriately. m_workspace.AddProcessUnit(apu); m_workspace.AddStream(s); m_workspace.AddUndo(new UndoRedoCollection("Undo creation of heat exchanger with utility", new Logic.Undos.DetachIncomingStream(apu, s), new Logic.Undos.SetStreamDestination(s, null, apu, s.DestinationLocation), new Logic.Undos.RemoveStream(s), new Logic.Undos.RemoveProcessUnit(apu))); // Select nothing on the canvas m_canvas.SelectedElement = null; m_palette.SwitchToSelect(); return; } // Otherwise, if we HAVE clicked on an endpoint, just deny it by doing nothing }
public void TestSaveLoad() { int i; Workspace ws1 = new Workspace(); Random rand = new Random(); // Add a random number of process units List <int> puIDs = new List <int>(); int numPU = rand.Next(25); for (i = 0; i < numPU; i++) { AbstractProcessUnit pu = new Mixer(); ws1.AddProcessUnit(pu); puIDs.Add(pu.Id); } // Add a random number of chemical streams int numStreams = rand.Next(10); for (i = 0; i < numStreams; i++) { AbstractStream stream = new ChemicalStream(AbstractStream.GetNextUID()); ws1.AddStream(stream); // Don't forget that the properties table needs to be created separately stream.PropertiesTable = new StreamPropertiesTable(stream); // 50% chance of connecting a destination (attempting a connect that is) if (0 == (rand.Next() % 2)) { AbstractProcessUnit pu = ws1.ProcessUnits[rand.Next(ws1.ProcessUnitCount)]; if (pu.AttachIncomingStream(stream)) { stream.Destination = pu; } } } // Save the workspace to a memory stream MemoryStream ms = new MemoryStream(); ws1.Save(ms, "TEST_VersionNA"); // Load to a new workspace Workspace ws2 = new Workspace(); ws2.Load(ms); // Make sure the number of process units and streams match Assert.IsTrue(numPU == ws2.ProcessUnitCount, "Number of process units between saved document (" + numPU.ToString() + ") and loaded document (" + ws2.ProcessUnitCount.ToString() + ") do not match"); Assert.IsTrue(numStreams == ws2.Streams.Count, "Number of streams between saved document (" + numStreams.ToString() + ") and loaded document (" + ws2.Streams.Count.ToString() + ") do not match"); // Test that the incoming/outgoing streams for process units match foreach (AbstractProcessUnit pu1 in ws1.ProcessUnits) { AbstractProcessUnit pu2 = ws2.GetProcessUnit(pu1.Id); Assert.IsNotNull(pu2, "Process unit with ID=" + pu1.Id.ToString() + " from workspace 1 (saved) was not " + "found in workspace 2 (loaded)."); // For now just compare outoging stream count Assert.IsTrue(pu1.OutgoingStreamCount == pu2.OutgoingStreamCount, "Mis-match outgoing stream count"); } }