📄 swingtools.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* 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 edu.udo.cs.yale.gui;
import edu.udo.cs.yale.tools.Tools;
import edu.udo.cs.yale.tools.ParameterService;
import edu.udo.cs.yale.operator.UserError;
import edu.udo.cs.yale.Yale;
import java.io.File;
import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.filechooser.FileFilter;
public class SwingTools {
/** Defines the extra height for each row in a table. */
public static final int TABLE_ROW_EXTRA_HEIGHT = 8;
// some color constants for Java Look and Feel
public static final Color DARK_YELLOW = new Color(180, 164, 148);
public static final Color LIGHT_YELLOW = new Color(206, 184, 163);
public static final Color DARK_BLUE = new Color(153, 153, 204);
public static final Color LIGHT_BLUE = new Color(204, 204, 255);
public static GradientPaint makeBluePaint(double width, double height) {
return new GradientPaint(0f, 0f, DARK_BLUE,
(float)width/2, (float)height/2, LIGHT_BLUE,
true);
}
public static GradientPaint makeYellowPaint(double width, double height) {
return new GradientPaint(0f, 0f, DARK_YELLOW,
(float)width/2, (float)height/2, LIGHT_YELLOW,
true);
}
public static void showVerySimpleErrorMessage(String message) {
JOptionPane.showMessageDialog(YaleGUI.getMainFrame(),
message,
"Exception",
JOptionPane.ERROR_MESSAGE);
}
public static void showSimpleErrorMessage(String message, Throwable e) {
JOptionPane.showMessageDialog(YaleGUI.getMainFrame(),
message + "\n"+
Tools.classNameWOPackage(e.getClass())+" caught:\n"+
e.getMessage(),
"Exception",
JOptionPane.ERROR_MESSAGE);
}
public static void showErrorMessage(String message, Throwable e) {
JDialog dialog = ErrorDialog.create(message, e);
dialog.setLocationRelativeTo(YaleGUI.getMainFrame());
dialog.show();
}
/** Opens a file chooser with a reasonable start directory. */
public static File chooseFile(Component parent, File file, boolean open) {
return chooseFile(parent, file, open, false);
}
/** Opens a file chooser with a reasonable start directory. */
public static File chooseFile(Component parent, File file, boolean open, boolean onlyDirs) {
return chooseFile(parent, file, open, onlyDirs, null);
}
/** Opens a file chooser with a reasonable start directory. onlyDirs indidcates if only files or only
* can be selected.
* @param file The initially selected value of the file chooser dialog
* @param open Open or save dialog?
* @param onlyDirs Only allow directories to be selected
* @param fileFilters List of FileFilters to use */
public static File chooseFile(Component parent, File file, boolean open, boolean onlyDirs,
FileFilter[] fileFilters) {
if (parent == null) parent = YaleGUI.getMainFrame();
JFileChooser fileChooser = createFileChooser(file, onlyDirs, fileFilters);
int returnValue = open ? fileChooser.showOpenDialog(parent) : fileChooser.showSaveDialog(parent);
switch (returnValue) {
case JFileChooser.APPROVE_OPTION:
return fileChooser.getSelectedFile();
default:
return null;
}
}
/** Creates file chooser with a reasonable start directory.
* You may use the following code snippet in order to retrieve the file:
* <pre>
* if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION)
* File selectedFile = fileChooser.getSelectedFile();
* </pre>
* Usually, the method {@link #chooseFile(Component, File, boolean, boolean, FileFilter[])}
* or one of the convenience wrapper methods can be used to do this. This method is only
* useful if one is interested, e.g., in the selected file filter.
*
* @param file The initially selected value of the file chooser dialog
* @param onlyDirs Only allow directories to be selected
* @param fileFilters List of FileFilters to use */
public static JFileChooser createFileChooser(File file,
boolean onlyDirs,
FileFilter[] fileFilters) {
File directory = null;
if (file != null) {
if (file.isDirectory()) {
directory = file;
} else {
directory = file.getAbsoluteFile().getParentFile();
}
} else {
File experimentFile = (Yale.getExperiment() != null) ?
Yale.getExperiment().getExperimentFile() :
null;
if (experimentFile != null) {
directory = experimentFile.getAbsoluteFile().getParentFile();
} else {
directory = new File(System.getProperty("user.dir"));
}
}
JFileChooser fileChooser = new JFileChooser(directory);
if (onlyDirs)
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fileFilters != null) {
fileChooser.setAcceptAllFileFilterUsed(false);
for (int i = 0; i < fileFilters.length; i++)
fileChooser.addChoosableFileFilter(fileFilters[i]);
}
if (file != null) fileChooser.setSelectedFile(file);
return fileChooser;
}
/** Creates a panel with title and text. */
public static JPanel createTextPanel(String title, String text) {
JPanel panel = new JPanel(new java.awt.BorderLayout());
JLabel label = new JLabel("<html><h2>"+title+"</h2><p>"+text+"</p></html>");
label.setBorder(BorderFactory.createEmptyBorder(11,11,11,11));
label.setFont(label.getFont().deriveFont(java.awt.Font.PLAIN));
panel.add(label, java.awt.BorderLayout.NORTH);
return panel;
}
// ================================================================================
/** Replaces simple html tags and quotes by Yale specific text elements. These can be used in XML files without
* confusing an XML parser. */
public static String html2YaleText(String html) {
if (html == null) return null;
String result = html.replaceAll("<", "#ylt#");
result = result.replaceAll(">", "#ygt#");
result = result.replaceAll("\"", "#yquot#");
return result;
}
/** Replaces the Yale specific tag elements by normal html tags. Additionally the given text is embedded in an
* html and body tag with proper style sheet definitions. */
public static String text2DisplayHtml(String text) {
String result = "<html><head><style type=text/css>body { font-family:sans-serif; font-size:12pt; }</style></head><body>" + text + "</body></html>";
result = text2SimpleHtml(result);
result = result.replaceAll("#yquot#", """);
while (result.indexOf("<icon>") != -1) {
int startIndex = result.indexOf("<icon>");
int endIndex = result.indexOf("</icon>");
String start = result.substring(0, startIndex);
String end = result.substring(endIndex + 7);
String icon = result.substring(startIndex + 6, endIndex).trim();
java.net.URL url = Tools.getResource("icons/"+icon+".gif");
result = start + "<img src=\""+url+"\">" + end;
}
return result;
}
/** Replaces the Yale specific tag elements by normal html tags. This method does not embed the given text in a
* html tag. */
public static String text2SimpleHtml(String text) {
if (text == null) return null;
String result = text.replaceAll("#ygt#", ">");
result = result.replaceAll("#ylt#", "<");
//result = result.replaceAll("#yquot#", """);
//result = result.replaceAll("\"", """);
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -