<commit_before>/*
 * Writesetter is a program for creating PDF documents from text files with markup.
 * Copyright (c) 2013 <NAME> <<EMAIL>>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package writesetter.editor

import swing._
import java.awt.Font
import java.util.Date
import java.awt.FileDialog
import writesetter.{ core, storage }
import writesetter.core.PreviewType

class TextFile {

  var fileIsReadOnly = false
  var fileLatestTimeStamp = 0L
  var fileLatestUpdated = "" // fileLatestTimeStamp converted to text
  var fileName = ""
  var encoding = ""
  var dictionary = ""
  var fileDirectory = ""
  var fullFileName = ""
  var untitledIndex = 1

  def getFileKey: String = {
    if (fullFileName != "") {
      fullFileName
    } else {
      core.Environment.GetConfigFilePath("untitled " + untitledIndex.toString + ".wr")
    }
  }

  private val labelFont = storage.GUIFonts.getStandardFont(14)
  private val fileStatusLabel = new Label {
    foreground = Colors.statusLabel
    border = Swing.EmptyBorder(0, 0, 1, 20) // top, left, bottom, right
    font = labelFont
  }
  private val fullFileNameLabel = new Label {
    foreground = Colors.fileInfo
    border = Swing.EmptyBorder(0, 0, 1, 20) // top, left, bottom, right
    font = labelFont
  }
  private val fileEncodingLabel = new Label {
    foreground = Colors.fileInfo
    border = Swing.EmptyBorder(0, 0, 1, 20) // top, left, bottom, right
    font = labelFont
  }
  private val fileLatestUpdatedLabel = new Label {
    foreground = Colors.fileInfo
    border = Swing.EmptyBorder(0, 0, 1, 0) // top, left, bottom, right
    font = labelFont
  }

  val editorPropertiesPane = new BoxPanel(Orientation.Horizontal) {
    contents += fileStatusLabel
    contents += fullFileNameLabel
    contents += fileEncodingLabel
    contents += fileLatestUpdatedLabel
    background = Colors.editorBackground
    border = Swing.EmptyBorder(2, 10, 0, 0) // top, left, bottom, right
    minimumSize = new Dimension(12, 14)
  }

  def updateColors() {
    fileStatusLabel.foreground = Colors.statusLabel
    fullFileNameLabel.foreground = Colors.fileInfo
    fileEncodingLabel.foreground = Colors.fileInfo
    fileLatestUpdatedLabel.foreground = Colors.fileInfo
    editorPropertiesPane.background = Colors.editorBackground
  }

  private def updatePropertiesPane() {
    fileStatusLabel.text =
      if (fullFileName == "") "Not Saved" else if (fileIsReadOnly) "Read-Only" else "Writable"
    fullFileNameLabel.text = fullFileName
    fileEncodingLabel.text = encoding
  }

  def updateTimeStamp(t: Long) {
    fileLatestTimeStamp = t
    fileLatestUpdated = (new Date(t)).toString
    fileLatestUpdatedLabel.text = fileLatestUpdated
  }

  def updateTimeStampAfterSave() {
    updateTimeStamp(storage.FileMethods.GetTimeStamp(fullFileName))
  }

  def newText(number: Int) {
    fileName = "untitled " + number.toString
    untitledIndex = number
    fileDirectory = ""
    fullFileName = ""
    encoding = storage.Configurations.GetCharacterEncoding
    dictionary = storage.Configurations.GetDefaultDictionary
    updatePropertiesPane()
  }

  private def ensureExtensionOnFullFileName() {
    val pathList = fullFileName.split(core.Environment.FileSeparator)
    val pathLength = pathList.length
    var localFileName = if (pathLength > 0) { pathList(pathLength - 1) } else { "" }
    val dotList = localFileName.split('.')
    val dotLength = dotList.length
    if (dotLength == 1) fullFileName += ".wr"
  }

  def updateFromFullName() {
    var FileHandle = new java.io.File(fullFileName)
    fileIsReadOnly = !FileHandle.canWrite
    updateTimeStamp(FileHandle.lastModified)

    val separator = core.Environment.FileSeparator
    val pathList = fullFileName.split(separator)
    val len = pathList.length
    fileName = if (len > 0) {
      pathList(len - 1)
    } else {
      throw new Exception("Could not split file path '" + fullFileName + "' with spearator '" + separator + "'.")
    }

    fileDirectory = ""
    if (len > 1) { fileDirectory = pathList(0) }
    var index = 0
    while (index < len - 1) {
      if (index > 0) fileDirectory += core.Environment.FileSeparator
      fileDirectory += pathList(index)
      index += 1
    }
    updatePropertiesPane()
  }

  def chooseFile(forcedEncoding: String) {
    var openFileChooser = new java.awt.FileDialog(Application.top.peer, "Open File", FileDialog.LOAD)
    openFileChooser.setDirectory(storage.Configurations.GetLatestDirectory("OpenFile"))
    openFileChooser.setVisible(true)

    if (openFileChooser.getFile != null) {
      fullFileName = openFileChooser.getDirectory + openFileChooser.getFile<commit_msg>Use java.io.File methods to get filename and directory.<commit_after>/*
 * Writesetter is a program for creating PDF documents from text files with markup.
 * Copyright (c) 2013 <NAME> <<EMAIL>>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package writesetter.editor

import swing._
import java.awt.Font
import java.util.Date
import java.awt.FileDialog
import writesetter.{ core, storage }
import writesetter.core.PreviewType

class TextFile {

  var fileIsReadOnly = false
  var fileLatestTimeStamp = 0L
  var fileLatestUpdated = "" // fileLatestTimeStamp converted to text
  var fileName = ""
  var encoding = ""
  var dictionary = ""
  var fileDirectory = ""
  var fullFileName = ""
  var untitledIndex = 1

  def getFileKey: String = {
    if (fullFileName != "") {
      fullFileName
    } else {
      core.Environment.GetConfigFilePath("untitled " + untitledIndex.toString + ".wr")
    }
  }

  private val labelFont = storage.GUIFonts.getStandardFont(14)
  private val fileStatusLabel = new Label {
    foreground = Colors.statusLabel
    border = Swing.EmptyBorder(0, 0, 1, 20) // top, left, bottom, right
    font = labelFont
  }
  private val fullFileNameLabel = new Label {
    foreground = Colors.fileInfo
    border = Swing.EmptyBorder(0, 0, 1, 20) // top, left, bottom, right
    font = labelFont
  }
  private val fileEncodingLabel = new Label {
    foreground = Colors.fileInfo
    border = Swing.EmptyBorder(0, 0, 1, 20) // top, left, bottom, right
    font = labelFont
  }
  private val fileLatestUpdatedLabel = new Label {
    foreground = Colors.fileInfo
    border = Swing.EmptyBorder(0, 0, 1, 0) // top, left, bottom, right
    font = labelFont
  }

  val editorPropertiesPane = new BoxPanel(Orientation.Horizontal) {
    contents += fileStatusLabel
    contents += fullFileNameLabel
    contents += fileEncodingLabel
    contents += fileLatestUpdatedLabel
    background = Colors.editorBackground
    border = Swing.EmptyBorder(2, 10, 0, 0) // top, left, bottom, right
    minimumSize = new Dimension(12, 14)
  }

  def updateColors() {
    fileStatusLabel.foreground = Colors.statusLabel
    fullFileNameLabel.foreground = Colors.fileInfo
    fileEncodingLabel.foreground = Colors.fileInfo
    fileLatestUpdatedLabel.foreground = Colors.fileInfo
    editorPropertiesPane.background = Colors.editorBackground
  }

  private def updatePropertiesPane() {
    fileStatusLabel.text =
      if (fullFileName == "") "Not Saved" else if (fileIsReadOnly) "Read-Only" else "Writable"
    fullFileNameLabel.text = fullFileName
    fileEncodingLabel.text = encoding
  }

  def updateTimeStamp(t: Long) {
    fileLatestTimeStamp = t
    fileLatestUpdated = (new Date(t)).toString
    fileLatestUpdatedLabel.text = fileLatestUpdated
  }

  def updateTimeStampAfterSave() {
    updateTimeStamp(storage.FileMethods.GetTimeStamp(fullFileName))
  }

  def newText(number: Int) {
    fileName = "untitled " + number.toString
    untitledIndex = number
    fileDirectory = ""
    fullFileName = ""
    encoding = storage.Configurations.GetCharacterEncoding
    dictionary = storage.Configurations.GetDefaultDictionary
    updatePropertiesPane()
  }

  private def ensureExtensionOnFullFileName() {
    val fileHandle = new java.io.File(fullFileName)
    val localName = fileHandle.getName
    val dotList = localName.split('.')
    val dotLength = dotList.length
    if (dotLength == 1) fullFileName += ".wr"
  }

  def updateFromFullName() {
    val fileHandle = new java.io.File(fullFileName)
    fileIsReadOnly = !fileHandle.canWrite
    updateTimeStamp(fileHandle.lastModified)
    fileName = fileHandle.getName
    fileDirectory = fileHandle.getParent + core.Environment.FileSeparator
    updatePropertiesPane()
  }

  def chooseFile(forcedEncoding: String) {
    val openFileChooser = new java.awt.FileDialog(Application.top.peer, "Open File", FileDialog.LOAD)
    openFileChooser.setDirectory(storage.Configurations.GetLatestDirectory("OpenFile"))
    openFileChooser.setVisible(true)

    if (openFileChooser.getFile != null) {
      fullFileName = openFileChooser.getDirectory + openFileChooser.getFile
{'fraction_non_alphanumeric': 0.08979347500748279, 'fraction_numerical': 0.008580265389603912, 'mean_word_length': 3.634304207119741, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '804951', 'n_tokens_mistral': 2963, 'n_tokens_neox': 2848, 'n_words': 835}