HolarchyPanel.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package ui.view.holarchy;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Stroke;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9. import java.awt.geom.Ellipse2D;
  10. import java.util.ArrayList;
  11. import java.util.Collection;
  12. import java.util.Collections;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import javax.swing.JLabel;
  17. import javax.swing.JPanel;
  18. import javax.swing.JSeparator;
  19. import javax.swing.JSlider;
  20. import javax.swing.SwingConstants;
  21. import javax.swing.event.ChangeEvent;
  22. import javax.swing.event.ChangeListener;
  23. import classes.Holon;
  24. import ui.controller.Control;
  25. import util.Random;
  26. public class HolarchyPanel extends JPanel {
  27. Control control;
  28. HolarchyWindow parentFrame;
  29. JLabel layerLabelTitle;
  30. JSlider slider;
  31. JSeparator sep, sep2;
  32. int displayedLayer;
  33. int minRadius = 24;
  34. Holon root;
  35. ArrayList<Map<Ellipse2D, Map<Ellipse2D, Holon>>> ellipsesChild;
  36. ArrayList<Map<Ellipse2D, Holon>> ellipses;
  37. ArrayList<ArrayList<Holon>> layeredHolons;
  38. ArrayList<Ellipse2D> surroundings;
  39. Map<Holon, Color> coloredHolons;
  40. public HolarchyPanel(HolarchyWindow parentFrame, Control control) {
  41. this.control = control;
  42. this.parentFrame = parentFrame;
  43. this.root = control.getModel().getStateHolon();
  44. this.setBounds(0, 0, parentFrame.getWidth()*4/5, parentFrame.getHeight());
  45. this.setLayout(null);
  46. this.setBackground(Color.white);
  47. this.addMouseListener(mouseListener);
  48. this.layerLabelTitle = new JLabel("Layer");
  49. this.layerLabelTitle.setBounds(5, 5, 50, 20);
  50. this.layerLabelTitle.setHorizontalAlignment(SwingConstants.CENTER);
  51. this.add(this.layerLabelTitle);
  52. this.slider = new JSlider();
  53. this.slider.setOrientation(SwingConstants.VERTICAL);
  54. this.slider.setBounds(5, 30, 50, 200);
  55. this.slider.setPaintLabels(true);
  56. this.slider.setPaintTicks(true);
  57. this.slider.setSnapToTicks(true);
  58. this.slider.addChangeListener(this.sliderChangeListener);
  59. this.slider.setValue(0);
  60. this.slider.setInverted(true);
  61. this.slider.setFocusable(false);
  62. this.slider.setBackground(Color.white);
  63. this.add(slider);
  64. this.sep = new JSeparator(SwingConstants.VERTICAL);
  65. this.sep.setBounds(60, 0, 1, 235);
  66. this.add(this.sep);
  67. this.sep2 = new JSeparator(SwingConstants.HORIZONTAL);
  68. this.sep2.setBounds(0, 235, 60, 1);
  69. this.add(this.sep2);
  70. this.ellipses = new ArrayList<Map<Ellipse2D, Holon>>();
  71. this.ellipsesChild = new ArrayList<Map<Ellipse2D, Map<Ellipse2D, Holon>>>();
  72. this.displayedLayer = 0;
  73. this.layeredHolons = new ArrayList<ArrayList<Holon>>();
  74. this.surroundings = new ArrayList<Ellipse2D>();
  75. this.coloredHolons = new HashMap<Holon, Color>();
  76. getAllHolons();
  77. setSliderMax(this.layeredHolons.size());
  78. createAllLayers();
  79. generateColorSet();
  80. }
  81. public void update() {
  82. ArrayList<ArrayList<Holon>> layeredHolonsCpy = new ArrayList<ArrayList<Holon>>();
  83. copyList(layeredHolonsCpy, this.layeredHolons);
  84. this.layeredHolons.clear();
  85. getAllHolons();
  86. //the holarchy did not change
  87. if(compareAllHolons(layeredHolonsCpy))
  88. return;
  89. this.ellipses = new ArrayList<Map<Ellipse2D, Holon>>();
  90. this.ellipsesChild = new ArrayList<Map<Ellipse2D, Map<Ellipse2D, Holon>>>();
  91. this.surroundings = new ArrayList<Ellipse2D>();
  92. this.coloredHolons = new HashMap<Holon, Color>();
  93. setSliderMax(this.layeredHolons.size());
  94. createAllLayers();
  95. generateColorSet();
  96. repaint();
  97. }
  98. public void paintComponent(Graphics g) {
  99. super.paintComponent(g);
  100. paintLayer(g, this.displayedLayer);
  101. }
  102. public void displayLayer(int layer) {
  103. this.displayedLayer = layer;
  104. this.repaint();
  105. }
  106. public void paintLayer(Graphics g, int layer) {
  107. this.displayedLayer = layer;
  108. Graphics2D g2 = (Graphics2D) g;
  109. Stroke stroke = new BasicStroke(3f);
  110. g2.setStroke(stroke);
  111. //resize panel
  112. Ellipse2D surrounding = this.surroundings.get(this.displayedLayer);
  113. if(surrounding.getWidth() > this.getWidth()) {
  114. this.setSize((int) surrounding.getWidth(), this.getHeight());
  115. }
  116. if(surrounding.getHeight() > this.getHeight()) {
  117. this.setSize(this.getWidth(), (int) surrounding.getHeight());
  118. }
  119. for(Ellipse2D e : this.ellipses.get(this.displayedLayer).keySet()) {
  120. //draw ellipse
  121. g2.setColor( this.coloredHolons.get(this.ellipses.get(this.displayedLayer).get(e)) );
  122. g2.draw(e);
  123. //draw child ellipses and add them to global list
  124. for(Ellipse2D e2 : this.ellipsesChild.get(layer).get(e).keySet()) {
  125. g2.setColor( this.coloredHolons.get(this.ellipsesChild.get(layer).get(e).get(e2)) );
  126. g2.draw(e2);
  127. }
  128. }
  129. }
  130. public void createAllLayers() {
  131. for(int layer=0; layer<this.layeredHolons.size(); layer++) {
  132. this.ellipsesChild.add(layer, new HashMap<Ellipse2D, Map<Ellipse2D, Holon>>());
  133. //estimate the required area, center if it is smaller than the panels size
  134. Ellipse2D surrounding = estimateSurrounding(60, 0, layer, this.minRadius);
  135. double newX = surrounding.getX(), newY = surrounding.getY();
  136. if(surrounding.getWidth() <= this.getWidth()) {
  137. double diff = this.getWidth() - surrounding.getWidth();
  138. newX = diff/2;
  139. }
  140. if(surrounding.getHeight() <= this.getHeight()) {
  141. double diff = this.getHeight() - surrounding.getHeight();
  142. newY = diff/2;
  143. }
  144. surrounding = new Ellipse2D.Double(newX, newY, surrounding.getWidth(), surrounding.getHeight());
  145. this.surroundings.add(layer, surrounding);
  146. //create ellipse for each holon
  147. this.ellipses.add(layer, placeHolons(this.layeredHolons.get(layer), surrounding, true));
  148. for(Ellipse2D e : this.ellipses.get(layer).keySet()) {
  149. //draw child ellipses and add them to global list
  150. Map<Ellipse2D, Holon> ellipses2 = placeHolons(ellipses.get(layer).get(e).childHolons, e, false);
  151. this.ellipsesChild.get(layer).put(e, ellipses2);
  152. }
  153. }
  154. }
  155. /**
  156. * randomly place an ellipse for each holon in holons inside the given ellipse without overlapping
  157. * @param holons
  158. * @param surrounding
  159. * @param drawChildren
  160. * @return
  161. */
  162. public Map<Ellipse2D, Holon> placeHolons(List<Holon> holons, Ellipse2D surrounding, boolean drawChildren) {
  163. double startX = surrounding.getX();
  164. double startY = surrounding.getY();
  165. double width = surrounding.getWidth();
  166. double height = surrounding.getHeight();
  167. Map<Ellipse2D, Holon> shapes = new HashMap<Ellipse2D, Holon>();
  168. int abord = 0;
  169. int i = 0;
  170. while(shapes.size() < holons.size() && abord < 1000) {
  171. Holon h = holons.get(i);
  172. int radius = this.minRadius;
  173. if(drawChildren)
  174. radius = this.minRadius * (h.getChildCount()+1) + 10;
  175. //randomly place an ellipse for the holon
  176. double x = startX + (width * Math.random());
  177. double y = startY + (height * Math.random());
  178. //check if ellipse is inside the surrounding
  179. boolean overlap = !surrounding.contains(x, y, radius*2, radius*2);
  180. if(overlap) {
  181. continue;
  182. }
  183. Ellipse2D e = new Ellipse2D.Double(x-5, y-5, radius*2+10, radius*2+10);
  184. //check whether or not the random ellipse overlaps with an existing one
  185. for(Ellipse2D e2 : shapes.keySet()) {
  186. if(e.intersects(e2.getX()-5, e2.getY()-5, e2.getWidth()+10, e2.getHeight()+10)) {
  187. overlap = true;
  188. break;
  189. }
  190. }
  191. abord++;
  192. //if not add the ellipse to the already existing ones
  193. if(!overlap) {
  194. shapes.put(e, h);
  195. i++;
  196. abord = 0;
  197. }
  198. }
  199. return shapes;
  200. }
  201. public Ellipse2D estimateSurrounding(int x, int y, int layer, int radius) {
  202. if(this.layeredHolons == null || this.layeredHolons.size() <= layer)
  203. return new Ellipse2D.Double(x, y, Integer.MAX_VALUE, Integer.MAX_VALUE);
  204. int w = 0;
  205. for(Holon h : this.layeredHolons.get(layer)) {
  206. w += radius * (h.getChildCount()+1);
  207. }
  208. w = w * 4 + 10;
  209. if(w > this.getHeight() - 50) {
  210. int div = radius / (w/(this.getHeight()-50));
  211. if(div == radius)
  212. div--;
  213. return estimateSurrounding(x, y, layer, div );
  214. }
  215. return new Ellipse2D.Double(x, y, w, w);
  216. }
  217. public void getAllHolons() {
  218. this.layeredHolons.add((ArrayList<Holon>) this.root.childHolons.clone());
  219. //TODO: potential infinit loop
  220. while(true) {
  221. //new layer
  222. ArrayList<Holon> children = new ArrayList<Holon>();
  223. for(Holon h : this.layeredHolons.get(this.layeredHolons.size()-1)) {
  224. children.addAll((Collection<? extends Holon>) h.childHolons.clone());
  225. }
  226. if(children.size() > 0) {
  227. this.layeredHolons.add(children);
  228. } else {
  229. break;
  230. }
  231. }
  232. }
  233. public void setSliderMax(int n) {
  234. if(this.slider == null || n < 0)
  235. return;
  236. this.slider.setMaximum(n-1);
  237. if(n < 11) {
  238. this.slider.setMajorTickSpacing(1);
  239. } else if (n < 101) {
  240. this.slider.setMajorTickSpacing(10);
  241. this.slider.setMinorTickSpacing(1);
  242. } else {
  243. this.slider.setMajorTickSpacing(100);
  244. this.slider.setMinorTickSpacing(10);
  245. }
  246. }
  247. /**
  248. * generate a distinct color for each Holon in each layer
  249. */
  250. public void generateColorSet() {
  251. for(int layer=0; layer<this.layeredHolons.size(); layer++) {
  252. int numColors = 0;
  253. numColors = this.layeredHolons.get(layer).size();
  254. if(numColors <= 0)
  255. continue;
  256. ArrayList<Color> colors = new ArrayList<Color>();
  257. float hue = Random.nextFloatInRange(0, 1);
  258. for(float i = 0; i < 360; i += 360 / numColors) {
  259. hue += 0.618033988749895f;
  260. float saturation = 0.5f; //90 + Random.nextFloatInRange(0, 10);
  261. float lightness = 0.95f; //50 + Random.nextFloatInRange(0, 10);
  262. Color c = Color.getHSBColor(hue%1, saturation, lightness);
  263. colors.add(c);
  264. }
  265. for(int j=0;j<this.layeredHolons.get(layer).size(); j++) {
  266. this.coloredHolons.put(this.layeredHolons.get(layer).get(j), colors.get(j));
  267. }
  268. }
  269. }
  270. /**
  271. * compares list with this.layeredHolons
  272. * @param list
  273. * @return true if list is equivalent to this.layeredHolons
  274. */
  275. private boolean compareAllHolons(ArrayList<ArrayList<Holon>> list) {
  276. if(this.layeredHolons.size() != list.size())
  277. return false;
  278. for(int i=0; i<this.layeredHolons.size(); i++) {
  279. if(this.layeredHolons.get(i).size() != list.get(i).size())
  280. return false;
  281. if(!list.get(i).containsAll(this.layeredHolons.get(i)))
  282. return false;
  283. }
  284. return true;
  285. }
  286. private void copyList(ArrayList<ArrayList<Holon>> dest, ArrayList<ArrayList<Holon>> src) {
  287. for(int i=0; i<src.size(); i++) {
  288. dest.add(new ArrayList<Holon>());
  289. for(int j=0; j<src.get(i).size(); j++) {
  290. dest.get(i).add(src.get(i).get(j));
  291. }
  292. }
  293. }
  294. MouseListener mouseListener = new MouseListener() {
  295. @Override
  296. public void mouseClicked(MouseEvent arg0) {
  297. //Get clicked Holon
  298. for(Ellipse2D e : ellipses.get(displayedLayer).keySet()) {
  299. if(e.contains(arg0.getX(), arg0.getY())) {
  300. //found ellipse
  301. boolean child = false;
  302. //check if one its children was clicked
  303. for(Ellipse2D e2 : ellipsesChild.get(displayedLayer).get(e).keySet()) {
  304. if(e2.contains(arg0.getX(), arg0.getY())) {
  305. //child was clicked
  306. child = true;
  307. parentFrame.displayHolonInfos(ellipsesChild.get(displayedLayer).get(e).get(e2));
  308. break;
  309. }
  310. }
  311. if(!child) {
  312. //no child was clicked
  313. parentFrame.displayHolonInfos(ellipses.get(displayedLayer).get(e));
  314. }
  315. break;
  316. }
  317. }
  318. }
  319. @Override
  320. public void mouseEntered(MouseEvent arg0) {
  321. //not important
  322. }
  323. @Override
  324. public void mouseExited(MouseEvent arg0) {
  325. //not important
  326. }
  327. @Override
  328. public void mousePressed(MouseEvent arg0) {
  329. //not important
  330. }
  331. @Override
  332. public void mouseReleased(MouseEvent arg0) {
  333. //not important
  334. }
  335. };
  336. ChangeListener sliderChangeListener = new ChangeListener() {
  337. @Override
  338. public void stateChanged(ChangeEvent arg0) {
  339. JSlider source = (JSlider) arg0.getSource();
  340. if(!source.getValueIsAdjusting()) {
  341. displayLayer(source.getValue());
  342. parentFrame.clearInfos();
  343. }
  344. }
  345. };
  346. }