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 | * GenericObjectNodeEditor.java |
---|
19 | * Copyright (C) 2006 Robert Jung |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.ensembleLibraryEditor.tree; |
---|
24 | |
---|
25 | import weka.gui.GenericObjectEditor; |
---|
26 | |
---|
27 | import java.awt.BorderLayout; |
---|
28 | import java.awt.Color; |
---|
29 | import java.awt.Font; |
---|
30 | import java.awt.event.ActionEvent; |
---|
31 | import java.awt.event.ActionListener; |
---|
32 | import java.awt.event.WindowAdapter; |
---|
33 | import java.awt.event.WindowEvent; |
---|
34 | import java.beans.PropertyChangeEvent; |
---|
35 | import java.beans.PropertyChangeListener; |
---|
36 | import java.beans.PropertyChangeSupport; |
---|
37 | |
---|
38 | import javax.swing.BorderFactory; |
---|
39 | import javax.swing.JButton; |
---|
40 | import javax.swing.JFrame; |
---|
41 | import javax.swing.JLabel; |
---|
42 | import javax.swing.JPanel; |
---|
43 | import javax.swing.JPopupMenu; |
---|
44 | import javax.swing.JScrollPane; |
---|
45 | import javax.swing.JTextArea; |
---|
46 | import javax.swing.UIManager; |
---|
47 | import javax.swing.event.PopupMenuEvent; |
---|
48 | import javax.swing.event.PopupMenuListener; |
---|
49 | |
---|
50 | /** |
---|
51 | * This class creates a simple user interface for selecting the Generic |
---|
52 | * Object Type. It consists of a button that brings up a popupMenu |
---|
53 | * allowing the user to select the type of Generic Object to be edited. |
---|
54 | * It also contains a JLabel that just prints out the type. Finally, we |
---|
55 | * also added the button from the PropertySheetPanel that brings up more |
---|
56 | * info about the Object |
---|
57 | * |
---|
58 | * @author Robert Jung (mrbobjung@gmail.com) |
---|
59 | * @version $Revision: 1.1 $ |
---|
60 | */ |
---|
61 | public class GenericObjectNodeEditor |
---|
62 | extends JPanel |
---|
63 | implements PropertyChangeListener, ActionListener, PopupMenuListener { |
---|
64 | |
---|
65 | /** for serialization */ |
---|
66 | private static final long serialVersionUID = -2382339640932830323L; |
---|
67 | |
---|
68 | /** a reference to the GenericObjectEditor for this node*/ |
---|
69 | private GenericObjectEditor m_GenericObjectEditor; |
---|
70 | |
---|
71 | /** the label that will display the node object type */ |
---|
72 | private JLabel m_Label; |
---|
73 | |
---|
74 | /** the button that will create the JPopupMenu to choose the object type */ |
---|
75 | private JButton m_ChooseClassButton; |
---|
76 | |
---|
77 | /** Button to pop up the full help text in a separate frame */ |
---|
78 | private JButton m_MoreInfoButton; |
---|
79 | |
---|
80 | /** Help frame */ |
---|
81 | private JFrame m_HelpFrame; |
---|
82 | |
---|
83 | /** a propertyChangeSupportListener to inform the TreeNodeEditor |
---|
84 | * when we stop editing - needs to be done manually for that popup*/ |
---|
85 | private PropertyChangeSupport m_propertyChangeSupport = new PropertyChangeSupport( |
---|
86 | this); |
---|
87 | |
---|
88 | /** the colors */ |
---|
89 | private Color textForeground, textBackground; |
---|
90 | |
---|
91 | /** A reference to the node */ |
---|
92 | private GenericObjectNode m_Node; |
---|
93 | |
---|
94 | /** the popup menu to show the node parameter GUI */ |
---|
95 | private JPopupMenu m_popup; |
---|
96 | |
---|
97 | /** |
---|
98 | * The constructor builds initializes the various member values from the |
---|
99 | * node |
---|
100 | * |
---|
101 | * @param node the node to base the node editor on |
---|
102 | */ |
---|
103 | public GenericObjectNodeEditor(GenericObjectNode node) { |
---|
104 | m_GenericObjectEditor = node.getEditor(); |
---|
105 | m_GenericObjectEditor.setValue(node.getObject()); |
---|
106 | m_GenericObjectEditor.addPropertyChangeListener(node); |
---|
107 | m_GenericObjectEditor.addPropertyChangeListener(this); |
---|
108 | |
---|
109 | m_Node = node; |
---|
110 | updateEditor(); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * This method updates the editor by creating the two buttons and the |
---|
115 | * JPanel that constiture the generic object editor GUI. |
---|
116 | */ |
---|
117 | public void updateEditor() { |
---|
118 | |
---|
119 | textForeground = UIManager.getColor("Tree.textForeground"); |
---|
120 | textBackground = UIManager.getColor("Tree.textBackground"); |
---|
121 | |
---|
122 | setForeground(textForeground); |
---|
123 | setBackground(textBackground); |
---|
124 | |
---|
125 | m_ChooseClassButton = new JButton("Choose"); |
---|
126 | m_ChooseClassButton.addActionListener(this); |
---|
127 | m_ChooseClassButton.setForeground(textForeground); |
---|
128 | m_ChooseClassButton.setBackground(textBackground); |
---|
129 | |
---|
130 | Font fontValue; |
---|
131 | fontValue = UIManager.getFont("Tree.font"); |
---|
132 | |
---|
133 | String labelString = null; |
---|
134 | |
---|
135 | //This bit just prunes of the weka.classifier part of the lass name |
---|
136 | //for feng shui purposes |
---|
137 | |
---|
138 | boolean replaceName = false; |
---|
139 | String className = m_Node.getObject().getClass().toString(); |
---|
140 | if (className.length() > 23) { |
---|
141 | if (className.substring(0, 23).equals("class weka.classifiers.")) { |
---|
142 | replaceName = true; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | if (replaceName) { |
---|
147 | labelString = new String(m_Node.getObject().getClass().toString() |
---|
148 | .replaceAll("class weka.classifiers.", "")); |
---|
149 | } else { |
---|
150 | labelString = new String(m_Node.getObject().getClass().toString()); |
---|
151 | } |
---|
152 | |
---|
153 | m_Label = new JLabel(labelString); |
---|
154 | |
---|
155 | if (fontValue != null) { |
---|
156 | m_Label.setFont(fontValue); |
---|
157 | } |
---|
158 | m_Label.setForeground(textForeground); |
---|
159 | m_Label.setBackground(textBackground); |
---|
160 | |
---|
161 | m_MoreInfoButton = new JButton("More Info"); |
---|
162 | |
---|
163 | // anonymous action listener shows a JTree popup and allows the user |
---|
164 | // to choose the class they want |
---|
165 | m_MoreInfoButton.addActionListener(this); |
---|
166 | m_MoreInfoButton.setForeground(textForeground); |
---|
167 | m_MoreInfoButton.setBackground(textBackground); |
---|
168 | |
---|
169 | add(m_ChooseClassButton); |
---|
170 | add(m_Label); |
---|
171 | add(m_MoreInfoButton); |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * Sets the prop change listener |
---|
176 | * |
---|
177 | * @param al the listener |
---|
178 | */ |
---|
179 | public void setPropertyChangeListener(PropertyChangeListener al) { |
---|
180 | if (m_GenericObjectEditor != null) |
---|
181 | m_GenericObjectEditor.addPropertyChangeListener(al); |
---|
182 | m_propertyChangeSupport.addPropertyChangeListener(al); |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * This method implements the property change listener for this node. |
---|
187 | * When this happens, we just need to update the node value |
---|
188 | * |
---|
189 | * @param evt the event |
---|
190 | */ |
---|
191 | public void propertyChange(PropertyChangeEvent evt) { |
---|
192 | updateEditor(); |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * This implements the action listener for the buttons in this editor, If |
---|
197 | * they hit the choose class button then the popup menu for choosing the |
---|
198 | * generic object ype is created. Otherwise if its the more info button |
---|
199 | * then the information frame is popped up |
---|
200 | * |
---|
201 | * @param e the event |
---|
202 | */ |
---|
203 | public void actionPerformed(ActionEvent e) { |
---|
204 | |
---|
205 | if (e.getSource() == m_ChooseClassButton) { |
---|
206 | |
---|
207 | m_popup = m_GenericObjectEditor.getChooseClassPopupMenu(); |
---|
208 | |
---|
209 | m_popup.addPopupMenuListener(this); |
---|
210 | m_popup.show(m_ChooseClassButton, m_ChooseClassButton.getX(), |
---|
211 | m_ChooseClassButton.getY()); |
---|
212 | |
---|
213 | m_popup.pack(); |
---|
214 | |
---|
215 | } else if (e.getSource() == m_MoreInfoButton) { |
---|
216 | |
---|
217 | openHelpFrame(); |
---|
218 | |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | * This method was copied and modified from the ProeprtySheetPanel |
---|
224 | * class. |
---|
225 | */ |
---|
226 | protected void openHelpFrame() { |
---|
227 | |
---|
228 | JTextArea ta = new JTextArea(); |
---|
229 | ta.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
---|
230 | ta.setLineWrap(true); |
---|
231 | ta.setWrapStyleWord(true); |
---|
232 | //ta.setBackground(getBackground()); |
---|
233 | ta.setEditable(false); |
---|
234 | ta.setText(m_Node.getHelpText().toString()); |
---|
235 | ta.setCaretPosition(0); |
---|
236 | final JFrame jf = new JFrame("Information"); |
---|
237 | jf.addWindowListener(new WindowAdapter() { |
---|
238 | public void windowClosing(WindowEvent e) { |
---|
239 | jf.dispose(); |
---|
240 | if (m_HelpFrame == jf) { |
---|
241 | m_MoreInfoButton.setEnabled(true); |
---|
242 | } |
---|
243 | } |
---|
244 | }); |
---|
245 | jf.getContentPane().setLayout(new BorderLayout()); |
---|
246 | jf.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER); |
---|
247 | jf.pack(); |
---|
248 | jf.setSize(400, 350); |
---|
249 | jf.setLocation(this.getTopLevelAncestor().getLocationOnScreen().x |
---|
250 | + this.getTopLevelAncestor().getSize().width, this |
---|
251 | .getTopLevelAncestor().getLocationOnScreen().y); |
---|
252 | jf.setVisible(true); |
---|
253 | m_HelpFrame = jf; |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | * in the case that the popup menu is cancelled we need to manually |
---|
258 | * inform the ModelTreeNodeEditor that we are done editing. If we |
---|
259 | * don't then we get stuck in a strange limbo state where the node |
---|
260 | * represented by this editor is completely frozen and the user |
---|
261 | * can't interact with it. |
---|
262 | * |
---|
263 | * @param e the event |
---|
264 | */ |
---|
265 | public void popupMenuCanceled(PopupMenuEvent e) { |
---|
266 | m_propertyChangeSupport.firePropertyChange(null, null, null); |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * Necessary for popup listener interface. Does nothing. |
---|
271 | * |
---|
272 | * @param e the event |
---|
273 | */ |
---|
274 | public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { |
---|
275 | //do nothing |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * Necessary for popup listener interface. Does nothing. |
---|
280 | * |
---|
281 | * @param e the event |
---|
282 | */ |
---|
283 | public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
---|
284 | //do nothing |
---|
285 | } |
---|
286 | } |
---|