printablecomponent.java
来自「Weka」· Java 代码 · 共 632 行 · 第 1/2 页
JAVA
632 行
/* * 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* * PrintableComponent.java * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand * */package weka.gui.visualize;import weka.gui.ExtensionFileFilter;import weka.gui.GenericObjectEditor;import java.awt.Dimension;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.util.Collections;import java.util.Enumeration;import java.util.Hashtable;import java.util.Properties;import java.util.Vector;import javax.swing.JCheckBox;import javax.swing.JComponent;import javax.swing.JFileChooser;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;/** * This class extends the component which is handed over in the constructor * by a print dialog. * The Print dialog is accessible via Alt+Shift+LeftMouseClick. <p> * The individual JComponentWriter-descendants can be accessed by the * <code>getWriter(String)</code> method, if the parameters need to be changed. * * @see #getWriters() * @see #getWriter(String) * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.8 $ */public class PrintableComponent implements PrintableHandler { /** the parent component of this print dialog. */ protected JComponent m_Component; /** the filechooser for saving the panel. */ protected static JFileChooser m_FileChooserPanel; /** the checkbox for the custom dimensions. */ protected static JCheckBox m_CustomDimensionsCheckBox; /** the edit field for the custom width. */ protected static JTextField m_CustomWidthText; /** the edit field for the custom height. */ protected static JTextField m_CustomHeightText; /** the checkbox for keeping the aspect ration. */ protected static JCheckBox m_AspectRatioCheckBox; /** the title of the save dialog. */ protected String m_SaveDialogTitle = "Save as..."; /** the x scale factor. */ protected double m_xScale = 1.0; /** the y scale factor. */ protected double m_yScale = 1.0; /** the aspect ratio. */ protected double m_AspectRatio; /** whether to ignore the update of the text field (in case of "keep ratio"). */ protected boolean m_IgnoreChange; /** whether to print some debug information. */ private static final boolean DEBUG = false; /** whether the user was already asked about the tooltip behavior. */ protected static boolean m_ToolTipUserAsked = false; /** the property name for showing the tooltip. */ protected final static String PROPERTY_SHOW = "PrintableComponentToolTipShow"; /** the property name whether the user was already asked. */ protected final static String PROPERTY_USERASKED = "PrintableComponentToolTipUserAsked"; /** whether to display the tooltip or not. */ protected static boolean m_ShowToolTip = true; static { try { m_ShowToolTip = Boolean.valueOf( VisualizeUtils.VISUALIZE_PROPERTIES.getProperty( PROPERTY_SHOW, "true")).booleanValue(); m_ToolTipUserAsked = Boolean.valueOf( VisualizeUtils.VISUALIZE_PROPERTIES.getProperty( PROPERTY_USERASKED, "false")).booleanValue(); } catch (Exception e) { // ignore exception m_ToolTipUserAsked = false; m_ShowToolTip = true; } } /** output if we're in debug mode */ static { if (DEBUG) System.err.println(PrintablePanel.class.getName() + ": DEBUG ON"); } /** * initializes the panel. * * @param component the component to enhance with printing functionality */ public PrintableComponent(JComponent component) { super(); m_Component = component; m_AspectRatio = Double.NaN; getComponent().addMouseListener(new PrintMouseListener(this)); getComponent().setToolTipText(getToolTipText(this)); initFileChooser(); } /** * returns the GUI component this print dialog is part of. * * @return the GUI component */ public JComponent getComponent() { return m_Component; } /** * Returns a tooltip only if the user wants it. If retrieved for the first, * a dialog pops up and asks the user whether the tooltip should always * appear or not. The weka/gui/visualize/Visualize.props is then written * in the user's home directory. * * @param component the PrintableComponent to ask for * @return null if the user doesn't want the tooltip, otherwise the text */ public static String getToolTipText(PrintableComponent component) { String result; int retVal; Properties props; String name; Enumeration names; String filename; // ToolTip is disabled for the moment... if (true) return null; // ask user whether the tooltip should be shown if (!m_ToolTipUserAsked) { m_ToolTipUserAsked = true; retVal = JOptionPane.showConfirmDialog( component.getComponent(), "Some panels enable the user to save the content as JPEG or EPS.\n" + "In order to see which panels support this, a tooltip can be " + "displayed. Enable tooltip?", "ToolTip for Panels...", JOptionPane.YES_NO_OPTION); m_ShowToolTip = (retVal == JOptionPane.YES_OPTION); // save props file VisualizeUtils.VISUALIZE_PROPERTIES.setProperty( PROPERTY_SHOW, "" + m_ShowToolTip); VisualizeUtils.VISUALIZE_PROPERTIES.setProperty( PROPERTY_USERASKED, "" + m_ToolTipUserAsked); try { // NOTE: properties that got inherited from another props file don't // get saved. I.e., one could overwrite the existing props // file with an (nearly) empty one. // => transfer all properties into a new one props = new Properties(); names = VisualizeUtils.VISUALIZE_PROPERTIES.propertyNames(); while (names.hasMoreElements()) { name = names.nextElement().toString(); props.setProperty( name, VisualizeUtils.VISUALIZE_PROPERTIES.getProperty(name, "")); } filename = System.getProperty("user.home") + "/Visualize.props"; props.store( new BufferedOutputStream(new FileOutputStream(filename)), null); // inform user about location of props file and name of property JOptionPane.showMessageDialog( component.getComponent(), "You can still manually enable or disable the ToolTip via the following property\n" + " " + PROPERTY_SHOW + "\n" + "in the following file\n" + " " + filename); } catch (Exception e) { JOptionPane.showMessageDialog( component.getComponent(), "Error saving the props file!\n" + e.getMessage() + "\n\n" + "Note:\n" + "If you want to disable these messages from popping up, place a file\n" + "called 'Visualize.props' either in your home directory or in the directory\n" + "you're starting Weka from and add the following lines:\n" + " " + PROPERTY_USERASKED + "=true\n" + " " + PROPERTY_SHOW + "=" + m_ShowToolTip, "Error...", JOptionPane.ERROR_MESSAGE); } } if (m_ShowToolTip) result = "Click left mouse button while holding <alt> and <shift> to display a save dialog."; else result = null; return result; } /** * initializes the filechooser, i.e. locates all the available writers in * the current package */ protected void initFileChooser() { Vector writerNames; int i; Class cls; JComponentWriter writer; JPanel accessory; JLabel label; // already initialized? if (m_FileChooserPanel != null) return; m_FileChooserPanel = new JFileChooser(); m_FileChooserPanel.resetChoosableFileFilters(); m_FileChooserPanel.setAcceptAllFileFilterUsed(false); // setup the accessory accessory = new JPanel(); accessory.setLayout(null); accessory.setPreferredSize(new Dimension(200, 200)); accessory.revalidate(); m_FileChooserPanel.setAccessory(accessory); m_CustomDimensionsCheckBox = new JCheckBox("Use custom dimensions"); m_CustomDimensionsCheckBox.setBounds(14, 7, 200, 21); m_CustomDimensionsCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { boolean custom = m_CustomDimensionsCheckBox.isSelected(); m_CustomWidthText.setEnabled(custom); m_CustomHeightText.setEnabled(custom); m_AspectRatioCheckBox.setEnabled(custom); if (custom) { m_IgnoreChange = true; m_CustomWidthText.setText("" + m_Component.getWidth()); m_CustomHeightText.setText("" + m_Component.getHeight()); m_IgnoreChange = false; } else { m_IgnoreChange = true; m_CustomWidthText.setText("-1"); m_CustomHeightText.setText("-1"); m_IgnoreChange = false; } } }); accessory.add(m_CustomDimensionsCheckBox); m_CustomWidthText = new JTextField(5); m_CustomWidthText.setText("-1"); m_CustomWidthText.setEnabled(false); m_CustomWidthText.setBounds(65, 35, 50, 21); m_CustomWidthText.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { updateDimensions(m_CustomWidthText); } public void insertUpdate(DocumentEvent e) { updateDimensions(m_CustomWidthText); } public void removeUpdate(DocumentEvent e) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?