From d517e65e733e4ba56987074755e9014ae42f0f78 Mon Sep 17 00:00:00 2001 From: Bryan Weber Date: Fri, 22 Jun 2012 23:27:02 -0400 Subject: [PATCH] adding menu for syntax highlighting xml file selection --- README.md | 2 +- src/clooj/core.clj | 10 +++++---- src/clooj/style.clj | 49 ++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0b5f185..d013101 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The source code editor offers a few simple things to make writing clojure code e * Press ctrl-ENTER to send either the nearest root form or the selected text to the REPL. * Double-click a paren to highlight a form * Source files are continuously saved in the background to prevent accidental loss of your work in the event of a crash. - * Syntax highlighting (using the [RSyntaxTextArea](http://fifesoft.com/rsyntaxtextarea/) library) + * Customizable syntax highlighting (using the [RSyntaxTextArea](http://fifesoft.com/rsyntaxtextarea/) library) ### clojure projects Each clojure project corresponds to a project directory somewhere in the file system, containing a src directory. Inside the src directory is the source code hierarchy, composed of directories and .clj files. Note this directory structure is completely compatible with the lein and cake clojure build programs and you are encouraged to use one of these from the command line in conjunction with the clooj editor. Clicking different source files in the projects tree will automatically change the source file currently being edited, as well as switch the REPL to the appropriate namespace. diff --git a/src/clooj/core.clj b/src/clooj/core.clj index 3cbbc05..6f86420 100644 --- a/src/clooj/core.clj +++ b/src/clooj/core.clj @@ -60,7 +60,7 @@ remove-text-change-listeners get-text-str scroll-to-line get-directories)] [clooj.indent :only (setup-autoindent fix-indent-selected-lines)] - [clooj.style :only (get-monospaced-fonts show-font-window)] + [clooj.style :only (get-monospaced-fonts show-font-window show-theme-window load-theme)] [clooj.navigate :only (attach-navigation-keys)]) (:require [clojure.main :only (repl repl-prompt)] [clojure.set]) @@ -92,7 +92,7 @@ token-type)))] (.. rsta getDocument (setTokenMakerFactory tmf)) rsta)) - + (defn make-text-area [wrap] (doto (RSyntaxTextArea.) (.setAnimateBracketMatching false) @@ -737,7 +737,8 @@ ["Increase font size" nil "cmd1 PLUS" #(grow-font app)] ["Decrease font size" nil "cmd1 MINUS" #(shrink-font app)] ["Choose font..." nil nil #(apply show-font-window - app set-font @current-font)]))) + app set-font @current-font)] + ["Choose theme..." nil nil #(show-theme-window app)]))) (defn add-visibility-shortcut [app] @@ -777,7 +778,8 @@ (let [tree (app :docs-tree)] (load-expanded-paths tree) (load-tree-selection tree)) - (load-font app))) + (load-font app) + (load-theme app))) (defn -show [] (reset! embedded true) diff --git a/src/clooj/style.clj b/src/clooj/style.clj index 5994dd0..67d68a0 100644 --- a/src/clooj/style.clj +++ b/src/clooj/style.clj @@ -5,14 +5,18 @@ (ns clooj.style (:import + (java.io FileInputStream) (javax.swing JComboBox JFrame JList JScrollPane JSplitPane - ListSelectionModel SpringLayout) + ListSelectionModel SpringLayout JOptionPane) (javax.swing.text SimpleAttributeSet StyleConstants) (java.awt Color Font FontMetrics GraphicsEnvironment) (java.awt.image BufferedImage) (javax.swing.event ListSelectionListener) + (org.fife.ui.rsyntaxtextarea Theme) (java.util Vector)) - (:use [clooj.utils :only (constrain-to-parent make-split-pane)])) + (:use [clooj.utils :only (constrain-to-parent make-split-pane choose-file + awt-event write-value-to-prefs clooj-prefs + read-value-from-prefs)])) (def graphics-object (memoize (fn [] (.createGraphics @@ -89,4 +93,43 @@ (when-not @font-window (reset! font-window (create-font-window app set-font init-name init-size))) (.show @font-window)) - \ No newline at end of file + +;; theme (optional) +(defonce current-theme (atom nil)) + +(defn set-theme [app apply-to] + (if @current-theme (.apply @current-theme (get app apply-to)))) + +(defn refresh-theme [app] + (do + (set-theme app :doc-text-area) + (set-theme app :repl-out-text-area) + (set-theme app :repl-in-text-area) + (set-theme app :help-text-area))) + +(defn swap-theme [input-stream] + (swap! current-theme + (fn [old-value new-value] + (let [new-theme (Theme/load (FileInputStream. new-value))] + (write-value-to-prefs clooj-prefs "app-theme" new-value) + new-theme)) + input-stream)) + +(defn load-theme [app] + (try + (let [theme-file (read-value-from-prefs clooj-prefs "app-theme")] + (swap-theme theme-file) + (refresh-theme app)) + (catch Exception e (println "pref not set, this is ok")))) + +(defn show-theme-window [app] + (try + (when-let [dir (choose-file (app :frame) "Select a theme file" "" true)] + (awt-event + (let [path (.getAbsolutePath dir)] + (swap-theme path) + (refresh-theme app)))) + (catch Exception e (do (JOptionPane/showMessageDialog nil + "Invalid theme file." + "Oops" JOptionPane/ERROR_MESSAGE) + (.printStackTrace e)))))