📄 jmacroeditor.java
字号:
if (n == JOptionPane.YES_OPTION) { getNeuralNet().getMacroPlugin().getMacroManager().removeMacro(actualMacro); String removed = actualMacro; actualMacro = null; list.removeElement(removed); } }//GEN-LAST:event_removeMenuItemActionPerformed private void addMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addMenuItemActionPerformed // Add your handling code here: String newName = getNewName("Add a macro", "Insert the name of the new Macro:", "newMacro"); if (newName != null) { getNeuralNet().getMacroPlugin().getMacroManager().addMacro(newName, ""); list.addElement(newName); jList1.setSelectedIndex(list.size() - 1); } }//GEN-LAST:event_addMenuItemActionPerformed /* Ask the user for a new name and loops until the name is valid */ private String getNewName(String title, String message, String initialValue) { boolean ok = false; String errorMsg = "ERROR: The name already exists.\n"; String newName = this.showInputDialog(title, message, initialValue); if (newName != null) while (!ok) { if (getNeuralNet().getMacroPlugin().getMacroManager().getMacro(newName) != null) { newName = this.showInputDialog(title, errorMsg+message, newName); if (newName == null) ok = true; } else ok = true; } return newName; } private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged saveMacro(); // Open the new selected macro int index = jList1.getSelectedIndex(); if (index > -1) { String name = ((String)jList1.getModel().getElementAt(index)); String macroText = getNeuralNet().getMacroPlugin().getMacroManager().getMacro(name); if (macroText.trim().equals(new String(""))) macroText = "/* " + name + " */\n"; ta.setText(macroText); actualMacro = name; enableMenuItems(actualMacro); } else jList1.setSelectedIndex(0); }//GEN-LAST:event_jList1ValueChanged private void enableMenuItems(String name) { boolean system = getNeuralNet().getMacroPlugin().getMacroManager().isEventMacro(name); if (system) { removeMenuItem.setEnabled(false); renameMenuItem.setEnabled(false); } else { removeMenuItem.setEnabled(true); renameMenuItem.setEnabled(true); } } private void enableMacroMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enableMacroMenuItemActionPerformed // Add your handling code here: neuralNet.setScriptingEnabled(enableMacroMenuItem.isSelected()); }//GEN-LAST:event_enableMacroMenuItemActionPerformed private void selectAllMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllMenuItemActionPerformed // Add your handling code here: ta.selectAll(); }//GEN-LAST:event_selectAllMenuItemActionPerformed private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed // Add your handling code here: saveAs(); }//GEN-LAST:event_saveAsMenuItemActionPerformed private void importMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importMenuItemActionPerformed // Add your handling code here: JFileChooser jfc = new JooneFileChooser(); jfc.setDialogTitle("Macro Editor - Import File..."); FileFilter bsh = new FileFilter() { public boolean accept(File checkFile) { // still display directories for navigation if (checkFile.isDirectory()) { return true; } else { boolean isFileExtensionMatch = false; String[] extension = getFileExtension(); for(int i=0; i<extension.length; i++) { if(checkFile.getName().endsWith("." + extension[i])) { isFileExtensionMatch = true; break; } } return isFileExtensionMatch; } } public String getDescription() { return getFileDescription(); } }; jfc.addChoosableFileFilter(bsh); if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { setFileName(jfc.getSelectedFile().getAbsolutePath()); File ff = new File(fileName); String txt = readFile(ff); if (txt != null) ta.setText(txt); } }//GEN-LAST:event_importMenuItemActionPerformed private void pasteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteMenuItemActionPerformed // Add your handling code here: ta.paste(); }//GEN-LAST:event_pasteMenuItemActionPerformed private void copyMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyMenuItemActionPerformed // Add your handling code here: ta.copy(); }//GEN-LAST:event_copyMenuItemActionPerformed private void cutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cutMenuItemActionPerformed // Add your handling code here: ta.cut(); }//GEN-LAST:event_cutMenuItemActionPerformed private void runMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runMenuItemActionPerformed // Add your handling code here: getNeuralNet().getMacroPlugin().runMacro(ta.getText()); }//GEN-LAST:event_runMenuItemActionPerformed private void macroMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_macroMenuActionPerformed // Add your handling code here: }//GEN-LAST:event_macroMenuActionPerformed private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed saveMacro(); this.setVisible(false); //System.exit(0); }//GEN-LAST:event_exitMenuItemActionPerformed /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm //System.exit(0); saveMacro(); }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { NeuralNet nn = new NeuralNet(); nn.setMacroPlugin(new MacroPlugin()); JMacroEditor jte = new JMacroEditor(nn); jte.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { System.exit(0); } }); jte.show(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenuItem addMenuItem; private javax.swing.JRadioButtonMenuItem beanShellMenuItem; private javax.swing.ButtonGroup buttonGroup1; private javax.swing.JMenuItem copyMenuItem; private javax.swing.JMenuItem cutMenuItem; private javax.swing.JMenu editMenu; private javax.swing.JCheckBoxMenuItem enableMacroMenuItem; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JMenu fileMenu; private javax.swing.JRadioButtonMenuItem groovyMenuItem; private javax.swing.JMenuItem importMenuItem; private javax.swing.JList jList1; private javax.swing.JMenu jMenuLanguage; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JSeparator jSeparator4; private javax.swing.JSeparator jSeparator5; private javax.swing.JSplitPane jSplitPane1; private javax.swing.JMenu macroMenu; private javax.swing.JMenuBar menuBar; private javax.swing.JMenuItem pasteMenuItem; private javax.swing.JMenuItem removeMenuItem; private javax.swing.JMenuItem renameMenuItem; private javax.swing.JMenuItem runMenuItem; private javax.swing.JMenuItem saveAsMenuItem; private javax.swing.JMenuItem selectAllMenuItem; private javax.swing.JMenuItem setRateMenuItem; // End of variables declaration//GEN-END:variables public void setText(String newText) { ta.setText(newText); } public String getText() { return ta.getText(); } /** Getter for property neuralNet. * @return Value of property NeuralNet. */ public org.joone.net.NeuralNet getNeuralNet() { return neuralNet; } /** Setter for property neuralNet. * @param neuralNet New value of property NeuralNet. */ public void setNeuralNet(org.joone.net.NeuralNet neuralNet) { this.neuralNet = neuralNet; } /** * Reading the content of the file. * * @param p_file the content of the file that is to be read. * @return the content of the file. */ private String readFile(File p_file) { String str = null; FileReader reader = null; try { reader = new FileReader(p_file); int tch = new Long(p_file.length()).intValue(); m_buf = new char[tch]; int nch = reader.read(m_buf); if (nch != -1) str = new String(m_buf, 0, nch); reader.close(); } catch (FileNotFoundException fnfe) { log.warn("File '" + p_file + "' not found. MEssage is : " + fnfe.getMessage(), fnfe) ; return str; } catch (IOException ioe) { log.error( "File '" + m_dir + "' input/output error. Message is : " + ioe.getMessage(), ioe ); } return str; } /** Getter for property FileDescription. * @return Value of property FileDescription. */ public java.lang.String getFileDescription() { return FileDescription; } /** Setter for property FileDescription. * @param FileDescription New value of property FileDescription. */ public void setFileDescription(java.lang.String FileDescription) { this.FileDescription = FileDescription; } /** Getter for property FileExtension. * @return Value of property FileExtension. */ public java.lang.String[] getFileExtension() { return (String[])FileExtension.clone(); } /** Setter for property FileExtension. * @param FileExtension New value of property FileExtension. */ public void setFileExtension(java.lang.String[] FileExtension) { this.FileExtension = (String[])FileExtension.clone(); } private void saveAs() { JFileChooser jfc; if (fileName == null) jfc = new JFileChooser(); else jfc = new JFileChooser(new File(fileName)); jfc.setDialogTitle("Macro Editor - Save as..."); if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { setFileName(jfc.getSelectedFile().getAbsolutePath()); save(); } } // TO DO // Add some comments private void save() { try { FileWriter fw = new FileWriter(new File(fileName)); fw.write(ta.getText()); fw.flush(); fw.close(); } catch (IOException ioe) { log.warn( "IOException thrown. Message is +" + ioe.getMessage(), ioe ); } } /** Getter for property fileName. * @return Value of property fileName. */ public java.lang.String getFileName() { return fileName; } /** Setter for property fileName. * @param fileName New value of property fileName. */ public void setFileName(java.lang.String fileName) { this.fileName = fileName; if (fileName != null) this.setTitle(fileName); else this.setTitle("New File"); } private void fillList() { Hashtable macros = getNeuralNet().getMacroPlugin().getMacroManager().getMacros(); list = new javax.swing.DefaultListModel(); for (Enumeration keys = macros.keys(); keys.hasMoreElements();) { String key = (String)keys.nextElement(); list.addElement(key); } jList1.setModel(list); jList1.setSelectedIndex(0); } private String showInputDialog(String title, String message, String initialValue) { JOptionPane pane = new JOptionPane(message, JOptionPane.QUESTION_MESSAGE); pane.setWantsInput(true); pane.setInitialSelectionValue(initialValue); pane.setOptionType(JOptionPane.OK_CANCEL_OPTION); JDialog dialog = pane.createDialog(this, title); dialog.show(); int selectedButton = ((Integer)pane.getValue()).intValue(); if (selectedButton == 0) { Object selectedValue = pane.getInputValue(); if(selectedValue != null) if(selectedValue instanceof String) return ((String)selectedValue); } return null; } // Saves the last edited macro private void saveMacro() { if (actualMacro != null) { String oldText = getNeuralNet().getMacroPlugin().getMacroManager().getMacro(actualMacro); if (!oldText.equals(ta.getText())) { getNeuralNet().getMacroPlugin().getMacroManager().addMacro(actualMacro, ta.getText()); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -