PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/gui/TipOfTheDay.java

#
Java | 166 lines | 105 code | 27 blank | 34 comment | 14 complexity | ae7e5f1a6192113a15285d49d15f9394 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. * TipOfTheDay.java - Tip of the day window
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2001, 2002 Slava Pestov
  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;
  23. //{{{ Imports
  24. import javax.swing.border.EmptyBorder;
  25. import javax.swing.*;
  26. import java.awt.event.*;
  27. import java.awt.*;
  28. import java.io.*;
  29. import java.util.Random;
  30. import org.gjt.sp.jedit.*;
  31. import org.gjt.sp.util.Log;
  32. //}}}
  33. public class TipOfTheDay extends EnhancedDialog
  34. {
  35. //{{{ TipOfTheDay constructor
  36. public TipOfTheDay(View view)
  37. {
  38. super(view,jEdit.getProperty("tip.title"),false);
  39. JPanel content = new JPanel(new BorderLayout(12,12));
  40. content.setBorder(new EmptyBorder(12,12,12,12));
  41. setContentPane(content);
  42. JLabel label = new JLabel(jEdit.getProperty("tip.caption"));
  43. label.setFont(new Font("SansSerif",Font.PLAIN,24));
  44. label.setForeground(UIManager.getColor("Button.foreground"));
  45. content.add(BorderLayout.NORTH,label);
  46. tipText = new JEditorPane();
  47. tipText.setEditable(false);
  48. tipText.setContentType("text/html");
  49. nextTip();
  50. JScrollPane scroller = new JScrollPane(tipText);
  51. scroller.setPreferredSize(new Dimension(150,150));
  52. content.add(BorderLayout.CENTER,scroller);
  53. ActionHandler actionHandler = new ActionHandler();
  54. Box buttons = new Box(BoxLayout.X_AXIS);
  55. showNextTime = new JCheckBox(jEdit.getProperty("tip.show-next-time"),
  56. jEdit.getBooleanProperty("tip.show"));
  57. showNextTime.addActionListener(actionHandler);
  58. buttons.add(showNextTime);
  59. buttons.add(Box.createHorizontalStrut(6));
  60. buttons.add(Box.createGlue());
  61. nextTip = new JButton(jEdit.getProperty("tip.next-tip"));
  62. nextTip.addActionListener(actionHandler);
  63. buttons.add(nextTip);
  64. buttons.add(Box.createHorizontalStrut(6));
  65. close = new JButton(jEdit.getProperty("common.close"));
  66. close.addActionListener(actionHandler);
  67. buttons.add(close);
  68. content.getRootPane().setDefaultButton(close);
  69. Dimension dim = nextTip.getPreferredSize();
  70. dim.width = Math.max(dim.width,close.getPreferredSize().width);
  71. nextTip.setPreferredSize(dim);
  72. close.setPreferredSize(dim);
  73. content.add(BorderLayout.SOUTH,buttons);
  74. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  75. pack();
  76. setLocationRelativeTo(view);
  77. show();
  78. } //}}}
  79. //{{{ ok() method
  80. public void ok()
  81. {
  82. dispose();
  83. } //}}}
  84. //{{{ cancel() method
  85. public void cancel()
  86. {
  87. dispose();
  88. } //}}}
  89. //{{{ Private members
  90. //{{{ Instance variables
  91. private JCheckBox showNextTime;
  92. private JButton nextTip, close;
  93. private JEditorPane tipText;
  94. private int currentTip = -1;
  95. //}}}
  96. //{{{ nextTip() method
  97. private void nextTip()
  98. {
  99. File[] tips = new File(MiscUtilities.constructPath(
  100. jEdit.getJEditHome(),"doc","tips")).listFiles();
  101. if(tips == null || tips.length == 0)
  102. {
  103. tipText.setText(jEdit.getProperty("tip.not-found"));
  104. return;
  105. }
  106. int count = tips.length;
  107. // so that we don't see the same tip again if the user
  108. // clicks 'Next Tip'
  109. int tipToShow = currentTip;
  110. while(tipToShow == currentTip || !tips[tipToShow].getName().endsWith(".html"))
  111. tipToShow = Math.abs(new Random().nextInt()) % count;
  112. try
  113. {
  114. tipText.setPage(tips[tipToShow].toURL());
  115. }
  116. catch(Exception e)
  117. {
  118. Log.log(Log.ERROR,this,e);
  119. }
  120. } //}}}
  121. //}}}
  122. //{{{ ActionHandler class
  123. class ActionHandler implements ActionListener
  124. {
  125. public void actionPerformed(ActionEvent evt)
  126. {
  127. Object source = evt.getSource();
  128. if(source == showNextTime)
  129. {
  130. jEdit.setBooleanProperty("tip.show",showNextTime
  131. .isSelected());
  132. }
  133. else if(source == nextTip)
  134. nextTip();
  135. else if(source == close)
  136. dispose();
  137. }
  138. } //}}}
  139. }