Randomizer.java 11 KB

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