Randomizer.java 11 KB

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