Browse Source

Flex window update

* Overview update automatically
* Better Selection
Tom Troppmann 4 years ago
parent
commit
b86994fd92

+ 1 - 1
src/algorithm/objectiveFunction/TopologieObjectiveFunction.java

@@ -152,12 +152,12 @@ public class TopologieObjectiveFunction {
 				continue;
 			}
 			
-			//k-edge-conneted
 			
 			double avgShortestPath = GraphMetrics.averageShortestDistance(G.V,  G.E);
 			double squached = squash(avgShortestPath, k_avg_shortest_path);
 			//System.out.println("AVG:" + avgShortestPath + "  squached:" + squached);
 			
+			//k-edge-conneted
 			int maximumK = G.V.length - 1;
 			if(maximumK != 0) {
 				int k = GraphMetrics.minimumCut(G.V, G.E);				

+ 12 - 2
src/ui/controller/FlexManager.java

@@ -97,8 +97,14 @@ public class FlexManager {
 	public boolean isAFlexInUseOfHolonElement(HolonElement ele) {
 		return ele.flexList.stream().filter(flex -> this.accessFlexMap.containsKey(flex)).anyMatch(flex -> (this.accessFlexMap.get(flex).getState() == FlexState.IN_USE));
 	}
-	
-	
+	/**
+	 * Or Return null
+	 * @param flex
+	 * @return
+	 */
+	public FlexWrapper getFlexWrapperFromFlexibility(Flexibility flex) {
+		return accessFlexMap.get(flex);
+	}
 	
 	public static enum FlexState{
 			IN_USE, ON_COOLDOWN, OFFERED, NOT_OFFERED, UNAVAILABLE
@@ -143,6 +149,10 @@ public class FlexManager {
 					!otherFlexInUseOrOnCooldown(); //No other flex of this ele in use
 		}
 		private boolean otherFlexInUseOrOnCooldown() {
+			System.out.println("Error " + this);
+			if(this.getFlex() == null )System.out.println("Error this.getFlex()");
+			if(this.getFlex().getElement() == null )System.out.println("Error this.getFlex().getElement()");
+			if(accessOtherFlex.get(this.getFlex().getElement()) == null) return false;
 			return accessOtherFlex.get(this.getFlex().getElement()).stream().anyMatch(flexWrapper -> flexWrapper != this && (flexWrapper.getState() == FlexState.IN_USE || flexWrapper.getState() == FlexState.ON_COOLDOWN));
 		}
 		public boolean order() {

+ 111 - 79
src/ui/view/FlexWindow.java

@@ -7,6 +7,7 @@ import java.awt.FlowLayout;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
+import java.awt.Rectangle;
 import java.awt.Dialog.ModalityType;
 import java.awt.Dimension;
 import java.awt.event.ItemEvent;
@@ -87,6 +88,7 @@ public class FlexWindow extends JFrame {
 	private DefaultTreeModel treeModel;
 	private FlexManager flexmanager;
 	
+	//FlexToFlexWrapper()
 	
 	
 	
@@ -103,7 +105,7 @@ public class FlexWindow extends JFrame {
 		    	isClosed = true;
 		    }
 		});
-		selectAll();
+		updateSelectedPanel();
 		//this.pack();
 	}
 
@@ -119,7 +121,7 @@ public class FlexWindow extends JFrame {
 		createNothingSelectedPanel();
 		createSelectedPanel();
 		createUsageViewPanel();
-		contentPanel.addTab("Settings", nothingSelectedPanel);
+		contentPanel.addTab("Overview", nothingSelectedPanel);
 		contentPanel.addTab("Order", usageViewPanel);
 		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
 		this.setContentPane(contentPanel);
@@ -131,11 +133,8 @@ public class FlexWindow extends JFrame {
 		JMenuBar menuBar = new JMenuBar();
 		JMenu canvas = new JMenu("Canvas");
 		menuBar.add(canvas);
-		JMenuItem selectAllMenuItem = new JMenuItem("Select All");
-		selectAllMenuItem.addActionListener(clicked -> selectAll());
-		canvas.add(selectAllMenuItem);
 		JMenuItem updateMenuItem = new JMenuItem("Update");
-		updateMenuItem.addActionListener(clicked -> selectedCpsObjectsChanged());
+		updateMenuItem.addActionListener(clicked -> updateSelectedPanel());
 		canvas.add(updateMenuItem);
 		JMenu flex = new JMenu("Flex");
 		menuBar.add(flex);
@@ -150,11 +149,6 @@ public class FlexWindow extends JFrame {
 	}
 	
 	
-	private void selectAll() {
-		model.getSelectedCpsObjects().clear();
-		model.getSelectedCpsObjects().addAll(model.getObjectsOnCanvas());
-		selectedCpsObjectsChanged();
-	}
 
 	
 	private void createUsageViewPanel() {
@@ -290,7 +284,8 @@ public class FlexWindow extends JFrame {
 
 
 
-	public void updateFlexOrderMenu() {
+	public void update() {
+		updateSelectedPanel();
 		createUsageViewPanel();
 		contentPanel.setComponentAt(contentPanel.indexOfTab("Order"), usageViewPanel);
 		contentPanel.revalidate();
@@ -304,7 +299,79 @@ public class FlexWindow extends JFrame {
 		listOfAllSelectedHolonObjects = new DefaultMutableTreeNode("HolonObjects");
 		treeModel = new DefaultTreeModel(listOfAllSelectedHolonObjects);
 		stateTree = new JTree(treeModel);
-		stateTree.setComponentPopupMenu(new PopUpDemo(stateTree, treeModel));
+		stateTree.addMouseListener ( new MouseAdapter ()
+		    {
+		        public void mousePressed ( MouseEvent e )
+		        {
+		            if ( SwingUtilities.isRightMouseButton ( e ) )
+		            {
+		            	
+		            	
+		                TreePath pathUnderCursor = stateTree.getPathForLocation ( e.getX (), e.getY () );
+		                Rectangle pathBounds = stateTree.getUI ().getPathBounds ( stateTree, pathUnderCursor );
+		                if ( pathBounds != null && pathBounds.contains ( e.getX (), e.getY () ) )
+		                {
+		                	TreePath[] selectedPaths = stateTree.getSelectionPaths();
+			   	        	if(selectedPaths == null) {
+			   	        		stateTree.addSelectionPath(pathUnderCursor);
+			   	        	}else {
+			   	        		boolean isInselectedPaths = false;
+			   	        		for (TreePath path : stateTree.getSelectionPaths()) {
+			   	        			if(path.equals(pathUnderCursor)) {
+			   	        				isInselectedPaths = true;
+			   	        			}
+			   	        		}
+			   	        		if(!isInselectedPaths) {
+			   	        			stateTree.clearSelection();
+			   	        			stateTree.addSelectionPath(pathUnderCursor);
+			   	        		}
+			   	        	}
+		                    JPopupMenu menu = new JPopupMenu ();
+		                    JMenuItem priorityItem = new JMenuItem("EditPriorities");
+			         	    JMenuItem flexItem = new JMenuItem("AddFlex");
+			         	    
+							priorityItem.addActionListener(clicked -> {
+								Priority prio = null;
+								if (stateTree.getSelectionPaths() != null)
+									for (TreePath path : stateTree.getSelectionPaths()) {
+										Object treeNodeUserObject = ((DefaultMutableTreeNode) path.getLastPathComponent())
+												.getUserObject();
+										if (treeNodeUserObject instanceof ElementInfo) {
+											if (prio == null)
+												prio = (Priority) JOptionPane.showInputDialog(stateTree,
+														"Select the Priority:", "Priority?", JOptionPane.OK_OPTION,
+														new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)),
+														Priority.values(), "");
+											if (prio == null)
+												break;
+											ElementInfo eleInfo = (ElementInfo) treeNodeUserObject;
+											eleInfo.ele.setPriority(prio);
+											treeModel.reload((DefaultMutableTreeNode) path.getLastPathComponent());
+										}
+									}
+							});
+							flexItem.addActionListener(clicked -> {
+								TreePath path = stateTree.getSelectionPath();
+								if (path == null)
+									return;
+								Object treeNodeUserObject = ((DefaultMutableTreeNode) path.getLastPathComponent())
+										.getUserObject();
+								if (!(treeNodeUserObject instanceof ElementInfo))
+									return;
+								createAddDialog(((ElementInfo) treeNodeUserObject).ele);
+			   	        });
+			         	    
+		                    menu.add ( priorityItem );
+		                    menu.add ( flexItem );
+		                    menu.show ( stateTree, pathBounds.x, pathBounds.y + pathBounds.height );
+		                }else {
+		                	JPopupMenu menu = new JPopupMenu ();
+		                    menu.add ( new JMenuItem ( "Other Test" ) );
+		                    menu.show ( stateTree, e.getX(), e.getY() );
+		                }
+		            }
+		        }
+		    } );
 		selectedPanel = new JPanel(new BorderLayout());
 		selectedPanel.add(new JScrollPane(stateTree));
 	}
@@ -313,7 +380,7 @@ public class FlexWindow extends JFrame {
 	private void createNothingSelectedPanel() {
 		nothingSelectedPanel = new JPanel();
 		nothingSelectedPanel.setLayout(new BoxLayout(nothingSelectedPanel, BoxLayout.PAGE_AXIS));
-		JLabel nothingSelectedTextLabel = new JLabel("No HolonObject selected.");
+		JLabel nothingSelectedTextLabel = new JLabel("No HolonObject exist.");
 		nothingSelectedTextLabel.setForeground(Color.gray);
 		nothingSelectedTextLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
 		nothingSelectedPanel.add(Box.createVerticalGlue());
@@ -321,18 +388,13 @@ public class FlexWindow extends JFrame {
 		nothingSelectedPanel.add(Box.createVerticalGlue());
 	}
 	
-	public void selectedCpsObjectsChanged() {
-		updateFlexOrderMenu();
-		if(model.getSelectedCpsObjects().isEmpty()) {
-			contentPanel.setComponentAt(contentPanel.indexOfTab("Settings"), nothingSelectedPanel);
-			contentPanel.revalidate();
-			//this.revalidate();
-			return;
-		}
+	public void updateSelectedPanel() {
+		//UpdateFlexManager
+		flexmanager = controller.getSimManager().getActualFlexManager();
 		
 		listOfAllSelectedHolonObjects.removeAllChildren();
 		//Init with HolonObjects
-		for(AbstractCanvasObject aCps: model.getSelectedCpsObjects()) {
+		for(AbstractCanvasObject aCps: model.getObjectsOnCanvas()) {
 			DefaultMutableTreeNode newObjectChild = new DefaultMutableTreeNode(aCps.getName() + " ID:" + aCps.getId());
 			if(aCps instanceof HolonObject) expandTreeHolonObject((HolonObject)aCps, newObjectChild);
 			if(aCps instanceof GroupNode)expandTreeUpperNode((GroupNode)aCps, newObjectChild);
@@ -341,15 +403,6 @@ public class FlexWindow extends JFrame {
 		treeModel.nodeStructureChanged(listOfAllSelectedHolonObjects);
 	
 		stateTree.revalidate();
-//		stateTree.addMouseListener(new MouseAdapter() {
-//
-//		    @Override
-//		    public void mousePressed(MouseEvent e) {
-//		    	System.out.println("MouseEvent");
-//		    	if (!SwingUtilities.isRightMouseButton(e)) return;
-//		    	
-//		    }
-//		});
 		expandAll(stateTree);
 		selectedPanel.revalidate();
 		contentPanel.setComponentAt(contentPanel.indexOfTab("Settings"), selectedPanel);
@@ -389,7 +442,13 @@ public class FlexWindow extends JFrame {
 	}
 	private void expandTreeFlex(HolonElement hElement, DefaultMutableTreeNode root) {
 		for(Flexibility flex: hElement.flexList) {
-			DefaultMutableTreeNode newChild = new DefaultMutableTreeNode("<html>"+"Flex: "+ "<b>" + flex.name+ "</b>" + "</html>");
+			FlexWrapper flexWrapper = this.flexmanager.getFlexWrapperFromFlexibility(flex);
+			String flexState = "";
+			if(flexWrapper != null) {
+				Color color = this.FlexStateToColor(flexWrapper.getState());
+				flexState = "<font bgcolor='#" + Integer.toHexString(color.getRGB()).substring(2) + "'>" + flexWrapper.getState().name() + "</font>";
+			}
+			DefaultMutableTreeNode newChild = new DefaultMutableTreeNode("<html>"+ flexState + " <b>" + flex.name+ "</b>" + "</html>");
 			root.add(newChild);
 		}
 	}
@@ -413,10 +472,9 @@ public class FlexWindow extends JFrame {
 		
 		Flexibility toDeleteFlex =(Flexibility) JOptionPane.showInputDialog(this, "Select to Delete Flexibility:", "Flexibility?",  JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , allFlexes, "");
 		if(toDeleteFlex != null) {
-			System.out.println("ToDelete:" + toDeleteFlex);
 			toDeleteFlex.getElement().flexList.remove(toDeleteFlex);
 			controller.getSimManager().calculateStateForTimeStep(model.getCurIteration(), true);
-			selectedCpsObjectsChanged();
+			updateSelectedPanel();
 		}
 	}
 	
@@ -476,7 +534,6 @@ public class FlexWindow extends JFrame {
 		
 		holonObjectSelector.addItemListener(aListener -> {
 			if(aListener.getStateChange() == ItemEvent.SELECTED) {
-				System.out.println("Selected");
 				DefaultComboBoxModel<HolonElement> newComboBoxModelElements = new DefaultComboBoxModel<HolonElement>( ((HolonObject) aListener.getItem()).getElements().stream().toArray(size -> new HolonElement[size]));
 				holonElementSelector.setModel(newComboBoxModelElements);
 			}
@@ -639,8 +696,9 @@ public class FlexWindow extends JFrame {
 			
 			
 			//if(!model.getSelectedCpsObjects().contains(holonObjectSelector.getSelectedItem()))model.getSelectedCpsObjects().add((AbstractCpsObject)holonObjectSelector.getSelectedItem());
-			controller.getSimManager().calculateStateForTimeStep(model.getCurIteration(), false);
-			selectedCpsObjectsChanged();
+			controller.getSimManager().calculateStateForTimeStep(model.getCurIteration(), true);
+			
+			update();
 			addDialog.dispose();
 		});
 		buttonPanel.add(createFlexButton);
@@ -657,46 +715,6 @@ public class FlexWindow extends JFrame {
 		addDialog.setVisible(true);
 	}
 	
-	class PopUpDemo extends JPopupMenu {
-	    JMenuItem priorityItem = new JMenuItem("EditPriorities");
-	    JMenuItem flexItem = new JMenuItem("AddFlex");
-	    public PopUpDemo(JTree jTree, DefaultTreeModel model) {
-	        priorityItem.addActionListener( clicked -> {
-	        	TreePath[] paths = jTree.getSelectionPaths();
-	        	if(paths == null) {
-	        		jTree.setSelectionInterval(0, jTree.getRowCount());
-	        		paths = jTree.getSelectionPaths();
-	        	}
-	        	Priority prio = null;
-                for (TreePath path : paths) {
-                    Object treeNodeUserObject = ((DefaultMutableTreeNode)path.getLastPathComponent()).getUserObject();
-                    if(treeNodeUserObject instanceof ElementInfo)
-                    {	
-                    	if(prio == null) prio = makePriorityDialog();
-                    	if(prio == null) break;
-                    	ElementInfo eleInfo = (ElementInfo)treeNodeUserObject;
-                    	eleInfo.ele.setPriority(prio);
-                    	model.reload((DefaultMutableTreeNode)path.getLastPathComponent());
-                    }
-                }
-	        });
-	        flexItem.addActionListener( clicked -> {
-	        	TreePath path  = jTree.getSelectionPath();
-	        	if(path == null) return;
-	        	Object treeNodeUserObject = ((DefaultMutableTreeNode)path.getLastPathComponent()).getUserObject();
-	        	if(!(treeNodeUserObject instanceof ElementInfo)) return;
-	        	createAddDialog(((ElementInfo)treeNodeUserObject).ele);
-	        });
-	        add(priorityItem);
-	        add(flexItem);
-	        
-	        
-	        
-	    }
-		private Priority makePriorityDialog() {
-			return (Priority) JOptionPane.showInputDialog(this, "Select the Priority:", "Priority?",  JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , Priority.values(), "");
-		}
-	}
 	
 	class ElementInfo {
 		HolonElement ele;
@@ -709,6 +727,20 @@ public class FlexWindow extends JFrame {
 		}
 	}
 	
-	
+	public Color FlexStateToColor(FlexState state) {
+		switch(state) {
+		case IN_USE:
+			return new Color(173,247,182);
+		case NOT_OFFERED:
+			return new Color(252,245,199);
+		case OFFERED:
+			return new Color(160,206,217);
+		case ON_COOLDOWN:
+			return new Color(255,238,147);
+		case UNAVAILABLE:
+		default:
+			return new Color(255,192,159);
+		}
+	}
 	
 }

+ 1 - 1
src/ui/view/GUI.java

@@ -2556,7 +2556,7 @@ public class GUI implements CategoryListener {
 		}
 		//update open Flex
 		for(FlexWindow out : flexList) {
-			out.updateFlexOrderMenu();
+			out.update();
 		}
 	}