Randomizer.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package holeg.addon;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.text.NumberFormat;
  8. import java.util.ArrayList;
  9. import java.util.Comparator;
  10. import java.util.HashMap;
  11. import java.util.Hashtable;
  12. import java.util.List;
  13. import javax.swing.BorderFactory;
  14. import javax.swing.Box;
  15. import javax.swing.BoxLayout;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JButton;
  18. import javax.swing.JCheckBox;
  19. import javax.swing.JFileChooser;
  20. import javax.swing.JFormattedTextField;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JPanel;
  24. import javax.swing.JScrollPane;
  25. import javax.swing.JSlider;
  26. import javax.swing.JSplitPane;
  27. import javax.swing.filechooser.FileNameExtensionFilter;
  28. import javax.swing.text.NumberFormatter;
  29. import com.google.gson.Gson;
  30. import com.google.gson.JsonArray;
  31. import com.google.gson.JsonElement;
  32. import com.google.gson.JsonIOException;
  33. import com.google.gson.JsonParser;
  34. import com.google.gson.JsonSyntaxException;
  35. import holeg.addon.helper.HolonElementSketch;
  36. import holeg.addon.helper.RandomPriotity;
  37. import holeg.api.AddOn;
  38. import holeg.model.AbstractCanvasObject;
  39. import holeg.model.HolonElement;
  40. import holeg.model.HolonObject;
  41. import holeg.ui.controller.Control;
  42. import holeg.ui.view.image.Import;
  43. import holeg.utility.math.Random;
  44. public class Randomizer implements AddOn {
  45. private Control control;
  46. private int minAmountOfElements = 3;
  47. private int maxAmountOfElements = 7;
  48. private final JPanel content = new JPanel();
  49. private final JPanel tablePanel = new JPanel();
  50. private final RandomPriotity prioPanel = new RandomPriotity();
  51. private final JCheckBox priorityCheckbox = new JCheckBox("Random");
  52. private boolean useRandomPriority = true;
  53. //To Test the Layout Faster
  54. public static void main(String[] args)
  55. {
  56. JFrame newFrame = new JFrame("exampleWindow");
  57. Randomizer instance = new Randomizer();
  58. newFrame.setContentPane(instance.getPanel());
  59. newFrame.pack();
  60. newFrame.setVisible(true);
  61. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62. }
  63. private File file;
  64. public double randomChance = 1.0;
  65. private final HashMap<HolonObject, Boolean> objectMap = new HashMap<>();
  66. private final List<JCheckBox> checkboxList = new ArrayList<>();
  67. public Randomizer(){
  68. for(int i = 0; i < 30; i++) {
  69. objectMap.put(new HolonObject("dude"), true);
  70. }
  71. content.setLayout(new BorderLayout());
  72. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, createParameterPanel(), createFilterPanel());
  73. splitPane.setDividerLocation(0.5);
  74. content.add(splitPane, BorderLayout.CENTER);
  75. JButton buttonRun = new JButton("Run");
  76. buttonRun.addActionListener(actionEvent -> run());
  77. content.add(buttonRun, BorderLayout.PAGE_END);
  78. }
  79. private JScrollPane createFilterPanel() {
  80. //Table
  81. tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.PAGE_AXIS));
  82. JScrollPane scrollPanel = new JScrollPane(tablePanel);
  83. scrollPanel.setPreferredSize(new Dimension(300,300));
  84. fillTablePanel();
  85. return scrollPanel;
  86. }
  87. private void fillTablePanel() {
  88. //Clear old Data
  89. tablePanel.removeAll();
  90. checkboxList.clear();
  91. //HeadPanel
  92. int lineSpace = 20;
  93. JPanel headPanel = new JPanel();
  94. headPanel.setLayout(new BoxLayout(headPanel, BoxLayout.LINE_AXIS));
  95. headPanel.add(new JLabel("FILTER"));
  96. JButton updateButton = new JButton();
  97. updateButton.setIcon(new ImageIcon(Import.loadImage("images/canvas/replace.png", 15, 15)));
  98. updateButton.addActionListener(action -> {
  99. this.updateFilterList();
  100. content.updateUI();
  101. });
  102. updateButton.setToolTipText("Manuel updates the filter list with all canvas objects.");
  103. headPanel.add(updateButton);
  104. headPanel.add(Box.createHorizontalGlue());
  105. headPanel.add(new JLabel("SelectAll"));
  106. JCheckBox selectAllCheckbox = new JCheckBox();
  107. selectAllCheckbox.setSelected(true);
  108. selectAllCheckbox.addItemListener(input -> checkboxList.forEach(box -> box.setSelected(selectAllCheckbox.isSelected())));
  109. headPanel.add(selectAllCheckbox);
  110. tablePanel.add(headPanel);
  111. //Entrys
  112. for(HolonObject hObject: objectMap.keySet().stream().sorted(Comparator.comparing(AbstractCanvasObject::getName)).toList()) {
  113. //Entry
  114. JPanel entryPanel = new JPanel();
  115. entryPanel.setLayout(new BoxLayout(entryPanel, BoxLayout.LINE_AXIS));
  116. JLabel label = new JLabel(hObject.getName() + "[" + hObject.getId() + "]", new ImageIcon(Import.loadImage(hObject.getImagePath(), lineSpace, lineSpace)), JLabel.LEFT);
  117. entryPanel.add(label);
  118. entryPanel.add(Box.createHorizontalGlue());
  119. JCheckBox checkbox = new JCheckBox();
  120. checkbox.setSelected(true);
  121. checkbox.addItemListener(change -> objectMap.put(hObject, checkbox.isSelected()));
  122. checkboxList.add(checkbox);
  123. entryPanel.add(checkbox);
  124. tablePanel.add(entryPanel);
  125. }
  126. }
  127. private JSplitPane createParameterPanel() {
  128. JPanel choosePanel = new JPanel(null);
  129. choosePanel.setPreferredSize(new Dimension(300,200));
  130. choosePanel.setMinimumSize(new Dimension(300,200));
  131. JLabel minAmount = new JLabel("minAmountOfElements:");
  132. minAmount.setBounds(20, 60 , 200, 20);
  133. choosePanel.add(minAmount);
  134. JLabel maxAmount = new JLabel("maxAmountOfElements:");
  135. maxAmount.setBounds(20, 85 , 200, 20);
  136. choosePanel.add(maxAmount);
  137. //Integer formatter
  138. NumberFormat format = NumberFormat.getIntegerInstance();
  139. format.setGroupingUsed(false);
  140. format.setParseIntegerOnly(true);
  141. NumberFormatter integerFormatter = new NumberFormatter(format);
  142. integerFormatter.setMinimum(0);
  143. integerFormatter.setCommitsOnValidEdit(true);
  144. JFormattedTextField minAmountTextField = new JFormattedTextField(integerFormatter);
  145. minAmountTextField.setValue(this.minAmountOfElements);
  146. minAmountTextField.setToolTipText("Only positive Integer.");
  147. minAmountTextField.addPropertyChangeListener(actionEvent -> this.minAmountOfElements = Integer.parseInt(minAmountTextField.getValue().toString()));
  148. minAmountTextField.setBounds(220, 60, 50, 20);
  149. choosePanel.add(minAmountTextField);
  150. JFormattedTextField maxAmountTextField = new JFormattedTextField(integerFormatter);
  151. maxAmountTextField.setValue(this.maxAmountOfElements);
  152. maxAmountTextField.setToolTipText("Only positive Integer.");
  153. maxAmountTextField.addPropertyChangeListener(actionEvent -> this.maxAmountOfElements = Integer.parseInt(maxAmountTextField.getValue().toString()));
  154. maxAmountTextField.setBounds(220, 85, 50, 20);
  155. choosePanel.add(maxAmountTextField);
  156. JButton chooseFileButton = new JButton("Choose File");
  157. chooseFileButton.setBounds(20, 10, 200, 30);
  158. chooseFileButton.addActionListener(actionEvent -> file = openCatalogFile());
  159. choosePanel.add(chooseFileButton);
  160. JSlider bitSlider = createFlipChanceSlider();
  161. bitSlider.setBounds(10, 110, 280, 80);
  162. choosePanel.add(bitSlider);
  163. JPanel prioritySelectionPanel = new JPanel();
  164. prioritySelectionPanel.setLayout(new BoxLayout(prioritySelectionPanel, BoxLayout.PAGE_AXIS));
  165. //priorityCheckbox.setAlignmentY(0.0f);
  166. //selection
  167. prioritySelectionPanel.add(this.priorityCheckbox);
  168. priorityCheckbox.addItemListener(change -> {
  169. prioPanel.setEnabled(priorityCheckbox.isSelected());
  170. useRandomPriority = priorityCheckbox.isSelected();
  171. });
  172. priorityCheckbox.setSelected(useRandomPriority);
  173. prioritySelectionPanel.add(prioPanel);
  174. JSplitPane parameterPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, choosePanel, prioritySelectionPanel);
  175. parameterPanel.setPreferredSize(new Dimension(600,200));
  176. return parameterPanel;
  177. }
  178. private void run() {
  179. List<HolonElementSketch> holonElementCatalog = new ArrayList<>();
  180. if(file == null)file = openCatalogFile();
  181. if(file == null) return;
  182. readCatalogFromJson(file, holonElementCatalog);
  183. holonElementCatalog.forEach(HolonElementSketch::checkValues);
  184. objectMap.forEach((hObject, state) ->{
  185. //Ignore Filtered objects
  186. System.out.println("hObject:" + hObject + " state:" + state);
  187. if(!state) {
  188. return;
  189. }
  190. //Randomize
  191. hObject.clearElements();
  192. int randomAmountOfElementsToAdd = Random.nextIntegerInRange(minAmountOfElements, maxAmountOfElements + 1);
  193. for(int i = 0; i < randomAmountOfElementsToAdd; i++) {
  194. HolonElement ele = holonElementCatalog.get(Random.nextIntegerInRange(0, holonElementCatalog.size())).createHolonElement(hObject, Random.nextDouble() < randomChance );
  195. if(this.useRandomPriority) ele.setPriority(prioPanel.getPriority());
  196. hObject.add(ele);
  197. }
  198. });
  199. control.calculateStateForCurrentIteration();
  200. }
  201. public void updateFilterList() {
  202. objectMap.clear();
  203. if(control != null) {
  204. control.getModel().getCanvas().getAllHolonObjectsRecursive().forEach(object -> objectMap.put(object, true));
  205. }
  206. this.fillTablePanel();
  207. }
  208. @Override
  209. public JPanel getPanel() {
  210. return content;
  211. }
  212. @Override
  213. public void setController(Control control) {
  214. this.control = control;
  215. //Update Filter List
  216. updateFilterList();
  217. }
  218. private JSlider createFlipChanceSlider() {
  219. JSlider flipChance =new JSlider(JSlider.HORIZONTAL,0, 100, 100);
  220. flipChance.setBorder(BorderFactory.createTitledBorder("ActiveProbability"));
  221. flipChance.setMajorTickSpacing(50);
  222. flipChance.setMinorTickSpacing(5);
  223. flipChance.setPaintTicks(true);
  224. Hashtable<Integer, JLabel> labelTable = new Hashtable<>();
  225. labelTable.put(0, new JLabel("0.0") );
  226. labelTable.put(50, new JLabel("0.5") );
  227. labelTable.put(100, new JLabel("1.0") );
  228. flipChance.setToolTipText("" +randomChance);
  229. flipChance.addChangeListener(actionEvent ->{
  230. randomChance =(double)flipChance.getValue()/100.0;
  231. flipChance.setToolTipText("" +randomChance);
  232. });
  233. flipChance.setLabelTable( labelTable );
  234. flipChance.setPaintLabels(true);
  235. return flipChance;
  236. }
  237. private void readCatalogFromJson(File file, List<HolonElementSketch> catalog) {
  238. Gson gson = new Gson();
  239. try {
  240. JsonElement jsonTreeRoot = JsonParser.parseReader(new FileReader(file));
  241. if(jsonTreeRoot.isJsonArray()) {
  242. JsonArray jsonArray = jsonTreeRoot.getAsJsonArray();
  243. jsonArray.forEach(jsonObject -> {
  244. HolonElementSketch newObject= gson.fromJson( jsonObject, HolonElementSketch.class);
  245. catalog.add(newObject);
  246. });
  247. }
  248. } catch (JsonSyntaxException | JsonIOException | FileNotFoundException e) {
  249. e.printStackTrace();
  250. }
  251. }
  252. public File openCatalogFile() {
  253. JFileChooser fileChooser = new JFileChooser();
  254. fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")+"/randomizerConfigFiles/"));
  255. fileChooser.setFileFilter(new FileNameExtensionFilter("JSON Files", "json"));
  256. fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  257. fileChooser.setAcceptAllFileFilterUsed(false);
  258. int result = fileChooser.showOpenDialog(content);
  259. if(result == JFileChooser.APPROVE_OPTION) {
  260. //Found a file
  261. return fileChooser.getSelectedFile();
  262. }
  263. return null;
  264. }
  265. }