📄 backgrounddialog.java
字号:
/*
* 11/14/2003
*
* BackgroundDialog.java - Dialog allowing you to change the background
* of RTextAreas.
* Copyright (C) 2003 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.rtextarea;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Image;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.URL;
import javax.imageio.ImageIO;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import org.fife.RUtilities;
import org.fife.ui.ImagePreviewPane;
import org.fife.ui.RButton;
import org.fife.ui.RFileChooser;
import org.fife.ui.UIUtilities;
import org.fife.ui.rtextfilechooser.filters.ImageFileFilter;
/**
* Dialog allowing the user to select the background to use for an
* <code>RTextArea</code>.
*
* @author Robert Futrell
* @version 1.0
*/
class BackgroundDialog extends JDialog implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1384333147126605631L;
private JRadioButton colorButton;
private JRadioButton imageButton;
private JButton colorBrowseButton;
private JButton imageBrowseButton;
private JButton okButton;
private JButton cancelButton;
private Color currentColor;
private String currentImageFileName;
private JTextField imageFileNameField;
private boolean viewingColors;
private Image previewImage;
private ResourceBundle msg;
private JFileChooser imageChooser;
/*****************************************************************************/
/**
* Creates and initializes a <code>BackgroundDialog</code>.
*
* @param owner The owner of this dialog.
*/
public BackgroundDialog(Dialog owner) {
// Call parent's constructor and set the dialog's title.
super(owner);
msg = ResourceBundle.getBundle(
"org.fife.ui.rtextarea.BackgroundDialog");
setTitle(msg.getString("BackgroundDialogTitle"));
Border empty5Border = UIUtilities.getEmpty5Border();
// Initialize the image chooser.
imageChooser = new RFileChooser();
ImagePreviewPane imagePreviewPane = new ImagePreviewPane();
imageChooser.setAccessory(imagePreviewPane);
imageChooser.addPropertyChangeListener(imagePreviewPane);
imageChooser.setFileFilter(new ImageFileFilter());
// Create a panel showing a "background preview."
JPanel previewPanel = new JPanel();
previewPanel.setLayout(new BoxLayout(previewPanel, BoxLayout.Y_AXIS));
previewPanel.setBorder(BorderFactory.createTitledBorder(
msg.getString("PreviewLabel")));
previewPanel.add(Box.createRigidArea(new Dimension(90,90)));
// Create a panel allowing you to go to the color picker or image
// selector to change the background.
JPanel changePanel = new JPanel();
GridBagLayout gridbag = new GridBagLayout();
changePanel.setLayout(gridbag);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
changePanel.setBorder(empty5Border);
colorButton = new JRadioButton(msg.getString("Color"), true);
colorButton.setActionCommand("PressedColorRadioButton");
colorButton.addActionListener(this);
c.gridx = 0; c.gridy = 0;
gridbag.setConstraints(colorButton, c);
changePanel.add(colorButton);
colorBrowseButton = new RButton(msg.getString("BrowseColors"));
colorBrowseButton.setActionCommand("BrowseColors");
colorBrowseButton.addActionListener(this);
colorBrowseButton.setEnabled(true);
c.gridx = 1; c.gridy = 0;
gridbag.setConstraints(colorBrowseButton, c);
changePanel.add(colorBrowseButton);
Component rigidArea = Box.createRigidArea(new Dimension(5,5));
c.gridx = 0; c.gridy = 1;
c.gridwidth = 2;
gridbag.setConstraints(rigidArea, c);
changePanel.add(rigidArea);
c.gridwidth = 1;
imageButton = new JRadioButton(msg.getString("Image"), false);
imageButton.setActionCommand("PressedImageRadioButton");
imageButton.addActionListener(this);
c.gridx = 0; c.gridy = 2;
gridbag.setConstraints(imageButton, c);
changePanel.add(imageButton);
imageBrowseButton = new RButton(msg.getString("BrowseImages"));
imageBrowseButton.setActionCommand("BrowseImages");
imageBrowseButton.addActionListener(this);
imageBrowseButton.setEnabled(false);
c.gridx = 1; c.gridy = 2;
gridbag.setConstraints(imageBrowseButton, c);
changePanel.add(imageBrowseButton);
Component rigidArea2 = Box.createRigidArea(new Dimension(5,10));
c.gridx = 0; c.gridy = 3;
c.gridwidth = 2;
gridbag.setConstraints(rigidArea2, c);
changePanel.add(rigidArea2);
c.gridwidth = 1;
imageFileNameField = new JTextField("", 10);
c.gridx = 0; c.gridy = 4;
c.gridwidth = 2;
gridbag.setConstraints(imageFileNameField, c);
imageFileNameField.setEnabled(false);
changePanel.add(imageFileNameField);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(colorButton);
buttonGroup.add(imageButton);
// Create a panel combining the first two.
JPanel topPanel = new JPanel();
topPanel.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
topPanel.add(previewPanel);
topPanel.add(changePanel);
// Create a panel for the OK and Cancel buttons.
okButton = RUtilities.createRButton(msg, "OK", "OKMnemonic");
okButton.setActionCommand("BackgroundOK");
okButton.addActionListener(this);
cancelButton = RUtilities.createRButton(msg, "Cancel", "CancelMnemonic");
cancelButton.setActionCommand("BackgroundCancel");
cancelButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
JPanel temp = new JPanel(new GridLayout(1,2, 5,0));
temp.add(okButton);
temp.add(cancelButton);
buttonPanel.add(temp);
// Arrange the dialog!
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
contentPanel.setBorder(empty5Border);
contentPanel.add(topPanel);
contentPanel.add(new JSeparator());
contentPanel.add(buttonPanel);
setContentPane(contentPanel);
// Get everything ready to go.
getRootPane().setDefaultButton(okButton);
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
setResizable(false);
setModal(true); // So the user can't go back to the editor.
setLocationRelativeTo(owner);
pack();
}
/*****************************************************************************/
// Callback for when the user clicks a button, etc.
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("PressedColorRadioButton")) {
colorBrowseButton.setEnabled(true);
imageBrowseButton.setEnabled(false);
viewingColors = true;
okButton.setEnabled(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -