PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/BungeniEditor/tags/0.9.958/BungeniEditorClient/src/org/bungeni/editor/selectors/BaseMetadataPanel.java

http://bungeni-editor.googlecode.com/
Java | 440 lines | 295 code | 118 blank | 27 comment | 40 complexity | 3cf015eb94f54c3eb38686ce9706846a MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. package org.bungeni.editor.selectors;
  2. //~--- non-JDK imports --------------------------------------------------------
  3. import org.apache.commons.chain.Catalog;
  4. import org.apache.commons.chain.Command;
  5. import org.bungeni.commands.chains.BungeniCatalogCommand;
  6. import org.bungeni.commands.chains.BungeniCommandsCatalogLoader;
  7. import org.bungeni.editor.actions.toolbarAction;
  8. import org.bungeni.editor.actions.toolbarSubAction;
  9. import org.bungeni.editor.selectors.BaseMetadataContainerPanel.ConditionSet;
  10. import org.bungeni.extutils.BungeniEditorProperties;
  11. import org.bungeni.extutils.CommonUIFunctions;
  12. import org.bungeni.ooo.OOComponentHelper;
  13. import org.bungeni.ooo.utils.CommonExceptionUtils;
  14. //~--- JDK imports ------------------------------------------------------------
  15. import com.sun.star.lang.IllegalArgumentException;
  16. import com.sun.star.text.XText;
  17. import com.sun.star.text.XTextCursor;
  18. import com.sun.star.text.XTextRange;
  19. import com.sun.star.text.XTextSection;
  20. import java.awt.Component;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import javax.swing.JFrame;
  24. import javax.swing.JPanel;
  25. /**
  26. *
  27. * @author Ashok Hariharan
  28. */
  29. public abstract class BaseMetadataPanel extends JPanel implements IMetadataPanel {
  30. private static org.apache.log4j.Logger log =
  31. org.apache.log4j.Logger.getLogger(BaseMetadataPanel.class.getName());
  32. private HashMap<String, Object> thePreInsertMap = new HashMap<String, Object>();
  33. protected HashMap<SelectorDialogModes, BungeniCatalogCommand> theCatalogCommands = new HashMap<SelectorDialogModes,
  34. BungeniCatalogCommand>();
  35. private ArrayList<String> fieldsWithNames = new ArrayList<String>(0);
  36. private HashMap<String, Boolean> fieldNamesMap = new HashMap<String, Boolean>();
  37. private BaseMetadataContainerPanel containerPanel;
  38. private BungeniFormContext formContext;
  39. public BaseMetadataPanel() {
  40. super();
  41. }
  42. public void initVariables(BaseMetadataContainerPanel panel) {
  43. this.containerPanel = panel;
  44. String popupDlgBackColor = BungeniEditorProperties.getEditorProperty("popupDialogBackColor");
  45. this.setBackground(java.awt.Color.decode(popupDlgBackColor));
  46. createContext();
  47. initFields();
  48. setFieldNamesMap();
  49. }
  50. private void setFieldNamesMap() {
  51. this.fieldsWithNames = CommonUIFunctions.findComponentsWithNames(this);
  52. for (String sField : fieldsWithNames) {
  53. this.fieldNamesMap.put(sField, Boolean.TRUE);
  54. }
  55. }
  56. private void createContext() {
  57. formContext = new BungeniFormContext();
  58. getFormContext().setTheAction(getTheAction());
  59. getFormContext().setTheSubAction(getTheSubAction());
  60. getFormContext().setOoDocument(getOoDocument());
  61. getFormContext().setPreInsertMap(getThePreInsertMap());
  62. getFormContext().setConditionSet(getConditionSet());
  63. }
  64. public void commonInitFields() {}
  65. private void initFields() {
  66. switch (getDialogMode()) {
  67. case TEXT_SELECTED_EDIT :
  68. commonInitFields();
  69. initFieldsSelectedEdit();
  70. return;
  71. case TEXT_SELECTED_INSERT :
  72. commonInitFields();
  73. initFieldsSelectedInsert();
  74. return;
  75. case TEXT_INSERTION :
  76. commonInitFields();
  77. initFieldsInsert();
  78. return;
  79. case TEXT_EDIT :
  80. commonInitFields();
  81. initFieldsEdit();
  82. return;
  83. default :
  84. return;
  85. }
  86. }
  87. /**
  88. * The following functions are called upon initalization of the panel, depening on the current mode,
  89. * the appropriate function is called.
  90. */
  91. abstract protected void initFieldsSelectedEdit();
  92. abstract protected void initFieldsSelectedInsert();
  93. abstract protected void initFieldsInsert();
  94. abstract protected void initFieldsEdit();
  95. abstract public String getPanelName();
  96. abstract public Component getPanelComponent();
  97. /** helper functions to get variables from parent container */
  98. public BaseMetadataContainerPanel getContainerPanel() {
  99. return containerPanel;
  100. }
  101. public OOComponentHelper getOoDocument() {
  102. return getContainerPanel().getOoDocument();
  103. }
  104. public JFrame getParentFrame() {
  105. return getContainerPanel().getParentFrame();
  106. }
  107. public toolbarAction getTheAction() {
  108. return getContainerPanel().getTheAction();
  109. }
  110. public toolbarSubAction getTheSubAction() {
  111. return getContainerPanel().getTheSubAction();
  112. }
  113. public SelectorDialogModes getDialogMode() {
  114. return getContainerPanel().getDialogMode();
  115. }
  116. public ConditionSet getConditionSet() {
  117. return getContainerPanel().getConditionSet();
  118. }
  119. public void addErrorMessage(java.awt.Component p, String msg) {
  120. getContainerPanel().addErrorMessage(this, p, msg);
  121. }
  122. public String ErrorMessagesAsString() {
  123. return getContainerPanel().ErrorMessagesAsString();
  124. }
  125. public boolean doApply() {
  126. switch (getDialogMode()) {
  127. case TEXT_SELECTED_EDIT :
  128. return applySelectEdit();
  129. case TEXT_SELECTED_INSERT :
  130. return applySelectInsert();
  131. case TEXT_EDIT :
  132. return applyFullEdit();
  133. case TEXT_INSERTION :
  134. return applyFullInsert();
  135. default :
  136. return true;
  137. }
  138. }
  139. public boolean applyFullEdit() {
  140. if (validateFields() == false) {
  141. return false;
  142. }
  143. if (preFullEdit() == false) {
  144. return false;
  145. }
  146. if (processFullEdit() == false) {
  147. return false;
  148. }
  149. if (postFullEdit() == false) {
  150. return false;
  151. }
  152. return true;
  153. }
  154. abstract public boolean preFullEdit();
  155. abstract public boolean processFullEdit();
  156. abstract public boolean postFullEdit();
  157. public boolean applyFullInsert() {
  158. // if (validateFields() == false) {
  159. // return false;
  160. // }
  161. if (preFullInsert() == false) {
  162. return false;
  163. }
  164. if (processFullInsert() == false) {
  165. return false;
  166. }
  167. if (postFullInsert() == false) {
  168. return false;
  169. }
  170. return true;
  171. }
  172. abstract public boolean preFullInsert();
  173. abstract public boolean processFullInsert();
  174. abstract public boolean postFullInsert();
  175. public boolean applySelectEdit() {
  176. // if (validateFields() == false) {
  177. // return false;
  178. // }
  179. if (preSelectEdit() == false) {
  180. return false;
  181. }
  182. if (processSelectEdit() == false) {
  183. return false;
  184. }
  185. if (postSelectEdit() == false) {
  186. return false;
  187. }
  188. return true;
  189. }
  190. abstract public boolean preSelectEdit();
  191. abstract public boolean processSelectEdit();
  192. abstract public boolean postSelectEdit();
  193. public boolean applySelectInsert() {
  194. // /if (validateFields() == false) {
  195. // return false;
  196. // }
  197. if (preSelectInsert() == false) {
  198. return false;
  199. }
  200. if (processSelectInsert() == false) {
  201. return false;
  202. }
  203. if (postSelectInsert() == false) {
  204. return false;
  205. }
  206. return true;
  207. }
  208. abstract public boolean preSelectInsert();
  209. abstract public boolean processSelectInsert();
  210. abstract public boolean postSelectInsert();
  211. public boolean doValidate() {
  212. return validateFields();
  213. }
  214. protected boolean validateFields() {
  215. switch (getDialogMode()) {
  216. case TEXT_SELECTED_EDIT :
  217. return validateSelectedEdit();
  218. case TEXT_SELECTED_INSERT :
  219. return validateSelectedInsert();
  220. case TEXT_EDIT :
  221. return validateFullEdit();
  222. case TEXT_INSERTION :
  223. return validateFullInsert();
  224. default :
  225. return true;
  226. }
  227. }
  228. abstract public boolean validateSelectedEdit();
  229. abstract public boolean validateSelectedInsert();
  230. abstract public boolean validateFullInsert();
  231. abstract public boolean validateFullEdit();
  232. abstract public boolean doCancel();
  233. abstract public boolean doReset();
  234. public BungeniFormContext getFormContext() {
  235. return formContext;
  236. }
  237. public HashMap<String, Object> getThePreInsertMap() {
  238. return thePreInsertMap;
  239. }
  240. protected boolean processCatalogCommand() {
  241. boolean bReturn = false;
  242. try {
  243. if (getTheSubAction() == null) {
  244. // get the current catalog command object for the mode.
  245. BungeniCatalogCommand cmd = theCatalogCommands.get(getDialogMode());
  246. log.info("processCatalogCommand, cmd is : " + ((cmd == null)
  247. ? "null"
  248. : "not null"));
  249. // /load the required catalog for the mode, getCatalogSource refers to the full path fo the command catalog
  250. log.debug("processCatalogCommand : loading catalog");
  251. BungeniCommandsCatalogLoader loader = new BungeniCommandsCatalogLoader(cmd);
  252. Catalog selectedCatalog;
  253. // selectedCatalog = loader.getCatalog(cmd.getCommandCatalog());
  254. selectedCatalog = loader.getCatalog();
  255. log.debug("processCatalogCommand : getting commandChain from catalog = " + cmd.getCommandChain());
  256. Command selectedCatalogCommand = selectedCatalog.getCommand(cmd.getCommandChain());
  257. log.debug("processCatalogCommand : executing command ");
  258. selectedCatalogCommand.execute(formContext);
  259. } else {
  260. // theSubAciton is not null use the
  261. if (getTheSubAction().action_command_chain().length() > 0) {
  262. BungeniCatalogCommand cmd = theCatalogCommands.get(getDialogMode());
  263. BungeniCommandsCatalogLoader loader =
  264. new BungeniCommandsCatalogLoader(cmd /* .getCatalogSource() */);
  265. Catalog selectedCatalog;
  266. selectedCatalog = loader.getCatalog( /* cmd.getCommandCatalog() */);
  267. // now load the command chain from the sub_action rather than from the catalogcommand object
  268. String commandChain = getTheSubAction().action_command_chain();
  269. Command selectedCatalogCommand = selectedCatalog.getCommand(commandChain);
  270. selectedCatalogCommand.execute(formContext);
  271. } else {
  272. log.debug("processCatalogCommand : command chain length = 0 ");
  273. }
  274. }
  275. bReturn = true;
  276. } catch (Exception ex) {
  277. log.error("exception in processCatalogCommand: " + ex.getMessage());
  278. log.error("exception in processCatalogCommand: " + CommonExceptionUtils.getStackTrace(ex));
  279. bReturn = false;
  280. } finally {
  281. return bReturn;
  282. }
  283. }
  284. public boolean doUpdateEvent() {
  285. return true;
  286. }
  287. public String getSectionMetadataValue(String valueName) {
  288. // connect fields to metadata...
  289. OOComponentHelper ooDoc = getContainerPanel().getOoDocument();
  290. XTextSection currentSection = ooDoc.currentSection();
  291. HashMap<String, String> sectionMeta = new HashMap<String, String>();
  292. if (currentSection != null) {
  293. sectionMeta = ooDoc.getSectionMetadataAttributes(currentSection);
  294. if (sectionMeta.containsKey(valueName)) {
  295. return sectionMeta.get(valueName);
  296. }
  297. }
  298. // return blank if not found
  299. return new String("");
  300. }
  301. protected boolean pasteTextIntoDocument(String text) {
  302. boolean bState = false;
  303. try {
  304. XTextRange xStartRange = getContainerPanel().getOoDocument().getViewCursor().getStart();
  305. XText xCursorText = getContainerPanel().getOoDocument().getViewCursor().getText();
  306. XTextCursor startCur = xCursorText.createTextCursorByRange(xStartRange);
  307. xCursorText.insertString(startCur, text, false);
  308. xCursorText.insertControlCharacter(startCur, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
  309. bState = true;
  310. } catch (IllegalArgumentException ex) {
  311. log.error("pasteTextIntoDocument : " + ex.getMessage());
  312. } finally {
  313. return bState;
  314. }
  315. }
  316. public void enableChildControls(boolean bState) {
  317. for (String fieldName : fieldsWithNames) {
  318. Component cc = CommonUIFunctions.findComponentByName(this, fieldName);
  319. cc.setEnabled(bState);
  320. }
  321. }
  322. }