PropertyPanel.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 3k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    PropertyPanel.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui;
  22. import java.awt.Dimension;
  23. import java.awt.Insets;
  24. import java.awt.SystemColor;
  25. import java.awt.Graphics;
  26. import java.awt.Rectangle;
  27. import java.awt.event.MouseEvent;
  28. import java.awt.event.MouseAdapter;
  29. import java.beans.PropertyEditor;
  30. import javax.swing.JPanel;
  31. import javax.swing.BorderFactory;
  32. /** 
  33.  * Support for drawing a property value in a component.
  34.  *
  35.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  36.  * @version $Revision: 1.7 $
  37.  */
  38. public class PropertyPanel extends JPanel {
  39.   /** The property editor */
  40.   private PropertyEditor m_Editor;
  41.   /** The currently displayed property dialog, if any */
  42.   private PropertyDialog m_PD;
  43.   
  44.   /**
  45.    * Create the panel with the supplied property editor.
  46.    *
  47.    * @param pe the PropertyEditor
  48.    */
  49.   public PropertyPanel(PropertyEditor pe) {
  50.     //    System.err.println("PropertyPanel::PropertyPanel()");
  51.     setBorder(BorderFactory.createEtchedBorder());
  52.     setToolTipText("Click to edit properties for this object");
  53.     setOpaque(true);
  54.     m_Editor = pe;
  55.     addMouseListener(new MouseAdapter() {
  56.       public void mouseClicked(MouseEvent evt) {
  57. if (m_Editor.getValue() != null) {
  58.   if (m_PD == null) {
  59.     int x = getLocationOnScreen().x;
  60.     int y = getLocationOnScreen().y;
  61.     m_PD = new PropertyDialog(m_Editor, x, y);
  62.   } else {
  63.     m_PD.setVisible(true);
  64.   }
  65. }
  66.       }
  67.     });
  68.     Dimension newPref = getPreferredSize();
  69.     newPref.height = getFontMetrics(getFont()).getHeight() * 5 / 4;
  70.     newPref.width = newPref.height * 5;
  71.     setPreferredSize(newPref);
  72.   }
  73.   public void removeNotify() {
  74.     if (m_PD != null) {
  75.       m_PD.dispose();
  76.       m_PD = null;
  77.     }
  78.   }
  79.   /**
  80.    * Paints the component, using the property editor's paint method.
  81.    *
  82.    * @param g the current graphics context
  83.    */
  84.   public void paintComponent(Graphics g) {
  85.     Insets i = getInsets();
  86.     Rectangle box = new Rectangle(i.left, i.top,
  87.   getSize().width - i.left - i.right - 1,
  88.   getSize().height - i.top - i.bottom - 1);
  89.     
  90.     g.clearRect(i.left, i.top,
  91. getSize().width - i.right - i.left,
  92. getSize().height - i.bottom - i.top);
  93.     m_Editor.paintValue(g, box);
  94.   }
  95. }