@Override public void keyPressed(KeyEvent e) { // Do not edit neural web while user is selecting key bindings if (expandContractKey.isActive() || newNeuronKey.isActive() || newClusterKey.isActive() || deleteKey.isActive()) return; try { if (e.getKeyChar() == expandContractKey.getText().charAt(0)) { if (selectedCluster != null && selectedCluster.clusterNum != 0) selectedCluster.expanded = !selectedCluster.expanded; else if (selectedNeuron != null) selectedNeuron.cluster.expanded = !selectedNeuron.cluster.expanded; redraw = true; } else if (e.getKeyChar() == newNeuronKey.getText().charAt(0)) { Cluster cluster; if (selectedCluster != null) cluster = selectedCluster; else if (selectedNeuron != null) cluster = selectedNeuron.cluster; else if (clusters.size() > 2) { cluster = clusters.get(2); } else { WindowTools.informationWindow( "Error: You cannot add a neuron without a cluster, please select a Neuron or a Cluster.", "Error"); return; } int x = Math.min(Main.canvasWidth - 11, Math.max(10, mouseX)); int y = Math.min(Main.canvasHeight - 33, Math.max(33, mouseY)); neurons.add(new Neuron(x, y, cluster)); redraw = true; } else if (e.getKeyChar() == newClusterKey.getText().charAt(0)) { int x = Math.min(Main.canvasWidth - 11, Math.max(10, mouseX)); int y = Math.min(Main.canvasHeight - 33, Math.max(33, mouseY)); clusters.add(new Cluster(x, y)); redraw = true; } else if (e.getKeyChar() == deleteKey.getText().charAt(0)) { if (selectedCluster != null) selectedCluster.delete(); else if (selectedNeuron != null) selectedNeuron.delete(); else if (selectedConnection != null) selectedConnection.delete(); redraw = true; } } catch (StringIndexOutOfBoundsException sioobe) { WindowTools.informationWindow( "Invalid keybinding, please select a character for each action.", "Whoops!"); } }
private void delete() { if (this.clusterNum <= 0) return; if (!WindowTools.confirmationWindow( "Are you sure you want to delete this cluster?", "Delete confirmation")) return; if (selectedCluster == this) { selectCluster(null); structureBeingMoved = false; } for (int i = 0; i < neurons.size(); i++) if (neurons.get(i).cluster == this) { neurons.remove(i); i--; } clusters.remove(this); redraw = true; }