/jEdit/branches/plugin_packages/org/gjt/sp/jedit/gui/statusbar/WrapWidgetFactory.java

# · Java · 114 lines · 73 code · 8 blank · 33 comment · 4 complexity · 511fd98f65eaf547a3f3b68b42d36c8b MD5 · raw file

  1. /*
  2. * WrapWidgetFactory.java - The wrap widget service
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2008 Matthieu Casanova
  7. * Portions Copyright (C) 2001, 2004 Slava Pestov
  8. * Portions copyright (C) 2001 Mike Dillon
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. package org.gjt.sp.jedit.gui.statusbar;
  25. //{{{ Imports
  26. import java.awt.Dimension;
  27. import java.awt.Font;
  28. import java.awt.FontMetrics;
  29. import java.awt.event.MouseAdapter;
  30. import java.awt.event.MouseEvent;
  31. import javax.swing.JComponent;
  32. import javax.swing.JLabel;
  33. import javax.swing.SwingConstants;
  34. import org.gjt.sp.jedit.Buffer;
  35. import org.gjt.sp.jedit.View;
  36. import org.gjt.sp.jedit.jEdit;
  37. //}}}
  38. /**
  39. * @author Matthieu Casanova
  40. * @since jEdit 4.3pre14
  41. */
  42. public class WrapWidgetFactory implements StatusWidgetFactory
  43. {
  44. //{{{ getWidget() method
  45. public Widget getWidget(View view)
  46. {
  47. Widget wrap = new WrapWidget(view);
  48. return wrap;
  49. } //}}}
  50. //{{{ WrapWidget class
  51. private static class WrapWidget implements Widget
  52. {
  53. private final JLabel wrap;
  54. private final View view;
  55. public WrapWidget(final View view)
  56. {
  57. wrap = new ToolTipLabel();
  58. wrap.setHorizontalAlignment(SwingConstants.CENTER);
  59. wrap.setToolTipText(jEdit.getProperty("view.status.wrap-tooltip"));
  60. this.view = view;
  61. wrap.addMouseListener(new MouseAdapter()
  62. {
  63. @Override
  64. public void mouseClicked(MouseEvent evt)
  65. {
  66. view.getBuffer().toggleWordWrap(view);
  67. }
  68. });
  69. }
  70. public JComponent getComponent()
  71. {
  72. return wrap;
  73. }
  74. public void update()
  75. {
  76. Buffer buffer = view.getBuffer();
  77. String wrap = buffer.getStringProperty("wrap");
  78. if(wrap.equals("none"))
  79. {
  80. this.wrap.setEnabled(false);
  81. this.wrap.setText("N");
  82. }
  83. else
  84. {
  85. this.wrap.setEnabled(true);
  86. if (wrap.equals("hard"))
  87. this.wrap.setText("H");
  88. else if (wrap.equals("soft"))
  89. this.wrap.setText("S");
  90. }
  91. }
  92. public void propertiesChanged()
  93. {
  94. // retarded GTK look and feel!
  95. Font font = new JLabel().getFont();
  96. //UIManager.getFont("Label.font");
  97. FontMetrics fm = wrap.getFontMetrics(font);
  98. Dimension dim = new Dimension(Math.max(Math.max(fm.charWidth('N'),
  99. fm.charWidth('H')),
  100. fm.charWidth('S')) + 1,
  101. fm.getHeight());
  102. wrap.setPreferredSize(dim);
  103. wrap.setMaximumSize(dim);
  104. }
  105. } //}}}
  106. }