PageRenderTime 164ms CodeModel.GetById 138ms app.highlight 22ms RepoModel.GetById 1ms 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
  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
 20package org.gjt.sp.jedit.gui;
 21
 22import javax.swing.border.EmptyBorder;
 23import javax.swing.*;
 24import java.awt.event.*;
 25import java.awt.*;
 26import java.io.IOException;
 27import java.net.URL;
 28import java.util.Random;
 29import org.gjt.sp.jedit.*;
 30import org.gjt.sp.util.Log;
 31
 32public class TipOfTheDay extends EnhancedDialog
 33{
 34	public TipOfTheDay(View view)
 35	{
 36		super(view,jEdit.getProperty("tip.title"),false);
 37		setContentPane(new TipPanel());
 38		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 39		pack();
 40		setLocationRelativeTo(view);
 41		show();
 42	}
 43
 44	public void ok()
 45	{
 46		dispose();
 47	}
 48
 49	public void cancel()
 50	{
 51		dispose();
 52	}
 53
 54	class TipPanel extends JPanel
 55	{
 56		TipPanel()
 57		{
 58			super(new BorderLayout(12,12));
 59			setBorder(new EmptyBorder(12,12,12,12));
 60
 61			JLabel label = new JLabel(jEdit.getProperty("tip.caption"));
 62			label.setFont(new Font("SansSerif",Font.PLAIN,24));
 63			label.setForeground(UIManager.getColor("Button.foreground"));
 64			TipPanel.this.add(BorderLayout.NORTH,label);
 65
 66			tipText = new JEditorPane();
 67			tipText.setEditable(false);
 68			tipText.setContentType("text/html");
 69
 70			nextTip();
 71
 72			JScrollPane scroller = new JScrollPane(tipText);
 73			scroller.setPreferredSize(new Dimension(150,150));
 74			TipPanel.this.add(BorderLayout.CENTER,scroller);
 75
 76			ActionHandler actionHandler = new ActionHandler();
 77
 78			Box buttons = new Box(BoxLayout.X_AXIS);
 79
 80			showNextTime = new JCheckBox(jEdit.getProperty("tip.show-next-time"),
 81				jEdit.getBooleanProperty("tip.show"));
 82			showNextTime.addActionListener(actionHandler);
 83			buttons.add(showNextTime);
 84
 85			buttons.add(Box.createHorizontalStrut(6));
 86			buttons.add(Box.createGlue());
 87
 88			nextTip = new JButton(jEdit.getProperty("tip.next-tip"));
 89			nextTip.addActionListener(actionHandler);
 90			buttons.add(nextTip);
 91
 92			buttons.add(Box.createHorizontalStrut(6));
 93
 94			close = new JButton(jEdit.getProperty("common.close"));
 95			close.addActionListener(actionHandler);
 96			buttons.add(close);
 97			TipOfTheDay.this.getRootPane().setDefaultButton(close);
 98
 99			Dimension dim = nextTip.getPreferredSize();
100			dim.width = Math.max(dim.width,close.getPreferredSize().width);
101			nextTip.setPreferredSize(dim);
102			close.setPreferredSize(dim);
103
104			TipPanel.this.add(BorderLayout.SOUTH,buttons);
105		}
106
107		// private members
108		private JCheckBox showNextTime;
109		private JButton nextTip, close;
110		private JEditorPane tipText;
111		private int currentTip = -1;
112
113		private void nextTip()
114		{
115			int count = jEdit.getIntegerProperty("tip.count",0);
116			// so that we don't see the same tip again if the user
117			// clicks 'Next Tip'
118			int tipToShow = currentTip;
119			while(tipToShow == currentTip)
120				tipToShow = Math.abs(new Random().nextInt()) % count;
121			try
122			{
123				tipText.setPage(TipOfTheDay.class.getResource(
124					"/org/gjt/sp/jedit/tips/tip"
125					+ tipToShow + ".html"));
126			}
127			catch(Exception e)
128			{
129				Log.log(Log.ERROR,this,e);
130			}
131		}
132
133		class ActionHandler implements ActionListener
134		{
135			public void actionPerformed(ActionEvent evt)
136			{
137				Object source = evt.getSource();
138				if(source == showNextTime)
139				{
140					jEdit.setBooleanProperty("tip.show",showNextTime
141						.isSelected());
142				}
143				else if(source == nextTip)
144					nextTip();
145				else if(source == close)
146					dispose();
147			}
148		}
149	}
150}