PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/gui/TipOfTheDay.java

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