src/Tools/isac/jEdit/src/jedit/Isac.scala
author Marco Steger <m.steger@student.tugraz.at>
Tue, 28 Sep 2010 13:09:19 +0200
branchthe isac plugin for jEdit
changeset 38028 720acd7764d2
parent 38023 fe51ebe89f9f
permissions -rw-r--r--
changed src
     1 /*
     2  * Isac.java
     3  * gadei
     4  */
     5 
     6 package isac.jedit
     7 
     8 
     9 // {{{ imports
    10 
    11 import java.awt.BorderLayout
    12 import java.awt.Dimension
    13 import java.awt.Font
    14 import java.io.BufferedReader
    15 import java.io.File
    16 import java.io.FileNotFoundException
    17 import java.io.FileReader
    18 import java.io.FileWriter
    19 import java.io.IOException
    20 
    21 import javax.swing.JFileChooser
    22 import javax.swing.JPanel
    23 import javax.swing.JScrollPane
    24 
    25 import org.gjt.sp.jedit.EBComponent
    26 import org.gjt.sp.jedit.EBMessage
    27 import org.gjt.sp.jedit.EditBus
    28 import org.gjt.sp.jedit.GUIUtilities
    29 import org.gjt.sp.jedit.View
    30 import org.gjt.sp.jedit.jEdit
    31 import org.gjt.sp.jedit.gui.DefaultFocusComponent
    32 import org.gjt.sp.jedit.gui.DockableWindowManager
    33 import org.gjt.sp.jedit.msg.PropertiesChanged
    34 import org.gjt.sp.util.Log
    35 import org.gjt.sp.util.StandardUtilities
    36 // }}}
    37 
    38 // {{{ Isac class
    39 /**
    40  * 
    41  * Isac - a dockable JPanel, a demonstration of a jEdit plugin.
    42  *
    43  */
    44 
    45 object Isac {
    46 
    47   var textArea = new IsacTextArea()
    48 
    49   val NAME = "isac"
    50   val OPTION_PREFIX = "options.isac."
    51 
    52   private final val serialVersionUID = 6412255692894321789L
    53 
    54   var filename = "IsacTestFile.txt"
    55 
    56   var defaultFilename = "IsacTestFile.txt"
    57 
    58   def focusOnDefaultComponent() {
    59     textArea.requestFocus()
    60   }
    61 
    62   def getFilename(): String ={
    63     val str = this.filename
    64     str
    65   }
    66 
    67 }
    68 
    69 class Isac(view: View, position: String) extends JPanel(new BorderLayout)
    70 {
    71   def floating: Boolean = position.equals(DockableWindowManager.FLOATING)
    72 
    73   def toolPanel = new IsacToolPanel(this)
    74 
    75 
    76   if (jEdit.getSettingsDirectory() != null) {
    77     Isac.filename = jEdit.getProperty(IsacPlugin.OPTION_PREFIX + "filepath")
    78     if (Isac.filename == null || Isac.filename.length() == 0) {
    79       Isac.filename = new String(jEdit.getSettingsDirectory()
    80                                   + File.separator + "qn.txt")
    81        jEdit.setProperty(IsacPlugin.OPTION_PREFIX + "filepath",Isac.filename)
    82     }
    83       Isac.defaultFilename = Isac.filename
    84   }
    85 
    86   add(BorderLayout.NORTH, this.toolPanel)
    87 
    88   if (floating)
    89           this.setPreferredSize(new Dimension(500, 250))
    90 
    91 
    92   Isac.textArea.setFont(IsacOptionPane.makeFont())
    93 
    94   val pane = new JScrollPane(Isac.textArea)
    95   add(BorderLayout.CENTER, pane)
    96 
    97   this.readFile()
    98 
    99   def getFilename(): String ={
   100     val str = Isac.getFilename()
   101     str
   102   }
   103   
   104   def propertiesChanged() {
   105     val propertyFilename = jEdit.getProperty(IsacPlugin.OPTION_PREFIX + "filepath");
   106     if (!StandardUtilities.objectsEqual(Isac.defaultFilename, propertyFilename)) {
   107       saveFile()
   108       toolPanel.propertiesChanged()
   109       Isac.defaultFilename = propertyFilename
   110       Isac.filename = Isac.defaultFilename
   111       readFile()
   112       }
   113     val newFont = IsacOptionPane.makeFont(): Font
   114     if (!newFont.equals(Isac.textArea.getFont())) {
   115             Isac.textArea.setFont(newFont);
   116     }
   117   }
   118 
   119   
   120   override def addNotify() {
   121     super.addNotify()
   122     EditBus.addToBus(this)
   123   }
   124 
   125 
   126   def saveFile() {
   127     if (Isac.filename == null || Isac.filename.length() == 0) {
   128 
   129     }
   130     else
   131     {
   132       val out = new FileWriter(Isac.filename)
   133       out.write(Isac.textArea.getText())
   134       out.close()
   135     }
   136   }
   137 
   138   override def removeNotify() {
   139     saveFile()
   140     super.removeNotify()
   141     EditBus.removeFromBus(this)
   142   }
   143 
   144 
   145   def chooseFile() {
   146     val paths = GUIUtilities.showVFSFileDialog(view, null,
   147                     JFileChooser.OPEN_DIALOG, false);
   148     if (paths != null && !paths(0).equals(Isac.filename)) {
   149             saveFile();
   150             Isac.filename = paths(0);
   151             toolPanel.propertiesChanged();
   152             readFile();
   153     }
   154   }
   155 
   156   def copyToBuffer() {
   157     jEdit.newFile(view)
   158     //GAdei Test view.getEditPane().getTextArea().setText(textArea.getText());
   159     view.getEditPane().getTextArea().setText("New Project: Gadei the king! BIIIIIG LOOOOOOOOL")
   160   }
   161  
   162   def readFile() {
   163     
   164     if (Isac.filename == null || Isac.filename.length() == 0)
   165 			return;
   166 
   167     var bf :BufferedReader = null
   168     var sb :StringBuffer = null
   169     try {
   170       bf = new BufferedReader(new FileReader(Isac.filename))
   171       sb = new StringBuffer(2048)
   172       var str : String = ""
   173       while ((str = bf.readLine()) != null) {
   174               sb.append(str).append('\n')
   175       }
   176       bf.close()
   177       Isac.textArea.setText(sb.toString())
   178 
   179     } catch {
   180       case _ =>
   181             Log.log(Log.ERROR, Isac, "notepad file " + Isac.filename
   182                             + " does not exist");
   183     }
   184 }
   185 
   186 }
   187 // }}}