PageRenderTime 127ms CodeModel.GetById 119ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/TipOfTheDay.java

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