FlexWindow.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. package ui.view.additional;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridBagConstraints;
  7. import java.awt.GridBagLayout;
  8. import java.awt.Insets;
  9. import java.awt.Rectangle;
  10. import java.awt.Dialog.ModalityType;
  11. import java.awt.Dimension;
  12. import java.awt.event.ItemEvent;
  13. import java.awt.event.MouseAdapter;
  14. import java.awt.event.MouseEvent;
  15. import java.awt.image.BufferedImage;
  16. import java.math.RoundingMode;
  17. import java.text.NumberFormat;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Locale;
  21. import java.util.stream.Collectors;
  22. import javax.swing.BorderFactory;
  23. import javax.swing.Box;
  24. import javax.swing.BoxLayout;
  25. import javax.swing.DefaultComboBoxModel;
  26. import javax.swing.ImageIcon;
  27. import javax.swing.JButton;
  28. import javax.swing.JCheckBox;
  29. import javax.swing.JComboBox;
  30. import javax.swing.JDialog;
  31. import javax.swing.JFormattedTextField;
  32. import javax.swing.JFrame;
  33. import javax.swing.JLabel;
  34. import javax.swing.JMenu;
  35. import javax.swing.JMenuBar;
  36. import javax.swing.JMenuItem;
  37. import javax.swing.JOptionPane;
  38. import javax.swing.JPanel;
  39. import javax.swing.JPopupMenu;
  40. import javax.swing.JScrollPane;
  41. import javax.swing.JTabbedPane;
  42. import javax.swing.JTree;
  43. import javax.swing.SwingUtilities;
  44. import javax.swing.border.EmptyBorder;
  45. import javax.swing.text.NumberFormatter;
  46. import javax.swing.tree.DefaultMutableTreeNode;
  47. import javax.swing.tree.DefaultTreeModel;
  48. import javax.swing.tree.TreePath;
  49. import classes.AbstractCanvasObject;
  50. import classes.Constrain;
  51. import classes.GroupNode;
  52. import classes.Flexibility;
  53. import classes.HolonElement;
  54. import classes.HolonElement.Priority;
  55. import model.Model;
  56. import classes.HolonObject;
  57. import ui.controller.Control;
  58. import ui.controller.FlexManager;
  59. import ui.controller.FlexManager.FlexState;
  60. import ui.controller.FlexManager.FlexWrapper;
  61. import utility.ImageImport;
  62. public class FlexWindow extends JFrame {
  63. private JPanel nothingSelectedPanel;
  64. private JPanel selectedPanel;
  65. private JTabbedPane contentPanel = new JTabbedPane();
  66. private JScrollPane usageViewPanel;
  67. private Control controller;
  68. private Model model;
  69. public boolean isClosed = false;
  70. //Flexibility Intermediate
  71. private Flexibility intermediateFlex = new Flexibility(null);
  72. private boolean offered = true, onConstrain = true, offConstrain =false;
  73. //JTree
  74. private DefaultMutableTreeNode listOfAllSelectedHolonObjects;
  75. private JTree stateTree;
  76. private DefaultTreeModel treeModel;
  77. private FlexManager flexmanager;
  78. //Tabs
  79. String gridTabString = "Grid";
  80. String orderTabString = "Order";
  81. //FlexToFlexWrapper()
  82. public FlexWindow(JFrame parentFrame, ui.controller.Control controller){
  83. this.intermediateFlex.name = "name";
  84. this.controller = controller;
  85. this.model = controller.getModel();
  86. //InitWindow
  87. createMenuBar();
  88. initWindowPanel(parentFrame);
  89. this.addWindowListener(new java.awt.event.WindowAdapter() {
  90. @Override
  91. public void windowClosing(java.awt.event.WindowEvent windowEvent) {
  92. isClosed = true;
  93. }
  94. });
  95. updateSelectedPanel();
  96. //this.pack();
  97. }
  98. private void initWindowPanel(JFrame parentFrame) {
  99. this.setBounds(0, 0, 400, parentFrame.getHeight()>20?parentFrame.getHeight()- 20:parentFrame.getHeight());
  100. this.setIconImage(ImageImport.loadImage("/Images/Holeg.png", 30, 30));
  101. this.setTitle("Flexibility");
  102. this.setLocationRelativeTo(parentFrame);
  103. this.setVisible(true);
  104. //this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  105. createNothingSelectedPanel();
  106. createSelectedPanel();
  107. createUsageViewPanel();
  108. contentPanel.addTab(gridTabString, nothingSelectedPanel);
  109. contentPanel.addTab(orderTabString, usageViewPanel);
  110. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  111. this.setContentPane(contentPanel);
  112. this.revalidate();
  113. }
  114. private void createMenuBar(){
  115. JMenuBar menuBar = new JMenuBar();
  116. JMenu canvas = new JMenu("Canvas");
  117. menuBar.add(canvas);
  118. JMenuItem updateMenuItem = new JMenuItem("Update");
  119. updateMenuItem.addActionListener(clicked -> updateSelectedPanel());
  120. canvas.add(updateMenuItem);
  121. JMenu flex = new JMenu("Flex");
  122. menuBar.add(flex);
  123. JMenuItem addMenuItem = new JMenuItem("Add Flexibility");
  124. addMenuItem.addActionListener(clicked -> createAddDialog(null));
  125. flex.add(addMenuItem);
  126. JMenuItem deleteMenuItem = new JMenuItem("Delete Flexibility");
  127. deleteMenuItem.addActionListener(clicked -> createDeleteDialog());
  128. flex.add(deleteMenuItem);
  129. this.setJMenuBar(menuBar);
  130. }
  131. private void createUsageViewPanel() {
  132. flexmanager = controller.getSimManager().getActualFlexManager();
  133. //GridBagApprouch
  134. JPanel gridbagPanel = new JPanel(new GridBagLayout());
  135. usageViewPanel = new JScrollPane(gridbagPanel);
  136. gridbagPanel.setBackground(Color.white);
  137. //5breit
  138. FlexState[] titles = FlexState.values();
  139. for(int i = 0; i<5; i++){
  140. List<FlexWrapper> listOfFlexWithState = flexmanager.getAllFlexWrapperWithState(titles[i]);
  141. JLabel label = new JLabel(titles[i].toString() + "[" + listOfFlexWithState.size()+ "]");
  142. GridBagConstraints labelC = new GridBagConstraints();
  143. labelC.gridx = 1;
  144. labelC.gridy = i*2;
  145. labelC.anchor = GridBagConstraints.LINE_START;
  146. labelC.fill = GridBagConstraints.HORIZONTAL;
  147. labelC.weightx = 0.5;
  148. labelC.weighty = 0.0;
  149. gridbagPanel.add(label, labelC);
  150. JPanel listPanel = new JPanel(new GridBagLayout());
  151. createFlexPanel(listPanel, listOfFlexWithState);
  152. GridBagConstraints panelC = new GridBagConstraints();
  153. panelC.gridx = 0;
  154. panelC.gridwidth = 2;
  155. panelC.gridy = i*2 +1;
  156. panelC.fill = GridBagConstraints.BOTH;
  157. gridbagPanel.add(listPanel, panelC);
  158. JButton expandButton = new JButton("-");
  159. GridBagConstraints buttonC = new GridBagConstraints();
  160. buttonC.gridx = 0;
  161. buttonC.gridy = i*2;
  162. gridbagPanel.add(expandButton, buttonC);
  163. expandButton.addActionListener(clicked -> {
  164. listPanel.setVisible(!listPanel.isVisible());
  165. expandButton.setText(listPanel.isVisible()?"-":"+");
  166. });
  167. }
  168. //Add Spacer
  169. JLabel spacer = new JLabel();
  170. GridBagConstraints c = new GridBagConstraints();
  171. c.gridx = 0;
  172. c.gridy = 5*2;
  173. c.fill = GridBagConstraints.VERTICAL;
  174. c.weightx = 0.0;
  175. c.weighty = 1;
  176. gridbagPanel.add(spacer, c);
  177. }
  178. private void createFlexPanel(JPanel listPanel, List<FlexWrapper> flexWrapperList) {
  179. listPanel.setBackground(Color.white);
  180. Insets insets = new Insets(2,2,2,2);
  181. //String[] flexesString = flexWrapperList.stream().map(flexWrapper -> flexWrapper.getFlex().name).toArray(String[]::new);
  182. for(int i = 0; i < flexWrapperList.size(); i++) {
  183. FlexWrapper actual = flexWrapperList.get(i);
  184. GridBagConstraints c = new GridBagConstraints();
  185. c.gridx = Math.floorMod(i, 5);
  186. c.weightx = 0.0;
  187. c.insets = insets;
  188. JButton labelButton = new JButton(actual.getFlex().name);
  189. labelButton.setPreferredSize(new Dimension(70,70));
  190. labelButton.setBorder(BorderFactory.createLineBorder(Color.black));
  191. listPanel.add(labelButton, c);
  192. labelButton.addActionListener(clicked ->{
  193. actual.order();
  194. controller.calculateStateAndVisualForCurrentTimeStep();
  195. controller.updateCanvas();
  196. });
  197. labelButton.setToolTipText(createToolTipp(actual));
  198. }
  199. //AddSpacer
  200. JLabel spacer = new JLabel();
  201. GridBagConstraints c = new GridBagConstraints();
  202. c.gridx = 5;
  203. c.gridy = 0;
  204. c.fill = GridBagConstraints.VERTICAL;
  205. c.weightx = 1;
  206. c.weighty = 0;
  207. listPanel.add(spacer, c);
  208. }
  209. private String createToolTipp(FlexWrapper actual) {
  210. String tooltipString = "<html>" +
  211. "<b>" + actual.getFlex().name + "( </b>" + actual.getFlex().getElement().getEleName() + "<b> )</b><br>"
  212. + ((actual.remainingDuration() != 0)?"<i>Remaining Duration:"+ actual.remainingDuration()+"</i><br>":"")
  213. + ((actual.remainingTimeTillActivation() != 0)?"<i>Remaining TimeTillActivation:"+ actual.remainingTimeTillActivation()+"</i><br>":"")
  214. + "Duration: " + actual.getFlex().getDuration() + "<br>"
  215. + "Cooldown: " + actual.getFlex().getCooldown() + "<br>"
  216. //+ "Speed: " + actual.getFlex().speed + "<br>"
  217. + "Cost: " + actual.getFlex().cost + "<br>"
  218. + "BrigtMir: " + actual.getFlex().bringtmir() + "<br>"
  219. + "Constrains: " + actual.getFlex().constrainList.stream().map(constrain -> constrain.getName()).collect(Collectors.joining( "," )) + "<br>"
  220. + "</html>";
  221. return tooltipString;
  222. }
  223. public void update() {
  224. updateSelectedPanel();
  225. createUsageViewPanel();
  226. contentPanel.setComponentAt(contentPanel.indexOfTab(orderTabString), usageViewPanel);
  227. contentPanel.revalidate();
  228. }
  229. private void createSelectedPanel() {
  230. //Liste aller Flexibilities
  231. listOfAllSelectedHolonObjects = new DefaultMutableTreeNode("HolonObjects");
  232. treeModel = new DefaultTreeModel(listOfAllSelectedHolonObjects);
  233. stateTree = new JTree(treeModel);
  234. stateTree.addMouseListener ( new MouseAdapter ()
  235. {
  236. public void mousePressed ( MouseEvent e )
  237. {
  238. if ( SwingUtilities.isRightMouseButton ( e ) )
  239. {
  240. TreePath pathUnderCursor = stateTree.getPathForLocation ( e.getX (), e.getY () );
  241. Rectangle pathBounds = stateTree.getUI ().getPathBounds ( stateTree, pathUnderCursor );
  242. if ( pathBounds != null && pathBounds.contains ( e.getX (), e.getY () ) )
  243. {
  244. TreePath[] selectedPaths = stateTree.getSelectionPaths();
  245. if(selectedPaths == null) {
  246. stateTree.addSelectionPath(pathUnderCursor);
  247. }else {
  248. boolean isInselectedPaths = false;
  249. for (TreePath path : stateTree.getSelectionPaths()) {
  250. if(path.equals(pathUnderCursor)) {
  251. isInselectedPaths = true;
  252. }
  253. }
  254. if(!isInselectedPaths) {
  255. stateTree.clearSelection();
  256. stateTree.addSelectionPath(pathUnderCursor);
  257. }
  258. }
  259. JPopupMenu menu = new JPopupMenu ();
  260. JMenuItem priorityItem = new JMenuItem("EditPriorities");
  261. JMenuItem flexItem = new JMenuItem("AddFlex");
  262. priorityItem.addActionListener(clicked -> {
  263. Priority prio = null;
  264. if (stateTree.getSelectionPaths() != null)
  265. for (TreePath path : stateTree.getSelectionPaths()) {
  266. Object treeNodeUserObject = ((DefaultMutableTreeNode) path.getLastPathComponent())
  267. .getUserObject();
  268. if (treeNodeUserObject instanceof ElementInfo) {
  269. if (prio == null)
  270. prio = (Priority) JOptionPane.showInputDialog(stateTree,
  271. "Select the Priority:", "Priority?", JOptionPane.OK_OPTION,
  272. new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)),
  273. Priority.values(), "");
  274. if (prio == null)
  275. break;
  276. ElementInfo eleInfo = (ElementInfo) treeNodeUserObject;
  277. eleInfo.ele.setPriority(prio);
  278. treeModel.reload((DefaultMutableTreeNode) path.getLastPathComponent());
  279. }
  280. }
  281. });
  282. flexItem.addActionListener(clicked -> {
  283. TreePath path = stateTree.getSelectionPath();
  284. if (path == null)
  285. return;
  286. Object treeNodeUserObject = ((DefaultMutableTreeNode) path.getLastPathComponent())
  287. .getUserObject();
  288. if (!(treeNodeUserObject instanceof ElementInfo))
  289. return;
  290. createAddDialog(((ElementInfo) treeNodeUserObject).ele);
  291. });
  292. menu.add ( priorityItem );
  293. menu.add ( flexItem );
  294. menu.show ( stateTree, pathBounds.x, pathBounds.y + pathBounds.height );
  295. }else {
  296. JPopupMenu menu = new JPopupMenu ();
  297. menu.add ( new JMenuItem ( "Other Test" ) );
  298. menu.show ( stateTree, e.getX(), e.getY() );
  299. }
  300. }
  301. }
  302. } );
  303. selectedPanel = new JPanel(new BorderLayout());
  304. selectedPanel.add(new JScrollPane(stateTree));
  305. }
  306. private void createNothingSelectedPanel() {
  307. nothingSelectedPanel = new JPanel();
  308. nothingSelectedPanel.setLayout(new BoxLayout(nothingSelectedPanel, BoxLayout.PAGE_AXIS));
  309. JLabel nothingSelectedTextLabel = new JLabel("No HolonObject exist.");
  310. nothingSelectedTextLabel.setForeground(Color.gray);
  311. nothingSelectedTextLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
  312. nothingSelectedPanel.add(Box.createVerticalGlue());
  313. nothingSelectedPanel.add(nothingSelectedTextLabel);
  314. nothingSelectedPanel.add(Box.createVerticalGlue());
  315. }
  316. public void updateSelectedPanel() {
  317. //UpdateFlexManager
  318. flexmanager = controller.getSimManager().getActualFlexManager();
  319. listOfAllSelectedHolonObjects.removeAllChildren();
  320. //Init with HolonObjects
  321. for(AbstractCanvasObject aCps: model.getObjectsOnCanvas()) {
  322. DefaultMutableTreeNode newObjectChild = new DefaultMutableTreeNode(aCps.getName() + " ID:" + aCps.getId());
  323. if(aCps instanceof HolonObject) expandTreeHolonObject((HolonObject)aCps, newObjectChild);
  324. if(aCps instanceof GroupNode)expandTreeUpperNode((GroupNode)aCps, newObjectChild);
  325. listOfAllSelectedHolonObjects.add(newObjectChild);
  326. }
  327. treeModel.nodeStructureChanged(listOfAllSelectedHolonObjects);
  328. stateTree.revalidate();
  329. expandAll(stateTree);
  330. selectedPanel.revalidate();
  331. contentPanel.setComponentAt(contentPanel.indexOfTab(gridTabString), selectedPanel);
  332. contentPanel.revalidate();
  333. this.revalidate();
  334. }
  335. private void expandAll(JTree tree) {
  336. for(int i = 0; i< tree.getRowCount() ; i++) {
  337. tree.expandRow(i);
  338. }
  339. }
  340. private void expandTreeUpperNode(GroupNode groupNode, DefaultMutableTreeNode root) {
  341. for(AbstractCanvasObject aCps: groupNode.getNodes()) {
  342. DefaultMutableTreeNode newObjectChild = new DefaultMutableTreeNode(aCps.getName() + " ID:" + aCps.getId());
  343. if(aCps instanceof HolonObject) expandTreeHolonObject((HolonObject)aCps, newObjectChild);
  344. if(aCps instanceof GroupNode)expandTreeUpperNode((GroupNode)aCps, newObjectChild);
  345. root.add(newObjectChild);
  346. }
  347. }
  348. private void expandTreeHolonObject(HolonObject hObject, DefaultMutableTreeNode root) {
  349. for(HolonElement hElement: hObject.getElements()) {
  350. DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(new ElementInfo(hElement));
  351. expandTreeFlex(hElement, newChild);
  352. root.add(newChild);
  353. }
  354. }
  355. private void expandTreeFlex(HolonElement hElement, DefaultMutableTreeNode root) {
  356. for(Flexibility flex: hElement.flexList) {
  357. FlexWrapper flexWrapper = this.flexmanager.getFlexWrapperFromFlexibility(flex);
  358. String flexState = "";
  359. if(flexWrapper != null) {
  360. Color color = this.FlexStateToColor(flexWrapper.getState());
  361. flexState = "<font bgcolor='#" + Integer.toHexString(color.getRGB()).substring(2) + "'>" + flexWrapper.getState().name() + "</font>";
  362. }
  363. DefaultMutableTreeNode newChild = new DefaultMutableTreeNode("<html>"+ flexState + " <b>" + flex.name+ "</b>" + "</html>");
  364. root.add(newChild);
  365. }
  366. }
  367. private void createDeleteDialog() {
  368. List<HolonObject> list= createListOfHolonObjects(model.getObjectsOnCanvas());
  369. //String test = list.stream().map(Object::toString).collect(Collectors.joining(","));
  370. Object[] allFlexes = list.stream().flatMap(hObject -> hObject.getElements().stream()).flatMap(hElement -> hElement.flexList.stream()).toArray(size -> new Flexibility[size]);
  371. if(allFlexes.length == 0) {
  372. JOptionPane.showMessageDialog(this,
  373. "No Flexibility exist.",
  374. "Warning",
  375. JOptionPane.WARNING_MESSAGE);
  376. return;
  377. }
  378. 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, "");
  379. if(toDeleteFlex != null) {
  380. toDeleteFlex.getElement().flexList.remove(toDeleteFlex);
  381. controller.getSimManager().calculateStateForTimeStep(model.getCurIteration(), true);
  382. updateSelectedPanel();
  383. }
  384. }
  385. private List<HolonObject> createListOfHolonObjects(List<AbstractCanvasObject> objectsOnCanvas) {
  386. List<HolonObject> list = new ArrayList<HolonObject>();
  387. for(AbstractCanvasObject aCps : objectsOnCanvas) {
  388. if(aCps instanceof HolonObject) list.add((HolonObject) aCps);
  389. else if (aCps instanceof GroupNode) list.addAll(createListOfHolonObjects(((GroupNode)aCps).getNodes()));
  390. }
  391. return list;
  392. }
  393. //Add Element
  394. private void createAddDialog(HolonElement element){
  395. if(model.getObjectsOnCanvas().isEmpty()) {
  396. JOptionPane.showMessageDialog(this,
  397. "No HolonObject exist.",
  398. "Warning",
  399. JOptionPane.WARNING_MESSAGE);
  400. return;
  401. }
  402. JDialog addDialog = new JDialog();
  403. addDialog.setTitle("Create Flexibility");
  404. addDialog.setBounds(0, 0, 820, 400);
  405. addDialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  406. JPanel dialogPanel = new JPanel(new BorderLayout());
  407. addDialog.setContentPane(dialogPanel);
  408. JPanel selectionPanel = new JPanel(null);
  409. dialogPanel.add(selectionPanel, BorderLayout.CENTER);
  410. JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  411. dialogPanel.add(buttonPanel, BorderLayout.PAGE_END);
  412. addDialog.setModalityType(ModalityType.APPLICATION_MODAL);
  413. //Erstelle HolonObject AuswahlBox
  414. HolonObject[] holonObjects = createListOfHolonObjects(model.getObjectsOnCanvas()).stream().toArray(HolonObject[]::new);
  415. DefaultComboBoxModel<HolonObject> comboBoxModel = new DefaultComboBoxModel<HolonObject>( holonObjects );
  416. JComboBox<HolonObject> holonObjectSelector = new JComboBox<HolonObject>(comboBoxModel);
  417. holonObjectSelector.setBounds(10,30, 800, 30);
  418. DefaultComboBoxModel<HolonElement> comboBoxModelElements = new DefaultComboBoxModel<HolonElement>( holonObjects[0].getElements().stream().toArray(size -> new HolonElement[size]));
  419. JComboBox<HolonElement> holonElementSelector = new JComboBox<HolonElement>(comboBoxModelElements);
  420. holonElementSelector.setBounds(10,80, 800, 30);
  421. holonObjectSelector.addItemListener(aListener -> {
  422. if(aListener.getStateChange() == ItemEvent.SELECTED) {
  423. DefaultComboBoxModel<HolonElement> newComboBoxModelElements = new DefaultComboBoxModel<HolonElement>( ((HolonObject) aListener.getItem()).getElements().stream().toArray(size -> new HolonElement[size]));
  424. holonElementSelector.setModel(newComboBoxModelElements);
  425. }
  426. });
  427. if(element == null) {
  428. selectionPanel.add(holonObjectSelector);
  429. selectionPanel.add(holonElementSelector);
  430. JLabel selectObjectLabel = new JLabel("Select HolonObject:");
  431. selectObjectLabel.setBounds(10, 10, 200, 20);
  432. selectionPanel.add(selectObjectLabel);
  433. JLabel selectElementLabel = new JLabel("Select HolonElement:");
  434. selectElementLabel.setBounds(10, 60, 200, 20);
  435. selectionPanel.add(selectElementLabel);
  436. }
  437. else {
  438. JLabel selectElementLabel = new JLabel("Selected: " +element.toString());
  439. selectElementLabel.setBounds(10, 60, 2000, 20);
  440. selectionPanel.add(selectElementLabel);
  441. }
  442. JPanel flexAttributesBorderPanel = new JPanel(null);
  443. flexAttributesBorderPanel.setBounds(10, 120, 800, 200);
  444. flexAttributesBorderPanel.setBorder(BorderFactory.createTitledBorder("Flexibility Attributes"));
  445. selectionPanel.add(flexAttributesBorderPanel);
  446. JLabel flexNameLabel = new JLabel("Name:");
  447. flexNameLabel.setBounds(10,20, 50, 20);
  448. flexAttributesBorderPanel.add(flexNameLabel);
  449. JFormattedTextField nameTextField = new JFormattedTextField(intermediateFlex.name);
  450. nameTextField.addPropertyChangeListener(changed -> intermediateFlex.name = nameTextField.getText());
  451. nameTextField.setBounds(80, 15, 200, 30);
  452. flexAttributesBorderPanel.add(nameTextField);
  453. JLabel flexSpeedLabel = new JLabel("Speed:");
  454. flexSpeedLabel.setBounds(10,55, 50, 20);
  455. flexAttributesBorderPanel.add(flexSpeedLabel);
  456. //Integer formatter
  457. NumberFormat format = NumberFormat.getIntegerInstance();
  458. format.setGroupingUsed(false);
  459. format.setParseIntegerOnly(true);
  460. NumberFormatter integerFormatter = new NumberFormatter(format);
  461. integerFormatter.setMinimum(0);
  462. integerFormatter.setCommitsOnValidEdit(true);
  463. JFormattedTextField speedTextField = new JFormattedTextField(integerFormatter);
  464. speedTextField.setValue(intermediateFlex.speed);
  465. speedTextField.setToolTipText("Only positive Integer.");
  466. speedTextField.addPropertyChangeListener(actionEvent -> intermediateFlex.speed = Integer.parseInt(speedTextField.getValue().toString()));
  467. speedTextField.setBounds(80, 50, 200, 30);
  468. flexAttributesBorderPanel.add(speedTextField);
  469. speedTextField.setEnabled(false);
  470. JLabel flexDurationLabel = new JLabel("Duration:");
  471. flexDurationLabel.setBounds(10,90, 70, 20);
  472. flexAttributesBorderPanel.add(flexDurationLabel);
  473. NumberFormatter moreThenZeroIntegerFormater = new NumberFormatter(format);
  474. moreThenZeroIntegerFormater.setMinimum(1);
  475. moreThenZeroIntegerFormater.setCommitsOnValidEdit(true);
  476. JFormattedTextField durationTextField = new JFormattedTextField(moreThenZeroIntegerFormater);
  477. durationTextField.setValue(intermediateFlex.getDuration());
  478. durationTextField.setToolTipText("Only positive Integer bigger then 0.");
  479. durationTextField.addPropertyChangeListener(actionEvent -> intermediateFlex.setDuration(Integer.parseInt(durationTextField.getValue().toString())));
  480. durationTextField.setBounds(80, 85, 200, 30);
  481. flexAttributesBorderPanel.add(durationTextField);
  482. JLabel flexCostsLabel = new JLabel("Costs:");
  483. flexCostsLabel.setBounds(10,125, 70, 20);
  484. flexAttributesBorderPanel.add(flexCostsLabel);
  485. //Double Format:
  486. NumberFormat doubleFormat = NumberFormat.getNumberInstance(Locale.US);
  487. doubleFormat.setMinimumFractionDigits(1);
  488. doubleFormat.setMaximumFractionDigits(2);
  489. doubleFormat.setRoundingMode(RoundingMode.HALF_UP);
  490. //CostFormatter:
  491. NumberFormatter costsFormatter = new NumberFormatter(doubleFormat);
  492. costsFormatter.setMinimum(0.0);
  493. JFormattedTextField costTextField = new JFormattedTextField(costsFormatter);
  494. costTextField.setValue(intermediateFlex.cost);
  495. costTextField.setToolTipText("Only non negative Double with DecimalSeperator Point('.').");
  496. costTextField.addPropertyChangeListener(propertyChange -> intermediateFlex.cost = Float.parseFloat(costTextField.getValue().toString()));
  497. costTextField.setBounds(80, 120, 200, 30);
  498. flexAttributesBorderPanel.add(costTextField);
  499. JLabel flexCooldownLabel = new JLabel("Cooldown:");
  500. flexCooldownLabel.setBounds(310,20, 70, 20);
  501. flexAttributesBorderPanel.add(flexCooldownLabel);
  502. JFormattedTextField cooldownTextField = new JFormattedTextField(moreThenZeroIntegerFormater);
  503. cooldownTextField.setValue(intermediateFlex.getCooldown());
  504. cooldownTextField.setToolTipText("Only positive Integer.");
  505. cooldownTextField.addPropertyChangeListener(actionEvent -> intermediateFlex.setCooldown(Integer.parseInt(cooldownTextField.getValue().toString())));
  506. cooldownTextField.setBounds(380, 15, 200, 30);
  507. flexAttributesBorderPanel.add(cooldownTextField);
  508. JCheckBox offeredCheckBox = new JCheckBox("Offered");
  509. offeredCheckBox.setSelected(this.offered);
  510. offeredCheckBox.setBounds(310, 55, 200, 20);
  511. flexAttributesBorderPanel.add(offeredCheckBox);
  512. JCheckBox onConstrainCheckBox = new JCheckBox("On_Constrain");
  513. onConstrainCheckBox.setSelected(this.onConstrain);
  514. onConstrainCheckBox.setBounds(310, 80, 200, 20);
  515. flexAttributesBorderPanel.add(onConstrainCheckBox);
  516. JCheckBox offConstrainCheckBox = new JCheckBox("Off_Constrain");
  517. offConstrainCheckBox.setSelected(this.offConstrain);
  518. offConstrainCheckBox.setBounds(310, 105, 200, 20);
  519. flexAttributesBorderPanel.add(offConstrainCheckBox);
  520. //Both cant be true....
  521. onConstrainCheckBox.addActionListener(clicked -> {
  522. if(onConstrainCheckBox.isSelected()) offConstrainCheckBox.setSelected(false);
  523. });
  524. offConstrainCheckBox.addActionListener(clicked -> {
  525. if(offConstrainCheckBox.isSelected()) onConstrainCheckBox.setSelected(false);
  526. });
  527. JButton createFlexButton = new JButton("Create");
  528. createFlexButton.addActionListener(clicked -> {
  529. //createFlexButton.requestFocus();
  530. //createFlexButton.grabFocus();
  531. HolonElement ele;
  532. if(element ==null) {
  533. ele = (HolonElement) holonElementSelector.getSelectedItem();
  534. }else {
  535. ele = element;
  536. }
  537. Flexibility toCreateFlex = new Flexibility(ele);
  538. toCreateFlex.name = intermediateFlex.name;
  539. toCreateFlex.speed = intermediateFlex.speed;
  540. toCreateFlex.setDuration(intermediateFlex.getDuration());
  541. toCreateFlex.cost = intermediateFlex.cost;
  542. toCreateFlex.setCooldown(intermediateFlex.getCooldown());
  543. toCreateFlex.offered=offeredCheckBox.isSelected();
  544. if(onConstrainCheckBox.isSelected())toCreateFlex.constrainList.add(Constrain.createOnConstrain());
  545. if(offConstrainCheckBox.isSelected())toCreateFlex.constrainList.add(Constrain.createOffConstrain());
  546. ele.flexList.add(toCreateFlex);
  547. //save checkboxes
  548. this.offered=offeredCheckBox.isSelected();
  549. this.onConstrain = onConstrainCheckBox.isSelected();
  550. this.offConstrain = offConstrainCheckBox.isSelected();
  551. //if(!model.getSelectedCpsObjects().contains(holonObjectSelector.getSelectedItem()))model.getSelectedCpsObjects().add((AbstractCpsObject)holonObjectSelector.getSelectedItem());
  552. controller.getSimManager().calculateStateForTimeStep(model.getCurIteration(), true);
  553. update();
  554. addDialog.dispose();
  555. });
  556. buttonPanel.add(createFlexButton);
  557. JButton cancelButton = new JButton("Cancel");
  558. cancelButton.addActionListener(clicked -> {
  559. addDialog.dispose();
  560. });
  561. buttonPanel.add(cancelButton);
  562. //last
  563. addDialog.setLocationRelativeTo(this);
  564. addDialog.setVisible(true);
  565. }
  566. class ElementInfo {
  567. HolonElement ele;
  568. public ElementInfo(HolonElement ele) {
  569. this.ele = ele;
  570. }
  571. @Override
  572. public String toString() {
  573. return ele.getEleName() + " Priority:" + ele.getPriority();
  574. }
  575. }
  576. public Color FlexStateToColor(FlexState state) {
  577. switch(state) {
  578. case IN_USE:
  579. return new Color(173,247,182);
  580. case NOT_OFFERED:
  581. return new Color(252,245,199);
  582. case OFFERED:
  583. return new Color(160,206,217);
  584. case ON_COOLDOWN:
  585. return new Color(255,238,147);
  586. case UNAVAILABLE:
  587. default:
  588. return new Color(255,192,159);
  589. }
  590. }
  591. }