PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 154 lines | 104 code | 13 blank | 37 comment | 8 complexity | e03a2d650d5a3d5a19fb217b37ae5c14 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. * BufferSetWidgetFactory.java - The bufferSet widget service
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2008, 2009 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. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.gui.statusbar;
  23. //{{{ Imports
  24. import java.awt.event.MouseAdapter;
  25. import java.awt.event.MouseEvent;
  26. import java.awt.*;
  27. import javax.swing.JComponent;
  28. import javax.swing.JLabel;
  29. import org.gjt.sp.jedit.*;
  30. import org.gjt.sp.jedit.bufferset.BufferSetManager;
  31. import org.gjt.sp.jedit.msg.EditPaneUpdate;
  32. import org.gjt.sp.jedit.bufferset.BufferSet;
  33. import org.gjt.sp.jedit.msg.PropertiesChanged;
  34. //}}}
  35. /**
  36. * A Statusbar widget that show the bufferSet's scope of the current edit pane.
  37. *
  38. * @author Matthieu Casanova
  39. * @since jEdit 4.3pre15
  40. */
  41. public class BufferSetWidgetFactory implements StatusWidgetFactory
  42. {
  43. //{{{ getWidget() method
  44. public Widget getWidget(View view)
  45. {
  46. Widget bufferSetWidget = new BufferSetWidget();
  47. return bufferSetWidget;
  48. } //}}}
  49. //{{{ BufferSetWidget class
  50. private static class BufferSetWidget implements Widget, EBComponent
  51. {
  52. private final JLabel bufferSetLabel;
  53. private BufferSet.Scope currentScope;
  54. BufferSetWidget()
  55. {
  56. bufferSetLabel = new ToolTipLabel()
  57. {
  58. @Override
  59. public void addNotify()
  60. {
  61. super.addNotify();
  62. BufferSetWidget.this.update();
  63. EditBus.addToBus(BufferSetWidget.this);
  64. }
  65. @Override
  66. public void removeNotify()
  67. {
  68. super.removeNotify();
  69. EditBus.removeFromBus(BufferSetWidget.this);
  70. }
  71. };
  72. update();
  73. bufferSetLabel.addMouseListener(new MouseAdapter()
  74. {
  75. @Override
  76. public void mouseClicked(MouseEvent evt)
  77. {
  78. if (evt.getClickCount() == 2)
  79. {
  80. BufferSetManager bufferSetManager = jEdit.getBufferSetManager();
  81. BufferSet.Scope scope = bufferSetManager.getScope();
  82. switch (scope)
  83. {
  84. case global:
  85. scope = BufferSet.Scope.view;
  86. break;
  87. case view:
  88. scope = BufferSet.Scope.editpane;
  89. break;
  90. case editpane:
  91. scope = BufferSet.Scope.global;
  92. break;
  93. }
  94. bufferSetManager.setScope(scope);
  95. }
  96. }
  97. });
  98. }
  99. //{{{ getComponent() method
  100. public JComponent getComponent()
  101. {
  102. return bufferSetLabel;
  103. } //}}}
  104. //{{{ update() method
  105. public void update()
  106. {
  107. BufferSet.Scope scope = jEdit.getBufferSetManager().getScope();
  108. if (currentScope == null || !currentScope.equals(scope))
  109. {
  110. bufferSetLabel.setText(scope.toString().substring(0,1).toUpperCase());
  111. bufferSetLabel.setToolTipText(jEdit.getProperty("view.status.bufferset-tooltip", new Object[] {scope}));
  112. currentScope = scope;
  113. }
  114. } //}}}
  115. //{{{ propertiesChanged() method
  116. public void propertiesChanged()
  117. {
  118. // retarded GTK look and feel!
  119. Font font = new JLabel().getFont();
  120. //UIManager.getFont("Label.font");
  121. FontMetrics fm = bufferSetLabel.getFontMetrics(font);
  122. Dimension dim = new Dimension(Math.max(fm.charWidth('E'),Math.max(fm.charWidth('V'),
  123. fm.charWidth('G'))),
  124. fm.getHeight());
  125. bufferSetLabel.setPreferredSize(dim);
  126. bufferSetLabel.setMaximumSize(dim);
  127. } //}}}
  128. //{{{ handleMessage() method
  129. public void handleMessage(EBMessage message)
  130. {
  131. if (message instanceof PropertiesChanged)
  132. {
  133. PropertiesChanged propertiesChanged = (PropertiesChanged) message;
  134. if (propertiesChanged.getSource() instanceof BufferSetManager)
  135. {
  136. update();
  137. }
  138. }
  139. } //}}}
  140. } //}}}
  141. }