PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 150 lines | 107 code | 22 blank | 21 comment | 9 complexity | 3bacad7c254e92974e63a6b585e7d394 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. * Copyright (C) 2000, 2001 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit.gui;
  20. import javax.swing.border.EmptyBorder;
  21. import javax.swing.*;
  22. import java.awt.event.*;
  23. import java.awt.*;
  24. import java.io.IOException;
  25. import java.net.URL;
  26. import java.util.Random;
  27. import org.gjt.sp.jedit.*;
  28. import org.gjt.sp.util.Log;
  29. public class TipOfTheDay extends EnhancedDialog
  30. {
  31. public TipOfTheDay(View view)
  32. {
  33. super(view,jEdit.getProperty("tip.title"),false);
  34. setContentPane(new TipPanel());
  35. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  36. pack();
  37. setLocationRelativeTo(view);
  38. show();
  39. }
  40. public void ok()
  41. {
  42. dispose();
  43. }
  44. public void cancel()
  45. {
  46. dispose();
  47. }
  48. class TipPanel extends JPanel
  49. {
  50. TipPanel()
  51. {
  52. super(new BorderLayout(12,12));
  53. setBorder(new EmptyBorder(12,12,12,12));
  54. JLabel label = new JLabel(jEdit.getProperty("tip.caption"));
  55. label.setFont(new Font("SansSerif",Font.PLAIN,24));
  56. label.setForeground(UIManager.getColor("Button.foreground"));
  57. TipPanel.this.add(BorderLayout.NORTH,label);
  58. tipText = new JEditorPane();
  59. tipText.setEditable(false);
  60. tipText.setContentType("text/html");
  61. nextTip();
  62. JScrollPane scroller = new JScrollPane(tipText);
  63. scroller.setPreferredSize(new Dimension(150,150));
  64. TipPanel.this.add(BorderLayout.CENTER,scroller);
  65. ActionHandler actionHandler = new ActionHandler();
  66. Box buttons = new Box(BoxLayout.X_AXIS);
  67. showNextTime = new JCheckBox(jEdit.getProperty("tip.show-next-time"),
  68. jEdit.getBooleanProperty("tip.show"));
  69. showNextTime.addActionListener(actionHandler);
  70. buttons.add(showNextTime);
  71. buttons.add(Box.createHorizontalStrut(6));
  72. buttons.add(Box.createGlue());
  73. nextTip = new JButton(jEdit.getProperty("tip.next-tip"));
  74. nextTip.addActionListener(actionHandler);
  75. buttons.add(nextTip);
  76. buttons.add(Box.createHorizontalStrut(6));
  77. close = new JButton(jEdit.getProperty("common.close"));
  78. close.addActionListener(actionHandler);
  79. buttons.add(close);
  80. TipOfTheDay.this.getRootPane().setDefaultButton(close);
  81. Dimension dim = nextTip.getPreferredSize();
  82. dim.width = Math.max(dim.width,close.getPreferredSize().width);
  83. nextTip.setPreferredSize(dim);
  84. close.setPreferredSize(dim);
  85. TipPanel.this.add(BorderLayout.SOUTH,buttons);
  86. }
  87. // private members
  88. private JCheckBox showNextTime;
  89. private JButton nextTip, close;
  90. private JEditorPane tipText;
  91. private int currentTip = -1;
  92. private void nextTip()
  93. {
  94. int count = jEdit.getIntegerProperty("tip.count",0);
  95. // so that we don't see the same tip again if the user
  96. // clicks 'Next Tip'
  97. int tipToShow = currentTip;
  98. while(tipToShow == currentTip)
  99. tipToShow = Math.abs(new Random().nextInt()) % count;
  100. try
  101. {
  102. tipText.setPage(TipOfTheDay.class.getResource(
  103. "/org/gjt/sp/jedit/tips/tip"
  104. + tipToShow + ".html"));
  105. }
  106. catch(Exception e)
  107. {
  108. Log.log(Log.ERROR,this,e);
  109. }
  110. }
  111. class ActionHandler implements ActionListener
  112. {
  113. public void actionPerformed(ActionEvent evt)
  114. {
  115. Object source = evt.getSource();
  116. if(source == showNextTime)
  117. {
  118. jEdit.setBooleanProperty("tip.show",showNextTime
  119. .isSelected());
  120. }
  121. else if(source == nextTip)
  122. nextTip();
  123. else if(source == close)
  124. dispose();
  125. }
  126. }
  127. }
  128. }