PageRenderTime 97ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/raptor/src/raptor/pref/page/ActionScriptsPage.java

http://raptor-chess-interface.googlecode.com/
Java | 334 lines | 290 code | 30 blank | 14 comment | 32 complexity | a70f9162d5c431b7e5d45bbab51de3de 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 org.apache.commons.lang.StringUtils;
  16. import org.apache.commons.lang.WordUtils;
  17. import org.eclipse.jface.preference.PreferencePage;
  18. import org.eclipse.swt.SWT;
  19. import org.eclipse.swt.custom.CLabel;
  20. import org.eclipse.swt.events.SelectionAdapter;
  21. import org.eclipse.swt.events.SelectionEvent;
  22. import org.eclipse.swt.layout.GridData;
  23. import org.eclipse.swt.layout.GridLayout;
  24. import org.eclipse.swt.layout.RowLayout;
  25. import org.eclipse.swt.widgets.Button;
  26. import org.eclipse.swt.widgets.Combo;
  27. import org.eclipse.swt.widgets.Composite;
  28. import org.eclipse.swt.widgets.Control;
  29. import org.eclipse.swt.widgets.Label;
  30. import org.eclipse.swt.widgets.Text;
  31. import raptor.Raptor;
  32. import raptor.action.RaptorAction;
  33. import raptor.action.ScriptedAction;
  34. import raptor.action.RaptorAction.Category;
  35. import raptor.action.RaptorAction.RaptorActionContainer;
  36. import raptor.international.L10n;
  37. import raptor.service.ActionScriptService;
  38. import raptor.swt.RaptorTable;
  39. import raptor.swt.SWTUtils;
  40. import raptor.swt.ScriptEditorDialog;
  41. import raptor.util.RaptorUtils;
  42. public class ActionScriptsPage extends PreferencePage {
  43. protected RaptorActionContainer container;
  44. protected Combo icons;
  45. protected Combo categories;
  46. protected RaptorTable scriptedActionsTable;
  47. protected Composite iconComposite;
  48. protected Composite composite;
  49. protected Label icon;
  50. protected Text nameText;
  51. protected Text descriptionText;
  52. protected CLabel scriptText;
  53. protected boolean wasLastSortAscending;
  54. protected static L10n local = L10n.getInstance();
  55. public ActionScriptsPage() {
  56. super();
  57. setPreferenceStore(Raptor.getInstance().getPreferences());
  58. setTitle(local.getString("actScr"));
  59. }
  60. @Override
  61. protected Control createContents(Composite parent) {
  62. composite = new Composite(parent, SWT.NONE);
  63. composite.setLayout(SWTUtils.createMarginlessGridLayout(3, false));
  64. Label textLabel = new Label(composite, SWT.WRAP);
  65. textLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
  66. false, 3, 1));
  67. textLabel
  68. .setText(WordUtils
  69. .wrap(local.getString("actScrPDesc1"), 70)
  70. + "\n"
  71. + local.getString("actScrPDesc2")
  72. + WordUtils
  73. .wrap(local.getString("actScrPDesc3"),
  74. 70));
  75. scriptedActionsTable = new RaptorTable(composite, SWT.BORDER
  76. | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
  77. scriptedActionsTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
  78. false, false, 3, 1));
  79. scriptedActionsTable.addColumn(local.getString("scrName"), SWT.LEFT, 30, true, null);
  80. scriptedActionsTable.addColumn(local.getString("scrCat"), SWT.LEFT, 20, true,
  81. null);
  82. scriptedActionsTable.addColumn(local.getString("description"), SWT.LEFT, 50, true, null);
  83. scriptedActionsTable.getTable().addSelectionListener(
  84. new SelectionAdapter() {
  85. @Override
  86. public void widgetSelected(SelectionEvent e) {
  87. int selectedIndex = scriptedActionsTable.getTable()
  88. .getSelectionIndex();
  89. String selection = scriptedActionsTable.getTable()
  90. .getItem(selectedIndex).getText(0);
  91. loadControls(selection);
  92. }
  93. });
  94. Composite nameComposite = new Composite(composite, SWT.NONE);
  95. nameComposite.setLayout(new GridLayout(2, false));
  96. nameComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
  97. false, 3, 1));
  98. Label nameLabel = new Label(nameComposite, SWT.NONE);
  99. nameLabel.setText(local.getString("name"));
  100. nameLabel
  101. .setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
  102. nameText = new Text(nameComposite, SWT.BORDER | SWT.SINGLE);
  103. nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
  104. iconComposite = new Composite(composite, SWT.NONE);
  105. iconComposite.setLayout(new GridLayout(5, false));
  106. iconComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
  107. false, 3, 1));
  108. Label iconLabel = new Label(iconComposite, SWT.NONE);
  109. iconLabel
  110. .setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
  111. iconLabel.setText(local.getString("icon"));
  112. icons = new Combo(iconComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
  113. icons.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
  114. icons.add("<None>");
  115. icons.setItems(RaptorUtils.getAllIconNames());
  116. icons.addSelectionListener(new SelectionAdapter() {
  117. @Override
  118. public void widgetSelected(SelectionEvent e) {
  119. String selectedText = icons.getItem(icons.getSelectionIndex());
  120. if (selectedText.equals("<None>")) {
  121. icon.setImage(null);
  122. } else {
  123. icon.setImage(Raptor.getInstance().getIcon(selectedText));
  124. }
  125. icon.redraw();
  126. iconComposite.layout(true);
  127. }
  128. });
  129. icons.select(0);
  130. icon = new Label(iconComposite, SWT.NONE);
  131. icon.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
  132. Label categoryLabel = new Label(iconComposite, SWT.NONE);
  133. categoryLabel.setText(" "+local.getString("category"));
  134. categoryLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
  135. false));
  136. categories = new Combo(iconComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
  137. categories.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
  138. false));
  139. for (Category category : Category.values()) {
  140. categories.add(category.toString());
  141. }
  142. categories.select(0);
  143. Composite descriptionComposite = new Composite(composite, SWT.NONE);
  144. descriptionComposite.setLayout(new GridLayout(2, false));
  145. descriptionComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
  146. true, false, 3, 1));
  147. Label descriptionLabel = new Label(descriptionComposite, SWT.NONE);
  148. descriptionLabel.setText(local.getString("description"));
  149. descriptionLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
  150. false, false));
  151. descriptionText = new Text(descriptionComposite, SWT.BORDER
  152. | SWT.SINGLE);
  153. descriptionText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
  154. false));
  155. Label scriptLabel = new Label(composite, SWT.NONE);
  156. scriptLabel.setText(local.getString("script"));
  157. scriptLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
  158. false, 1, 1));
  159. scriptText = new CLabel(composite, SWT.LEFT);
  160. scriptText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
  161. 1, 1));
  162. scriptText.setText(" \n \n \n \n \n");
  163. Button scriptEdit = new Button(composite, SWT.NONE);
  164. scriptEdit.setText(local.getString("edit"));
  165. scriptEdit.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
  166. false, 1, 1));
  167. scriptEdit.addSelectionListener(new SelectionAdapter() {
  168. @Override
  169. public void widgetSelected(SelectionEvent e) {
  170. ScriptEditorDialog dialog = new ScriptEditorDialog(getShell(),
  171. local.getString("edScr") + nameText.getText());
  172. dialog.setInput(scriptText.getText());
  173. String result = dialog.open();
  174. if (StringUtils.isNotBlank(result)) {
  175. scriptText.setText(result.trim());
  176. }
  177. }
  178. });
  179. Composite buttonsComposite = new Composite(composite, SWT.NONE);
  180. buttonsComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
  181. true, false, 3, 1));
  182. buttonsComposite.setLayout(new RowLayout());
  183. Button saveButton = new Button(buttonsComposite, SWT.PUSH);
  184. saveButton.setText(local.getString("save"));
  185. saveButton.addSelectionListener(new SelectionAdapter() {
  186. @Override
  187. public void widgetSelected(SelectionEvent e) {
  188. onSave();
  189. }
  190. });
  191. Button deleteButton = new Button(buttonsComposite, SWT.PUSH);
  192. deleteButton.setText(local.getString("delete"));
  193. deleteButton.addSelectionListener(new SelectionAdapter() {
  194. @Override
  195. public void widgetSelected(SelectionEvent e) {
  196. RaptorAction action = ActionScriptService.getInstance()
  197. .getAction(nameText.getText());
  198. if (action != null && action.isSystemAction()) {
  199. Raptor.getInstance().alert(
  200. local.getString("actScrPAlert1"));
  201. } else if (action == null) {
  202. Raptor.getInstance().alert(
  203. local.getString("actScrPAlert2")
  204. + nameText.getText());
  205. } else {
  206. ActionScriptService.getInstance().deleteAction(
  207. nameText.getText());
  208. refreshActions();
  209. }
  210. }
  211. });
  212. // scriptedActionsTable.sort(0);
  213. refreshActions();
  214. scriptedActionsTable.setSize(scriptedActionsTable.computeSize(
  215. SWT.DEFAULT, 200));
  216. scriptText.setSize(scriptedActionsTable.computeSize(SWT.DEFAULT, 100));
  217. return composite;
  218. }
  219. protected void loadControls(String actionName) {
  220. RaptorAction currentAction = ActionScriptService.getInstance()
  221. .getAction(actionName);
  222. nameText.setText(currentAction.getName());
  223. descriptionText.setText(currentAction.getDescription());
  224. if (currentAction instanceof ScriptedAction) {
  225. scriptText.setText(((ScriptedAction) currentAction).getScript());
  226. } else {
  227. scriptText.setText(local.getString("actScrPAlert3"));
  228. }
  229. if (currentAction.getIcon() == null) {
  230. icon.setImage(null);
  231. icons.select(0);
  232. } else {
  233. icon
  234. .setImage(Raptor.getInstance().getIcon(
  235. currentAction.getIcon()));
  236. int index = 0;
  237. String[] items = icons.getItems();
  238. for (int i = 0; i < items.length; i++) {
  239. if (items[i].equals(currentAction.getIcon())) {
  240. index = i;
  241. break;
  242. }
  243. }
  244. icons.select(index);
  245. }
  246. iconComposite.layout(true);
  247. int categoryIndex = 0;
  248. String[] categoryItems = categories.getItems();
  249. for (int i = 0; i < categoryItems.length; i++) {
  250. if (categoryItems[i].equals(currentAction.getCategory().toString())) {
  251. categoryIndex = i;
  252. break;
  253. }
  254. }
  255. categories.select(categoryIndex);
  256. }
  257. protected void onSave() {
  258. ScriptedAction scriptedAction = new ScriptedAction();
  259. scriptedAction.setName(nameText.getText());
  260. scriptedAction.setDescription(descriptionText.getText());
  261. scriptedAction.setScript(scriptText.getText());
  262. scriptedAction.setCategory(Category.valueOf(categories
  263. .getItem(categories.getSelectionIndex())));
  264. if (icons.getSelectionIndex() == 0) {
  265. scriptedAction.setIcon(null);
  266. } else {
  267. scriptedAction.setIcon(icons.getItem(icons.getSelectionIndex()));
  268. }
  269. if (StringUtils.isBlank(scriptedAction.getName())) {
  270. Raptor.getInstance().alert(local.getString("actScrPAlert4"));
  271. } else if (StringUtils.isBlank(scriptedAction.getDescription())) {
  272. Raptor.getInstance().alert(local.getString("actScrPAlert5"));
  273. } else if (StringUtils.isBlank(scriptedAction.getScript())) {
  274. Raptor.getInstance().alert(local.getString("actScrPAlert6"));
  275. } else {
  276. ActionScriptService.getInstance().saveAction(scriptedAction);
  277. refreshActions();
  278. scriptedActionsTable.getTable().deselectAll();
  279. for (int i = 0; i < scriptedActionsTable.getTable().getItemCount(); i++) {
  280. if (scriptedActionsTable.getTable().getItem(i).getText(0)
  281. .equals(scriptedAction.getName())) {
  282. scriptedActionsTable.getTable().select(i);
  283. break;
  284. }
  285. }
  286. }
  287. }
  288. @Override
  289. protected void performApply() {
  290. onSave();
  291. super.performApply();
  292. }
  293. protected void refreshActions() {
  294. RaptorAction[] scripts = ActionScriptService.getInstance()
  295. .getAllScriptedActions();
  296. String[][] data = new String[scripts.length][];
  297. for (int i = 0; i < data.length; i++) {
  298. RaptorAction action = scripts[i];
  299. data[i] = new String[] { action.getName(),
  300. action.getCategory().toString(), action.getDescription() };
  301. }
  302. scriptedActionsTable.refreshTable(data);
  303. }
  304. }