Code Sketch


bhimraj
By: Mhalsakant School
Category: Art
import scala.swing._
import java.io._
import scala.collection.mutable

case class CodeSnippet(title: String, content: String, date: String) extends Serializable
case class User(email: String, password: String, var codes: List[CodeSnippet]) extends Serializable

object BhimrajSwingApp extends SimpleSwingApplication {
  private val users = mutable.Map[String, User]()
  private val dbFile = "bhimraj_swing_database.dat"
  loadDatabase()

  def top = new MainFrame {
    title = "Bhimraj App - Code Library (Scala Swing)"
    minimumSize = new Dimension(650, 550)
    preferredSize = new Dimension(650, 550)
    
    val mainPanel = new BoxPanel(Orientation.Vertical) {
      border = Swing.EmptyBorder(20, 20, 20, 20)
    }

    contents = mainPanel
    showLoginScreen()

    // Window visible karnyasathi mahatvachi line:
    visible = true
    pack()

    def showLoginScreen(): Unit = {
      mainPanel.contents.clear()
      
      val titleLabel = new Label("? Bhimraj App - Login / Register") {
        font = new Font("Arial", java.awt.Font.BOLD, 18)
      }
      
      val emailField = new TextField(20)
      val passwordField = new PasswordField(20)
      
      val messageLabel = new Label("") {
        foreground = java.awt.Color.RED
      }

      val loginButton = new Button(Action("Login") {
        val email = emailField.text.trim
        val password = new String(passwordField.password).trim
        if (email.isEmpty || password.isEmpty) {
          messageLabel.text = "Krupaya email ani password donhi taka!"
        } else {
          users.get(email) match {
            case Some(user) if user.password == password =>
              showDashboard(user)
            case Some(_) =>
              messageLabel.text = "? Chukaicha password!"
            case None =>
              messageLabel.text = "? Account milale nahi. Adhi Register kara."
          }
        }
      })

      val registerButton = new Button(Action("Register") {
        val email = emailField.text.trim
        val password = new String(passwordField.password).trim
        if (email.isEmpty || password.isEmpty) {
          messageLabel.text = "Email ani password rikama asu shakat nahi!"
        } else if (users.contains(email)) {
          messageLabel.text = "? He email adhipasun registered aahe. Login kara."
        } else {
          users(email) = User(email, password, List())
          saveDatabase()
          messageLabel.foreground = new java.awt.Color(0, 128, 0)
          messageLabel.text = "? Account tayar zale! Aata Login kara."
        }
      })

      mainPanel.contents += titleLabel
      mainPanel.contents += Swing.VStrut(15)
      mainPanel.contents += new Label("Email ID:")
      mainPanel.contents += emailField
      mainPanel.contents += Swing.VStrut(10)
      mainPanel.contents += new Label("Password:")
      mainPanel.contents += passwordField
      mainPanel.contents += Swing.VStrut(15)
      
      val buttonBox = new BoxPanel(Orientation.Horizontal) {
        contents += loginButton
        contents += Swing.HStrut(10)
        contents += registerButton
      }
      mainPanel.contents += buttonBox
      mainPanel.contents += Swing.VStrut(15)
      mainPanel.contents += messageLabel

      mainPanel.revalidate()
      mainPanel.repaint()
    }

    def showDashboard(user: User): Unit = {
      mainPanel.contents.clear()

      val welcomeLabel = new Label(s"Swagat aahe, ${user.email}!") {
        font = new Font("Arial", java.awt.Font.BOLD, 16)
      }
      
      val logoutButton = new Button(Action("Logout") {
        showLoginScreen()
      })

      val topBox = new BoxPanel(Orientation.Horizontal) {
        contents += welcomeLabel
        contents += Swing.HGlue
        contents += logoutButton
      }

      val codeTitleField = new TextField(25)
      val codeContentArea = new TextArea(5, 25)
      
      val libraryArea = new TextArea(8, 40) {
        editable = false
      }
      
      val refreshLibrary = () => {
        if (user.codes.isEmpty) {
          libraryArea.text = "Tumchya library madhe konatcha code nahiye."
        } else {
          libraryArea.text = user.codes.zipWithIndex.map { case (c, i) =>
            s"[${i+1}] Title: ${c.title} (${c.date})\nContent:\n${c.content}\n-----------------------------------\n"
          }.mkString("\n")
        }
      }
      refreshLibrary()

      val saveCodeButton = new Button(Action("Library madhe Save Kara") {
        val title = codeTitleField.text.trim
        val content = codeContentArea.text.trim
        if (title.isEmpty || content.isEmpty) {
          Dialog.showMessage(mainPanel, "Title ani code donhi takane garjeche aahe!", "Error", Dialog.Message.Error)
        } else {
          val date = java.time.LocalDateTime.now().toString
          user.codes = user.codes :+ CodeSnippet(title, content, date)
          saveDatabase()
          codeTitleField.text = ""
          codeContentArea.text = ""
          refreshLibrary()
          Dialog.showMessage(mainPanel, "Code safaltapoorvak save zala!", "Success", Dialog.Message.Info)
        }
      })

      val copyButton = new Button(Action("? Sevatcha Code Copy Kara") {
        if (user.codes.nonEmpty) {
          val lastCode = user.codes.last.content
          val clipboard = java.awt.Toolkit.getDefaultToolkit.getSystemClipboard
          clipboard.setContents(new java.awt.datatransfer.StringSelection(lastCode), null)
          Dialog.showMessage(mainPanel, "Code clipboard var safaltapoorvak copy zala!", "Copied", Dialog.Message.Info)
        } else {
          Dialog.showMessage(mainPanel, "Copy karnyasathi konatcha code nahiye!", "Warning", Dialog.Message.Warning)
        }
      })

      mainPanel.contents += topBox
      mainPanel.contents += Swing.VStrut(10)
      mainPanel.contents += new Label("Codeche Nav (Title):")
      mainPanel.contents += codeTitleField
      mainPanel.contents += new Label("Code Content:")
      mainPanel.contents += new ScrollPane(codeContentArea)
      mainPanel.contents += Swing.VStrut(5)
      mainPanel.contents += saveCodeButton
      mainPanel.contents += Swing.VStrut(10)
      mainPanel.contents += new Label("Tumchi Code Library ?:")
      mainPanel.contents += new ScrollPane(libraryArea)
      mainPanel.contents += Swing.VStrut(5)
      mainPanel.contents += copyButton

      mainPanel.revalidate()
      mainPanel.repaint()
    }
  }

  def saveDatabase(): Unit = {
    try {
      val oos = new ObjectOutputStream(new FileOutputStream(dbFile))
      oos.writeObject(users.toMap)
      oos.close()
    } catch { case _: Exception => }
  }

  def loadDatabase(): Unit = {
    val file = new File(dbFile)
    if (file.exists()) {
      try {
        val ois = new ObjectInputStream(new FileInputStream(file))
        val saved = ois.readObject().asInstanceOf[Map[String, User]]
        ois.close()
        users.clear()
        users ++= saved
      } catch { case _: Exception => }
    }
  }
}