tmpDisabledRules = new HashSet<>(disabledRulesUI);
80 | for (String ruleId : tmpDisabledRules) {
81 | if(!disabledRules.contains(ruleId)) {
82 | disabledRulesUI.remove(ruleId);
83 | }
84 | }
85 | documents.setDisabledRules(docLanguage.getShortCodeWithCountryAndVariant(), disabledRulesUI);
86 | config.removeDisabledRuleIds(disabledRulesUI);
87 | config.saveConfiguration(docLanguage);
88 | documents.resetDocumentCaches();
89 | documents.resetConfiguration();
90 | } else {
91 | config.removeDisabledRuleIds(WtDocumentsHandler.getDisabledRules(docLanguage.getShortCodeWithCountryAndVariant()));
92 | }
93 | } catch (Throwable e) {
94 | WtMessageHandler.showError(e);
95 | }
96 | documents.setConfigurationDialog(null);
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/config/WtRuleNode.java:
--------------------------------------------------------------------------------
1 | /* WritingTool, a LibreOffice Extension based on LanguageTool
2 | * Copyright (C) 2024 Fred Kruse (https://writingtool.org)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.config;
20 |
21 | import javax.swing.tree.DefaultMutableTreeNode;
22 | import org.languagetool.rules.Rule;
23 |
24 | /**
25 | * @author Panagiotis Minos
26 | * @since 1.0
27 | */
28 | public class WtRuleNode extends DefaultMutableTreeNode {
29 |
30 | private static final long serialVersionUID = 1L;
31 | private final Rule rule;
32 | private boolean enabled;
33 |
34 | public WtRuleNode(Rule rule, boolean enabled) {
35 | super(rule);
36 | this.rule = rule;
37 | this.enabled = enabled;
38 | }
39 |
40 | public Rule getRule() {
41 | return rule;
42 | }
43 |
44 | public boolean isEnabled() {
45 | return enabled;
46 | }
47 |
48 | public void setEnabled(boolean enabled) {
49 | this.enabled = enabled;
50 | }
51 |
52 | @Override
53 | public String toString() {
54 | return rule.getDescription();
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/config/WtSavablePanel.java:
--------------------------------------------------------------------------------
1 | /* WritingTool, a LibreOffice Extension based on LanguageTool
2 | * Copyright (C) 2024 Fred Kruse (https://writingtool.org)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.config;
20 |
21 | import org.writingtool.dialogs.WtConfigurationDialog;
22 |
23 | /**
24 | * Interface for JPanel that can persist its state.
25 | *
26 | * See {@link WtConfigurationDialog#addExtraPanel}
27 | *
28 | * @author Panagiotis Minos
29 | * @since 1.0
30 | */
31 | public interface WtSavablePanel {
32 |
33 | /**
34 | * Called when {@link WtConfigurationDialog} is about to be shown.
35 | */
36 | public void componentShowing();
37 |
38 | /**
39 | * Invoke the save operation.
40 | */
41 | public void save();
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/config/WtTreeListener.java:
--------------------------------------------------------------------------------
1 | /* WritingTool, a LibreOffice Extension based on LanguageTool
2 | * Copyright (C) 2024 Fred Kruse (https://writingtool.org)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.config;
20 |
21 | import java.awt.Dimension;
22 | import java.awt.MouseInfo;
23 | import java.awt.Point;
24 | import java.awt.event.KeyEvent;
25 | import java.awt.event.KeyListener;
26 | import java.awt.event.MouseEvent;
27 | import java.awt.event.MouseListener;
28 | import javax.swing.JCheckBox;
29 | import javax.swing.JTree;
30 | import javax.swing.event.TreeExpansionEvent;
31 | import javax.swing.event.TreeWillExpandListener;
32 | import javax.swing.tree.DefaultTreeModel;
33 | import javax.swing.tree.ExpandVetoException;
34 | import javax.swing.tree.TreePath;
35 |
36 | /**
37 | *
38 | * @author Panagiotis Minos
39 | * @since 1.0
40 | */
41 | public class WtTreeListener implements KeyListener, MouseListener, TreeWillExpandListener {
42 |
43 | public static void install(JTree tree) {
44 | WtTreeListener listener = new WtTreeListener(tree);
45 | tree.addMouseListener(listener);
46 | tree.addKeyListener(listener);
47 | tree.addTreeWillExpandListener(listener);
48 | }
49 |
50 | private static final Dimension checkBoxDimension = new JCheckBox().getPreferredSize();
51 |
52 | private final JTree tree;
53 |
54 | private WtTreeListener(JTree tree) {
55 | this.tree = tree;
56 | }
57 |
58 | @Override
59 | public void keyTyped(KeyEvent e) {
60 | }
61 |
62 | @Override
63 | public void keyPressed(KeyEvent e) {
64 | if (e.getKeyCode() == KeyEvent.VK_SPACE) {
65 | TreePath[] paths = tree.getSelectionPaths();
66 | if (paths != null) {
67 | for (TreePath path : paths) {
68 | handle(path);
69 | }
70 | }
71 | }
72 | }
73 |
74 | @Override
75 | public void keyReleased(KeyEvent e) {
76 | }
77 |
78 | @Override
79 | public void mouseClicked(MouseEvent e) {
80 | }
81 |
82 | @Override
83 | public void mousePressed(MouseEvent e) {
84 | int x = e.getX();
85 | int y = e.getY();
86 | TreePath path = tree.getPathForLocation(x, y);
87 | if (isOverCheckBox(x, y, path)) {
88 | handle(path);
89 | }
90 | }
91 |
92 | @Override
93 | public void mouseReleased(MouseEvent e) {
94 | }
95 |
96 | @Override
97 | public void mouseEntered(MouseEvent e) {
98 | }
99 |
100 | @Override
101 | public void mouseExited(MouseEvent e) {
102 | }
103 |
104 | private void handle(TreePath path) {
105 | if ((path != null) && (path.getPathCount() > 0)) {
106 | if (path.getLastPathComponent() instanceof WtCategoryNode) {
107 | DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
108 |
109 | WtCategoryNode node = (WtCategoryNode) path.getLastPathComponent();
110 | node.setEnabled(!node.isEnabled());
111 | model.nodeChanged(node);
112 |
113 | for (int i = 0; i < node.getChildCount(); i++) {
114 | WtRuleNode child = (WtRuleNode) node.getChildAt(i);
115 | if (child.isEnabled() != node.isEnabled()) {
116 | child.setEnabled(node.isEnabled());
117 | model.nodeChanged(child);
118 | }
119 | }
120 | }
121 | if (path.getLastPathComponent() instanceof WtRuleNode) {
122 | DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
123 |
124 | WtRuleNode node = (WtRuleNode) path.getLastPathComponent();
125 | node.setEnabled(!node.isEnabled());
126 | model.nodeChanged(node);
127 |
128 | if (node.isEnabled()) {
129 | WtCategoryNode parent = (WtCategoryNode) node.getParent();
130 | parent.setEnabled(true);
131 | }
132 | model.nodeChanged(node.getParent());
133 | }
134 | }
135 | }
136 |
137 | private boolean isOverCheckBox(int x, int y, TreePath path) {
138 | if ((path == null) || (path.getPathCount() == 0)) {
139 | return false;
140 | }
141 | if (!isValidNode(path.getLastPathComponent())) {
142 | return false;
143 | }
144 | //checkbox is east
145 | //int offset = tree.getPathBounds(path).x + tree.getPathBounds(path).width - checkBoxDimension.width;
146 | //if (x < offset) {
147 |
148 | //checkbox is west
149 | int offset = tree.getPathBounds(path).x + checkBoxDimension.width;
150 | if (x > offset) {
151 | return false;
152 | }
153 | return true;
154 | }
155 |
156 | private boolean isValidNode(Object c) {
157 | return ((c instanceof WtCategoryNode) || (c instanceof WtRuleNode));
158 | }
159 |
160 | @Override
161 | public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException {
162 | Point cursorPosition = MouseInfo.getPointerInfo().getLocation();
163 | Point treePosition = tree.getLocationOnScreen();
164 | int x = (int) (cursorPosition.getX() - treePosition.getX());
165 | int y = (int) (cursorPosition.getY() - treePosition.getY());
166 | TreePath path = tree.getPathForLocation(x, y);
167 | if (isOverCheckBox(x, y, path)) {
168 | throw new ExpandVetoException(e);
169 | }
170 | }
171 |
172 | @Override
173 | public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
174 | treeWillExpand(e);
175 | }
176 | }
177 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/dialogs/WtMoreInfoDialog.java:
--------------------------------------------------------------------------------
1 | /* WritingTool, a LibreOffice Extension based on LanguageTool
2 | * Copyright (C) 2024 Fred Kruse (https://writingtool.org)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.dialogs;
20 |
21 | import java.awt.Color;
22 | import java.awt.Container;
23 | import java.awt.Dimension;
24 | import java.awt.Frame;
25 | import java.awt.GridBagConstraints;
26 | import java.awt.GridBagLayout;
27 | import java.awt.Image;
28 | import java.awt.Insets;
29 | import java.awt.Toolkit;
30 | import java.awt.event.WindowEvent;
31 | import java.awt.event.WindowFocusListener;
32 | import java.net.URL;
33 | import java.util.ResourceBundle;
34 | import javax.swing.BorderFactory;
35 | import javax.swing.BoxLayout;
36 | import javax.swing.JButton;
37 | import javax.swing.JDialog;
38 | import javax.swing.JPanel;
39 | import javax.swing.JScrollPane;
40 | import javax.swing.JTextPane;
41 |
42 | import org.languagetool.rules.Category;
43 | import org.languagetool.rules.Rule;
44 | import org.languagetool.rules.patterns.FalseFriendPatternRule;
45 | import org.writingtool.tools.WtGeneralTools;
46 | import org.writingtool.tools.WtMessageHandler;
47 | import org.writingtool.tools.WtOfficeTools;
48 |
49 |
50 | /**
51 | * A dialog with information about a special rule.
52 | *
53 | * @author Fred Kruse
54 | * @since 1.0
55 | */
56 | public class WtMoreInfoDialog {
57 |
58 | private final JDialog dialog = new JDialog();
59 | private final String title;
60 | private final String message;
61 | private final Rule rule;
62 | private final URL matchUrl;
63 | private final ResourceBundle messages;
64 | private final String lang;
65 |
66 | public WtMoreInfoDialog(String title, String message, Rule rule, URL matchUrl, ResourceBundle messages, String lang) {
67 | this.title = title;
68 | this.message = message;
69 | this.rule = rule;
70 | this.matchUrl = matchUrl;
71 | this.messages = messages;
72 | this.lang = lang;
73 | }
74 |
75 | public void show() {
76 | try {
77 | int dialogWidth = 320;
78 | JTextPane textPane = new JTextPane();
79 | textPane.setEditable(false);
80 | textPane.setContentType("text/html");
81 | textPane.setBorder(BorderFactory.createEmptyBorder());
82 | textPane.setOpaque(false);
83 | textPane.setBackground(new Color(0, 0, 0, 0));
84 | WtGeneralTools.addHyperlinkListener(textPane);
85 | textPane.setSize(dialogWidth, Short.MAX_VALUE);
86 | String messageWithBold = message.replaceAll("", "").replaceAll("", "");
87 | String exampleSentences = WtGeneralTools.getExampleSentences(rule, messages);
88 | String url = "http://community.languagetool.org/rule/show/" + WtGeneralTools.encodeUrl(rule)
89 | + "?lang=" + lang + "&ref=standalone-gui";
90 | boolean isExternal = rule.getCategory().getLocation() == Category.Location.EXTERNAL;
91 | String ruleDetailLink = rule instanceof FalseFriendPatternRule || isExternal ?
92 | "" : "" + messages.getString("ruleDetailsLink") +"";
93 | textPane.setText(""
94 | + messageWithBold + exampleSentences + WtGeneralTools.formatURL(matchUrl)
95 | + "
"
96 | + ruleDetailLink
97 | + "");
98 | JScrollPane scrollPane = new JScrollPane(textPane);
99 | scrollPane.setPreferredSize(
100 | new Dimension(dialogWidth, textPane.getPreferredSize().height));
101 | scrollPane.setBorder(BorderFactory.createEmptyBorder());
102 |
103 | String cleanTitle = title.replace("", "'").replace("", "'");
104 |
105 | dialog.setName(cleanTitle);
106 | dialog.setTitle(cleanTitle);
107 | dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
108 | Image ltImage = WtOfficeTools.getLtImage();
109 | ((Frame) dialog.getOwner()).setIconImage(ltImage);
110 |
111 | dialog.addWindowFocusListener(new WindowFocusListener() {
112 | @Override
113 | public void windowGainedFocus(WindowEvent e) {
114 | }
115 |
116 | @Override
117 | public void windowLostFocus(WindowEvent e) {
118 | close();
119 | }
120 | });
121 |
122 | JButton close = new JButton(messages.getString("guiOOoCloseButton"));
123 | close.addActionListener(e -> {
124 | close();
125 | });
126 |
127 | JPanel infoPanel = new JPanel();
128 | infoPanel.setLayout(new GridBagLayout());
129 | GridBagConstraints cons = new GridBagConstraints();
130 | cons.insets = new Insets(6, 0, 0, 15);
131 | cons.gridx = 0;
132 | cons.gridy = 0;
133 | cons.anchor = GridBagConstraints.WEST;
134 | cons.fill = GridBagConstraints.NONE;
135 | cons.weightx = 1.0f;
136 | cons.weighty = 1.0f;
137 | infoPanel.add(textPane, cons);
138 |
139 | JPanel closeButtonPanel = new JPanel();
140 | closeButtonPanel.setLayout(new GridBagLayout());
141 | cons = new GridBagConstraints();
142 | cons.insets = new Insets(6, 12, 0, 15);
143 | cons.gridx = 0;
144 | cons.gridy = 0;
145 | cons.anchor = GridBagConstraints.EAST;
146 | cons.fill = GridBagConstraints.NONE;
147 | cons.weightx = 1.0f;
148 | cons.weighty = 1.0f;
149 | closeButtonPanel.add(close, cons);
150 |
151 | JPanel panel = new JPanel();
152 | panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
153 | panel.add(infoPanel);
154 | panel.add(closeButtonPanel);
155 | Container contentPane = dialog.getContentPane();
156 | contentPane.setLayout(new GridBagLayout());
157 | cons = new GridBagConstraints();
158 | cons.insets = new Insets(8, 8, 8, 8);
159 | cons.gridx = 0;
160 | cons.gridy = 0;
161 | cons.weightx = 10.0f;
162 | cons.weighty = 10.0f;
163 | cons.fill = GridBagConstraints.BOTH;
164 | cons.anchor = GridBagConstraints.NORTHWEST;
165 | contentPane.add(panel, cons);
166 | dialog.pack();
167 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
168 | Dimension frameSize = dialog.getSize();
169 | dialog.setLocation(screenSize.width / 2 - frameSize.width / 2,
170 | screenSize.height / 2 - frameSize.height / 2);
171 | dialog.setLocationByPlatform(true);
172 | dialog.setAutoRequestFocus(true);
173 | dialog.setVisible(true);
174 | dialog.setAlwaysOnTop(true);
175 | dialog.toFront();
176 | } catch (Throwable t) {
177 | WtMessageHandler.showError(t);
178 | }
179 | }
180 |
181 | public void close() {
182 | dialog.setVisible(false);
183 | }
184 |
185 | }
186 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/dialogs/WtOptionPane.java:
--------------------------------------------------------------------------------
1 | /* WritingTool, a LibreOffice Extension based on LanguageTool
2 | * Copyright (C) 2024 Fred Kruse (https://writingtool.org)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.dialogs;
20 |
21 | import java.awt.Color;
22 | import java.awt.Component;
23 |
24 | import javax.swing.JOptionPane;
25 |
26 | import org.writingtool.tools.WtMessageHandler;
27 |
28 | /**
29 | * simple panes for information and confirmation
30 | * (adapter for JOptionPane)
31 | * NOTE: JOptionPane does not work with FlatLight and FlatDark themes
32 | * @since 1.2
33 | * @author Fred Kruse
34 | */
35 | public class WtOptionPane {
36 |
37 | public static final int OK_OPTION = JOptionPane.OK_OPTION;
38 | public static final int CANCEL_OPTION = JOptionPane.NO_OPTION;
39 | public static final int OK_CANCEL_OPTION = JOptionPane.OK_CANCEL_OPTION;
40 | public static final int QUESTION_MESSAGE = JOptionPane.QUESTION_MESSAGE;
41 | public static final int ERROR_MESSAGE = JOptionPane.ERROR_MESSAGE;
42 | public static final int INFORMATION_MESSAGE = JOptionPane.INFORMATION_MESSAGE;
43 | public static final int MESSAGE_BOX = 0;
44 | public static final int INPUT_BOX = 1;
45 | public static final Color DARK_FOREGROUND = new Color(0xbabab2);
46 | public static final Color DARK_BACKGROUND = new Color(0x3c3f41);
47 | public static final Color DARK_BUTTON_BACKGROUND = new Color(0x4e5052);
48 | public static final Color DARK_BORDER = new Color(0x565c5f);
49 |
50 | public static void showMessageDialog (Component parent, String msg) {
51 | try {
52 | JOptionPane.showMessageDialog(parent, msg);
53 | } catch (Exception e) {
54 | WtMessageHandler.printException(e);
55 | }
56 | }
57 |
58 | public static void showMessageDialog (Component parent, String msg, String title, int opt) {
59 | try {
60 | JOptionPane.showMessageDialog(parent, msg, title, opt);
61 | } catch (Exception e) {
62 | WtMessageHandler.printException(e);
63 | }
64 | }
65 |
66 | public static String showInputDialog(Component parent, Object msg, String title, int opt) {
67 | try {
68 | String txt = JOptionPane.showInputDialog(parent, msg, title, opt);
69 | return txt;
70 | } catch (Exception e) {
71 | WtMessageHandler.printException(e);
72 | }
73 | return null;
74 | }
75 |
76 | public static String showInputDialog(Component parent, String title, String initial) {
77 | try {
78 | String txt = JOptionPane.showInputDialog(parent, title, initial);
79 | return txt;
80 | } catch (Exception e) {
81 | WtMessageHandler.printException(e);
82 | }
83 | return null;
84 | }
85 |
86 | public static int showConfirmDialog (Component parent, String msg, String title, int opt) {
87 | try {
88 | int ret = JOptionPane.showConfirmDialog(parent, msg, title, opt);
89 | return ret;
90 | } catch (Exception e) {
91 | WtMessageHandler.printException(e);
92 | }
93 | return CANCEL_OPTION;
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/languagedetectors/WtKhmerDetector.java:
--------------------------------------------------------------------------------
1 | /* WritingTool, a LibreOffice Extension based on LanguageTool
2 | * Copyright (C) 2024 Fred Kruse (https://writingtool.org)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.languagedetectors;
20 |
21 | /**
22 | * Helps to detect Khmer strings by their Unicode range.
23 | */
24 | public
25 | class WtKhmerDetector extends WtUnicodeLanguageDetector {
26 |
27 | @Override
28 | protected boolean isInAlphabet(int numericValue) {
29 | return numericValue >= 6016 && numericValue <= 6143;
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/languagedetectors/WtTamilDetector.java:
--------------------------------------------------------------------------------
1 | /* WritingTool, a LibreOffice Extension based on LanguageTool
2 | * Copyright (C) 2024 Fred Kruse (https://writingtool.org)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.languagedetectors;
20 |
21 | /**
22 | * Helps to detect Tamil strings by their Unicode range.
23 | * @since 1.0
24 | */
25 | public
26 | class WtTamilDetector extends WtUnicodeLanguageDetector {
27 |
28 | @Override
29 | protected boolean isInAlphabet(int numericValue) {
30 | return numericValue >= 2946 && numericValue <= 3066;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/languagedetectors/WtUnicodeLanguageDetector.java:
--------------------------------------------------------------------------------
1 | /* WritingTool, a LibreOffice Extension based on LanguageTool
2 | * Copyright (C) 2024 Fred Kruse (https://writingtool.org)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.languagedetectors;
20 |
21 | /**
22 | * Helps to detect the language of strings by the Unicode range used by the characters.
23 | * @since 1.0
24 | */
25 | public abstract class WtUnicodeLanguageDetector {
26 |
27 | private static final int MAX_CHECK_LENGTH = 100;
28 |
29 | protected abstract boolean isInAlphabet(int numericValue);
30 |
31 | public boolean isThisLanguage(String str) {
32 | int maxCheckLength = Math.min(str.length(), MAX_CHECK_LENGTH);
33 | for (int i = 0; i < maxCheckLength; i++) {
34 | int numericValue = str.charAt(i);
35 | if (isInAlphabet(numericValue)) {
36 | return true;
37 | }
38 | }
39 | return false;
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/remote/CheckConfiguration.java:
--------------------------------------------------------------------------------
1 | /* LanguageTool, a natural language style checker
2 | * Copyright (C) 2016 Daniel Naber (http://www.danielnaber.de)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.remote;
20 |
21 | import java.util.List;
22 | import java.util.Objects;
23 | import java.util.Optional;
24 |
25 | import org.jetbrains.annotations.Nullable;
26 |
27 | /**
28 | * Configuration for checking a text with {@link RemoteLanguageTool}.
29 | * Use {@link CheckConfigurationBuilder} to create a configuration.
30 | * @since 3.4
31 | */
32 | public class CheckConfiguration {
33 |
34 | private final String langCode;
35 | private final String motherTongueLangCode;
36 | private final boolean guessLanguage;
37 | private final List enabledRuleIds;
38 | private final boolean enabledOnly;
39 | private final List disabledRuleIds;
40 | private final String mode;
41 | private final String level;
42 | private final List ruleValues;
43 | @Nullable
44 | private final String textSessionID;
45 | @Nullable
46 | private final String username;
47 | @Nullable
48 | private final String apiKey;
49 |
50 | CheckConfiguration(String langCode, String motherTongueLangCode, boolean guessLanguage, List enabledRuleIds, boolean enabledOnly,
51 | List disabledRuleIds, String mode, String level, List ruleValues, String textSessionID,
52 | String username, String apiKey) {
53 | if (langCode == null && !guessLanguage) {
54 | throw new IllegalArgumentException("No language was set but language guessing was not activated either");
55 | }
56 | if (langCode != null && guessLanguage) {
57 | throw new IllegalArgumentException("Language was set but language guessing was also activated");
58 | }
59 | this.langCode = langCode;
60 | this.motherTongueLangCode = motherTongueLangCode;
61 | this.guessLanguage = guessLanguage;
62 | this.enabledRuleIds = Objects.requireNonNull(enabledRuleIds);
63 | this.enabledOnly = enabledOnly;
64 | this.disabledRuleIds = Objects.requireNonNull(disabledRuleIds);
65 | this.mode = mode;
66 | this.level = level;
67 | this.ruleValues = Objects.requireNonNull(ruleValues);
68 | this.textSessionID = textSessionID;
69 | this.username = username;
70 | this.apiKey = apiKey;
71 | }
72 |
73 | public Optional getLangCode() {
74 | return Optional.ofNullable(langCode);
75 | }
76 |
77 | public String getMotherTongueLangCode() {
78 | return motherTongueLangCode;
79 | }
80 |
81 | public boolean guessLanguage() {
82 | return guessLanguage;
83 | }
84 |
85 | public List getEnabledRuleIds() {
86 | return enabledRuleIds;
87 | }
88 |
89 | public boolean enabledOnly() {
90 | return enabledOnly;
91 | }
92 |
93 | public List getDisabledRuleIds() {
94 | return disabledRuleIds;
95 | }
96 |
97 | public String getMode() {
98 | return mode;
99 | }
100 |
101 | public String getLevel() {
102 | return level;
103 | }
104 |
105 | public List getRuleValues() {
106 | return ruleValues;
107 | }
108 |
109 | @Nullable
110 | public String getTextSessionID() {
111 | return textSessionID;
112 | }
113 |
114 | @Nullable
115 | public String getUsername() {
116 | return username;
117 | }
118 |
119 | @Nullable
120 | public String getAPIKey() {
121 | return apiKey;
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/remote/CheckConfigurationBuilder.java:
--------------------------------------------------------------------------------
1 | /* LanguageTool, a natural language style checker
2 | * Copyright (C) 2016 Daniel Naber (http://www.danielnaber.de)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.remote;
20 |
21 | import java.util.ArrayList;
22 | import java.util.Arrays;
23 | import java.util.List;
24 | import java.util.Objects;
25 |
26 | /**
27 | * Builder for a {@link CheckConfiguration}.
28 | * @since 3.4
29 | */
30 | public class CheckConfigurationBuilder {
31 |
32 | private final String langCode;
33 |
34 | private String motherTongueLangCode;
35 | private boolean autoDetectLanguage;
36 | private boolean enabledOnly;
37 | private List enabledRuleIds = new ArrayList<>();
38 | private List disabledRuleIds = new ArrayList<>();
39 | private String mode = null;
40 | private String level = null;
41 | private List ruleValues = new ArrayList<>();
42 | private String textSessionID = null;
43 | private String username = null;
44 | private String apiKey = null;
45 |
46 | /**
47 | * @param langCode a language code like {@code en} or {@code en-US}
48 | */
49 | public CheckConfigurationBuilder(String langCode) {
50 | this.langCode = Objects.requireNonNull(langCode);
51 | }
52 |
53 | /**
54 | * A configuration that causes the server to automatically detected the text language.
55 | * Note that this requires at least a few sentences of text to work reliably.
56 | */
57 | public CheckConfigurationBuilder() {
58 | this.langCode = null;
59 | this.autoDetectLanguage = true;
60 | }
61 |
62 | public CheckConfiguration build() {
63 | if (enabledOnly && enabledRuleIds.isEmpty()) {
64 | throw new IllegalStateException("You cannot use 'enabledOnly' when you haven't set rule ids to be enabled");
65 | }
66 | return new CheckConfiguration(langCode, motherTongueLangCode, autoDetectLanguage, enabledRuleIds, enabledOnly,
67 | disabledRuleIds, mode, level, ruleValues, textSessionID, username, apiKey);
68 | }
69 |
70 | public CheckConfigurationBuilder setMotherTongueLangCode(String motherTongueLangCode) {
71 | this.motherTongueLangCode = motherTongueLangCode;
72 | return this;
73 | }
74 |
75 | public CheckConfigurationBuilder enabledRuleIds(List ruleIds) {
76 | this.enabledRuleIds = Objects.requireNonNull(ruleIds);
77 | return this;
78 | }
79 |
80 | public CheckConfigurationBuilder enabledRuleIds(String... ruleIds) {
81 | return enabledRuleIds(Arrays.asList(ruleIds));
82 | }
83 |
84 | public CheckConfigurationBuilder enabledOnly() {
85 | this.enabledOnly = true;
86 | return this;
87 | }
88 |
89 | public CheckConfigurationBuilder disabledRuleIds(List ruleIds) {
90 | this.disabledRuleIds = Objects.requireNonNull(ruleIds);
91 | return this;
92 | }
93 |
94 | public CheckConfigurationBuilder disabledRuleIds(String... ruleIds) {
95 | return disabledRuleIds(Arrays.asList(ruleIds));
96 | }
97 |
98 | public CheckConfigurationBuilder mode(String mode) {
99 | this.mode = mode;
100 | return this;
101 | }
102 |
103 | public CheckConfigurationBuilder level(String level) {
104 | this.level = level;
105 | return this;
106 | }
107 |
108 | public CheckConfigurationBuilder ruleValues(List ruleValues) {
109 | this.ruleValues = Objects.requireNonNull(ruleValues);
110 | return this;
111 | }
112 |
113 | public CheckConfigurationBuilder textSessionID(String textSessionID) {
114 | this.textSessionID = textSessionID;
115 | return this;
116 | }
117 |
118 | public CheckConfigurationBuilder username(String username) {
119 | this.username = username;
120 | return this;
121 | }
122 |
123 | public CheckConfigurationBuilder apiKey(String apiKey) {
124 | this.apiKey = apiKey;
125 | return this;
126 | }
127 |
128 | }
129 |
--------------------------------------------------------------------------------
/src/main/java/org/writingtool/remote/RemoteConfigurationInfo.java:
--------------------------------------------------------------------------------
1 | /* LanguageTool, a natural language style checker
2 | * Copyright (C) 2016 Daniel Naber (http://www.danielnaber.de)
3 | *
4 | * This library is free software; you can redistribute it and/or
5 | * modify it under the terms of the GNU Lesser General Public
6 | * License as published by the Free Software Foundation; either
7 | * version 2.1 of the License, or (at your option) any later version.
8 | *
9 | * This library is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 | * Lesser General Public License for more details.
13 | *
14 | * You should have received a copy of the GNU Lesser General Public
15 | * License along with this library; if not, write to the Free Software
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
17 | * USA
18 | */
19 | package org.writingtool.remote;
20 |
21 | import com.fasterxml.jackson.databind.ObjectMapper;
22 |
23 | import java.io.*;
24 | import java.util.List;
25 | import java.util.Map;
26 |
27 | /**
28 | * Check a text using a remote LanguageTool server via HTTP or HTTPS.
29 | * Our public HTTPS API and its restrictions are documented
30 | * here.
31 | * @since 4.8
32 | */
33 | public class RemoteConfigurationInfo {
34 |
35 | private final int maxTextLength;
36 | private final Map softwareInfo;
37 | private final List