source: src/main/java/weka/gui/PropertyDialog.java @ 18

Last change on this file since 18 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 6.2 KB
Line 
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/*
18 *    PropertyDialog.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui;
24
25import java.awt.BorderLayout;
26import java.awt.Component;
27import java.awt.Container;
28import java.awt.Dialog;
29import java.awt.Frame;
30import java.awt.event.WindowAdapter;
31import java.awt.event.WindowEvent;
32import java.beans.PropertyEditor;
33
34import javax.swing.JDialog;
35import javax.swing.JInternalFrame;
36
37/**
38 * Support for PropertyEditors with custom editors: puts the editor into
39 * a separate frame.
40 *
41 * @author Len Trigg (trigg@cs.waikato.ac.nz)
42 * @version $Revision: 5144 $
43 */
44public class PropertyDialog
45  extends JDialog {
46
47  /** for serialization. */
48  private static final long serialVersionUID = -2314850859392433539L;
49
50  /** The property editor. */
51  private PropertyEditor m_Editor;
52
53  /** The custom editor component. */
54  private Component m_EditorComponent;
55 
56  /**
57   * Creates the editor frame - only kept for backward-compatibility.
58   *
59   * @param pe          the PropertyEditor
60   * @param x           initial x coord for the frame
61   * @param y           initial y coord for the frame
62   * @deprecated        instead of this constructor, one should use the constructors
63   *                    with an explicit owner (either derived from
64   *                    <code>java.awt.Dialog</code> or from
65   *                    <code>java.awt.Frame</code>) or, if none available,
66   *                    using <code>(Frame) null</code> as owner.
67   */
68  public PropertyDialog(PropertyEditor pe, int x, int y) {
69    this((Frame) null, pe, x, y);
70    setVisible(true);
71  }
72 
73  /**
74   * Creates the (screen-centered) editor dialog. The dialog is automatically
75   * modal in case the owner is non-null.
76   *
77   * @param owner       the dialog that opens this dialog
78   * @param pe          the PropertyEditor
79   */
80  public PropertyDialog(Dialog owner, PropertyEditor pe) {
81    this(owner, pe, -1, -1);
82  }
83 
84  /**
85   * Creates the editor dialog at the given position. The dialog is automatically
86   * modal in case the owner is non-null.
87   *
88   * @param owner       the dialog that opens this dialog
89   * @param pe          the PropertyEditor
90   * @param x           initial x coord for the dialog
91   * @param y           initial y coord for the dialog
92   */
93  public PropertyDialog(Dialog owner, PropertyEditor pe, int x, int y) {
94    super(owner, pe.getClass().getName(), true);
95    initialize(pe, x, y);
96  }
97 
98  /**
99   * Creates the (screen-centered) editor dialog. The dialog is automatically
100   * modal in case the owner is non-null.
101   *
102   * @param owner       the frame that opens this dialog
103   * @param pe          the PropertyEditor
104   */
105  public PropertyDialog(Frame owner, PropertyEditor pe) {
106    this(owner, pe, -1, -1);
107  }
108 
109  /**
110   * Creates the editor dialog at the given position. The dialog is automatically
111   * modal in case the owner is non-null.
112   *
113   * @param owner       the frame that opens this dialog
114   * @param pe          the PropertyEditor
115   * @param x           initial x coord for the dialog
116   * @param y           initial y coord for the dialog
117   */
118  public PropertyDialog(Frame owner, PropertyEditor pe, int x, int y) {
119    super(owner, pe.getClass().getName(), true);
120   
121    initialize(pe, x, y);
122  }
123 
124  /**
125   * Initializes the dialog.
126   *
127   * @param pe          the PropertyEditor
128   * @param x           initial x coord for the dialog
129   * @param y           initial y coord for the dialog
130   */
131  protected void initialize(PropertyEditor pe, int x, int y) {
132    addWindowListener(new WindowAdapter() {
133      public void windowClosing(WindowEvent e) {
134        e.getWindow().dispose();
135      }
136    });
137    getContentPane().setLayout(new BorderLayout());
138
139    m_Editor = pe;
140    m_EditorComponent = pe.getCustomEditor();
141    getContentPane().add(m_EditorComponent, BorderLayout.CENTER);
142
143    pack();
144   
145    if ((x == -1) && (y == -1))
146      setLocationRelativeTo(null);
147    else
148      setLocation(x, y);
149  }
150
151  /**
152   * Gets the current property editor.
153   *
154   * @return a value of type 'PropertyEditor'
155   */
156  public PropertyEditor getEditor() {
157    return m_Editor;
158  }
159
160  /**
161   * Tries to determine the frame this panel is part of.
162   *
163   * @param c           the container to start with
164   * @return            the parent frame if one exists or null if not
165   */
166  public static Frame getParentFrame(Container c) {
167    Frame       result;
168    Container   parent;
169   
170    result = null;
171   
172    parent = c;
173    while (parent != null) {
174      if (parent instanceof Frame) {
175        result = (Frame) parent;
176        break;
177      }
178      else {
179        parent = parent.getParent();
180      }
181    }
182   
183    return result;
184  }
185
186  /**
187   * Tries to determine the internal frame this panel is part of.
188   *
189   * @param c           the container to start with
190   * @return            the parent internal frame if one exists or null if not
191   */
192  public static JInternalFrame getParentInternalFrame(Container c) {
193    JInternalFrame      result;
194    Container           parent;
195   
196    result = null;
197   
198    parent = c;
199    while (parent != null) {
200      if (parent instanceof JInternalFrame) {
201        result = (JInternalFrame) parent;
202        break;
203      }
204      else {
205        parent = parent.getParent();
206      }
207    }
208   
209    return result;
210  }
211
212  /**
213   * Tries to determine the dialog this panel is part of.
214   *
215   * @param c           the container to start with
216   * @return            the parent dialog if one exists or null if not
217   */
218  public static Dialog getParentDialog(Container c) {
219    Dialog      result;
220    Container   parent;
221   
222    result = null;
223   
224    parent = c;
225    while (parent != null) {
226      if (parent instanceof Dialog) {
227        result = (Dialog) parent;
228        break;
229      }
230      else {
231        parent = parent.getParent();
232      }
233    }
234   
235    return result;
236  }
237}
238
Note: See TracBrowser for help on using the repository browser.