PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/DockingLayoutManager.java

#
Java | 272 lines | 241 code | 26 blank | 5 comment | 59 complexity | 7dd1b8e817f1c2d2b6e5898844d12517 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. package org.gjt.sp.jedit.gui;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.swing.JOptionPane;
  5. import org.gjt.sp.jedit.ActionSet;
  6. import org.gjt.sp.jedit.Buffer;
  7. import org.gjt.sp.jedit.EBComponent;
  8. import org.gjt.sp.jedit.EBMessage;
  9. import org.gjt.sp.jedit.EditAction;
  10. import org.gjt.sp.jedit.EditBus;
  11. import org.gjt.sp.jedit.Mode;
  12. import org.gjt.sp.jedit.View;
  13. import org.gjt.sp.jedit.jEdit;
  14. import org.gjt.sp.jedit.gui.DockableWindowManager.DockingLayout;
  15. import org.gjt.sp.jedit.msg.BufferUpdate;
  16. import org.gjt.sp.jedit.msg.EditPaneUpdate;
  17. import org.gjt.sp.jedit.msg.ViewUpdate;
  18. import org.gjt.sp.jedit.options.DockingOptionPane;
  19. /** Saves and loads dockable layouts to disk
  20. @author Shlomy Reinstein
  21. @since jEdit 4.3pre16
  22. */
  23. public class DockingLayoutManager implements EBComponent
  24. {
  25. private static final String NO_SETTINGS_MESSAGE = "no-settings.message";
  26. private static final String SAVE_LAYOUT_FAILED = "save-layout-failed.message";
  27. private static final String LOAD_LAYOUT_TITLE = "load-layout.title";
  28. private static final String LOAD_LAYOUT_MESSAGE = "load-layout.message";
  29. private static final String SAVE_LAYOUT_TITLE = "save-layout.title";
  30. private static final String SAVE_LAYOUT_MESSAGE = "save-layout.message";
  31. private static ActionSet actions;
  32. private static DockingLayoutManager instance;
  33. private Map<View, String> currentMode;
  34. private DockingLayoutManager()
  35. {
  36. currentMode = new HashMap<View, String>();
  37. }
  38. private static boolean save(View view, String layoutName)
  39. {
  40. DockingLayout docking = view.getViewConfig().docking;
  41. if (docking != null)
  42. {
  43. boolean ret = docking.saveLayout(layoutName, DockingLayout.NO_VIEW_INDEX);
  44. if (! ret)
  45. return false;
  46. addAction(layoutName);
  47. }
  48. return true;
  49. }
  50. public static void saveAs(View view)
  51. {
  52. if (jEdit.getSettingsDirectory() == null)
  53. {
  54. JOptionPane.showMessageDialog(view, jEdit.getProperty(NO_SETTINGS_MESSAGE));
  55. return;
  56. }
  57. String layoutName = JOptionPane.showInputDialog(view,
  58. jEdit.getProperty(SAVE_LAYOUT_MESSAGE),
  59. jEdit.getProperty(SAVE_LAYOUT_TITLE),
  60. JOptionPane.QUESTION_MESSAGE);
  61. if (layoutName == null)
  62. return;
  63. if (! save(view, layoutName))
  64. JOptionPane.showMessageDialog(view, jEdit.getProperty(SAVE_LAYOUT_FAILED));
  65. }
  66. private static void load(View view, String layoutName)
  67. {
  68. DockingLayout docking = View.getDockingFrameworkProvider().createDockingLayout();
  69. if (docking.loadLayout(layoutName, DockingLayout.NO_VIEW_INDEX))
  70. view.getDockableWindowManager().setDockingLayout(docking);
  71. }
  72. public static void load(View view)
  73. {
  74. if (jEdit.getSettingsDirectory() == null)
  75. {
  76. JOptionPane.showMessageDialog(view, jEdit.getProperty(NO_SETTINGS_MESSAGE));
  77. return;
  78. }
  79. String layoutName = (String) JOptionPane.showInputDialog(view,
  80. jEdit.getProperty(LOAD_LAYOUT_MESSAGE),
  81. jEdit.getProperty(LOAD_LAYOUT_TITLE),
  82. JOptionPane.QUESTION_MESSAGE,
  83. null,
  84. getSavedLayouts(),
  85. null);
  86. if (layoutName == null)
  87. return;
  88. load(view, layoutName);
  89. }
  90. private static String[] getSavedLayouts()
  91. {
  92. DockingLayout docking = View.getDockingFrameworkProvider().createDockingLayout();
  93. String[] layouts = null;
  94. if (docking != null)
  95. layouts = docking.getSavedLayouts();
  96. if (layouts == null)
  97. return new String[0];
  98. return layouts;
  99. }
  100. private static void addAction(String layoutName)
  101. {
  102. if ((actions != null) && (! actions.contains(layoutName)))
  103. actions.addAction(new LoadPerspectiveAction(layoutName));
  104. }
  105. public static void init()
  106. {
  107. createActions();
  108. instance = new DockingLayoutManager();
  109. EditBus.addToBus(instance);
  110. }
  111. private static void createActions()
  112. {
  113. actions = new ActionSet("Docking Layouts");
  114. String[] layouts = getSavedLayouts();
  115. for (String layout: layouts)
  116. addAction(layout);
  117. jEdit.addActionSet(actions);
  118. actions.initKeyBindings();
  119. }
  120. public static void removeActions()
  121. {
  122. jEdit.removeActionSet(actions);
  123. }
  124. private static class LoadPerspectiveAction extends EditAction
  125. {
  126. private static final String LOAD_PREFIX = "load-";
  127. public LoadPerspectiveAction(String layoutName)
  128. {
  129. super(LOAD_PREFIX + layoutName, new String[] { layoutName });
  130. jEdit.setTemporaryProperty(LOAD_PREFIX + layoutName + ".label", LOAD_PREFIX + layoutName);
  131. }
  132. @Override
  133. public void invoke(View view)
  134. {
  135. DockingLayoutManager.load(view, (String) args[0]);
  136. }
  137. }
  138. private boolean canChangeEditMode(EBMessage message)
  139. {
  140. if (message instanceof BufferUpdate)
  141. {
  142. BufferUpdate bu = (BufferUpdate) message;
  143. Object what = bu.getWhat();
  144. if ((what == BufferUpdate.CLOSED) ||
  145. (what == BufferUpdate.CREATED) ||
  146. (what == BufferUpdate.PROPERTIES_CHANGED))
  147. {
  148. return true;
  149. }
  150. }
  151. else if (message instanceof EditPaneUpdate)
  152. {
  153. EditPaneUpdate ep = (EditPaneUpdate) message;
  154. Object what = ep.getWhat();
  155. if ((what == EditPaneUpdate.BUFFER_CHANGED) ||
  156. (what == EditPaneUpdate.CREATED))
  157. {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. public void handleMessage(EBMessage message)
  164. {
  165. boolean autoLoadModeLayout = jEdit.getBooleanProperty(
  166. DockingOptionPane.AUTO_LOAD_MODE_LAYOUT_PROP, false);
  167. if (! autoLoadModeLayout)
  168. return;
  169. if (message instanceof ViewUpdate)
  170. {
  171. ViewUpdate vu = (ViewUpdate) message;
  172. if (vu.getWhat() == ViewUpdate.CLOSED)
  173. {
  174. View view = jEdit.getActiveView();
  175. String mode = currentMode.get(view);
  176. saveModeLayout(view, mode);
  177. return;
  178. }
  179. }
  180. // Check for a change in the edit mode
  181. View view = jEdit.getActiveView();
  182. if (view == null)
  183. return;
  184. if (! canChangeEditMode(message))
  185. return;
  186. String newMode = getCurrentEditMode(view);
  187. String mode = currentMode.get(view);
  188. boolean sameMode =
  189. (mode == null && newMode == null) ||
  190. (mode != null && mode.equals(newMode));
  191. if (! sameMode)
  192. {
  193. boolean autoSaveModeLayout = jEdit.getBooleanProperty(
  194. DockingOptionPane.AUTO_SAVE_MODE_LAYOUT_PROP, false);
  195. if (autoSaveModeLayout)
  196. saveModeLayout(view, mode);
  197. currentMode.put(view, newMode);
  198. loadModeLayout(view, newMode);
  199. }
  200. }
  201. private String getCurrentEditMode(View view)
  202. {
  203. Buffer buffer = view.getBuffer();
  204. if (buffer == null)
  205. return null;
  206. Mode bufferMode = buffer.getMode();
  207. if (bufferMode == null)
  208. return null;
  209. return bufferMode.getName();
  210. }
  211. private static final String GLOBAL_MODE = "DEFAULT";
  212. private void saveModeLayout(View view, String mode)
  213. {
  214. String modeLayout = getModePerspective(mode);
  215. if (modeLayout == null)
  216. return;
  217. save(view, modeLayout);
  218. }
  219. private void loadModeLayout(View view, String mode)
  220. {
  221. String modeLayout = getModePerspective(mode);
  222. if (modeLayout == null)
  223. return;
  224. load(view, modeLayout);
  225. }
  226. public static void loadCurrentModeLayout(View view)
  227. {
  228. if (view == null)
  229. return;
  230. String mode = instance.getCurrentEditMode(view);
  231. instance.loadModeLayout(view, mode);
  232. }
  233. public static void saveCurrentModeLayout(View view)
  234. {
  235. if (view == null)
  236. return;
  237. String mode = instance.getCurrentEditMode(view);
  238. instance.saveModeLayout(view, mode);
  239. }
  240. private String getModePerspective(String mode)
  241. {
  242. if (mode == null)
  243. mode = GLOBAL_MODE;
  244. return "mode-" + mode;
  245. }
  246. }