Extensions+for+Weka's+main+GUI

toc = Description = The main GUI (= ) contains a plugin mechanism to add functionality to the main menu without having to modify the code of that class (the GUIChooser in the developer version as well). Thanks to the automatic class discovery, Weka will display all components that are found in packages listed in the GenericPropertiesCreator.props file.

= Version = >3.5.5 (or snapshot/Subversion after 25/05/2007)

= Requirements = The are only //two// requirements for components to be listed in the main menu (under the //Extensions// menu):
 * they have to implement the interface
 * the packages they reside in must be listed in the GenericPropertiesCreator.props under the entry

= Examples = In the following, I'll present two really simple examples of how to add //stuff// to the main menu. An item that gets added to the main menu either handles everything itself, i.e., creating frame and displaying it, or it needs a frame to place its GUI components in. In the first case, one only needs to let the method return an  and implement the  method with an empty body. In the other case, one lets the method return  and uses the  method to fill the frame with life.

Launching a browser
Launching a browser with the Weka homepage is a really example, since one only needs to use the class to launch a browser with a specific URL. Since we don't need a frame for this, we don't add any functionality to the, but only let the method return an  that launches the browser with the Weka homepage. The extension will be listed in the sub-menu //Internet// as //Start browser//. code format="java" public String getSubmenuTitle { return "Internet"; } public String getMenuTitle { return "Start browser"; } public ActionListener getActionListener(JFrame owner) { final JFrame finalOwner = owner; ActionListener result = new ActionListener { public void actionPerformed(ActionEvent evt) { BrowserHelper.openURL(finalOwner, "http://www.cs.waikato.ac.nz/~ml/weka/"); }   };    return result; } public void fillFrame(Component frame) { } code Since the class is part of the package, we must add this package to the  entry of the GenericPropertiesCreator.props file, e.g.: code format="ini" weka.gui.MainMenuExtension=\ weka.gui.extensions code

SQL Worksheet
The class shows how to implement an extension that needs a frame to populate with other graphical components, in contrast to the //Start browser// extension, which didn't need a frame at all. In this case, the returns  and the  method does all the work. Since the main GUI can be launched either in [|MDI] or [|SDI] mode, one needs to take care of to check for and  as ancestor of the frame that is being passed through. The method only instantiates an panel, places it in the center of the frame, resizes the frame to 800x600 and moves it into the center of the screen. code format="java" public String getSubmenuTitle { return null; } public String getMenuTitle { return "SQL Worksheet"; } public ActionListener getActionListener(JFrame owner) { return null; } public void fillFrame(Component frame) { SqlViewer sql = new SqlViewer(null); // add sql viewer component if (frame instanceof JFrame) { JFrame f = (JFrame) frame; f.setLayout(new BorderLayout); f.add(sql, BorderLayout.CENTER); f.pack; }   else if (frame instanceof JInternalFrame) { JInternalFrame f = (JInternalFrame) frame; f.setLayout(new BorderLayout); f.add(sql, BorderLayout.CENTER); f.pack; }   // size + location (= centered) frame.setSize(800, 600); frame.validate; int screenHeight = frame.getGraphicsConfiguration.getBounds.height; int screenWidth = frame.getGraphicsConfiguration.getBounds.width; frame.setLocation(	 (screenWidth - frame.getBounds.width) / 2,	  (screenHeight - frame.getBounds.height) / 2); } code This class is part of the package and we therefore must add this package to the  entry of the GenericPropertiesCreator.props file: code format="ini" weka.gui.MainMenuExtension=\ weka.gui.extensions code

= Downloads =
 * [[file:StartBrowser.java]] ([|stable-3.6], [|developer])
 * [[file:SqlWorksheet.java]] ([|stable-3.6], [|developer])