PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/raptor/src/raptor/pref/page/ChatConsoleRightClickScripts.java

http://raptor-chess-interface.googlecode.com/
Java | 372 lines | 321 code | 38 blank | 13 comment | 21 complexity | f5d7dfa25d33c672d83b9416571cd3f8 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, IPL-1.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0, LGPL-2.1
  1. /**
  2. * New BSD License
  3. * http://www.opensource.org/licenses/bsd-license.php
  4. * Copyright 2009-2011 RaptorProject (http://code.google.com/p/raptor-chess-interface/)
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  8. *
  9. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  11. * Neither the name of the RaptorProject nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  12. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13. */
  14. package raptor.pref.page;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17. import java.util.List;
  18. import org.apache.commons.lang.StringUtils;
  19. import org.apache.commons.lang.WordUtils;
  20. import org.eclipse.jface.preference.PreferencePage;
  21. import org.eclipse.swt.SWT;
  22. import org.eclipse.swt.custom.CLabel;
  23. import org.eclipse.swt.events.SelectionAdapter;
  24. import org.eclipse.swt.events.SelectionEvent;
  25. import org.eclipse.swt.layout.GridData;
  26. import org.eclipse.swt.layout.GridLayout;
  27. import org.eclipse.swt.layout.RowLayout;
  28. import org.eclipse.swt.widgets.Button;
  29. import org.eclipse.swt.widgets.Combo;
  30. import org.eclipse.swt.widgets.Composite;
  31. import org.eclipse.swt.widgets.Control;
  32. import org.eclipse.swt.widgets.Label;
  33. import org.eclipse.swt.widgets.MessageBox;
  34. import org.eclipse.swt.widgets.Text;
  35. import raptor.Raptor;
  36. import raptor.international.L10n;
  37. import raptor.script.ParameterScript;
  38. import raptor.script.ScriptConnectorType;
  39. import raptor.script.ParameterScript.Type;
  40. import raptor.service.ScriptService;
  41. import raptor.swt.RaptorTable;
  42. import raptor.swt.SWTUtils;
  43. import raptor.swt.ScriptEditorDialog;
  44. public class ChatConsoleRightClickScripts extends PreferencePage {
  45. protected RaptorTable icsActiveScriptsTable;
  46. protected RaptorTable icsInactiveScriptsTable;
  47. protected Composite composite;
  48. protected Text nameText;
  49. protected Text descriptionText;
  50. protected Button isActiveButton;
  51. protected CLabel scriptText;
  52. protected Combo connectorTypeCombo;
  53. protected Button saveButton;
  54. protected Button deleteButton;
  55. protected static L10n local = L10n.getInstance();
  56. public ChatConsoleRightClickScripts() {
  57. super();
  58. setPreferenceStore(Raptor.getInstance().getPreferences());
  59. setTitle(local.getString("rClkScr"));
  60. }
  61. @Override
  62. protected Control createContents(Composite parent) {
  63. composite = new Composite(parent, SWT.NONE);
  64. composite.setLayout(new GridLayout(3, false));
  65. Label textLabel = new Label(composite, SWT.WRAP);
  66. textLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
  67. false, 3, 1));
  68. textLabel
  69. .setText(WordUtils
  70. .wrap(local.getString("rClkScrLbl1"), 70));
  71. Composite tableComposite = new Composite(composite, SWT.NONE);
  72. tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
  73. true, 3, 1));
  74. tableComposite.setLayout(new GridLayout(3, false));
  75. icsActiveScriptsTable = new RaptorTable(tableComposite, SWT.BORDER
  76. | SWT.V_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
  77. icsActiveScriptsTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
  78. true, true));
  79. icsActiveScriptsTable.addColumn(local.getString("rClkScrLbl2"), SWT.LEFT,
  80. 100, false, null);
  81. icsActiveScriptsTable.getTable().addSelectionListener(
  82. new SelectionAdapter() {
  83. @Override
  84. public void widgetSelected(SelectionEvent e) {
  85. int selectedIndex = icsActiveScriptsTable.getTable()
  86. .getSelectionIndex();
  87. String selection = icsActiveScriptsTable.getTable()
  88. .getItem(selectedIndex).getText(0);
  89. loadControls(selection);
  90. }
  91. });
  92. Composite addRemoveComposite = new Composite(tableComposite, SWT.NONE);
  93. addRemoveComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
  94. false, true));
  95. RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
  96. addRemoveComposite.setLayout(rowLayout);
  97. Label strut = new Label(addRemoveComposite, SWT.NONE);
  98. strut.setText(" ");
  99. Label strut2 = new Label(addRemoveComposite, SWT.NONE);
  100. strut2.setText(" ");
  101. Label strut3 = new Label(addRemoveComposite, SWT.NONE);
  102. strut3.setText(" ");
  103. Label strut4 = new Label(addRemoveComposite, SWT.NONE);
  104. strut4.setText(" ");
  105. Button addButton = new Button(addRemoveComposite, SWT.PUSH);
  106. addButton.setImage(Raptor.getInstance().getIcon("back"));
  107. addButton.addSelectionListener(new SelectionAdapter() {
  108. @Override
  109. public void widgetSelected(SelectionEvent e) {
  110. int selectedIndex = icsInactiveScriptsTable.getTable()
  111. .getSelectionIndex();
  112. if (selectedIndex == -1) {
  113. return;
  114. }
  115. String selection = icsInactiveScriptsTable.getTable().getItem(
  116. selectedIndex).getText(0);
  117. ParameterScript script = ScriptService.getInstance()
  118. .getParameterScript(selection);
  119. script.setActive(true);
  120. ScriptService.getInstance().save(script);
  121. refreshTables();
  122. loadControls(script.getName());
  123. }
  124. });
  125. Button removeButton = new Button(addRemoveComposite, SWT.PUSH);
  126. removeButton.setImage(Raptor.getInstance().getIcon("next"));
  127. removeButton.addSelectionListener(new SelectionAdapter() {
  128. @Override
  129. public void widgetSelected(SelectionEvent e) {
  130. int selectedIndex = icsActiveScriptsTable.getTable()
  131. .getSelectionIndex();
  132. if (selectedIndex == -1) {
  133. return;
  134. }
  135. String selection = icsActiveScriptsTable.getTable().getItem(
  136. selectedIndex).getText(0);
  137. ParameterScript script = ScriptService.getInstance()
  138. .getParameterScript(selection);
  139. script.setActive(false);
  140. ScriptService.getInstance().save(script);
  141. refreshTables();
  142. loadControls(script.getName());
  143. }
  144. });
  145. icsInactiveScriptsTable = new RaptorTable(tableComposite, SWT.BORDER
  146. | SWT.V_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
  147. icsInactiveScriptsTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
  148. true, true));
  149. icsInactiveScriptsTable.addColumn(local.getString("rClkScrLbl3"),
  150. SWT.LEFT, 100, true, null);
  151. icsInactiveScriptsTable.getTable().addSelectionListener(
  152. new SelectionAdapter() {
  153. @Override
  154. public void widgetSelected(SelectionEvent e) {
  155. int selectedIndex = icsInactiveScriptsTable.getTable()
  156. .getSelectionIndex();
  157. if (selectedIndex == -1) {
  158. return;
  159. }
  160. String selection = icsInactiveScriptsTable.getTable()
  161. .getItem(selectedIndex).getText(0);
  162. loadControls(selection);
  163. }
  164. });
  165. isActiveButton = new Button(composite, SWT.CHECK);
  166. isActiveButton.setText(local.getString("active"));
  167. isActiveButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
  168. false, 3, 1));
  169. Composite nameComposite = new Composite(composite, SWT.NONE);
  170. nameComposite.setLayout(SWTUtils.createMarginlessGridLayout(2, false));
  171. nameComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
  172. false, 3, 1));
  173. Label nameLabel = new Label(nameComposite, SWT.NONE);
  174. nameLabel.setText(local.getString("name"));
  175. nameLabel
  176. .setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
  177. nameText = new Text(nameComposite, SWT.BORDER);
  178. nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
  179. Composite descriptionComposite = new Composite(composite, SWT.NONE);
  180. descriptionComposite.setLayout(SWTUtils.createMarginlessGridLayout(2,
  181. false));
  182. descriptionComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
  183. true, false, 3, 1));
  184. Label descriptionLabel = new Label(descriptionComposite, SWT.NONE);
  185. descriptionLabel.setText(local.getString("description"));
  186. descriptionLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
  187. false, false));
  188. descriptionText = new Text(descriptionComposite, SWT.BORDER);
  189. descriptionText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
  190. false));
  191. Composite controlsComposite = new Composite(composite, SWT.NONE);
  192. controlsComposite.setLayout(SWTUtils.createMarginlessGridLayout(2,
  193. false));
  194. controlsComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
  195. true, false, 3, 1));
  196. Label connectorTypeLabel = new Label(controlsComposite, SWT.NONE);
  197. connectorTypeLabel.setText(local.getString("connector"));
  198. connectorTypeCombo = new Combo(controlsComposite, SWT.DROP_DOWN
  199. | SWT.READ_ONLY);
  200. for (ScriptConnectorType scriptConnectorType : ScriptConnectorType
  201. .values()) {
  202. connectorTypeCombo.add(scriptConnectorType.name());
  203. }
  204. connectorTypeCombo.select(0);
  205. Label scriptLabel = new Label(composite, SWT.NONE);
  206. scriptLabel.setText(local.getString("script"));
  207. scriptLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
  208. false, 1, 1));
  209. scriptText = new CLabel(composite, SWT.LEFT);
  210. scriptText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
  211. false, 1, 1));
  212. scriptText.setText("\n \n \n \n \n \n");
  213. Button scriptEdit = new Button(composite, SWT.PUSH);
  214. scriptEdit.setText(local.getString("edit"));
  215. scriptEdit.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
  216. false, 1, 1));
  217. scriptEdit.addSelectionListener(new SelectionAdapter() {
  218. @Override
  219. public void widgetSelected(SelectionEvent e) {
  220. ScriptEditorDialog dialog = new ScriptEditorDialog(getShell(),
  221. local.getString("edScr") + nameText.getText());
  222. dialog.setInput(scriptText.getText());
  223. String result = dialog.open();
  224. if (StringUtils.isNotBlank(result)) {
  225. scriptText.setText(result.trim());
  226. }
  227. }
  228. });
  229. Composite buttonComposite = new Composite(composite, SWT.NONE);
  230. buttonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
  231. false, 3, 1));
  232. buttonComposite.setLayout(new RowLayout());
  233. saveButton = new Button(buttonComposite, SWT.PUSH);
  234. saveButton.setText(local.getString("save"));
  235. saveButton.addSelectionListener(new SelectionAdapter() {
  236. @Override
  237. public void widgetSelected(SelectionEvent e) {
  238. onSave();
  239. }
  240. });
  241. deleteButton = new Button(buttonComposite, SWT.PUSH);
  242. deleteButton.setText(local.getString("delete"));
  243. deleteButton.addSelectionListener(new SelectionAdapter() {
  244. @Override
  245. public void widgetSelected(SelectionEvent e) {
  246. ParameterScript script = ScriptService.getInstance()
  247. .getParameterScript(nameText.getText());
  248. if (script.isSystemScript()) {
  249. script.setActive(false);
  250. MessageBox messageBox = new MessageBox(getShell(),
  251. SWT.ICON_INFORMATION);
  252. messageBox.setText(local.getString("alert"));
  253. messageBox
  254. .setMessage(local.getString("rClkScrAl1"));
  255. messageBox.open();
  256. } else {
  257. ScriptService.getInstance().deleteParameterScript(
  258. nameText.getText());
  259. }
  260. refreshTables();
  261. }
  262. });
  263. refreshTables();
  264. return composite;
  265. }
  266. protected void loadControls(String scriptName) {
  267. ParameterScript currentScript = ScriptService.getInstance()
  268. .getParameterScript(scriptName);
  269. nameText.setText(currentScript.getName());
  270. descriptionText.setText(currentScript.getDescription());
  271. isActiveButton.setSelection(currentScript.isActive());
  272. int connectorTypeSelection = 0;
  273. String[] connectorTypeItems = connectorTypeCombo.getItems();
  274. for (int i = 0; i < connectorTypeItems.length; i++) {
  275. if (connectorTypeItems[i].equals(currentScript.getConnectorType()
  276. .name())) {
  277. connectorTypeSelection = i;
  278. break;
  279. }
  280. }
  281. connectorTypeCombo.select(connectorTypeSelection);
  282. scriptText.setText(currentScript.getScript());
  283. }
  284. protected void onSave() {
  285. ParameterScript newScript = ScriptService.getInstance()
  286. .getParameterScript(nameText.getText());
  287. if (newScript == null) {
  288. newScript = new ParameterScript();
  289. }
  290. newScript.setActive(isActiveButton.getSelection());
  291. newScript.setName(nameText.getText());
  292. newScript.setDescription(descriptionText.getText());
  293. newScript.setScript(scriptText.getText().trim());
  294. newScript.setConnectorType(ScriptConnectorType
  295. .valueOf(connectorTypeCombo.getItem(connectorTypeCombo
  296. .getSelectionIndex())));
  297. newScript.setType(Type.ConsoleRightClickScripts);
  298. ScriptService.getInstance().save(newScript);
  299. refreshTables();
  300. }
  301. @Override
  302. protected void performApply() {
  303. onSave();
  304. super.performApply();
  305. }
  306. protected void refreshTables() {
  307. ParameterScript[] allScripts = ScriptService.getInstance()
  308. .getParameterScripts();
  309. List<ParameterScript> inactiveScripts = new ArrayList<ParameterScript>(
  310. 10);
  311. List<ParameterScript> activeScripts = new ArrayList<ParameterScript>(10);
  312. for (ParameterScript script : allScripts) {
  313. if (script.getType() == Type.ConsoleRightClickScripts) {
  314. if (script.isActive()) {
  315. activeScripts.add(script);
  316. } else {
  317. inactiveScripts.add(script);
  318. }
  319. }
  320. }
  321. Collections.sort(activeScripts);
  322. Collections.sort(inactiveScripts);
  323. String[][] activeData = new String[activeScripts.size()][1];
  324. String[][] inactiveData = new String[inactiveScripts.size()][1];
  325. for (int i = 0; i < activeData.length; i++) {
  326. activeData[i][0] = activeScripts.get(i).getName();
  327. }
  328. for (int i = 0; i < inactiveData.length; i++) {
  329. inactiveData[i][0] = inactiveScripts.get(i).getName();
  330. }
  331. icsActiveScriptsTable.refreshTable(activeData);
  332. icsInactiveScriptsTable.refreshTable(inactiveData);
  333. }
  334. }