PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/Ancestor/src/gatchan/jedit/ancestor/AncestorPlugin.java

#
Java | 197 lines | 151 code | 14 blank | 32 comment | 24 complexity | 31c05d5a22e3e86c1bc6de4594ed8627 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. /*
  2. * AncestorPlugin.java - The Ancestor plugin
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2007, 2011 Matthieu Casanova
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package gatchan.jedit.ancestor;
  22. import java.awt.Component;
  23. import java.awt.FlowLayout;
  24. import java.lang.reflect.Field;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import javax.swing.JComponent;
  28. import javax.swing.JPanel;
  29. import org.gjt.sp.jedit.EditBus;
  30. import org.gjt.sp.jedit.EditPane;
  31. import org.gjt.sp.jedit.EditPlugin;
  32. import org.gjt.sp.jedit.View;
  33. import org.gjt.sp.jedit.jEdit;
  34. import org.gjt.sp.jedit.msg.BufferUpdate;
  35. import org.gjt.sp.jedit.msg.EditPaneUpdate;
  36. import org.gjt.sp.jedit.msg.ViewUpdate;
  37. import org.gjt.sp.util.Log;
  38. /**
  39. * @author Matthieu Casanova
  40. * @version $Id: Server.java,v 1.33 2007/01/05 15:15:17 matthieu Exp $
  41. */
  42. public class AncestorPlugin extends EditPlugin
  43. {
  44. private final Map<View, AncestorToolBar> viewAncestorToolBar = new HashMap<View, AncestorToolBar>();
  45. private final Map<View, JComponent> topToolbars = new HashMap<View, JComponent>();
  46. //{{{ start() method
  47. @Override
  48. public void start()
  49. {
  50. View[] views = jEdit.getViews();
  51. for (int i = 0; i < views.length; i++)
  52. {
  53. addAncestorToolBar(views[i]);
  54. }
  55. EditBus.addToBus(this);
  56. } //}}}
  57. //{{{ addAncestorToolBar() method
  58. private void addAncestorToolBar(View view)
  59. {
  60. if (viewAncestorToolBar.containsKey(view))
  61. return;
  62. AncestorToolBar ancestorToolBar = new AncestorToolBar(view);
  63. EditPane editPane = view.getEditPane();
  64. ancestorToolBar.setBuffer(editPane.getBuffer());
  65. JComponent toolBar = getViewToolbar(view);
  66. toolBar.add(ancestorToolBar);
  67. toolBar.validate();
  68. topToolbars.put(view, toolBar);
  69. viewAncestorToolBar.put(view, ancestorToolBar);
  70. } //}}}
  71. private JComponent getViewToolbar(View view)
  72. {
  73. try
  74. {
  75. Field topToolBarsField = view.getClass().getDeclaredField("topToolBars");
  76. topToolBarsField.setAccessible(true);
  77. JPanel topToolBars = (JPanel) topToolBarsField.get(view);
  78. Component[] components = topToolBars.getComponents();
  79. if (components.length > 1)
  80. {
  81. for (int i = 1; i < components.length; i++)
  82. {
  83. Component component = components[i];
  84. if (component instanceof JComponent)
  85. {
  86. JComponent toolBar = (JComponent) component;
  87. if (toolBar.getClientProperty("Ancestor-SmartOpen") == Boolean.TRUE)
  88. return toolBar;
  89. }
  90. }
  91. }
  92. }
  93. catch (NoSuchFieldException e)
  94. {
  95. Log.log(Log.ERROR, this, e);
  96. }
  97. catch (IllegalAccessException e)
  98. {
  99. Log.log(Log.ERROR, this, e);
  100. }
  101. JPanel customToolbar = new JPanel(new FlowLayout(FlowLayout.LEADING));
  102. customToolbar.putClientProperty("Ancestor-SmartOpen", Boolean.TRUE);
  103. view.addToolBar(customToolbar);
  104. return customToolbar;
  105. }
  106. //{{{ removeAncestorToolBar() method
  107. private void removeAncestorToolBar(View view)
  108. {
  109. AncestorToolBar toolBar = viewAncestorToolBar.get(view);
  110. JComponent top = topToolbars.get(view);
  111. top.remove(toolBar);
  112. if (top.getComponentCount() == 0)
  113. {
  114. topToolbars.remove(view);
  115. view.removeToolBar(top);
  116. }
  117. else
  118. {
  119. top.validate();
  120. top.repaint();
  121. }
  122. viewAncestorToolBar.remove(view);
  123. } //}}}
  124. //{{{ handleViewUpdate() method
  125. @EditBus.EBHandler
  126. public void handleViewUpdate(ViewUpdate viewUpdate)
  127. {
  128. if (viewUpdate.getWhat() == ViewUpdate.CREATED)
  129. {
  130. View view = viewUpdate.getView();
  131. addAncestorToolBar(view);
  132. }
  133. else if (viewUpdate.getWhat() == ViewUpdate.CLOSED)
  134. {
  135. viewAncestorToolBar.remove(viewUpdate.getView());
  136. }
  137. else if (viewUpdate.getWhat() == ViewUpdate.EDIT_PANE_CHANGED)
  138. {
  139. View view = viewUpdate.getView();
  140. EditPane editPane = view.getEditPane();
  141. AncestorToolBar bar = viewAncestorToolBar.get(view);
  142. bar.setBuffer(editPane.getBuffer());
  143. }
  144. } //}}}
  145. //{{{ handleEditPaneUpdate() method
  146. @EditBus.EBHandler
  147. public void handleEditPaneUpdate(EditPaneUpdate editPaneUpdate)
  148. {
  149. if (editPaneUpdate.getWhat() == EditPaneUpdate.BUFFER_CHANGED)
  150. {
  151. EditPane editPane = editPaneUpdate.getEditPane();
  152. View view = editPane.getView();
  153. AncestorToolBar bar = viewAncestorToolBar.get(view);
  154. if (bar == null)
  155. addAncestorToolBar(view);
  156. bar = viewAncestorToolBar.get(view);
  157. bar.setBuffer(editPane.getBuffer());
  158. }
  159. } //}}}
  160. //{{{ handleMessage() method
  161. @EditBus.EBHandler
  162. public void handleBufferUpdate(BufferUpdate bufferUpdate)
  163. {
  164. // Needed to catch renaming of buffers / saving of new buffers
  165. if (bufferUpdate.getWhat() == BufferUpdate.SAVED)
  166. {
  167. View view = bufferUpdate.getView();
  168. AncestorToolBar bar = viewAncestorToolBar.get(view);
  169. bar.setBuffer(bufferUpdate.getBuffer());
  170. }
  171. } //}}}
  172. //{{{ stop() method
  173. @Override
  174. public void stop()
  175. {
  176. EditBus.removeFromBus(this);
  177. View[] views = jEdit.getViews();
  178. for (int i = 0; i < views.length; i++)
  179. {
  180. removeAncestorToolBar(views[i]);
  181. }
  182. } //}}}
  183. }