📄 textviewerdialog.java
字号:
/*************************************************************************** * Copyright 2006-2008 by Christian Ihle * * kontakt@usikkert.net * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/package net.usikkert.kouchat.ui.swing;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Point;import java.awt.Rectangle;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.BorderFactory;import javax.swing.JDialog;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextPane;import javax.swing.WindowConstants;import javax.swing.text.AbstractDocument;import javax.swing.text.BadLocationException;import javax.swing.text.MutableAttributeSet;import javax.swing.text.SimpleAttributeSet;import javax.swing.text.StyledDocument;import net.usikkert.kouchat.Constants;import net.usikkert.kouchat.misc.ErrorHandler;/** * Opens a text file in a 80x24 character dialog window. * * @author Christian Ihle */public class TextViewerDialog extends JDialog{ private static final Logger LOG = Logger.getLogger( TextViewerDialog.class.getName() ); private static final long serialVersionUID = 1L; private final ErrorHandler errorHandler; private final JTextPane viewerTP; private final JScrollPane viewerScroll; private final MutableAttributeSet viewerAttr; private final StyledDocument viewerDoc; private final String textFile; private boolean fileOpened; /** * Constructor. * * Creates the dialog window, and opens the file. * * @param textFile The text file to open and view. * @param title The title to use for the dialog window. * @param links True to enabled support for opening urls by clicking on them. */ public TextViewerDialog( final String textFile, final String title, final boolean links ) { this.textFile = textFile; errorHandler = ErrorHandler.getErrorHandler(); viewerTP = new JTextPane(); viewerTP.setFont( new Font( "Monospaced", Font.PLAIN, viewerTP.getFont().getSize() ) ); viewerTP.setEditable( false ); viewerDoc = viewerTP.getStyledDocument(); // Enables the url support if ( links ) { URLMouseListener urlML = new URLMouseListener( viewerTP ); viewerTP.addMouseListener( urlML ); viewerTP.addMouseMotionListener( urlML ); AbstractDocument doc = (AbstractDocument) viewerDoc; doc.setDocumentFilter( new URLDocumentFilter() ); } new ChatPopup( viewerTP ); viewerAttr = new SimpleAttributeSet(); viewerScroll = new JScrollPane( viewerTP ); viewerScroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ); JPanel panel = new JPanel( new BorderLayout() ); panel.setBorder( BorderFactory.createEmptyBorder( 5, 4, 4, 4 ) ); panel.add( viewerScroll, BorderLayout.CENTER ); add( panel, BorderLayout.CENTER ); // To get 80 columns and 24 rows FontMetrics fm = viewerTP.getFontMetrics( viewerTP.getFont() ); int width = fm.charWidth( '_' ) * 80; int height = fm.getHeight() * 24; viewerTP.setPreferredSize( new Dimension( width, height ) ); setDefaultCloseOperation( WindowConstants.HIDE_ON_CLOSE ); setTitle( Constants.APP_NAME + " - " + title ); setResizable( false ); readFile(); pack(); } /** * Shows the window if the text file was opened, and scrolls to the * beginning of the text. */ @Override public void setVisible( final boolean visible ) { if ( fileOpened ) { if ( visible ) { try { Rectangle r = viewerTP.modelToView( 0 ); viewerScroll.getViewport().setViewPosition( new Point( r.x, r.y ) ); } catch ( final BadLocationException e ) { LOG.log( Level.SEVERE, e.toString() ); } } super.setVisible( visible ); } else { errorHandler.showError( "The file " + textFile + " could not be opened." ); } } /** * Reads the text file, and adds the contents to the text area. * * @return True if the text file was read without any problems. */ private void readFile() { URL fileURL = getClass().getResource( "/" + textFile ); if ( fileURL != null ) { BufferedReader reader = null; try { reader = new BufferedReader( new InputStreamReader( fileURL.openStream() ) ); while ( reader.ready() ) { viewerDoc.insertString( viewerDoc.getLength(), reader.readLine() + "\n", viewerAttr ); } viewerTP.setCaretPosition( 0 ); fileOpened = true; } catch ( final IOException e ) { LOG.log( Level.SEVERE, e.toString() ); } catch ( final BadLocationException e ) { LOG.log( Level.SEVERE, e.toString() ); } finally { if ( reader != null ) { try { reader.close(); } catch ( final IOException e ) { LOG.log( Level.WARNING, "Problems closing: " + textFile ); } } } } else { LOG.log( Level.SEVERE, "Text file not found: " + textFile ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -