PageRenderTime 73ms CodeModel.GetById 44ms RepoModel.GetById 1ms app.codeStats 0ms

/flexodesktop/GUI/flexo/src/main/java/org/openflexo/ch/FCH.java

https://github.com/agilebirds/openflexo
Java | 334 lines | 259 code | 39 blank | 36 comment | 70 complexity | bff39bd7a8a6b49102c2b0f947fff928 MD5 | raw file
  1. /*
  2. * (c) Copyright 2010-2011 AgileBirds
  3. *
  4. * This file is part of OpenFlexo.
  5. *
  6. * OpenFlexo is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * OpenFlexo is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package org.openflexo.ch;
  21. import java.awt.Component;
  22. import java.awt.Container;
  23. import java.awt.Window;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Map.Entry;
  27. import java.util.WeakHashMap;
  28. import java.util.logging.Logger;
  29. import javax.swing.JComponent;
  30. import org.openflexo.GeneralPreferences;
  31. import org.openflexo.drm.DocItem;
  32. import org.openflexo.drm.DocItemFolder;
  33. import org.openflexo.drm.DocResourceCenter;
  34. import org.openflexo.drm.DocResourceManager;
  35. import org.openflexo.drm.Language;
  36. import org.openflexo.localization.FlexoLocalization;
  37. import org.openflexo.module.FlexoModule;
  38. import org.openflexo.view.FlexoFrame;
  39. import org.openflexo.view.FlexoMainPane;
  40. import org.openflexo.view.ModuleView;
  41. /**
  42. * Utility class provinding facilities to handle contextual help Note: FCH stands for FlexoContextualHelp (ou bien
  43. * "Fuck cette Connerie de Help")
  44. *
  45. * @author sguerin
  46. */
  47. public class FCH {
  48. private static final Logger logger = Logger.getLogger(FCH.class.getPackage().getName());
  49. private static WeakHashMap<JComponent, DocItem> _docForComponent = new WeakHashMap<JComponent, DocItem>();
  50. private static WeakHashMap<JComponent, String> _pendingComponents = new WeakHashMap<JComponent, String>();
  51. private static synchronized Window getWindowForComponent(JComponent component) {
  52. JComponent current = component;
  53. Container parent;
  54. while (current.getParent() != null) {
  55. parent = current.getParent();
  56. if (parent instanceof JComponent) {
  57. current = (JComponent) parent;
  58. } else if (parent instanceof Window) {
  59. return (Window) parent;
  60. } else {
  61. return null;
  62. }
  63. }
  64. return null;
  65. }
  66. private static class SortComponents {
  67. private List<JComponent> initialVector;
  68. private List<Component> sortedVector;
  69. protected SortComponents(List<JComponent> sortingSet, Window window) {
  70. initialVector = sortingSet;
  71. sortedVector = new ArrayList<Component>();
  72. populateFrom(window);
  73. }
  74. private void populateFrom(Container component) {
  75. if (initialVector.contains(component)) {
  76. sortedVector.add(component);
  77. }
  78. for (int i = 0; i < component.getComponentCount(); i++) {
  79. Component comp = component.getComponent(i);
  80. if (comp instanceof Container) {
  81. populateFrom((Container) comp);
  82. } else if (initialVector.contains(comp)) {
  83. sortedVector.add(comp);
  84. }
  85. }
  86. }
  87. protected List<Component> getSortedVector() {
  88. return sortedVector;
  89. }
  90. }
  91. public static synchronized void validateWindow(Window window) {
  92. // logger.info("Validate window");
  93. List<JComponent> concernedComponents = new ArrayList<JComponent>();
  94. for (Entry<JComponent, String> e : _pendingComponents.entrySet()) {
  95. JComponent next = e.getKey();
  96. if (next != null && getWindowForComponent(next) == window) {
  97. concernedComponents.add(next);
  98. }
  99. }
  100. List<Component> sortedConcernedComponents = new SortComponents(concernedComponents, window).getSortedVector();
  101. for (Component next : sortedConcernedComponents) {
  102. validateHelpItem((JComponent) next, _pendingComponents.get(next));
  103. _pendingComponents.remove(next);
  104. }
  105. }
  106. public static synchronized void setHelpItem(final JComponent component, final String anIdentifier) {
  107. // logger.info("setHelpItem for "+anIdentifier);
  108. if (component != null && anIdentifier != null) {
  109. _pendingComponents.put(component, anIdentifier);
  110. }
  111. }
  112. public static synchronized void validateHelpItem(final JComponent component, final String anIdentifier) {
  113. JComponent parent = getClosestDocumentedAncestorComponent(component);
  114. String identifier = anIdentifier;
  115. if (parent != null) {
  116. DocItem parentDocItem = getDocForComponent(parent);
  117. if (!anIdentifier.startsWith(parentDocItem.getIdentifier())) {
  118. identifier = parentDocItem.getIdentifier() + "-" + anIdentifier;
  119. }
  120. validateHelpItem(component, identifier, parentDocItem);
  121. return;
  122. }
  123. validateHelpItem(component, identifier, null);
  124. }
  125. public static synchronized void validateHelpItem(final JComponent component, final String anIdentifier, final DocItem parentDocItem) {
  126. // logger.info(">>>>>>> Validate for "+anIdentifier+" under "+parentDocItem);
  127. String identifier = anIdentifier;
  128. if (parentDocItem != null) {
  129. if (!anIdentifier.startsWith(parentDocItem.getIdentifier())) {
  130. identifier = parentDocItem.getIdentifier() + "-" + anIdentifier;
  131. }
  132. }
  133. DocItem item = DocResourceManager.getDocItem(identifier);
  134. if (item == null) {
  135. item = createCHEntry(component, identifier, parentDocItem);
  136. }
  137. _docForComponent.put(component, item);
  138. Language lang = DocResourceManager.instance().getLanguage(GeneralPreferences.getLanguage());
  139. String tooltipText = null;
  140. DocItem currentItem = item;
  141. while (tooltipText == null && currentItem != null) {
  142. if (currentItem.getLastApprovedActionForLanguage(lang) != null) {
  143. tooltipText = currentItem.getLastApprovedActionForLanguage(lang).getVersion().getShortHTMLDescription();
  144. }
  145. currentItem = currentItem.getInheritanceParentItem();
  146. }
  147. /*if (item.getLastApprovedActionForLanguage(lang) != null) {
  148. tooltipText = item.getLastApprovedActionForLanguage(lang).getVersion().getShortHTMLDescription();
  149. }
  150. else {
  151. tooltipText = FlexoLocalization.localizedForKeyAndLanguage("no_documentation",GeneralPreferences.getLanguage());
  152. }*/
  153. if (tooltipText == null) {
  154. tooltipText = FlexoLocalization.localizedForKeyAndLanguage("no_documentation", GeneralPreferences.getLanguage());
  155. }
  156. component.setToolTipText("<html>" + tooltipText + "</html>");
  157. }
  158. public static synchronized void setHelpItem(JComponent component, DocItem item) {
  159. setHelpItem(component, item.getIdentifier());
  160. }
  161. private static synchronized JComponent getClosestDocumentedAncestorComponent(JComponent component) {
  162. JComponent parent = null;
  163. DocItem parentItem = null;
  164. JComponent current = component;
  165. while (parentItem == null && current.getParent() != null && current.getParent() instanceof JComponent) {
  166. current = (JComponent) current.getParent();
  167. parentItem = getDocForComponent(current);
  168. if (parentItem != null) {
  169. parent = current;
  170. }
  171. }
  172. return parent;
  173. }
  174. private static synchronized DocItem createCHEntry(JComponent component, String identifier, DocItem parentItem) {
  175. if (parentItem == null) {
  176. parentItem = DocResourceManager.instance().getFlexoToolSetItem();
  177. }
  178. logger.info("Create entry for " + identifier + " under " + parentItem);
  179. DocItem newEntry = DocItem.createDocItem(identifier, "", parentItem.getFolder(), DocResourceManager.instance()
  180. .getDocResourceCenter(), false);
  181. parentItem.addToEmbeddingChildItems(newEntry);
  182. return newEntry;
  183. }
  184. public static DocItem getDocForComponent(JComponent component) {
  185. return _docForComponent.get(component);
  186. }
  187. private static final String MAIN_PANE_ID = "main-pane";
  188. private static final String CONTROL_PANEL_ID = "control-panel";
  189. private static final String LEFT_VIEW_ID = "left-view";
  190. private static final String RIGHT_VIEW_ID = "right-view";
  191. public static void ensureHelpEntryForModuleHaveBeenCreated(FlexoModule module) {
  192. DocItem newModuleItem = getDocItemFor(module);
  193. DocItem mainPaneItem = getMainPaneItemFor(module);
  194. DocItem controlPanelItem = getControlPanelItemFor(module);
  195. DocItem leftViewItem = getLeftViewItemFor(module);
  196. DocItem rightViewItem = getRightViewItemFor(module);
  197. FlexoFrame frame = module.getFlexoFrame();
  198. FlexoMainPane mainPane = module.getFlexoController().getMainPane();
  199. if (mainPane != null) {
  200. // TODO: restore help on main pane top bar
  201. // setHelpItem(mainPane.getControlPanel(), controlPanelItem);
  202. }
  203. }
  204. public static DocItem getDocItemFor(FlexoModule module) {
  205. DocResourceCenter drc = DocResourceManager.instance().getDocResourceCenter();
  206. String identifier = module.getModule().getHelpTopic();
  207. if (DocResourceManager.getDocItem(identifier) == null) {
  208. DocItemFolder newModuleFolder = DocItemFolder.createDocItemFolder(identifier, "", DocResourceManager.instance()
  209. .getFlexoToolSetItem().getFolder(), drc);
  210. DocResourceManager.instance().getAbstractModuleItem().addToInheritanceChildItems(newModuleFolder.getPrimaryDocItem());
  211. }
  212. return DocResourceManager.getDocItem(identifier);
  213. }
  214. public static DocItem getMainPaneItemFor(FlexoModule module) {
  215. DocResourceCenter drc = DocResourceManager.instance().getDocResourceCenter();
  216. String identifier = module.getModule().getHelpTopic();
  217. String mainPaneId = identifier + "-" + MAIN_PANE_ID;
  218. DocItem newModuleItem = getDocItemFor(module);
  219. if (DocResourceManager.getDocItem(mainPaneId) == null) {
  220. DocItemFolder mainPaneFolder = DocItemFolder.createDocItemFolder(mainPaneId, "", newModuleItem.getFolder(), drc);
  221. DocResourceManager.instance().getAbstractMainPaneItem().addToInheritanceChildItems(mainPaneFolder.getPrimaryDocItem());
  222. newModuleItem.addToEmbeddingChildItems(mainPaneFolder.getPrimaryDocItem());
  223. }
  224. return DocResourceManager.getDocItem(mainPaneId);
  225. }
  226. public static DocItem getModuleViewItemFor(FlexoModule module, ModuleView view) {
  227. DocResourceCenter drc = DocResourceManager.instance().getDocResourceCenter();
  228. DocItem mainPaneItem = getMainPaneItemFor(module);
  229. DocItemFolder folder;
  230. if (view.getPerspective() != null) {
  231. String perspectiveIdentifier = view.getPerspective().getName();
  232. if (DocResourceManager.getDocItem(perspectiveIdentifier) == null) {
  233. DocItemFolder perspectiveFolder = DocItemFolder.createDocItemFolder(perspectiveIdentifier,
  234. "Documentation on that perspective", mainPaneItem.getFolder(), drc);
  235. mainPaneItem.addToEmbeddingChildItems(perspectiveFolder.getPrimaryDocItem());
  236. }
  237. folder = DocResourceManager.getDocItem(perspectiveIdentifier).getFolder();
  238. } else {
  239. folder = mainPaneItem.getFolder();
  240. }
  241. String moduleViewIdentifier = view.getClass().getName();
  242. if (moduleViewIdentifier.lastIndexOf('.') > 0) {
  243. moduleViewIdentifier = moduleViewIdentifier.substring(moduleViewIdentifier.lastIndexOf('.') + 1);
  244. }
  245. if (DocResourceManager.getDocItem(moduleViewIdentifier) == null) {
  246. DocItemFolder moduleViewFolder = DocItemFolder.createDocItemFolder(moduleViewIdentifier, "Documentation on that view", folder,
  247. drc);
  248. mainPaneItem.addToInheritanceChildItems(moduleViewFolder.getPrimaryDocItem());
  249. if (view.getPerspective() != null && folder.getPrimaryDocItem() != null) {
  250. moduleViewFolder.getPrimaryDocItem().addToRelatedToItems(folder.getPrimaryDocItem());
  251. }
  252. }
  253. return DocResourceManager.getDocItem(moduleViewIdentifier);
  254. }
  255. public static DocItem getControlPanelItemFor(FlexoModule module) {
  256. DocResourceCenter drc = DocResourceManager.instance().getDocResourceCenter();
  257. String identifier = module.getModule().getHelpTopic();
  258. String controlPanelId = identifier + "-" + CONTROL_PANEL_ID;
  259. DocItem newModuleItem = getDocItemFor(module);
  260. if (DocResourceManager.getDocItem(controlPanelId) == null) {
  261. DocItemFolder controlPanelFolder = DocItemFolder.createDocItemFolder(controlPanelId, "", newModuleItem.getFolder(), drc);
  262. DocResourceManager.instance().getAbstractControlPanelItem().addToInheritanceChildItems(controlPanelFolder.getPrimaryDocItem());
  263. newModuleItem.addToEmbeddingChildItems(controlPanelFolder.getPrimaryDocItem());
  264. }
  265. return DocResourceManager.getDocItem(controlPanelId);
  266. }
  267. public static DocItem getLeftViewItemFor(FlexoModule module) {
  268. DocResourceCenter drc = DocResourceManager.instance().getDocResourceCenter();
  269. String identifier = module.getModule().getHelpTopic();
  270. String leftViewId = identifier + "-" + LEFT_VIEW_ID;
  271. DocItem newModuleItem = getDocItemFor(module);
  272. if (DocResourceManager.getDocItem(leftViewId) == null) {
  273. DocItemFolder leftViewFolder = DocItemFolder.createDocItemFolder(leftViewId, "", newModuleItem.getFolder(), drc);
  274. DocResourceManager.instance().getAbstractLeftViewItem().addToInheritanceChildItems(leftViewFolder.getPrimaryDocItem());
  275. newModuleItem.addToEmbeddingChildItems(leftViewFolder.getPrimaryDocItem());
  276. }
  277. return DocResourceManager.getDocItem(leftViewId);
  278. }
  279. public static DocItem getRightViewItemFor(FlexoModule module) {
  280. DocResourceCenter drc = DocResourceManager.instance().getDocResourceCenter();
  281. String identifier = module.getModule().getHelpTopic();
  282. String rightViewId = identifier + "-" + RIGHT_VIEW_ID;
  283. DocItem newModuleItem = getDocItemFor(module);
  284. if (DocResourceManager.getDocItem(rightViewId) == null) {
  285. DocItemFolder rightViewFolder = DocItemFolder.createDocItemFolder(rightViewId, "", newModuleItem.getFolder(), drc);
  286. DocResourceManager.instance().getAbstractRightViewItem().addToInheritanceChildItems(rightViewFolder.getPrimaryDocItem());
  287. newModuleItem.addToEmbeddingChildItems(rightViewFolder.getPrimaryDocItem());
  288. }
  289. return DocResourceManager.getDocItem(rightViewId);
  290. }
  291. public static void clearComponentsHashtable() {
  292. _docForComponent.clear();
  293. _pendingComponents.clear();
  294. }
  295. }