Revision: 13845
          http://gate.svn.sourceforge.net/gate/?rev=13845&view=rev
Author:   markagreenwood
Date:     2011-05-21 09:19:29 +0000 (Sat, 21 May 2011)

Log Message:
-----------
a little bit of code tidying and added a licence header

Modified Paths:
--------------
    gate/trunk/plugins/Groovy/src/gate/groovy/gui/ScriptPREditor.java

Modified: gate/trunk/plugins/Groovy/src/gate/groovy/gui/ScriptPREditor.java
===================================================================
--- gate/trunk/plugins/Groovy/src/gate/groovy/gui/ScriptPREditor.java   
2011-05-21 01:15:57 UTC (rev 13844)
+++ gate/trunk/plugins/Groovy/src/gate/groovy/gui/ScriptPREditor.java   
2011-05-21 09:19:29 UTC (rev 13845)
@@ -1,3 +1,15 @@
+/*
+ * ScriptPREditor
+ * 
+ * Copyright (c) 2011, The University of Sheffield.
+ * 
+ * This file is part of GATE (see http://gate.ac.uk/), and is free software,
+ * licenced under the GNU Library General Public License, Version 3, June 2007
+ * (in the distribution as file licence.html, and also available at
+ * http://gate.ac.uk/gate/licence.html).
+ * 
+ * Mark A. Greenwood, 20/05/2011
+ */
 package gate.groovy.gui;
 
 import gate.Resource;
@@ -16,7 +28,6 @@
 import java.awt.event.ActionListener;
 import java.io.File;
 import java.io.FileWriter;
-import java.io.IOException;
 
 import javax.swing.Box;
 import javax.swing.JButton;
@@ -24,6 +35,11 @@
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
 
+/**
+ * A VR for viewing and editing the script behind a Groovy Scripting PR
+ * 
+ * @author Mark A. Greenwood
+ */
 @CreoleResource(name = "Script Editor", comment = "Editor for the Groovy 
script behind this PR", guiType = GuiType.LARGE, mainViewer = true, 
resourceDisplayed = "gate.groovy.ScriptPR")
 public class ScriptPREditor extends AbstractVisualResource implements
                                                           ProgressListener,
@@ -45,20 +61,33 @@
 
   protected void initGuiComponents() {
     setLayout(new BorderLayout());
+
+    // create a Groovy source code editor, register to listen for changes to 
the
+    // source code and then add it as the main component of the VR
     editor = new ConsoleTextEditor();
     editor.getTextEditor().getDocument().addDocumentListener(this);
     add(editor, BorderLayout.CENTER);
 
-    btnSave = new JButton("Save and Reinitialize", 
MainFrame.getIcon("crystal-clear-app-download-manager"));
+    // create the "Save and Reinitialize" button
+    btnSave =
+            new JButton("Save and Reinitialize",
+                    MainFrame.getIcon("crystal-clear-app-download-manager"));
     btnSave.addActionListener(new ActionListener() {
 
       public void actionPerformed(ActionEvent e) {
         try {
+          // when the save button is clicked write the contents of the editor
+          // out into the script file the PR is backed by and then...
           FileWriter out = new FileWriter(file);
           out.write(editor.getTextEditor().getText());
           out.flush();
           out.close();
+
+          // re-init the PR so the script is reloaded
           pr.reInit();
+
+          // we have reloaded so there won't be any changes that can be
+          // saved/reverted so disable the buttons
           btnRevert.setEnabled(false);
           btnSave.setEnabled(false);
         } catch(Exception ioe) {
@@ -67,26 +96,35 @@
       }
     });
 
-    btnRevert = new JButton("Revert Changes", 
MainFrame.getIcon("crystal-clear-action-reload"));
+    // create the "Revert Changes" button
+    btnRevert =
+            new JButton("Revert Changes",
+                    MainFrame.getIcon("crystal-clear-action-reload"));
     btnRevert.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
+        // when the button is clicked just set the source back to that storred
+        // in the PR instance
         editor.getTextEditor().setText(pr.getGroovySrc());
+
+        // we have reverted so there won't be any changes that can be
+        // saved/reverted so disable the buttons
         btnRevert.setEnabled(false);
         btnSave.setEnabled(false);
       }
     });
 
+    // add both buttons to a toolbar at the top of the VR
     JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
     toolbar.setFloatable(false);
     toolbar.add(btnSave);
     toolbar.add(Box.createHorizontalStrut(5));
     toolbar.add(btnRevert);
-
     add(toolbar, BorderLayout.NORTH);
   }
 
   public void setTarget(Object target) {
 
+    // make sure we are being given a target that we know how to display
     if(target == null) return;
     if(!(target instanceof ScriptPR)) { throw new GateRuntimeException(this
             .getClass().getName()
@@ -96,24 +134,31 @@
             + target.getClass().getName()
             + " is not a " + ScriptPR.class.getName() + "!"); }
 
+    // if this VR is being reused then stop listening to changes from the
+    // previous target
     if(pr != null) {
       pr.removeProgressListener(this);
     }
 
+    // store the PR we are displaying so we can keep refering to it
     pr = (ScriptPR)target;
 
+    // get the script file or null if loaded from a non-file url
     try {
       file = Files.fileFromURL(pr.getScriptURL());
     } catch(Exception e) {
       file = null;
     }
 
+    // get the editor to display the script
     editor.getTextEditor().setText(pr.getGroovySrc());
 
+    // disable editing if we loaded from a URL
     editor.getTextEditor().setEditable(file != null);
     btnSave.setEnabled(false);
     btnRevert.setEnabled(false);
 
+    // listen out for updates to the PR so we can keep in sync
     pr.addProgressListener(this);
   }
 
@@ -126,16 +171,17 @@
   }
 
   public void changedUpdate(DocumentEvent e) {
-    // ignore these
-
+    // ignore these events
   }
 
   public void insertUpdate(DocumentEvent e) {
+    // the script has changed so enable the toolbar buttons
     btnRevert.setEnabled(true);
     btnSave.setEnabled(file != null);
   }
 
   public void removeUpdate(DocumentEvent e) {
+    // the script has changed so enable the toolbar buttons
     btnRevert.setEnabled(true);
     btnSave.setEnabled(file != null);
   }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
What Every C/C++ and Fortran developer Should Know!
Read this article and learn how Intel has extended the reach of its 
next-generation tools to help Windows* and Linux* C/C++ and Fortran 
developers boost performance applications - including clusters. 
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to