[sikuli] 221/385: added a Set Type action to the IDE tab context menu: allows to switch the scripting language of the tab (TODO: use popSelect for more languages/content types)

Gilles Filippini pini at moszumanska.debian.org
Sun Jun 29 19:26:16 UTC 2014


This is an automated email from the git hooks/post-receive script.

pini pushed a commit to tag upstream/1.1.0_beta1
in repository sikuli.

commit ac72eba3a99275f577047d2d1e31f2bf321a0433
Author: Raimund Hocke <info at its-me-raiman.de>
Date:   Tue Feb 18 11:35:24 2014 +0100

    added a Set Type action to the IDE tab context menu: allows to switch the scripting language of the tab (TODO: use popSelect for more languages/content types)
---
 .../src/main/java/org/sikuli/basics/Settings.java  |   2 +
 IDE/src/main/java/org/sikuli/ide/EditorPane.java   | 106 ++++++++++++++++-----
 .../java/org/sikuli/ide/SikuliIDEPopUpMenu.java    |  29 +++++-
 3 files changed, 113 insertions(+), 24 deletions(-)

diff --git a/Basics/src/main/java/org/sikuli/basics/Settings.java b/Basics/src/main/java/org/sikuli/basics/Settings.java
index c4af0ea..09c5db7 100644
--- a/Basics/src/main/java/org/sikuli/basics/Settings.java
+++ b/Basics/src/main/java/org/sikuli/basics/Settings.java
@@ -107,6 +107,8 @@ public class Settings {
 	public static String RPYTHON = "jython";
 	public static String RRUBY = "jruby";
 	public static String EDEFAULT = EPYTHON;
+	public static String TypeCommentToken = "---SikuliX---";
+	public static String TypeCommentDefault = "# This script uses %s " + TypeCommentToken + "\n";
 
 	static {
     Properties props = System.getProperties(); //for debugging
diff --git a/IDE/src/main/java/org/sikuli/ide/EditorPane.java b/IDE/src/main/java/org/sikuli/ide/EditorPane.java
index 7fee103..2786d74 100755
--- a/IDE/src/main/java/org/sikuli/ide/EditorPane.java
+++ b/IDE/src/main/java/org/sikuli/ide/EditorPane.java
@@ -68,23 +68,23 @@ public class EditorPane extends JTextPane implements KeyListener, CaretListener
 		showThumbs = !pref.getPrefMorePlainText();
 	}
 
-	private void initEditorPane() {
-		addKeyListener(this);
-		addCaretListener(this);
-		popMenuImage = new SikuliIDEPopUpMenu("POP_IMAGE", this);
-		if (!popMenuImage.isValidMenu()) {
-			popMenuImage = null;
-		}
+	public void initBeforeLoad(String scriptType) {
+		initBeforeLoad(scriptType, false);
 	}
 
-	public void initBeforeLoad(String scriptType) {
-		//TODO ask for scripttype on new pane
+	public void reInit(String scriptType) {
+		initBeforeLoad(scriptType, true);
+	}
+
+	public void initBeforeLoad(String scriptType, boolean reInit) {
 		String scrType = null;
 		boolean paneIsEmpty = false;
+
 		if (scriptType == null) {
 			scriptType = Settings.EDEFAULT;
 			paneIsEmpty = true;
 		}
+
 		if (Settings.EPYTHON.equals(scriptType)) {
 			scrType = Settings.CPYTHON;
 			_indentationLogic = SikuliIDE.getIDESupport(scriptType).getIndentationLogic();
@@ -93,40 +93,56 @@ public class EditorPane extends JTextPane implements KeyListener, CaretListener
 			scrType = Settings.CRUBY;
 			_indentationLogic = null;
 		}
+
 		if (scrType != null) {
 			sikuliContentType = scrType;
 			SikuliEditorKit ek = new SikuliEditorKit(this);
 			setEditorKit(ek);
 			setContentType(scrType);
-			if (paneIsEmpty) {
-				this.setText("");
-			}
-			pref.addPreferenceChangeListener(new PreferenceChangeListener() {
-				@Override
-				public void preferenceChange(PreferenceChangeEvent event) {
-					if (event.getKey().equals("TAB_WIDTH")) {
-						_indentationLogic.setTabWidth(Integer.parseInt(event.getNewValue()));
+
+			if (_indentationLogic != null) {
+				pref.addPreferenceChangeListener(new PreferenceChangeListener() {
+					@Override
+					public void preferenceChange(PreferenceChangeEvent event) {
+						if (event.getKey().equals("TAB_WIDTH")) {
+							_indentationLogic.setTabWidth(Integer.parseInt(event.getNewValue()));
+						}
 					}
-				}
-			});
+				});
+			}
 		}
-		Debug.log(3, "InitTab: %s :--: %s", getContentType(), getEditorKit());
+
 		initKeyMap();
+
 		if (transferHandler == null) {
 			transferHandler = new MyTransferHandler();
 		}
 		setTransferHandler(transferHandler);
 		_highlighter = new EditorCurrentLineHighlighter(this);
+
 		addCaretListener(_highlighter);
+
 		setFont(new Font(pref.getFontName(), Font.PLAIN, pref.getFontSize()));
 		setMargin(new Insets(3, 3, 3, 3));
 		setBackground(Color.WHITE);
 		if (!Settings.isMac()) {
 			setSelectionColor(new Color(170, 200, 255));
 		}
+
 		updateDocumentListeners();
 
-		initEditorPane();
+		addKeyListener(this);
+		addCaretListener(this);
+		popMenuImage = new SikuliIDEPopUpMenu("POP_IMAGE", this);
+		if (!popMenuImage.isValidMenu()) {
+			popMenuImage = null;
+		}
+
+		if (paneIsEmpty || reInit) {
+			this.setText(String.format(Settings.TypeCommentDefault, getSikuliContentType()));
+		}
+
+		Debug.log(3, "InitTab: %s :--: %s", getContentType(), getEditorKit());
 	}
 
 	public String getSikuliContentType() {
@@ -657,15 +673,59 @@ public class EditorPane extends JTextPane implements KeyListener, CaretListener
 
 	//<editor-fold defaultstate="collapsed" desc="replace text patterns with image buttons">
 	public boolean reparse() {
-		File temp = FileManager.createTempFile("py");
+		File temp = null;
 		Element e = this.getDocument().getDefaultRootElement();
 		if (e.getEndOffset() - e.getStartOffset() == 1) {
 			return true;
 		}
+		if ((temp = reparseBefore()) != null) {
+			if (reparseAfter(temp)) {
+			updateDocumentListeners();
+			return true;
+			}
+		}
+		return false;
+	}
+
+	public File reparseBefore() {
+		Element e = this.getDocument().getDefaultRootElement();
+		if (e.getEndOffset() - e.getStartOffset() == 1) {
+			return null;
+		}
+		File temp = FileManager.createTempFile("reparse");
 		try {
 			writeFile(temp.getAbsolutePath());
+			return temp;
+		} catch (IOException ex) {
+		}
+		return null;
+	}
+
+	public boolean reparseCheckContent() {
+		Element e = this.getDocument().getDefaultRootElement();
+		String txt;
+		if (e.getElementCount() > 2) {
+			return false;
+		} else if (e.getElement(1).getEndOffset() - e.getElement(1).getStartOffset() > 1) {
+			return false;
+		} else {
+			int is = e.getElement(0).getStartOffset();
+			int ie = e.getElement(0).getEndOffset();
+			try {
+				txt = e.getElement(0).getDocument().getText(is, ie - 1);
+				if (txt.endsWith(Settings.TypeCommentToken)) {
+					return true;
+				}
+			} catch (BadLocationException ex) {
+				return false;
+			}
+		}
+		return false;
+	}
+
+	public boolean reparseAfter(File temp) {
+		try {
 			this.read(new BufferedReader(new InputStreamReader(new FileInputStream(temp), "UTF8")), null);
-			updateDocumentListeners();
 			return true;
 		} catch (IOException ex) {
 		}
diff --git a/IDE/src/main/java/org/sikuli/ide/SikuliIDEPopUpMenu.java b/IDE/src/main/java/org/sikuli/ide/SikuliIDEPopUpMenu.java
index 9806d31..b22d874 100644
--- a/IDE/src/main/java/org/sikuli/ide/SikuliIDEPopUpMenu.java
+++ b/IDE/src/main/java/org/sikuli/ide/SikuliIDEPopUpMenu.java
@@ -8,6 +8,8 @@ import javax.swing.JMenu;
 import javax.swing.JMenuItem;
 import javax.swing.JPopupMenu;
 import org.sikuli.basics.Debug;
+import org.sikuli.basics.Settings;
+import org.sikuli.basics.SikuliX;
 import org.sikuli.script.Image;
 import org.sikuli.script.ImagePath;
 
@@ -198,8 +200,33 @@ public class SikuliIDEPopUpMenu extends JPopupMenu {
       super(item);
     }
 
-    public void doSetType(ActionEvent ae) {
+		public void doSetType(ActionEvent ae) {
+			//TODO use a popUpSelect for more language options
 			Debug.log(3, "doSetType: selected");
+			String error = "";
+			EditorPane cp = SikuliIDE.getInstance().getCurrentCodePane();
+			String targetType = Settings.CRUBY;
+			String targetEnding = Settings.ERUBY;
+			if (cp.getSikuliContentType().equals(Settings.CRUBY)) {
+				targetEnding = Settings.EPYTHON;
+				targetType = Settings.CPYTHON;
+			}
+			if (cp.reparseBefore() != null) {
+				if (!cp.reparseCheckContent()) {
+					if (!SikuliX.popAsk(String.format(
+									"Switch to %s requested, but tab is not empty!\n"
+									+ "Click YES, to discard content and switch\n"
+									+ "Click NO to cancel this action and keep content.",
+									targetType))) {
+						error = "with errors";
+					}
+				}
+			}
+			if (error.isEmpty()) {
+				cp.reInit(targetEnding);
+				cp.setText(String.format(Settings.TypeCommentDefault, cp.getSikuliContentType()));
+			}
+			Debug.log(3, "doSetType: completed " + error);
 		}
 
     public void doMoveTab(ActionEvent ae) throws NoSuchMethodException {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/sikuli.git



More information about the pkg-java-commits mailing list