HolarchyPanel.java 12 KB

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