import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Locale; import java.util.ResourceBundle; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JToolBar; import javax.swing.text.StyledEditorKit; import eln.editors.GenericEditorWrapper; public class ExampleELNEditor extends GenericEditorWrapper{ /** Underlying frame */ JFrame currentFrame; /** Editor textarea */ JTextArea mTextEditor; /** Constructor */ public ExampleELNEditor(){ super(); } /** Launch - called by the parent class to launch the editor * (this is required) */ public void Launch(){ createDisplay(); } /** Launch - called by the parent class to launch the editor * @param byte[] data for editing existing node * (this is required) */ public void Launch(byte[] data){ createDisplay(); mTextEditor.setText(new String(data)); } /** Launch - called by the parent class to launch the editor * @param String dataref for editing existing node * (this is required) */ public void Launch(String dataref){ createDisplay(); byte[] data = new byte[0]; try{ FileInputStream fis = new FileInputStream(dataref); data = new byte[fis.available()]; fis.read(data); fis.close(); }catch (IOException e) { e.printStackTrace(); } mTextEditor.setText(new String(data)); } /** getIcon - get the editor icon * @returns Image containing the editor icon * (this extension is optional) */ public Image getIcon(){ return null; } /** getLabel - get the editor label * @returns String containing the editor label * (this extension is optional) */ public String getLabel(){ return "Example Editor"; } /** * Save button is pushed, entered text is saved to the notebook client. * @param ActionEvent - the event performed on the button */ public void SaveButton_actionPerformed(ActionEvent e) { // store the entered text in the data field String text = mTextEditor.getText(); /* * SAVE VALUE TO CLIENT * Save Options * save(String reference(the path to the data), String mimeType, String Label) * save(byte[] value, String mimeType, String Label) */ try{ File f = new File("tmp.txt"); FileOutputStream fout = new FileOutputStream(f); fout.write(text.getBytes()); fout.close(); ///SAVE BY REFERENCE EXAMPLE save(f.getAbsolutePath(), "text/plain", "ExampleEditor File"); }catch (IOException ioe) { ///SAVE BY DATA EXAMPLE save(text.getBytes(), "text/plain", "ExampleEditor File"); } currentFrame.dispose(); } // SaveButton_actionPerformed //BUILD EXAMPLE GUI /** * createDisplay - Creates the display for your editor. */ private void createDisplay() { currentFrame = new JFrame(); Locale currentLocale = Locale.getDefault(); String editorL, saveL, cancelL; editorL = "ExampleEditor"; saveL = "Save"; cancelL = "Cancel"; // Set the frame title, size, layout, and background color currentFrame.setTitle(editorL); currentFrame.setSize (400, 300); currentFrame.setBackground (Color.gray); currentFrame.getContentPane().setLayout(new BorderLayout()); // instantiate a JPanel, set its layout, and add it to the contentPane JPanel topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); currentFrame.getContentPane().add(topPanel, BorderLayout.CENTER); // Create a TextPane with a StyledEditorKit. StyledEditorKit editorKit = new StyledEditorKit(); mTextEditor = new JTextArea (); mTextEditor.setBackground(Color.white); mTextEditor.setVisible(true); // Add the pane to a scrollpane for scolling capabilites JScrollPane scrollPane = new JScrollPane(); scrollPane.getViewport().add(mTextEditor); topPanel.add(scrollPane, BorderLayout.CENTER); // Instantiate a new panel for the buttons JPanel buttonPanel = new JPanel(); // add the add and canel buttons to the panel JButton mSaveButton = new JButton(saveL); mSaveButton.setToolTipText("Save this note to the client"); buttonPanel.add(mSaveButton); JButton cancelButton = new JButton(cancelL); cancelButton.setToolTipText("Exit this editor."); buttonPanel.add(cancelButton); // add adapters as actionListener to the buttons mSaveButton.addActionListener(new ExampleEditor_SaveButton_actionAdapter(this)); cancelButton.addActionListener(new ExampleEditor_CancelButton_actionAdapter(this)); // add the buttonPanel to the main panel topPanel.add(buttonPanel, BorderLayout.SOUTH); // add a window listener currentFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { // close the gui currentFrame.dispose(); } }); // enable buttons according to readonly mode boolean enabled = true; mSaveButton.setEnabled(enabled); mTextEditor.setEnabled(enabled); topPanel.setVisible(true); currentFrame.setVisible(true); } /** * Cancel button is pushed and the TextEditor quits. * @param ActionEvent - the event performed on the button */ public void CancelButton_actionPerformed(ActionEvent e) { // the class disposes of itself currentFrame.dispose(); } // CancelButton_actionPerformed class ExampleEditor_SaveButton_actionAdapter implements java.awt.event.ActionListener{ ExampleELNEditor adaptee; public ExampleEditor_SaveButton_actionAdapter(ExampleELNEditor adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.SaveButton_actionPerformed(e); } } class ExampleEditor_CancelButton_actionAdapter implements java.awt.event.ActionListener{ ExampleELNEditor adaptee; public ExampleEditor_CancelButton_actionAdapter(ExampleELNEditor adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.CancelButton_actionPerformed(e); } } }