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 | * PropertyPanel.java |
---|
19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | |
---|
24 | package weka.gui; |
---|
25 | |
---|
26 | import weka.core.OptionHandler; |
---|
27 | import weka.core.Utils; |
---|
28 | |
---|
29 | import java.awt.BorderLayout; |
---|
30 | import java.awt.Component; |
---|
31 | import java.awt.Dimension; |
---|
32 | import java.awt.Graphics; |
---|
33 | import java.awt.Insets; |
---|
34 | import java.awt.Rectangle; |
---|
35 | import java.awt.Toolkit; |
---|
36 | import java.awt.datatransfer.Clipboard; |
---|
37 | import java.awt.datatransfer.StringSelection; |
---|
38 | import java.awt.event.ActionEvent; |
---|
39 | import java.awt.event.ActionListener; |
---|
40 | import java.awt.event.MouseAdapter; |
---|
41 | import java.awt.event.MouseEvent; |
---|
42 | import java.beans.PropertyChangeEvent; |
---|
43 | import java.beans.PropertyChangeListener; |
---|
44 | import java.beans.PropertyEditor; |
---|
45 | |
---|
46 | import javax.swing.BorderFactory; |
---|
47 | import javax.swing.JMenuItem; |
---|
48 | import javax.swing.JOptionPane; |
---|
49 | import javax.swing.JPanel; |
---|
50 | import javax.swing.JPopupMenu; |
---|
51 | |
---|
52 | /** |
---|
53 | * Support for drawing a property value in a component. |
---|
54 | * |
---|
55 | * @author Len Trigg (trigg@cs.waikato.ac.nz) |
---|
56 | * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz) |
---|
57 | * @version $Revision: 5841 $ |
---|
58 | */ |
---|
59 | public class PropertyPanel |
---|
60 | extends JPanel { |
---|
61 | |
---|
62 | /** for serialization */ |
---|
63 | static final long serialVersionUID = 5370025273466728904L; |
---|
64 | |
---|
65 | /** The property editor */ |
---|
66 | private PropertyEditor m_Editor; |
---|
67 | |
---|
68 | /** The currently displayed property dialog, if any */ |
---|
69 | private PropertyDialog m_PD; |
---|
70 | |
---|
71 | /** Whether the editor has provided its own panel */ |
---|
72 | private boolean m_HasCustomPanel = false; |
---|
73 | |
---|
74 | /** The custom panel (if any) */ |
---|
75 | private JPanel m_CustomPanel; |
---|
76 | |
---|
77 | /** |
---|
78 | * Create the panel with the supplied property editor. |
---|
79 | * |
---|
80 | * @param pe the PropertyEditor |
---|
81 | */ |
---|
82 | public PropertyPanel(PropertyEditor pe) { |
---|
83 | |
---|
84 | this(pe, false); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Create the panel with the supplied property editor, |
---|
89 | * optionally ignoring any custom panel the editor can provide. |
---|
90 | * |
---|
91 | * @param pe the PropertyEditor |
---|
92 | * @param ignoreCustomPanel whether to make use of any available custom panel |
---|
93 | */ |
---|
94 | public PropertyPanel(PropertyEditor pe, boolean ignoreCustomPanel) { |
---|
95 | |
---|
96 | m_Editor = pe; |
---|
97 | |
---|
98 | if (!ignoreCustomPanel && m_Editor instanceof CustomPanelSupplier) { |
---|
99 | setLayout(new BorderLayout()); |
---|
100 | m_CustomPanel = ((CustomPanelSupplier)m_Editor).getCustomPanel(); |
---|
101 | add(m_CustomPanel, BorderLayout.CENTER); |
---|
102 | m_HasCustomPanel = true; |
---|
103 | } else { |
---|
104 | createDefaultPanel(); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Creates the default style of panel for editors that do not |
---|
110 | * supply their own. |
---|
111 | */ |
---|
112 | protected void createDefaultPanel() { |
---|
113 | |
---|
114 | setBorder(BorderFactory.createEtchedBorder()); |
---|
115 | setToolTipText("Left-click to edit properties for this object, right-click/Alt+Shift+left-click for menu"); |
---|
116 | setOpaque(true); |
---|
117 | final Component comp = this; |
---|
118 | addMouseListener(new MouseAdapter() { |
---|
119 | public void mouseClicked(MouseEvent evt) { |
---|
120 | if (evt.getClickCount() == 1) { |
---|
121 | if ( (evt.getButton() == MouseEvent.BUTTON1) && !evt.isAltDown() && !evt.isShiftDown() ) { |
---|
122 | showPropertyDialog(); |
---|
123 | } |
---|
124 | else if ( (evt.getButton() == MouseEvent.BUTTON3) |
---|
125 | || ((evt.getButton() == MouseEvent.BUTTON1) && evt.isAltDown() && evt.isShiftDown()) ) { |
---|
126 | JPopupMenu menu = new JPopupMenu(); |
---|
127 | JMenuItem item; |
---|
128 | |
---|
129 | if (m_Editor.getValue() != null) { |
---|
130 | item = new JMenuItem("Show properties..."); |
---|
131 | item.addActionListener(new ActionListener() { |
---|
132 | public void actionPerformed(ActionEvent e) { |
---|
133 | showPropertyDialog(); |
---|
134 | } |
---|
135 | }); |
---|
136 | menu.add(item); |
---|
137 | |
---|
138 | item = new JMenuItem("Copy configuration to clipboard"); |
---|
139 | item.addActionListener(new ActionListener() { |
---|
140 | public void actionPerformed(ActionEvent e) { |
---|
141 | String str = m_Editor.getValue().getClass().getName(); |
---|
142 | if (m_Editor.getValue() instanceof OptionHandler) |
---|
143 | str += " " + Utils.joinOptions(((OptionHandler) m_Editor.getValue()).getOptions()); |
---|
144 | StringSelection selection = new StringSelection(str.trim()); |
---|
145 | Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
---|
146 | clipboard.setContents(selection, selection); |
---|
147 | } |
---|
148 | }); |
---|
149 | menu.add(item); |
---|
150 | } |
---|
151 | |
---|
152 | item = new JMenuItem("Enter configuration..."); |
---|
153 | item.addActionListener(new ActionListener() { |
---|
154 | public void actionPerformed(ActionEvent e) { |
---|
155 | String str = JOptionPane.showInputDialog( |
---|
156 | comp, |
---|
157 | "Configuration (<classname> [<options>])"); |
---|
158 | if (str != null) { |
---|
159 | try { |
---|
160 | String[] options = Utils.splitOptions(str); |
---|
161 | String classname = options[0]; |
---|
162 | options[0] = ""; |
---|
163 | m_Editor.setValue( |
---|
164 | Utils.forName( |
---|
165 | Object.class, classname, options)); |
---|
166 | } |
---|
167 | catch (Exception ex) { |
---|
168 | ex.printStackTrace(); |
---|
169 | JOptionPane.showMessageDialog( |
---|
170 | comp, |
---|
171 | "Error parsing commandline:\n" + ex, |
---|
172 | "Error...", |
---|
173 | JOptionPane.ERROR_MESSAGE); |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | }); |
---|
178 | menu.add(item); |
---|
179 | |
---|
180 | menu.show(comp, evt.getX(), evt.getY()); |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | }); |
---|
185 | Dimension newPref = getPreferredSize(); |
---|
186 | newPref.height = getFontMetrics(getFont()).getHeight() * 5 / 4; |
---|
187 | newPref.width = newPref.height * 5; |
---|
188 | setPreferredSize(newPref); |
---|
189 | |
---|
190 | m_Editor.addPropertyChangeListener(new PropertyChangeListener () { |
---|
191 | public void propertyChange(PropertyChangeEvent evt) { |
---|
192 | repaint(); |
---|
193 | } |
---|
194 | }); |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Displays the property edit dialog for the panel. |
---|
199 | */ |
---|
200 | public void showPropertyDialog() { |
---|
201 | |
---|
202 | if (m_Editor.getValue() != null) { |
---|
203 | if (m_PD == null) { |
---|
204 | int x = getLocationOnScreen().x; |
---|
205 | int y = getLocationOnScreen().y; |
---|
206 | if (PropertyDialog.getParentDialog(this) != null) |
---|
207 | m_PD = new PropertyDialog(PropertyDialog.getParentDialog(this), m_Editor, x, y); |
---|
208 | else |
---|
209 | m_PD = new PropertyDialog(PropertyDialog.getParentFrame(this), m_Editor, x, y); |
---|
210 | m_PD.setVisible(true); |
---|
211 | } else { |
---|
212 | m_PD.setVisible(true); |
---|
213 | } |
---|
214 | // make sure that m_Backup is correctly initialized! |
---|
215 | m_Editor.setValue(m_Editor.getValue()); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Cleans up when the panel is destroyed. |
---|
221 | */ |
---|
222 | public void removeNotify() { |
---|
223 | |
---|
224 | super.removeNotify(); |
---|
225 | if (m_PD != null) { |
---|
226 | m_PD.dispose(); |
---|
227 | m_PD = null; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * Passes on enabled/disabled status to the custom |
---|
233 | * panel (if one is set). |
---|
234 | * |
---|
235 | * @param enabled true if this panel (and the custom panel is enabled) |
---|
236 | */ |
---|
237 | public void setEnabled(boolean enabled) { |
---|
238 | super.setEnabled(enabled); |
---|
239 | if (m_HasCustomPanel) { |
---|
240 | m_CustomPanel.setEnabled(enabled); |
---|
241 | } |
---|
242 | |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * Paints the component, using the property editor's paint method. |
---|
247 | * |
---|
248 | * @param g the current graphics context |
---|
249 | */ |
---|
250 | public void paintComponent(Graphics g) { |
---|
251 | |
---|
252 | if (!m_HasCustomPanel) { |
---|
253 | Insets i = getInsets(); |
---|
254 | Rectangle box = new Rectangle(i.left, i.top, |
---|
255 | getSize().width - i.left - i.right - 1, |
---|
256 | getSize().height - i.top - i.bottom - 1); |
---|
257 | |
---|
258 | g.clearRect(i.left, i.top, |
---|
259 | getSize().width - i.right - i.left, |
---|
260 | getSize().height - i.bottom - i.top); |
---|
261 | m_Editor.paintValue(g, box); |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | } |
---|