printablecomponent.java

来自「Weka」· Java 代码 · 共 632 行 · 第 1/2 页

JAVA
632
字号
	updateDimensions(m_CustomWidthText);      }    });    label = new JLabel("Width");    label.setLabelFor(m_CustomWidthText);    label.setDisplayedMnemonic('W');    label.setBounds(14, 35, 50, 21);    accessory.add(label);    accessory.add(m_CustomWidthText);        m_CustomHeightText = new JTextField(5);    m_CustomHeightText.setText("-1");    m_CustomHeightText.setEnabled(false);    m_CustomHeightText.setBounds(65, 63, 50, 21);    m_CustomHeightText.getDocument().addDocumentListener(new DocumentListener() {      public void changedUpdate(DocumentEvent e) {	updateDimensions(m_CustomHeightText);      }            public void insertUpdate(DocumentEvent e) {	updateDimensions(m_CustomHeightText);      }            public void removeUpdate(DocumentEvent e) {	updateDimensions(m_CustomHeightText);      }    });    label = new JLabel("Height");    label.setLabelFor(m_CustomHeightText);    label.setDisplayedMnemonic('H');    label.setBounds(14, 63, 50, 21);    accessory.add(label);    accessory.add(m_CustomHeightText);        m_AspectRatioCheckBox = new JCheckBox("Keep aspect ratio");    m_AspectRatioCheckBox.setBounds(14, 91, 200, 21);    m_AspectRatioCheckBox.setEnabled(false);    m_AspectRatioCheckBox.setSelected(true);    m_AspectRatioCheckBox.addItemListener(new ItemListener() {      public void itemStateChanged(ItemEvent e) {	boolean keep = m_AspectRatioCheckBox.isSelected();	if (keep) {	  m_IgnoreChange = true;	  m_CustomWidthText.setText("" + m_Component.getWidth());	  m_CustomHeightText.setText("" + m_Component.getHeight());	  m_IgnoreChange = false;	}      }    });    accessory.add(m_AspectRatioCheckBox);        // determine all available writers and add them to the filechooser    writerNames = GenericObjectEditor.getClassnames(JComponentWriter.class.getName());    Collections.sort(writerNames);    for (i = 0; i < writerNames.size(); i++) {      try {        cls    = Class.forName(writerNames.get(i).toString());        writer = (JComponentWriter) cls.newInstance();        m_FileChooserPanel.addChoosableFileFilter(            new JComponentWriterFileFilter(        	writer.getExtension(),         	writer.getDescription() + " (*" + writer.getExtension() + ")",         	writer));      }      catch (Exception e) {        System.err.println(writerNames.get(i) + ": " + e);      }    }        // set first filter as active filter    if (m_FileChooserPanel.getChoosableFileFilters().length > 0)      m_FileChooserPanel.setFileFilter(m_FileChooserPanel.getChoosableFileFilters()[0]);  }    /**   * updates the dimensions if necessary (i.e., if aspect ratio is to be kept).   *    * @param sender	the JTextField which send the notification to update   */  protected void updateDimensions(JTextField sender) {    int		newValue;    int		baseValue;        // some sanity checks    if (!m_AspectRatioCheckBox.isSelected() || m_IgnoreChange)      return;    if (!(sender instanceof JTextField) || (sender == null))      return;    if (sender.getText().length() == 0)      return;        // is it a valid integer, greater than 0?    try {      baseValue = Integer.parseInt(sender.getText());      newValue  = 0;      if (baseValue <= 0)	return;      if (Double.isNaN(m_AspectRatio)) {	m_AspectRatio = (double) getComponent().getWidth() / 	(double) getComponent().getHeight();      }    }    catch (Exception e) {      // we can't parse the string!      return;    }    // computer and update    m_IgnoreChange = true;    if (sender == m_CustomWidthText) {      newValue = (int) (((double) baseValue) * (1/m_AspectRatio));      m_CustomHeightText.setText("" + newValue);    }    else if (sender == m_CustomHeightText) {      newValue = (int) (((double) baseValue) * m_AspectRatio);      m_CustomWidthText.setText("" + newValue);    }    m_IgnoreChange = false;  }    /**   * returns a Hashtable with the current available JComponentWriters in the    * save dialog. the key of the Hashtable is the description of the writer.   *    * @return all currently available JComponentWriters    * @see JComponentWriter#getDescription()   */  public Hashtable getWriters() {    Hashtable         result;    int               i;    JComponentWriter  writer;        result = new Hashtable();        for (i = 0; i < m_FileChooserPanel.getChoosableFileFilters().length; i++) {      writer = ((JComponentWriterFileFilter) m_FileChooserPanel.getChoosableFileFilters()[i]).getWriter();      result.put(writer.getDescription(), writer);    }        return result;  }    /**   * returns the JComponentWriter associated with the given name, is    * <code>null</code> if not found.   *    * @param name the name of the writer   * @return the writer associated with the given name   * @see JComponentWriter#getDescription()   */  public JComponentWriter getWriter(String name) {    return (JComponentWriter) getWriters().get(name);  }  /**   * sets the title for the save dialog.   *    * @param title the title of the save dialog   */  public void setSaveDialogTitle(String title) {    m_SaveDialogTitle = title;  }    /**   * returns the title for the save dialog.   *    * @return the title of the save dialog   */  public String getSaveDialogTitle() {    return m_SaveDialogTitle;  }    /**   * sets the scale factor.   *    * @param x the scale factor for the x-axis    * @param y the scale factor for the y-axis    */  public void setScale(double x, double y) {    m_xScale = x;    m_yScale = y;    if (DEBUG)      System.err.println("x = " + x + ", y = " + y);  }    /**   * returns the scale factor for the x-axis.   *    * @return the scale factor   */  public double getXScale() {    return m_xScale;  }    /**   * returns the scale factor for the y-axis.   *    * @return the scale factor   */  public double getYScale() {    return m_xScale;  }    /**   * displays a save dialog for saving the panel to a file.     * Fixes a bug with the Swing JFileChooser: if you entered a new   * filename in the save dialog and press Enter the <code>getSelectedFile</code>   * method returns <code>null</code> instead of the filename.<br>   * To solve this annoying behavior we call the save dialog once again s.t. the   * filename is set. Might look a little bit strange to the user, but no    * NullPointerException! ;-)   */  public void saveComponent() {    int                           result;    JComponentWriter              writer;    File                          file;    JComponentWriterFileFilter    filter;        // display save dialog    m_FileChooserPanel.setDialogTitle(getSaveDialogTitle());    do {      result = m_FileChooserPanel.showSaveDialog(getComponent());      if (result != JFileChooser.APPROVE_OPTION)        return;    }    while (m_FileChooserPanel.getSelectedFile() == null);        // save the file    try {      filter = (JComponentWriterFileFilter) m_FileChooserPanel.getFileFilter();      file   = m_FileChooserPanel.getSelectedFile();      writer = filter.getWriter();      if (!file.getAbsolutePath().toLowerCase().endsWith(writer.getExtension().toLowerCase()))        file = new File(file.getAbsolutePath() + writer.getExtension());       writer.setComponent(getComponent());      writer.setFile(file);      writer.setScale(getXScale(), getYScale());      writer.setUseCustomDimensions(m_CustomDimensionsCheckBox.isSelected());      if (m_CustomDimensionsCheckBox.isSelected()) {	writer.setCustomWidth(Integer.parseInt(m_CustomWidthText.getText()));	writer.setCustomHeight(Integer.parseInt(m_CustomHeightText.getText()));      }      else {	writer.setCustomWidth(-1);	writer.setCustomHeight(-1);      }      writer.toOutput();    }    catch (Exception e) {      e.printStackTrace();    }  }    /**   * a specialized filter that also contains the associated filter class.   */  protected class JComponentWriterFileFilter extends ExtensionFileFilter {    /** the associated writer. */    private JComponentWriter m_Writer;         /**     * Creates the ExtensionFileFilter.     *     * @param extension       the extension of accepted files.     * @param description     a text description of accepted files.     * @param writer          the associated writer      */    public JComponentWriterFileFilter(String extension, String description, JComponentWriter writer) {      super(extension, description);      m_Writer = writer;    }        /**     * returns the associated writer.     *      * @return		the writer     */    public JComponentWriter getWriter() {      return m_Writer;    }  }  /**   * The listener to wait for Ctrl-Shft-Left Mouse Click.   */  private class PrintMouseListener extends MouseAdapter {    /** the listener's component. */    private PrintableComponent m_Component;        /**     * initializes the listener.     *      * @param component the component for which to create the listener     */    public PrintMouseListener(PrintableComponent component){      m_Component = component;    }        /**     * Invoked when the mouse has been clicked on a component.     *      * @param e	the event     */    public void mouseClicked(MouseEvent e) {      int modifiers = e.getModifiers();      if (((modifiers & MouseEvent.SHIFT_MASK) == MouseEvent.SHIFT_MASK) &&           ((modifiers & MouseEvent.ALT_MASK) == MouseEvent.ALT_MASK) &&          ((modifiers & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK)) {        e.consume();        m_Component.saveComponent();      }    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?