Revision: 7677
          
http://languagetool.svn.sourceforge.net/languagetool/?rev=7677&view=rev
Author:   dnaber
Date:     2012-07-08 11:49:27 +0000 (Sun, 08 Jul 2012)
Log Message:
-----------
stand-alone GUI: Rules can now be disabled and enabled again with a single 
click. Code is not as clean as I would like though...

Modified Paths:
--------------
    trunk/JLanguageTool/CHANGES.txt
    trunk/JLanguageTool/src/java/org/languagetool/MessagesBundle.properties
    trunk/JLanguageTool/src/java/org/languagetool/MessagesBundle_en.properties
    trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java
    trunk/JLanguageTool/src/java/org/languagetool/gui/ResultArea.java

Added Paths:
-----------
    trunk/JLanguageTool/src/java/org/languagetool/gui/RuleLink.java
    trunk/JLanguageTool/src/test/org/languagetool/gui/RuleLinkTest.java

Modified: trunk/JLanguageTool/CHANGES.txt
===================================================================
--- trunk/JLanguageTool/CHANGES.txt     2012-07-08 11:41:01 UTC (rev 7676)
+++ trunk/JLanguageTool/CHANGES.txt     2012-07-08 11:49:27 UTC (rev 7677)
@@ -20,6 +20,10 @@
  -French:
    -several new rules and fixed false alarms
 
+
+ -stand-alone GUI: rules can now be disabled and enabled again with a single 
click
+
+
 1.8 (2012-06-30)
 
  -Greek:

Modified: 
trunk/JLanguageTool/src/java/org/languagetool/MessagesBundle.properties
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/MessagesBundle.properties     
2012-07-08 11:41:01 UTC (rev 7676)
+++ trunk/JLanguageTool/src/java/org/languagetool/MessagesBundle.properties     
2012-07-08 11:49:27 UTC (rev 7677)
@@ -21,7 +21,7 @@
 
 category_typo = Possible Typo
 
-checkDone = Check done, {0} potential problems found ({1}ms)
+checkDone = Potential problems found: {0} (time: {1}ms)
 
 checkText = &Check Text
 
@@ -39,6 +39,12 @@
 
 de-CH = German (Swiss)
 
+# deactivate rule with one click (stand-alone GUI):
+deactivateRule = (deactivate)
+
+# information about which rules are currently de-activated (stand-alone GUI):
+deactivatedRulesText = <br>Deactivated rules - click to activate again:
+
 desc_comma_whitespace = Use of whitespace before comma and before/after 
parentheses
 
 desc_double_punct = Use of two consecutive dots or commas

Modified: 
trunk/JLanguageTool/src/java/org/languagetool/MessagesBundle_en.properties
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/MessagesBundle_en.properties  
2012-07-08 11:41:01 UTC (rev 7676)
+++ trunk/JLanguageTool/src/java/org/languagetool/MessagesBundle_en.properties  
2012-07-08 11:49:27 UTC (rev 7677)
@@ -21,11 +21,11 @@
 
 category_typo = Possible Typo
 
-checkDone = Check done, {0} potential problems found ({1}ms)
+checkDone = Potential problems found: {0} (time: {1}ms)
 
 checkText = &Check Text
 
-correctionMessage = Correction\:
+correctionMessage = Correction:
 
 cs = Czech
 
@@ -39,6 +39,12 @@
 
 de-CH = German (Swiss)
 
+# deactivate rule with one click (stand-alone GUI):
+deactivateRule = (deactivate)
+
+# information about which rules are currently de-activated (stand-alone GUI):
+deactivatedRulesText = <br>Deactivated rules - click to activate again:
+
 desc_comma_whitespace = Use of whitespace before comma and before/after 
parentheses
 
 desc_double_punct = Use of two consecutive dots or commas
@@ -91,17 +97,17 @@
 
 eo = Esperanto
 
-errorContext = Context\:
+errorContext = Context:
 
-errorMessage = Message\:
+errorMessage = Message:
 
 es = Spanish
 
 false_friend = False friend
 
-false_friend_desc = false friend hint for\:
+false_friend_desc = false friend hint for:
 
-false_friend_hint = Hint\: "{0}" ({1}) means {2} ({3}).
+false_friend_hint = Hint: "{0}" ({1}) means {2} ({3}).
 
 false_friend_suggestion = Did you mean {0}?
 
@@ -120,7 +126,7 @@
 guiDemoText = This is a example input to to show you how LanguageTool works.
 guiLanguageManagerDialog=Language Module Manager
 
-guiMatchCount = Potential errors\:
+guiMatchCount = Potential errors:
 
 guiMenuAbout = &About...
 
@@ -142,11 +148,11 @@
 
 guiMenuShowMainWindow = Open Main Window
 
-guiMotherTongue = Your mother tongue\:
+guiMotherTongue = Your mother tongue:
 
-guiNoErrorsFound = No errors or warnings found (language\: {0})
+guiNoErrorsFound = No errors or warnings found (language: {0})
 
-guiNoErrorsFoundSelectedText = No errors or warnings found in selected text 
(language\: {0})
+guiNoErrorsFoundSelectedText = No errors or warnings found in selected text 
(language: {0})
 
 guiOKButton = &OK
 
@@ -160,7 +166,7 @@
 
 guiOOoOptionsButton = Options...
 
-guiProgressWindowTitle = LanguageTool\: Checking Text...
+guiProgressWindowTitle = LanguageTool: Checking Text...
 
 guiRemoveButton=Remove
 guiReplaceWindowTitle = Replace text
@@ -197,13 +203,13 @@
 
 pl = Polish
 
-repetition = Possible typo\: you repeated a word
+repetition = Possible typo: you repeated a word
 
 result1 = <br><b> {0}. Line {1}, column {2}</b><br>
 
 resultAreaText = Results will appear here
 
-resultTime = <br>Time\: {0}ms<br>
+resultTime = <br>Time: {0}ms<br>
 
 ru = Russian
 
@@ -221,7 +227,7 @@
 
 tl = Tagalog
 
-textLanguage = Text Language\:
+textLanguage = Text Language:
 
 two_commas = Two consecutive commas
 
@@ -233,13 +239,13 @@
 
 useGUIConfig = Use above settings for the server
 
-whitespace_repetition = Possible typo\: you repeated a whitespace
+whitespace_repetition = Possible typo: you repeated a whitespace
 
 ro = Romanian
 
 zh = Chinese
 
-moreInfo = More info\:
+moreInfo = More info:
 
 pt = Portuguese
 
@@ -249,6 +255,6 @@
 
 guiWarning = Warning
 
-guiDuplicate = Duplicate rule file\!
+guiDuplicate = Duplicate rule file!
 
 ja = Japanese
\ No newline at end of file

Modified: trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java 2012-07-08 
11:41:01 UTC (rev 7676)
+++ trunk/JLanguageTool/src/java/org/languagetool/gui/Main.java 2012-07-08 
11:49:27 UTC (rev 7677)
@@ -30,8 +30,6 @@
 import org.languagetool.tools.StringTools;
 
 import javax.swing.*;
-import javax.swing.event.HyperlinkEvent;
-import javax.swing.event.HyperlinkListener;
 import javax.swing.filechooser.FileFilter;
 import java.awt.*;
 import java.awt.datatransfer.Clipboard;
@@ -42,8 +40,6 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.Reader;
-import java.net.URISyntaxException;
-import java.net.URL;
 import java.util.*;
 import java.util.List;
 
@@ -57,7 +53,6 @@
   static final String EXTERNAL_LANGUAGE_SUFFIX = " (ext.)";
   static final String HTML_FONT_START = "<font face='Arial,Helvetica'>";
   static final String HTML_FONT_END = "</font>";
-  static final String DEACTIVATE_URL = "http://languagetool.org/deactivate/";;
   static final String HTML_GREY_FONT_START = "<font face='Arial,Helvetica' 
color='#666666'>";
 
   private static final String SYSTEM_TRAY_ICON_NAME = "/TrayIcon.png";
@@ -113,11 +108,7 @@
     textArea.setLineWrap(true);
     textArea.setWrapStyleWord(true);
     textArea.addKeyListener(new ControlReturnTextCheckingListener());
-    resultArea = new ResultArea(messages);
-    resultArea.setContentType("text/html");
-    resultArea.setText(HTML_GREY_FONT_START + 
messages.getString("resultAreaText") + HTML_FONT_END);
-    resultArea.setEditable(false);
-    resultArea.addHyperlinkListener(new MyHyperlinkListener());
+    resultArea = new ResultArea(messages, textArea, config);
     checkTextButton = new 
JButton(StringTools.getLabel(messages.getString("checkText")));
     
checkTextButton.setMnemonic(StringTools.getMnemonic(messages.getString("checkText")));
     checkTextButton.addActionListener(this);
@@ -427,18 +418,19 @@
     final JLanguageTool langTool;
     try {
       config = new Configuration(new File(System.getProperty("user.home")), 
CONFIG_FILE, currentLanguage);
+      resultArea.setConfiguration(config);
       final ConfigurationDialog configDialog = 
getCurrentConfigDialog(currentLanguage);
       langTool = new JLanguageTool(currentLanguage, 
configDialog.getMotherTongue());
       langTool.activateDefaultPatternRules();
       langTool.activateDefaultFalseFriendRules();
+      resultArea.setLanguageTool(langTool);
       final Set<String> disabledRules = configDialog.getDisabledRuleIds();
       if (disabledRules != null) {
         for (final String ruleId : disabledRules) {
           langTool.disableRule(ruleId);
         }
       }
-      final Set<String> disabledCategories = configDialog
-          .getDisabledCategoryNames();
+      final Set<String> disabledCategories = 
configDialog.getDisabledCategoryNames();
       if (disabledCategories != null) {
         for (final String categoryName : disabledCategories) {
           langTool.disableCategory(categoryName);
@@ -487,6 +479,7 @@
                             resultArea.setInputText(textArea.getText());
                             resultArea.setRuleMatches(ruleMatches);
                             resultArea.setRunTime(System.currentTimeMillis() - 
startTime);
+                            resultArea.setLanguageTool(langTool);
                             resultArea.displayResult();
                           } catch (final Exception e) {
                             final String error = "<br><br><b><font 
color=\"red\">"
@@ -588,55 +581,6 @@
     }
   }
 
-  private class MyHyperlinkListener implements HyperlinkListener {
-    @Override
-    public void hyperlinkUpdate(HyperlinkEvent e) {
-      if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
-        final URL url = e.getURL();
-        try {
-          final String uri= url.toURI().toString();
-          if (uri.startsWith(DEACTIVATE_URL)) {
-            final String ruleId = uri.substring(DEACTIVATE_URL.length());
-            // TODO: store in member var
-            final Set<String> disabledRuleIds = config.getDisabledRuleIds();
-            disabledRuleIds.add(ruleId);
-            config.setDisabledRuleIds(disabledRuleIds);
-            ruleMatches = filterRuleMatches();
-            resultArea.setInputText(textArea.getText());
-            resultArea.setRuleMatches(ruleMatches);
-            resultArea.displayResult();
-          } else {
-            handleHttpClick(url);
-          }
-        } catch (URISyntaxException e1) {
-          throw new RuntimeException("Could not handle URL click: " + url, e1);
-        }
-      }
-    }
-
-    private List<RuleMatch> filterRuleMatches() {
-      final List<RuleMatch> filtered = new ArrayList<RuleMatch>();
-      final Set<String> disabledRuleIds = config.getDisabledRuleIds();
-      for (RuleMatch ruleMatch : ruleMatches) {
-        if (!disabledRuleIds.contains(ruleMatch.getRule().getId())) {
-          filtered.add(ruleMatch);
-        }
-      }
-      return filtered;
-    }
-
-    private void handleHttpClick(URL url) {
-      if (Desktop.isDesktopSupported()) {
-        try {
-          final Desktop desktop = Desktop.getDesktop();
-          desktop.browse(url.toURI());
-        } catch (Exception ex) {
-          throw new RuntimeException("Could not open URL: " + url, ex);
-        }
-      }
-    }
-  }
-
   private class ControlReturnTextCheckingListener implements KeyListener {
 
     @Override

Modified: trunk/JLanguageTool/src/java/org/languagetool/gui/ResultArea.java
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/gui/ResultArea.java   
2012-07-08 11:41:01 UTC (rev 7676)
+++ trunk/JLanguageTool/src/java/org/languagetool/gui/ResultArea.java   
2012-07-08 11:49:27 UTC (rev 7677)
@@ -1,32 +1,68 @@
+/* LanguageTool, a natural language style checker
+ * Copyright (C) 2012 Daniel Naber (http://www.danielnaber.de)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+ * USA
+ */
 package org.languagetool.gui;
 
 import org.apache.commons.lang.StringUtils;
+import org.languagetool.JLanguageTool;
+import org.languagetool.rules.Rule;
 import org.languagetool.rules.RuleMatch;
 import org.languagetool.rules.spelling.SpellingCheckRule;
 import org.languagetool.tools.StringTools;
 
 import javax.swing.*;
+import javax.swing.event.HyperlinkEvent;
+import javax.swing.event.HyperlinkListener;
 import java.awt.*;
+import java.io.IOException;
+import java.net.URL;
+import java.util.*;
 import java.util.List;
-import java.util.ResourceBundle;
 
 /**
  * Area where the result of text checking is displayed.
  */
 class ResultArea extends JTextPane {
 
+  private static final String DEACTIVATE_URL = 
"http://languagetool.org/deactivate/";;
+  private static final String REACTIVATE_URL = 
"http://languagetool.org/reactivate/";;
   private static final String LT_ERROR_MARKER_START = "<b><font 
bgcolor=\"#d7d7ff\">";
   private static final String SPELL_ERROR_MARKER_START = "<b><font 
bgcolor=\"#ffd7d7\">";
 
   private final ResourceBundle messages;
+  private final JTextArea textArea;
 
+  private Configuration config;
   private String inputText;
   private String startText;
-  private List<RuleMatch> ruleMatches;
+  private List<RuleMatch> allRuleMatches;
+  private List<RuleMatch> ruleMatches;    // will be filtered to not show 
disabled rules
   private long runTime;
+  private JLanguageTool languageTool;
 
-  ResultArea(ResourceBundle messages) {
+  ResultArea(ResourceBundle messages, JTextArea textArea, Configuration 
config) {
     this.messages = messages;
+    this.textArea = textArea;
+    this.config = config;
+    setContentType("text/html");
+    setText(Main.HTML_GREY_FONT_START + messages.getString("resultAreaText") + 
Main.HTML_FONT_END);
+    setEditable(false);
+    addHyperlinkListener(new MyHyperlinkListener());
   }
 
   String getRuleMatchHtml(List<RuleMatch> ruleMatches, String text, String 
startCheckText) {
@@ -39,19 +75,16 @@
       final String output = Tools.makeTexti18n(messages, "result1",
           new Object[] {i + 1, match.getLine() + 1, match.getColumn()});
       sb.append(output);
-      String msg = match.getMessage();
-      msg = msg.replaceAll("<suggestion>", "<b>");
-      msg = msg.replaceAll("</suggestion>", "</b>");
-      msg = msg.replaceAll("<old>", "<b>");
-      msg = msg.replaceAll("</old>", "</b>");
-      sb.append("<b>" + messages.getString("errorMessage") + "</b> ");
+      final String msg = match.getMessage()
+          .replaceAll("<suggestion>", "<b>").replaceAll("</suggestion>", 
"</b>")
+          .replaceAll("<old>", "<b>").replaceAll("</old>", "</b>");
+      sb.append("<b>").append(messages.getString("errorMessage")).append("</b> 
");
       sb.append(msg);
-      // TODO: enable this:
-      //sb.append(" <a href=\"" + Main.DEACTIVATE_URL + 
match.getRule().getId() + "\">(deactivate)</a>");
-      sb.append("<br>\n");
+      final RuleLink ruleLink = 
RuleLink.buildDeactivationLink(match.getRule());
+      sb.append(" <a 
href=\"").append(ruleLink).append("\">").append(messages.getString("deactivateRule")).append("</a><br>\n");
       if (match.getSuggestedReplacements().size() > 0) {
-        final String repl = 
StringTools.listToString(match.getSuggestedReplacements(), "; ");
-        sb.append("<b>" + messages.getString("correctionMessage") + "</b> " + 
repl + "<br>\n");
+        final String replacement = 
StringTools.listToString(match.getSuggestedReplacements(), "; ");
+        
sb.append("<b>").append(messages.getString("correctionMessage")).append("</b> 
").append(replacement).append("<br>\n");
       }
       if (match.getRule() instanceof SpellingCheckRule) {
         contextTools.setErrorMarkerStart(SPELL_ERROR_MARKER_START);
@@ -59,26 +92,63 @@
         contextTools.setErrorMarkerStart(LT_ERROR_MARKER_START);
       }
       final String context = contextTools.getContext(match.getFromPos(), 
match.getToPos(), text);
-      sb.append("<b>" + messages.getString("errorContext") + "</b> " + 
context);
+      sb.append("<b>").append(messages.getString("errorContext")).append("</b> 
").append(context);
       sb.append("<br>\n");
       if (match.getRule().getUrl() != null && Desktop.isDesktopSupported()) {
-         sb.append("<b>" + messages.getString("moreInfo") + "</b> <a href=\"");
+         sb.append("<b>").append(messages.getString("moreInfo")).append("</b> 
<a href=\"");
         final String url = match.getRule().getUrl().toString();
         sb.append(url);
         final String shortUrl = StringUtils.abbreviate(url, 60);
-         sb.append("\">" + shortUrl +"</a><br>\n");
+         sb.append("\">").append(shortUrl).append("</a><br>\n");
       }
       i++;
     }
     sb.append(Main.HTML_GREY_FONT_START);
+    sb.append(getDisabledRulesHtml());
     final String checkDone = Tools.makeTexti18n(messages, "checkDone", new 
Object[] {ruleMatches.size(), runTime});
-    sb.append("<br>\n");
-    sb.append(checkDone);
-    sb.append(Main.HTML_FONT_END);
-    sb.append("<br>\n");
+    sb.append("<br>\n").append(checkDone);
+    sb.append(Main.HTML_FONT_END).append("<br>\n");
     return sb.toString();
   }
 
+  private String getDisabledRulesHtml() {
+    final StringBuilder sb = new StringBuilder();
+    sb.append(messages.getString("deactivatedRulesText"));
+    int i = 0;
+    int deactivatedRuleCount = 0;
+    for (String ruleId : config.getDisabledRuleIds()) {
+      if (ruleId.trim().isEmpty()) {
+        continue;
+      }
+      final Rule rule = getRuleForId(ruleId);
+      if (rule.isDefaultOff()) {
+        continue;
+      }
+      if (i++ > 0) {
+        sb.append(",");
+      }
+      final RuleLink reactivationLink = RuleLink.buildReactivationLink(rule);
+      sb.append(" <a 
href=\"").append(reactivationLink).append("\">").append(rule.getDescription()).append("</a>");
+      deactivatedRuleCount++;
+    }
+    sb.append("<br>");
+    if (deactivatedRuleCount == 0) {
+      return "";
+    } else {
+      return sb.toString();
+    }
+  }
+
+  private Rule getRuleForId(String ruleId) {
+    final List<Rule> allRules = languageTool.getAllRules();
+    for (Rule rule : allRules) {
+      if (rule.getId().equals(ruleId)) {
+        return rule;
+      }
+    }
+    return null;
+  }
+
   void setInputText(String inputText) {
     this.inputText = inputText;
   }
@@ -92,10 +162,12 @@
   }
 
   void setRuleMatches(List<RuleMatch> ruleMatches) {
-    this.ruleMatches = ruleMatches;
+    this.allRuleMatches = new ArrayList<RuleMatch>(ruleMatches);
+    this.ruleMatches = new ArrayList<RuleMatch>(ruleMatches);
   }
 
   void displayResult() {
+    ruleMatches = filterRuleMatches();
     final String ruleMatchHtml = getRuleMatchHtml(ruleMatches, inputText, 
startText);
     displayText(ruleMatchHtml);
   }
@@ -105,4 +177,81 @@
     setCaretPosition(0);
   }
 
+  void setConfiguration(Configuration config) {
+    this.config = config;
+  }
+
+  void setLanguageTool(JLanguageTool languageTool) {
+    this.languageTool = languageTool;
+  }
+
+  private List<RuleMatch> filterRuleMatches() {
+    final List<RuleMatch> filtered = new ArrayList<RuleMatch>();
+    final Set<String> disabledRuleIds = config.getDisabledRuleIds();
+    for (RuleMatch ruleMatch : allRuleMatches) {
+      if (!disabledRuleIds.contains(ruleMatch.getRule().getId())) {
+        filtered.add(ruleMatch);
+      }
+    }
+    return filtered;
+  }
+
+  private class MyHyperlinkListener implements HyperlinkListener {
+
+    private MyHyperlinkListener() {
+    }
+
+    @Override
+    public void hyperlinkUpdate(HyperlinkEvent e) {
+      if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
+        final URL url = e.getURL();
+        try {
+          final String uri = url.toURI().toString();
+          if (uri.startsWith(DEACTIVATE_URL) || 
uri.startsWith(REACTIVATE_URL)) {
+            handleRuleLinkClick(uri);
+          } else {
+            handleHttpClick(url);
+          }
+        } catch (Exception ex) {
+          throw new RuntimeException("Could not handle URL click: " + url, ex);
+        }
+      }
+    }
+
+    private void handleRuleLinkClick(String uri) throws IOException {
+      final RuleLink ruleLink = RuleLink.getFromString(uri);
+      final String ruleId = ruleLink.getId();
+      final Set<String> disabledRuleIds = config.getDisabledRuleIds();
+      if (uri.startsWith(DEACTIVATE_URL)) {
+        disabledRuleIds.add(ruleId);
+        languageTool.disableRule(ruleId);
+      } else {
+        disabledRuleIds.remove(ruleId);
+        languageTool.enableRule(ruleId);
+      }
+      config.setDisabledRuleIds(disabledRuleIds);
+      config.saveConfiguration(languageTool.getLanguage());
+      allRuleMatches = languageTool.check(textArea.getText());
+      reDisplayRuleMatches();
+    }
+
+    private void reDisplayRuleMatches() {
+      ruleMatches = filterRuleMatches();
+      setInputText(textArea.getText());
+      displayResult();
+    }
+
+    private void handleHttpClick(URL url) {
+      if (Desktop.isDesktopSupported()) {
+        try {
+          final Desktop desktop = Desktop.getDesktop();
+          desktop.browse(url.toURI());
+        } catch (Exception ex) {
+          throw new RuntimeException("Could not open URL: " + url, ex);
+        }
+      }
+    }
+
+  }
+
 }

Added: trunk/JLanguageTool/src/java/org/languagetool/gui/RuleLink.java
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/gui/RuleLink.java             
                (rev 0)
+++ trunk/JLanguageTool/src/java/org/languagetool/gui/RuleLink.java     
2012-07-08 11:49:27 UTC (rev 7677)
@@ -0,0 +1,69 @@
+/* LanguageTool, a natural language style checker
+ * Copyright (C) 2012 Daniel Naber (http://www.danielnaber.de)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+ * USA
+ */
+package org.languagetool.gui;
+
+import org.languagetool.rules.Rule;
+
+/**
+ * A link for enabling or disabling a rule.
+ */
+class RuleLink {
+
+  private static final String DEACTIVATE_URL = 
"http://languagetool.org/deactivate/";;
+  private static final String REACTIVATE_URL = 
"http://languagetool.org/reactivate/";;
+
+  private final String urlPrefix;
+  private final String id;
+
+  private RuleLink(String urlPrefix, String id) {
+    this.urlPrefix = urlPrefix;
+    this.id = id;
+  }
+
+  static RuleLink buildDeactivationLink(Rule rule) {
+    return new RuleLink(DEACTIVATE_URL, rule.getId());
+  }
+
+  static RuleLink buildReactivationLink(Rule rule) {
+    return new RuleLink(REACTIVATE_URL, rule.getId());
+  }
+
+  static RuleLink getFromString(String ruleLink) {
+    final String id;
+    if (ruleLink.startsWith(DEACTIVATE_URL)) {
+      id = ruleLink.substring(DEACTIVATE_URL.length());
+      return new RuleLink(DEACTIVATE_URL, id);
+    } else if (ruleLink.startsWith(REACTIVATE_URL)) {
+      id = ruleLink.substring(REACTIVATE_URL.length());
+      return new RuleLink(REACTIVATE_URL, id);
+    } else {
+      throw new RuntimeException("Unknown link prefix: " + ruleLink);
+    }
+  }
+
+  String getId() {
+    return id;
+  }
+
+  @Override
+  public String toString() {
+    return urlPrefix + id;
+  }
+
+}

Added: trunk/JLanguageTool/src/test/org/languagetool/gui/RuleLinkTest.java
===================================================================
--- trunk/JLanguageTool/src/test/org/languagetool/gui/RuleLinkTest.java         
                (rev 0)
+++ trunk/JLanguageTool/src/test/org/languagetool/gui/RuleLinkTest.java 
2012-07-08 11:49:27 UTC (rev 7677)
@@ -0,0 +1,52 @@
+/* LanguageTool, a natural language style checker
+ * Copyright (C) 2012 Daniel Naber (http://www.danielnaber.de)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+ * USA
+ */
+package org.languagetool.gui;
+
+import junit.framework.TestCase;
+import org.languagetool.Language;
+import org.languagetool.TestTools;
+import org.languagetool.rules.WordRepeatRule;
+
+public class RuleLinkTest extends TestCase {
+
+  public void testBuildDeactivationLink() {
+    final Language language = Language.ENGLISH;
+    final RuleLink ruleLink = RuleLink.buildDeactivationLink(new 
WordRepeatRule(TestTools.getMessages(language.getShortName()), language));
+    assertEquals("WORD_REPEAT_RULE", ruleLink.getId());
+    assertEquals("http://languagetool.org/deactivate/WORD_REPEAT_RULE";, 
ruleLink.toString());
+  }
+
+  public void testBuildReactivationLink() {
+    final Language language = Language.ENGLISH;
+    final RuleLink ruleLink = RuleLink.buildReactivationLink(new 
WordRepeatRule(TestTools.getMessages(language.getShortName()), language));
+    assertEquals("WORD_REPEAT_RULE", ruleLink.getId());
+    assertEquals("http://languagetool.org/reactivate/WORD_REPEAT_RULE";, 
ruleLink.toString());
+  }
+
+  public void testGetFromString() {
+    final RuleLink ruleLink1 = 
RuleLink.getFromString("http://languagetool.org/reactivate/FOO_BAR_ID";);
+    assertEquals("FOO_BAR_ID", ruleLink1.getId());
+    assertEquals("http://languagetool.org/reactivate/FOO_BAR_ID";, 
ruleLink1.toString());
+
+    final RuleLink ruleLink2 = 
RuleLink.getFromString("http://languagetool.org/deactivate/FOO_BAR_ID2";);
+    assertEquals("FOO_BAR_ID2", ruleLink2.getId());
+    assertEquals("http://languagetool.org/deactivate/FOO_BAR_ID2";, 
ruleLink2.toString());
+  }
+
+}

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Languagetool-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/languagetool-cvs

Reply via email to