📄 textfilepropertiesdialog.java~1~
字号:
/*
* 12/08/2004
*
* TextFilePropertiesDialog.java - Dialog allowing you to view/edit a
* text file's properties.
* Copyright (C) 2004 Robert Futrell
* email@address.com
* www.website.com
*
* 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 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 org.fife.ui;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import org.fife.RUtilities;
import org.fife.ui.RButton;
import org.fife.ui.rtextarea.RTextAreaEditorKit;
import org.fife.rtext.RTextEditorPane;
/**
* A dialog that displays the properties of an individual text file being
* edited by an <code>RTextEditorPane</code>. Some properties can
* be modified directly from this dialog.
*
* @author Robert Futrell
* @version 0.1
*/
public class TextFilePropertiesDialog extends JDialog
implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 3143244782095735798L;
public static final String LINE_TERMINATOR_CR = "\r";
public static final String LINE_TERMINATOR_LF = "\n";
public static final String LINE_TERMINATOR_CRLF = "\r\n";
public static final String LINE_TERMINATOR_SYSTEM = System.getProperty("line.separator");
private SpecialValueComboBox terminatorCombo;
private JComboBox encodingCombo;
private RButton okButton;
private RTextEditorPane textArea;
/*****************************************************************************/
/**
* Constructor.
*
* @param parent The main application window.
* @param textArea The text area on which to report.
*/
public TextFilePropertiesDialog(Frame parent,
RTextEditorPane textArea) {
super(parent);
this.textArea = textArea;
ResourceBundle msg = ResourceBundle.getBundle(
"org.fife.ui.TextFilePropertiesDialog");
setTitle(msg.getString("Title") + textArea.getFileName());
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.setBorder(UIUtilities.getEmpty5Border());
// Where we actually add our content.
JPanel content2 = new JPanel();
content2.setLayout(new SpringLayout());
contentPane.add(content2, BorderLayout.NORTH);
JLabel filePathLabel = new JLabel(msg.getString("Path"));
JTextField filePathField = new JTextField(30);
filePathField.setText(textArea.getFileFullPath());
filePathField.setEditable(false);
filePathLabel.setLabelFor(filePathField);
content2.add(filePathLabel);
content2.add(filePathField);
JLabel linesLabel = new JLabel(msg.getString("Lines"));
JLabel linesCountLabel = new JLabel("" + textArea.getLineCount());
content2.add(linesLabel);
content2.add(linesCountLabel);
JLabel charsLabel = new JLabel(msg.getString("Characters"));
JLabel charsCountLabel = new JLabel("" +
textArea.getDocument().getLength());
content2.add(charsLabel);
content2.add(charsCountLabel);
JLabel terminatorLabel = RUtilities.createLabel(msg,
"LineTerminator", "LineTerminatorMnemonic");
terminatorCombo = new SpecialValueComboBox();
terminatorCombo.addSpecialItem(msg.getString("SysDef"),
LINE_TERMINATOR_SYSTEM);
terminatorCombo.addSpecialItem(msg.getString("CR"),
LINE_TERMINATOR_CR);
terminatorCombo.addSpecialItem(msg.getString("LF"),
LINE_TERMINATOR_LF);
terminatorCombo.addSpecialItem(msg.getString("CRLF"),
LINE_TERMINATOR_CRLF);
terminatorCombo.setSelectedSpecialItem((String)textArea.getDocument().
getProperty(RTextAreaEditorKit.
EndOfLineStringProperty));
terminatorCombo.setActionCommand("TerminatorComboBox");
terminatorCombo.addActionListener(this);
terminatorLabel.setLabelFor(terminatorCombo);
content2.add(terminatorLabel);
content2.add(terminatorCombo);
JLabel encodingLabel = RUtilities.createLabel(msg,
"Encoding", "EncodingMnemonic");
encodingCombo = new JComboBox();
// Populate the combo box with all available encodings.
Map availcs = Charset.availableCharsets();
Set keys = availcs.keySet();
for (Iterator i=keys.iterator(); i.hasNext(); )
encodingCombo.addItem((String)i.next());
setEncoding(textArea.getEncoding());
encodingCombo.setActionCommand("encodingCombo");
encodingCombo.addActionListener(this);
encodingLabel.setLabelFor(encodingCombo);
content2.add(encodingLabel);
content2.add(encodingCombo);
JLabel sizeLabel = new JLabel(msg.getString("FileSize"));
File file = new File(textArea.getFileFullPath());
String size = "";
if (file.exists() && !file.isDirectory()) {
size = "" + file.length();
}
JLabel sizeLabel2 = new JLabel(size);
content2.add(sizeLabel);
content2.add(sizeLabel2);
long temp = textArea.getLastModified();
String modifiedString;
if (temp<=0) { // 0 or -1; too lazy to check which one...
modifiedString = "";
}
else {
Date modifiedDate = new Date(temp);
SimpleDateFormat rob = new SimpleDateFormat("hh:mm a EEE, MMM d, yyyy");
modifiedString = rob.format(modifiedDate);
}
JLabel modifiedLabel = new JLabel(msg.getString("LastModified"));
JLabel modified = new JLabel(modifiedString);
content2.add(modifiedLabel);
content2.add(modified);
RUtilities.makeSpringCompactGrid(content2,
7,2, // rows,cols,
0,0, // initial-x, initial-y,
5,5); // x-spacing, y-spacing.
// Make a panel for OK and cancel buttons.
JPanel bottomPanel = new JPanel();
JPanel buttonPanel = new JPanel(new GridLayout(1,2, 5,0));
okButton = RUtilities.createRButton(msg, "OK", "OKMnemonic");
okButton.setActionCommand("OKButton");
okButton.addActionListener(this);
okButton.setEnabled(false);
buttonPanel.add(okButton);
RButton cancelButton = RUtilities.createRButton(msg,
"Cancel", "CancelMnemonic");
cancelButton.setActionCommand("CancelButton");
cancelButton.addActionListener(this);
buttonPanel.add(cancelButton);
bottomPanel.add(buttonPanel);
contentPane.add(bottomPanel, BorderLayout.SOUTH);
setContentPane(contentPane);
setModal(true);
pack();
setLocationRelativeTo(parent);
}
/*****************************************************************************/
/**
* Listens for actions in this dialog.
*
* @param e The action event.
*/
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("TerminatorComboBox")) {
okButton.setEnabled(true);
}
else if (actionCommand.equals("encodingCombo")) {
okButton.setEnabled(true);
}
else if (actionCommand.equals("OKButton")) {
String terminator = terminatorCombo.getSelectedSpecialItem();
if (terminator!=null) {
textArea.getDocument().putProperty(
RTextAreaEditorKit.EndOfLineStringProperty,
terminator);
}
String encoding = (String)encodingCombo.getSelectedItem();
if (encoding!=null) {
textArea.setEncoding(encoding);
}
setVisible(false);
}
else if (actionCommand.equals("CancelButton")) {
setVisible(false);
}
}
/*****************************************************************************/
/**
* Sets the encoding selected by this dialog.
*
* @param encoding The desired encoding. If this value is invalid or not
* supported by this OS, <code>US-ASCII</code> is used.
* @see #getEncoding
*/
private void setEncoding(String encoding) {
Charset cs1 = Charset.forName(encoding);
int count = encodingCombo.getItemCount();
for (int i=0; i<count; i++) {
String item = (String)encodingCombo.getItemAt(i);
Charset cs2 = Charset.forName(item);
if (cs1.equals(cs2)) {
encodingCombo.setSelectedIndex(i);
return;
}
}
// Encoding not found: select default.
cs1 = Charset.forName("US-ASCII");
for (int i=0; i<count; i++) {
String item = (String)encodingCombo.getItemAt(i);
Charset cs2 = Charset.forName(item);
if (cs1.equals(cs2)) {
encodingCombo.setSelectedIndex(i);
return;
}
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -