PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/weka-3-6-9/weka-src/src/main/java/weka/gui/beans/KnowledgeFlowApp.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 1395 lines | 975 code | 154 blank | 266 comment | 160 complexity | 887968fe9ff4c6bd0dab424804d87142 MD5 | raw file
  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. * KnowledgeFlowApp.java
  18. * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
  19. *
  20. */
  21. package weka.gui.beans;
  22. import java.awt.BorderLayout;
  23. import java.awt.Cursor;
  24. import java.awt.Dimension;
  25. import java.awt.Font;
  26. import java.awt.FontMetrics;
  27. import java.awt.Graphics;
  28. import java.awt.Graphics2D;
  29. import java.awt.GridLayout;
  30. import java.awt.Image;
  31. import java.awt.MenuItem;
  32. import java.awt.Point;
  33. import java.awt.PopupMenu;
  34. import java.awt.Rectangle;
  35. import java.awt.Toolkit;
  36. import java.awt.event.ActionEvent;
  37. import java.awt.event.ActionListener;
  38. import java.awt.event.InputEvent;
  39. import java.awt.event.MouseAdapter;
  40. import java.awt.event.MouseEvent;
  41. import java.awt.event.MouseMotionAdapter;
  42. import java.awt.image.BufferedImage;
  43. import java.beans.BeanInfo;
  44. import java.beans.Beans;
  45. import java.beans.Customizer;
  46. import java.beans.EventSetDescriptor;
  47. import java.beans.IntrospectionException;
  48. import java.beans.Introspector;
  49. import java.beans.MethodDescriptor;
  50. import java.beans.PropertyChangeEvent;
  51. import java.beans.PropertyChangeListener;
  52. import java.beans.beancontext.BeanContextChild;
  53. import java.beans.beancontext.BeanContextSupport;
  54. import java.io.BufferedWriter;
  55. import java.io.File;
  56. import java.io.FileInputStream;
  57. import java.io.FileOutputStream;
  58. import java.io.IOException;
  59. import java.io.InputStream;
  60. import java.io.InputStreamReader;
  61. import java.io.LineNumberReader;
  62. import java.io.ObjectInputStream;
  63. import java.io.ObjectOutputStream;
  64. import java.io.OutputStream;
  65. import java.io.OutputStreamWriter;
  66. import java.lang.reflect.Method;
  67. import java.text.SimpleDateFormat;
  68. import java.util.ArrayList;
  69. import java.util.Date;
  70. import java.util.Enumeration;
  71. import java.util.Hashtable;
  72. import java.util.Iterator;
  73. import java.util.Properties;
  74. import java.util.StringTokenizer;
  75. import java.util.TreeMap;
  76. import java.util.Vector;
  77. import javax.swing.Box;
  78. import javax.swing.ButtonGroup;
  79. import javax.swing.ImageIcon;
  80. import javax.swing.JButton;
  81. import javax.swing.JComponent;
  82. import javax.swing.JFileChooser;
  83. import javax.swing.JFrame;
  84. import javax.swing.JLabel;
  85. import javax.swing.JOptionPane;
  86. import javax.swing.JPanel;
  87. import javax.swing.JScrollPane;
  88. import javax.swing.JTabbedPane;
  89. import javax.swing.JTextArea;
  90. import javax.swing.JToggleButton;
  91. import javax.swing.JToolBar;
  92. import javax.swing.JWindow;
  93. import javax.swing.filechooser.FileFilter;
  94. import weka.core.ClassloaderUtil;
  95. import weka.core.Copyright;
  96. import weka.core.Environment;
  97. import weka.core.EnvironmentHandler;
  98. import weka.core.Memory;
  99. import weka.core.SerializedObject;
  100. import weka.core.Utils;
  101. import weka.core.xml.KOML;
  102. import weka.core.xml.XStream;
  103. import weka.gui.ExtensionFileFilter;
  104. import weka.gui.GenericObjectEditor;
  105. import weka.gui.GenericPropertiesCreator;
  106. import weka.gui.HierarchyPropertyParser;
  107. import weka.gui.LookAndFeel;
  108. import weka.gui.beans.xml.XMLBeans;
  109. import weka.gui.visualize.PrintablePanel;
  110. /**
  111. * Main GUI class for the KnowledgeFlow. Modifications to allow interoperability
  112. * with swt provided by Davide Zerbetto (davide dot zerbetto at eng dot it).
  113. *
  114. * @author Mark Hall
  115. * @version $Revision: 9495 $
  116. * @since 1.0
  117. * @see JPanel
  118. * @see PropertyChangeListener
  119. */
  120. public class KnowledgeFlowApp extends JPanel implements PropertyChangeListener {
  121. /** for serialization */
  122. private static final long serialVersionUID = -7064906770289728431L;
  123. /**
  124. * Location of the property file for the KnowledgeFlowApp
  125. */
  126. protected static String PROPERTY_FILE = "weka/gui/beans/Beans.props";
  127. /** Contains the editor properties */
  128. protected static Properties BEAN_PROPERTIES;
  129. private static ArrayList<Properties> BEAN_PLUGINS_PROPERTIES;
  130. /**
  131. * Holds the details needed to construct button bars for various supported
  132. * classes of weka algorithms/tools
  133. */
  134. private static Vector TOOLBARS = new Vector();
  135. /**
  136. * Loads KnowledgeFlow properties and any plugins (adds jars to the classpath)
  137. */
  138. public static void loadProperties() {
  139. if (BEAN_PROPERTIES == null) {
  140. System.out.println(Messages.getInstance().getString(
  141. "KnowledgeFlowApp_LoadProperties_Text_First"));
  142. /** Loads the configuration property file */
  143. // static {
  144. // Allow a properties file in the current directory to override
  145. try {
  146. BEAN_PROPERTIES = Utils.readProperties(PROPERTY_FILE);
  147. java.util.Enumeration keys = BEAN_PROPERTIES.propertyNames();
  148. if (!keys.hasMoreElements()) {
  149. throw new Exception(Messages.getInstance().getString(
  150. "KnowledgeFlowApp_LoadProperties_Exception_Text_First")
  151. + Messages.getInstance().getString(
  152. "KnowledgeFlowApp_LoadProperties_Exception_Text_Second")
  153. + PROPERTY_FILE
  154. + Messages.getInstance().getString(
  155. "KnowledgeFlowApp_LoadProperties_Exception_Text_Third")
  156. + System.getProperties().getProperty("user.home")
  157. + Messages.getInstance().getString(
  158. "KnowledgeFlowApp_LoadProperties_Exception_Text_Fourth"));
  159. }
  160. } catch (Exception ex) {
  161. JOptionPane
  162. .showMessageDialog(
  163. null,
  164. ex.getMessage(),
  165. Messages
  166. .getInstance()
  167. .getString(
  168. "KnowledgeFlowApp_LoadProperties_Exception_JOptionPaneShowMessageDialog_Text"),
  169. JOptionPane.ERROR_MESSAGE);
  170. }
  171. // try and load any plugin beans properties
  172. File pluginDir = new File(System.getProperty("user.home")
  173. + File.separator + ".knowledgeFlow" + File.separator + "plugins");
  174. if (pluginDir.exists() && pluginDir.isDirectory()) {
  175. BEAN_PLUGINS_PROPERTIES = new ArrayList<Properties>();
  176. // How many sub-dirs are there?
  177. File[] contents = pluginDir.listFiles();
  178. for (int i = 0; i < contents.length; i++) {
  179. if (contents[i].isDirectory() && contents[i].listFiles().length > 0) {
  180. try {
  181. Properties tempP = new Properties();
  182. File propFile = new File(contents[i].getPath() + File.separator
  183. + "Beans.props");
  184. tempP.load(new FileInputStream(propFile));
  185. BEAN_PLUGINS_PROPERTIES.add(tempP);
  186. // Now try and add all jar files in this directory to the
  187. // classpath
  188. File anyJars[] = contents[i].listFiles();
  189. for (int j = 0; j < anyJars.length; j++) {
  190. if (anyJars[j].getPath().endsWith(".jar")) {
  191. System.out.println(Messages.getInstance().getString(
  192. "KnowledgeFlowApp_LoadProperties_Text_Second")
  193. + anyJars[j].getPath()
  194. + Messages.getInstance().getString(
  195. "KnowledgeFlowApp_LoadProperties_Text_Third"));
  196. ClassloaderUtil.addFile(anyJars[j].getPath());
  197. }
  198. }
  199. } catch (Exception ex) {
  200. // Don't make a fuss
  201. System.err.println(Messages.getInstance().getString(
  202. "KnowledgeFlowApp_LoadProperties_Error_Text_First")
  203. + contents[i].getPath());
  204. }
  205. }
  206. // BEAN_PLUGINS_PROPERTIES = new Properties();
  207. // BEAN_PLUGINS_PROPERTIES.load(new FileInputStream(pluginDir));
  208. }
  209. } else {
  210. // make the plugin directory for the user
  211. pluginDir.mkdir();
  212. }
  213. }
  214. }
  215. /**
  216. * Initializes the temporary files necessary to construct the toolbars from.
  217. */
  218. private static void init() {
  219. System.out.println(Messages.getInstance().getString(
  220. "KnowledgeFlowApp_Init_Text_First"));
  221. try {
  222. TreeMap wrapList = new TreeMap();
  223. GenericPropertiesCreator creator = new GenericPropertiesCreator();
  224. Properties GEOProps = null;
  225. if (creator.useDynamic()) {
  226. creator.execute(false);
  227. /*
  228. * now process the keys in the GenericObjectEditor.props. For each key
  229. * that has an entry in the Beans.props associating it with a bean
  230. * component a button tool bar will be created
  231. */
  232. GEOProps = creator.getOutputProperties();
  233. } else {
  234. // Read the static information from the GenericObjectEditor.props
  235. GEOProps = Utils.readProperties("weka/gui/GenericObjectEditor.props");
  236. }
  237. Enumeration en = GEOProps.propertyNames();
  238. while (en.hasMoreElements()) {
  239. String geoKey = (String) en.nextElement();
  240. // try to match this key with one in the Beans.props file
  241. String beanCompName = BEAN_PROPERTIES.getProperty(geoKey);
  242. if (beanCompName != null) {
  243. // add details necessary to construct a button bar for this class
  244. // of algorithms
  245. Vector newV = new Vector();
  246. // check for a naming alias for this toolbar
  247. String toolBarNameAlias = BEAN_PROPERTIES.getProperty(geoKey
  248. + ".alias");
  249. String toolBarName = (toolBarNameAlias != null) ? toolBarNameAlias
  250. : geoKey.substring(geoKey.lastIndexOf('.') + 1, geoKey.length());
  251. // look for toolbar ordering information for this wrapper type
  252. String order = BEAN_PROPERTIES.getProperty(geoKey + ".order");
  253. Integer intOrder = (order != null) ? new Integer(order)
  254. : new Integer(0);
  255. // Name for the toolbar (name of weka algorithm class)
  256. newV.addElement(toolBarName);
  257. // Name of bean capable of handling this class of algorithm
  258. newV.addElement(beanCompName);
  259. // add the root package for this key
  260. String rootPackage = geoKey.substring(0, geoKey.lastIndexOf('.'));
  261. newV.addElement(rootPackage);
  262. // All the weka algorithms of this class of algorithm
  263. String wekaAlgs = GEOProps.getProperty(geoKey);
  264. Hashtable roots = GenericObjectEditor.sortClassesByRoot(wekaAlgs);
  265. Hashtable hpps = new Hashtable();
  266. Enumeration enm = roots.keys();
  267. while (enm.hasMoreElements()) {
  268. String root = (String) enm.nextElement();
  269. String classes = (String) roots.get(root);
  270. weka.gui.HierarchyPropertyParser hpp = new weka.gui.HierarchyPropertyParser();
  271. hpp.build(classes, ", ");
  272. // System.err.println(hpp.showTree());
  273. hpps.put(root, hpp);
  274. }
  275. // ------ test the HierarchyPropertyParser
  276. /*
  277. * weka.gui.HierarchyPropertyParser hpp = new
  278. * weka.gui.HierarchyPropertyParser(); hpp.build(wekaAlgs, ", ");
  279. *
  280. * System.err.println(hpp.showTree());
  281. */
  282. // ----- end test the HierarchyPropertyParser
  283. // newV.addElement(hpp); // add the hierarchical property parser
  284. newV.addElement(hpps); // add the hierarchical property parser
  285. StringTokenizer st = new StringTokenizer(wekaAlgs, ", ");
  286. while (st.hasMoreTokens()) {
  287. String current = st.nextToken().trim();
  288. newV.addElement(current);
  289. }
  290. wrapList.put(intOrder, newV);
  291. // TOOLBARS.addElement(newV);
  292. }
  293. }
  294. Iterator keysetIt = wrapList.keySet().iterator();
  295. while (keysetIt.hasNext()) {
  296. Integer key = (Integer) keysetIt.next();
  297. Vector newV = (Vector) wrapList.get(key);
  298. if (newV != null) {
  299. TOOLBARS.addElement(newV);
  300. }
  301. }
  302. } catch (Exception ex) {
  303. JOptionPane
  304. .showMessageDialog(
  305. null,
  306. Messages
  307. .getInstance()
  308. .getString(
  309. "KnowledgeFlowApp_Init_Exception_JOptionPaneShowMessageDialog_Text_First")
  310. + System.getProperties().getProperty("user.home")
  311. + Messages
  312. .getInstance()
  313. .getString(
  314. "KnowledgeFlowApp_Init_Exception_JOptionPaneShowMessageDialog_Text_Second"),
  315. Messages
  316. .getInstance()
  317. .getString(
  318. "KnowledgeFlowApp_Init_Exception_JOptionPaneShowMessageDialog_Text_Third"),
  319. JOptionPane.ERROR_MESSAGE);
  320. }
  321. try {
  322. String standardToolBarNames = BEAN_PROPERTIES
  323. .getProperty("weka.gui.beans.KnowledgeFlow.standardToolBars");
  324. StringTokenizer st = new StringTokenizer(standardToolBarNames, ", ");
  325. while (st.hasMoreTokens()) {
  326. String tempBarName = st.nextToken().trim();
  327. // construct details for this toolbar
  328. Vector newV = new Vector();
  329. // add the name of the toolbar
  330. newV.addElement(tempBarName);
  331. // indicate that this is a standard toolbar (no wrapper bean)
  332. newV.addElement("null");
  333. String toolBarContents = BEAN_PROPERTIES
  334. .getProperty("weka.gui.beans.KnowledgeFlow." + tempBarName);
  335. StringTokenizer st2 = new StringTokenizer(toolBarContents, ", ");
  336. while (st2.hasMoreTokens()) {
  337. String tempBeanName = st2.nextToken().trim();
  338. newV.addElement(tempBeanName);
  339. }
  340. TOOLBARS.addElement(newV);
  341. }
  342. } catch (Exception ex) {
  343. JOptionPane
  344. .showMessageDialog(
  345. null,
  346. ex.getMessage(),
  347. Messages
  348. .getInstance()
  349. .getString(
  350. "KnowledgeFlowApp_Init_Exception_JOptionPaneShowMessageDialog_Text_Fourth"),
  351. JOptionPane.ERROR_MESSAGE);
  352. }
  353. }
  354. /**
  355. * Used for displaying the bean components and their visible connections
  356. *
  357. * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a>
  358. * @version $Revision: 9495 $
  359. * @since 1.0
  360. * @see PrintablePanel
  361. */
  362. protected class BeanLayout extends PrintablePanel {
  363. /** for serialization */
  364. private static final long serialVersionUID = -146377012429662757L;
  365. @Override
  366. public void paintComponent(Graphics gx) {
  367. super.paintComponent(gx);
  368. BeanInstance.paintLabels(gx);
  369. BeanConnection.paintConnections(gx);
  370. // BeanInstance.paintConnections(gx);
  371. if (m_mode == CONNECTING) {
  372. gx.drawLine(m_startX, m_startY, m_oldX, m_oldY);
  373. } else if (m_mode == SELECTING) {
  374. gx.drawRect((m_startX < m_oldX) ? m_startX : m_oldX,
  375. (m_startY < m_oldY) ? m_startY : m_oldY,
  376. Math.abs(m_oldX - m_startX), Math.abs(m_oldY - m_startY));
  377. }
  378. }
  379. @Override
  380. public void doLayout() {
  381. super.doLayout();
  382. Vector comps = BeanInstance.getBeanInstances();
  383. for (int i = 0; i < comps.size(); i++) {
  384. BeanInstance bi = (BeanInstance) comps.elementAt(i);
  385. JComponent c = (JComponent) bi.getBean();
  386. Dimension d = c.getPreferredSize();
  387. c.setBounds(bi.getX(), bi.getY(), d.width, d.height);
  388. c.revalidate();
  389. }
  390. }
  391. }
  392. // Used for measuring and splitting icon labels
  393. // over multiple lines
  394. FontMetrics m_fontM;
  395. // constants for operations in progress
  396. protected static final int NONE = 0;
  397. protected static final int MOVING = 1;
  398. protected static final int CONNECTING = 2;
  399. protected static final int ADDING = 3;
  400. protected static final int SELECTING = 4;
  401. // which operation is in progress
  402. private int m_mode = NONE;
  403. /** the extension for the user components, when serialized to XML */
  404. protected final static String USERCOMPONENTS_XML_EXTENSION = ".xml";
  405. /**
  406. * Button group to manage all toolbar buttons
  407. */
  408. private final ButtonGroup m_toolBarGroup = new ButtonGroup();
  409. /**
  410. * Holds the selected toolbar bean
  411. */
  412. private Object m_toolBarBean;
  413. /**
  414. * The layout area
  415. */
  416. private final BeanLayout m_beanLayout = new BeanLayout();
  417. /**
  418. * Tabbed pane to hold tool bars
  419. */
  420. private final JTabbedPane m_toolBars = new JTabbedPane();
  421. /**
  422. * Stuff relating to plugin beans
  423. */
  424. private JToolBar m_pluginsToolBar = null;
  425. private Box m_pluginsBoxPanel = null;
  426. /**
  427. * Stuff relating to user created meta beans
  428. */
  429. private JToolBar m_userToolBar = null;
  430. private Box m_userBoxPanel = null;
  431. private final Vector m_userComponents = new Vector();
  432. private boolean m_firstUserComponentOpp = true;
  433. private JToggleButton m_pointerB;
  434. private JButton m_saveB;
  435. private JButton m_loadB;
  436. private JButton m_stopB;
  437. private JButton m_helpB;
  438. private JButton m_newB;
  439. /**
  440. * Reference to bean being manipulated
  441. */
  442. private BeanInstance m_editElement;
  443. /**
  444. * Event set descriptor for the bean being manipulated
  445. */
  446. private EventSetDescriptor m_sourceEventSetDescriptor;
  447. /**
  448. * Used to record screen coordinates during move, select and connect
  449. * operations
  450. */
  451. private int m_oldX, m_oldY;
  452. private int m_startX, m_startY;
  453. /** The file chooser for selecting layout files */
  454. protected JFileChooser m_FileChooser = new JFileChooser(new File(
  455. System.getProperty("user.dir")));
  456. protected LogPanel m_logPanel = new LogPanel();// new LogPanel(null, true);
  457. protected BeanContextSupport m_bcSupport = new BeanContextSupport();
  458. /** the extension for the serialized setups (Java serialization) */
  459. public final static String FILE_EXTENSION = ".kf";
  460. /** the extension for the serialized setups (Java serialization) */
  461. public final static String FILE_EXTENSION_XML = ".kfml";
  462. /**
  463. * A filter to ensure only KnowledgeFlow files in binary format get shown in
  464. * the chooser
  465. */
  466. protected FileFilter m_KfFilter = new ExtensionFileFilter(FILE_EXTENSION,
  467. Messages.getInstance().getString("KnowledgeFlowApp_KfFilter_Text_First")
  468. + FILE_EXTENSION
  469. + Messages.getInstance().getString(
  470. "KnowledgeFlowApp_KfFilter_Text_Second"));
  471. /**
  472. * A filter to ensure only KnowledgeFlow files in KOML format get shown in the
  473. * chooser
  474. */
  475. protected FileFilter m_KOMLFilter = new ExtensionFileFilter(
  476. KOML.FILE_EXTENSION + "kf", Messages.getInstance().getString(
  477. "KnowledgeFlowApp_KOMLFilter_Text_Second")
  478. + KOML.FILE_EXTENSION
  479. + Messages.getInstance().getString(
  480. "KnowledgeFlowApp_KOMLFilter_Text_Third"));
  481. /**
  482. * A filter to ensure only KnowledgeFlow files in XStream format get shown in
  483. * the chooser
  484. */
  485. protected FileFilter m_XStreamFilter = new ExtensionFileFilter(
  486. XStream.FILE_EXTENSION + "kf", Messages.getInstance().getString(
  487. "KnowledgeFlowApp_XStreamFilter_Text_Second")
  488. + XStream.FILE_EXTENSION
  489. + Messages.getInstance().getString(
  490. "KnowledgeFlowApp_XStreamFilter_Text_Third"));
  491. /**
  492. * A filter to ensure only KnowledgeFlow layout files in XML format get shown
  493. * in the chooser
  494. */
  495. protected FileFilter m_XMLFilter = new ExtensionFileFilter(
  496. FILE_EXTENSION_XML, Messages.getInstance().getString(
  497. "KnowledgeFlowApp_XMLFilter_Text_First")
  498. + FILE_EXTENSION_XML
  499. + Messages.getInstance().getString(
  500. "KnowledgeFlowApp_XMLFilter_Text_Second"));
  501. /** the scrollbar increment of the layout scrollpane */
  502. protected int m_ScrollBarIncrementLayout = 20;
  503. /** the scrollbar increment of the components scrollpane */
  504. protected int m_ScrollBarIncrementComponents = 50;
  505. /** the flow layout width */
  506. protected int m_FlowWidth = 1024;
  507. /** the flow layout height */
  508. protected int m_FlowHeight = 768;
  509. /** the preferred file extension */
  510. protected String m_PreferredExtension = FILE_EXTENSION;
  511. /** whether to store the user components in XML or in binary format */
  512. protected boolean m_UserComponentsInXML = false;
  513. /** Environment variables for the current flow */
  514. protected Environment m_flowEnvironment = new Environment();
  515. /**
  516. * Set the environment variables to use. NOTE: loading a new layout resets
  517. * back to the default set of variables
  518. *
  519. * @param env
  520. */
  521. public void setEnvironment(Environment env) {
  522. m_flowEnvironment = env;
  523. setEnvironment();
  524. }
  525. private void setEnvironment() {
  526. // pass m_flowEnvironment to all components
  527. // that implement EnvironmentHandler
  528. Vector beans = BeanInstance.getBeanInstances();
  529. for (int i = 0; i < beans.size(); i++) {
  530. Object temp = ((BeanInstance) beans.elementAt(i)).getBean();
  531. if (temp instanceof EnvironmentHandler) {
  532. ((EnvironmentHandler) temp).setEnvironment(m_flowEnvironment);
  533. }
  534. }
  535. }
  536. /**
  537. * Creates a new <code>KnowledgeFlowApp</code> instance.
  538. */
  539. // modifications by Zerbetto
  540. // public KnowledgeFlowApp() {
  541. public KnowledgeFlowApp(boolean showFileMenu) {
  542. if (BEAN_PROPERTIES == null) {
  543. loadProperties();
  544. init();
  545. }
  546. m_showFileMenu = showFileMenu;
  547. // end modifications by Zerbetto
  548. // Grab a fontmetrics object
  549. JWindow temp = new JWindow();
  550. temp.setVisible(true);
  551. temp.getGraphics().setFont(new Font(null, Font.PLAIN, 9));
  552. m_fontM = temp.getGraphics().getFontMetrics();
  553. temp.setVisible(false);
  554. // some GUI defaults
  555. try {
  556. m_ScrollBarIncrementLayout = Integer.parseInt(BEAN_PROPERTIES
  557. .getProperty("ScrollBarIncrementLayout", ""
  558. + m_ScrollBarIncrementLayout));
  559. m_ScrollBarIncrementComponents = Integer.parseInt(BEAN_PROPERTIES
  560. .getProperty("ScrollBarIncrementComponents", ""
  561. + m_ScrollBarIncrementComponents));
  562. m_FlowWidth = Integer.parseInt(BEAN_PROPERTIES.getProperty("FlowWidth",
  563. "" + m_FlowWidth));
  564. m_FlowHeight = Integer.parseInt(BEAN_PROPERTIES.getProperty("FlowHeight",
  565. "" + m_FlowHeight));
  566. m_PreferredExtension = BEAN_PROPERTIES.getProperty("PreferredExtension",
  567. m_PreferredExtension);
  568. m_UserComponentsInXML = Boolean.valueOf(
  569. BEAN_PROPERTIES.getProperty("UserComponentsInXML", ""
  570. + m_UserComponentsInXML)).booleanValue();
  571. } catch (Exception ex) {
  572. ex.printStackTrace();
  573. }
  574. // FileChooser
  575. m_FileChooser.addChoosableFileFilter(m_KfFilter);
  576. if (KOML.isPresent()) {
  577. m_FileChooser.addChoosableFileFilter(m_KOMLFilter);
  578. }
  579. if (XStream.isPresent()) {
  580. m_FileChooser.addChoosableFileFilter(m_XStreamFilter);
  581. }
  582. m_FileChooser.addChoosableFileFilter(m_XMLFilter);
  583. if (m_PreferredExtension.equals(FILE_EXTENSION_XML)) {
  584. m_FileChooser.setFileFilter(m_XMLFilter);
  585. } else if (KOML.isPresent()
  586. && m_PreferredExtension.equals(KOML.FILE_EXTENSION + "kf")) {
  587. m_FileChooser.setFileFilter(m_KOMLFilter);
  588. } else if (XStream.isPresent()
  589. && m_PreferredExtension.equals(XStream.FILE_EXTENSION + "kf")) {
  590. m_FileChooser.setFileFilter(m_XStreamFilter);
  591. } else {
  592. m_FileChooser.setFileFilter(m_KfFilter);
  593. }
  594. m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  595. m_bcSupport.setDesignTime(true);
  596. m_beanLayout.setLayout(null);
  597. // handle mouse events
  598. m_beanLayout.addMouseListener(new MouseAdapter() {
  599. @Override
  600. public void mousePressed(MouseEvent me) {
  601. if (m_toolBarBean == null) {
  602. if (((me.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK)
  603. && m_mode == NONE) {
  604. BeanInstance bi = BeanInstance.findInstance(me.getPoint());
  605. JComponent bc = null;
  606. if (bi != null) {
  607. bc = (JComponent) (bi.getBean());
  608. }
  609. if (bc != null && (bc instanceof Visible)) {
  610. m_editElement = bi;
  611. m_oldX = me.getX();
  612. m_oldY = me.getY();
  613. m_mode = MOVING;
  614. }
  615. if (m_mode != MOVING) {
  616. m_mode = SELECTING;
  617. m_oldX = me.getX();
  618. m_oldY = me.getY();
  619. m_startX = m_oldX;
  620. m_startY = m_oldY;
  621. Graphics2D gx = (Graphics2D) m_beanLayout.getGraphics();
  622. gx.setXORMode(java.awt.Color.white);
  623. // gx.drawRect(m_oldX, m_oldY, m_oldX, m_oldY);
  624. // gx.drawLine(m_startX, m_startY, m_startX, m_startY);
  625. gx.dispose();
  626. m_mode = SELECTING;
  627. }
  628. }
  629. }
  630. }
  631. @Override
  632. public void mouseReleased(MouseEvent me) {
  633. if (m_editElement != null && m_mode == MOVING) {
  634. m_editElement = null;
  635. revalidate();
  636. m_beanLayout.repaint();
  637. m_mode = NONE;
  638. }
  639. if (m_mode == SELECTING) {
  640. revalidate();
  641. m_beanLayout.repaint();
  642. m_mode = NONE;
  643. checkSubFlow(m_startX, m_startY, me.getX(), me.getY());
  644. }
  645. }
  646. @Override
  647. public void mouseClicked(MouseEvent me) {
  648. BeanInstance bi = BeanInstance.findInstance(me.getPoint());
  649. if (m_mode == ADDING || m_mode == NONE) {
  650. // try and popup a context sensitive menu if we have
  651. // been clicked over a bean.
  652. if (bi != null) {
  653. JComponent bc = (JComponent) bi.getBean();
  654. // if we've been double clicked, then popup customizer
  655. // as long as we're not a meta bean
  656. if (me.getClickCount() == 2 && !(bc instanceof MetaBean)) {
  657. try {
  658. Class custClass = Introspector.getBeanInfo(bc.getClass())
  659. .getBeanDescriptor().getCustomizerClass();
  660. if (custClass != null) {
  661. if (bc instanceof BeanCommon) {
  662. if (!((BeanCommon) bc).isBusy()) {
  663. popupCustomizer(custClass, bc);
  664. }
  665. } else {
  666. popupCustomizer(custClass, bc);
  667. }
  668. }
  669. } catch (IntrospectionException ex) {
  670. ex.printStackTrace();
  671. }
  672. } else if (((me.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK)
  673. || me.isAltDown()) {
  674. doPopup(me.getPoint(), bi, me.getX(), me.getY());
  675. }
  676. } else {
  677. if (((me.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK)
  678. || me.isAltDown()) {
  679. // find connections if any close to this point
  680. int delta = 10;
  681. deleteConnectionPopup(
  682. BeanConnection.getClosestConnections(
  683. new Point(me.getX(), me.getY()), delta), me.getX(),
  684. me.getY());
  685. } else if (m_toolBarBean != null) {
  686. // otherwise, if a toolbar button is active then
  687. // add the component
  688. addComponent(me.getX(), me.getY());
  689. }
  690. }
  691. }
  692. if (m_mode == CONNECTING) {
  693. // turn off connecting points and remove connecting line
  694. m_beanLayout.repaint();
  695. Vector beanInstances = BeanInstance.getBeanInstances();
  696. for (int i = 0; i < beanInstances.size(); i++) {
  697. JComponent bean = (JComponent) ((BeanInstance) beanInstances
  698. .elementAt(i)).getBean();
  699. if (bean instanceof Visible) {
  700. ((Visible) bean).getVisual().setDisplayConnectors(false);
  701. }
  702. }
  703. if (bi != null) {
  704. boolean doConnection = false;
  705. if (!(bi.getBean() instanceof BeanCommon)) {
  706. doConnection = true;
  707. } else {
  708. // Give the target bean a chance to veto the proposed
  709. // connection
  710. if (((BeanCommon) bi.getBean()).
  711. // connectionAllowed(m_sourceEventSetDescriptor.getName())) {
  712. connectionAllowed(m_sourceEventSetDescriptor)) {
  713. doConnection = true;
  714. }
  715. }
  716. if (doConnection) {
  717. // attempt to connect source and target beans
  718. if (bi.getBean() instanceof MetaBean) {
  719. BeanConnection.doMetaConnection(m_editElement, bi,
  720. m_sourceEventSetDescriptor, m_beanLayout);
  721. } else {
  722. BeanConnection bc = new BeanConnection(m_editElement, bi,
  723. m_sourceEventSetDescriptor);
  724. }
  725. }
  726. m_beanLayout.repaint();
  727. }
  728. m_mode = NONE;
  729. m_editElement = null;
  730. m_sourceEventSetDescriptor = null;
  731. }
  732. }
  733. });
  734. m_beanLayout.addMouseMotionListener(new MouseMotionAdapter() {
  735. @Override
  736. public void mouseDragged(MouseEvent me) {
  737. if (m_editElement != null && m_mode == MOVING) {
  738. ImageIcon ic = ((Visible) m_editElement.getBean()).getVisual()
  739. .getStaticIcon();
  740. int width = ic.getIconWidth() / 2;
  741. int height = ic.getIconHeight() / 2;
  742. /*
  743. * m_editElement.setX(m_oldX-width);
  744. * m_editElement.setY(m_oldY-height);
  745. */
  746. m_editElement.setXY(m_oldX - width, m_oldY - height);
  747. m_beanLayout.repaint();
  748. // note the new points
  749. m_oldX = me.getX();
  750. m_oldY = me.getY();
  751. }
  752. if (m_mode == SELECTING) {
  753. m_beanLayout.repaint();
  754. m_oldX = me.getX();
  755. m_oldY = me.getY();
  756. }
  757. }
  758. @Override
  759. public void mouseMoved(MouseEvent e) {
  760. if (m_mode == CONNECTING) {
  761. m_beanLayout.repaint();
  762. // note the new coordinates
  763. m_oldX = e.getX();
  764. m_oldY = e.getY();
  765. }
  766. }
  767. });
  768. String date = (new SimpleDateFormat("EEEE, d MMMM yyyy"))
  769. .format(new Date());
  770. m_logPanel.logMessage(Messages.getInstance().getString(
  771. "KnowledgeFlowApp_MouseClicked_LogPanel_LogMessage_Text_First"));
  772. m_logPanel.logMessage(Messages.getInstance().getString(
  773. "KnowledgeFlowApp_MouseClicked_LogPanel_LogMessage_Text_Second"));
  774. m_logPanel.logMessage(Messages.getInstance().getString(
  775. "KnowledgeFlowApp_MouseClicked_LogPanel_LogMessage_Text_Third")
  776. + Copyright.getToYear()
  777. + " "
  778. + Copyright.getOwner()
  779. + ", "
  780. + Copyright.getAddress());
  781. m_logPanel.logMessage(Messages.getInstance().getString(
  782. "KnowledgeFlowApp_MouseClicked_LogPanel_LogMessage_Text_Fourth")
  783. + Copyright.getURL());
  784. m_logPanel.logMessage(date);
  785. m_logPanel.statusMessage(Messages.getInstance().getString(
  786. "KnowledgeFlowApp_MouseClicked_LogPanel_StatusMessage_Text_First"));
  787. m_logPanel.getStatusTable().addMouseListener(new MouseAdapter() {
  788. @Override
  789. public void mouseClicked(MouseEvent e) {
  790. if (m_logPanel.getStatusTable().rowAtPoint(e.getPoint()) == 0) {
  791. if (((e.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK)
  792. || e.isAltDown()) {
  793. System.gc();
  794. Runtime currR = Runtime.getRuntime();
  795. long freeM = currR.freeMemory();
  796. long totalM = currR.totalMemory();
  797. long maxM = currR.maxMemory();
  798. m_logPanel.logMessage(Messages.getInstance().getString(
  799. "KnowledgeFlowApp_MouseClicked_LogPanel_LogMessage_Text_Fifth")
  800. + String.format("%,d", freeM)
  801. + " / "
  802. + String.format("%,d", totalM)
  803. + " / "
  804. + String.format("%,d", maxM));
  805. m_logPanel
  806. .statusMessage(Messages
  807. .getInstance()
  808. .getString(
  809. "KnowledgeFlowApp_MouseClicked_LogPanel_StatusMessage_Text_Second")
  810. + String.format("%,d", freeM)
  811. + " / "
  812. + String.format("%,d", totalM)
  813. + " / "
  814. + String.format("%,d", maxM));
  815. }
  816. }
  817. }
  818. });
  819. JPanel p1 = new JPanel();
  820. p1.setLayout(new BorderLayout());
  821. p1.setBorder(javax.swing.BorderFactory.createCompoundBorder(
  822. javax.swing.BorderFactory
  823. .createTitledBorder(Messages
  824. .getInstance()
  825. .getString(
  826. "KnowledgeFlowApp_P1_JPanel_BorderFactoryCreateTitledBorder_Text")),
  827. javax.swing.BorderFactory.createEmptyBorder(0, 5, 5, 5)));
  828. final JScrollPane js = new JScrollPane(m_beanLayout);
  829. p1.add(js, BorderLayout.CENTER);
  830. js.getVerticalScrollBar().setUnitIncrement(m_ScrollBarIncrementLayout);
  831. js.getHorizontalScrollBar().setUnitIncrement(m_ScrollBarIncrementLayout);
  832. setLayout(new BorderLayout());
  833. add(p1, BorderLayout.CENTER);
  834. m_beanLayout.setSize(m_FlowWidth, m_FlowHeight);
  835. Dimension d = m_beanLayout.getPreferredSize();
  836. m_beanLayout.setMinimumSize(d);
  837. m_beanLayout.setMaximumSize(d);
  838. m_beanLayout.setPreferredSize(d);
  839. Dimension d2 = new Dimension(100, 170);
  840. m_logPanel.setPreferredSize(d2);
  841. m_logPanel.setMinimumSize(d2);
  842. add(m_logPanel, BorderLayout.SOUTH);
  843. setUpToolBars();
  844. loadUserComponents();
  845. }
  846. private Image loadImage(String path) {
  847. Image pic = null;
  848. // Modified by Zerbetto
  849. // java.net.URL imageURL = ClassLoader.getSystemResource(path);
  850. java.net.URL imageURL = this.getClass().getClassLoader().getResource(path);
  851. // end modifications
  852. if (imageURL == null) {
  853. // System.err.println("Warning: unable to load "+path);
  854. } else {
  855. pic = Toolkit.getDefaultToolkit().getImage(imageURL);
  856. }
  857. return pic;
  858. }
  859. /**
  860. * Describe <code>setUpToolBars</code> method here.
  861. */
  862. private void setUpToolBars() {
  863. JPanel toolBarPanel = new JPanel();
  864. toolBarPanel.setLayout(new BorderLayout());
  865. // modifications by Zerbetto
  866. // first construct the toolbar for saving, loading etc
  867. if (m_showFileMenu) {
  868. JToolBar fixedTools = new JToolBar();
  869. fixedTools.setOrientation(JToolBar.VERTICAL);
  870. m_saveB = new JButton(new ImageIcon(loadImage(BeanVisual.ICON_PATH
  871. + "Save24.gif")));
  872. m_saveB.setToolTipText(Messages.getInstance().getString(
  873. "KnowledgeFlowApp_SaveB_SetToolTipText_Text"));
  874. m_loadB = new JButton(new ImageIcon(loadImage(BeanVisual.ICON_PATH
  875. + "Open24.gif")));
  876. m_loadB.setToolTipText(Messages.getInstance().getString(
  877. "KnowledgeFlowApp_LoadB_SetToolTipText_Text"));
  878. m_newB = new JButton(new ImageIcon(loadImage(BeanVisual.ICON_PATH
  879. + "New24.gif")));
  880. m_newB.setToolTipText(Messages.getInstance().getString(
  881. "KnowledgeFlowApp_NewB_SetToolTipText_Text"));
  882. fixedTools.add(m_newB);
  883. fixedTools.add(m_saveB);
  884. fixedTools.add(m_loadB);
  885. m_saveB.addActionListener(new ActionListener() {
  886. public void actionPerformed(ActionEvent e) {
  887. saveLayout();
  888. }
  889. });
  890. m_loadB.addActionListener(new ActionListener() {
  891. public void actionPerformed(ActionEvent e) {
  892. m_flowEnvironment = new Environment();
  893. loadLayout();
  894. }
  895. });
  896. m_newB.addActionListener(new ActionListener() {
  897. public void actionPerformed(ActionEvent ae) {
  898. clearLayout();
  899. }
  900. });
  901. fixedTools.setFloatable(false);
  902. toolBarPanel.add(fixedTools, BorderLayout.WEST);
  903. }
  904. m_stopB = new JButton(new ImageIcon(loadImage(BeanVisual.ICON_PATH
  905. + "Stop24.gif")));
  906. m_helpB = new JButton(new ImageIcon(loadImage(BeanVisual.ICON_PATH
  907. + "Help24.gif")));
  908. m_stopB.setToolTipText(Messages.getInstance().getString(
  909. "KnowledgeFlowApp_StopB_SetToolTipText_Text"));
  910. m_helpB.setToolTipText(Messages.getInstance().getString(
  911. "KnowledgeFlowApp_HelpB_SetToolTipText_Text"));
  912. Image tempI = loadImage(BeanVisual.ICON_PATH + "Pointer.gif");
  913. m_pointerB = new JToggleButton(new ImageIcon(tempI));
  914. m_pointerB.addActionListener(new ActionListener() {
  915. public void actionPerformed(ActionEvent e) {
  916. m_toolBarBean = null;
  917. m_mode = NONE;
  918. setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  919. }
  920. });
  921. // Dimension dP = m_saveB.getPreferredSize();
  922. // Dimension dM = m_saveB.getMaximumSize();
  923. // Dimension dP = m_stopB.getPreferredSize();
  924. // Dimension dM = m_stopB.getMaximumSize();
  925. // m_pointerB.setPreferredSize(dP);
  926. // m_pointerB.setMaximumSize(dM);
  927. m_toolBarGroup.add(m_pointerB);
  928. JToolBar fixedTools2 = new JToolBar();
  929. fixedTools2.setOrientation(JToolBar.VERTICAL);
  930. fixedTools2.setFloatable(false);
  931. fixedTools2.add(m_pointerB);
  932. fixedTools2.add(m_helpB);
  933. fixedTools2.add(m_stopB);
  934. // m_helpB.setPreferredSize(dP);
  935. // m_helpB.setMaximumSize(dP);
  936. m_helpB.setSize(m_pointerB.getSize().width, m_pointerB.getSize().height);
  937. toolBarPanel.add(fixedTools2, BorderLayout.EAST);
  938. // end modifications by Zerbetto
  939. m_stopB.addActionListener(new ActionListener() {
  940. public void actionPerformed(ActionEvent e) {
  941. m_logPanel.statusMessage(Messages.getInstance().getString(
  942. "KnowledgeFlowApp_StopB_LogPanel_StatusMessage_Text_First"));
  943. stopFlow();
  944. m_logPanel.statusMessage(Messages.getInstance().getString(
  945. "KnowledgeFlowApp_StopB_LogPanel_StatusMessage_Text_Second"));
  946. }
  947. });
  948. m_helpB.addActionListener(new ActionListener() {
  949. public void actionPerformed(ActionEvent ae) {
  950. popupHelp();
  951. }
  952. });
  953. final int STANDARD_TOOLBAR = 0;
  954. final int WEKAWRAPPER_TOOLBAR = 1;
  955. int toolBarType = STANDARD_TOOLBAR;
  956. // set up wrapper toolbars
  957. for (int i = 0; i < TOOLBARS.size(); i++) {
  958. Vector tempBarSpecs = (Vector) TOOLBARS.elementAt(i);
  959. // name for the tool bar
  960. String tempBarName = (String) tempBarSpecs.elementAt(0);
  961. // Used for weka leaf packages
  962. Box singletonHolderPanel = null;
  963. // name of the bean component to handle this class of weka algorithms
  964. String tempBeanCompName = (String) tempBarSpecs.elementAt(1);
  965. // a JPanel holding an instantiated bean + label ready to be added
  966. // to the current toolbar
  967. JPanel tempBean;
  968. // the root package for weka algorithms
  969. String rootPackage = "";
  970. weka.gui.HierarchyPropertyParser hpp = null;
  971. Hashtable hpps = null;
  972. // Is this a wrapper toolbar?
  973. if (tempBeanCompName.compareTo("null") != 0) {
  974. tempBean = null;
  975. toolBarType = WEKAWRAPPER_TOOLBAR;
  976. rootPackage = (String) tempBarSpecs.elementAt(2);
  977. // hpp = (weka.gui.HierarchyPropertyParser)tempBarSpecs.elementAt(3);
  978. hpps = (Hashtable) tempBarSpecs.elementAt(3);
  979. try {
  980. // modifications by Zerbetto
  981. // Beans.instantiate(null, tempBeanCompName);
  982. Beans.instantiate(this.getClass().getClassLoader(), tempBeanCompName);
  983. // end modifications by Zerbetto
  984. } catch (Exception ex) {
  985. // ignore
  986. System.err.println(Messages.getInstance().getString(
  987. "KnowledgeFlowApp_Error_Text")
  988. + tempBeanCompName);
  989. break;
  990. }
  991. } else {
  992. toolBarType = STANDARD_TOOLBAR;
  993. }
  994. // a toolbar to hold buttons---one for each algorithm
  995. JToolBar tempToolBar = new JToolBar();
  996. // System.err.println(tempToolBar.getLayout());
  997. // tempToolBar.setLayout(new FlowLayout());
  998. int z = 2;
  999. if (toolBarType == WEKAWRAPPER_TOOLBAR) {
  1000. Enumeration enm = hpps.keys();
  1001. while (enm.hasMoreElements()) {
  1002. String root = (String) enm.nextElement();
  1003. String userPrefix = "";
  1004. hpp = (HierarchyPropertyParser) hpps.get(root);
  1005. if (!hpp.goTo(rootPackage)) {
  1006. System.out.println(Messages.getInstance().getString(
  1007. "KnowledgeFlowApp_Text_First"));
  1008. // System.exit(1);
  1009. userPrefix = root + ".";
  1010. }
  1011. String[] primaryPackages = hpp.childrenValues();
  1012. for (int kk = 0; kk < primaryPackages.length; kk++) {
  1013. hpp.goToChild(primaryPackages[kk]);
  1014. // check to see if this is a leaf - if so then there are no
  1015. // sub packages
  1016. if (hpp.isLeafReached()) {
  1017. if (singletonHolderPanel == null) {
  1018. singletonHolderPanel = Box.createHorizontalBox();
  1019. singletonHolderPanel.setBorder(javax.swing.BorderFactory
  1020. .createTitledBorder(tempBarName));
  1021. }
  1022. String algName = hpp.fullValue();
  1023. tempBean = instantiateToolBarBean(true, tempBeanCompName, algName);
  1024. if (tempBean != null) {
  1025. // tempToolBar.add(tempBean);
  1026. singletonHolderPanel.add(tempBean);
  1027. }
  1028. hpp.goToParent();
  1029. } else {
  1030. // make a titledborder JPanel to hold all the schemes in this
  1031. // package
  1032. // JPanel holderPanel = new JPanel();
  1033. Box holderPanel = Box.createHorizontalBox();
  1034. holderPanel.setBorder(javax.swing.BorderFactory
  1035. .createTitledBorder(userPrefix + primaryPackages[kk]));
  1036. processPackage(holderPanel, tempBeanCompName, hpp);
  1037. tempToolBar.add(holderPanel);
  1038. }
  1039. }
  1040. if (singletonHolderPanel != null) {
  1041. tempToolBar.add(singletonHolderPanel);
  1042. singletonHolderPanel = null;
  1043. }
  1044. }
  1045. } else {
  1046. Box holderPanel = Box.createHorizontalBox();
  1047. holderPanel.setBorder(javax.swing.BorderFactory
  1048. .createTitledBorder(tempBarName));
  1049. for (int j = z; j < tempBarSpecs.size(); j++) {
  1050. tempBean = null;
  1051. tempBeanCompName = (String) tempBarSpecs.elementAt(j);
  1052. tempBean = instantiateToolBarBean(
  1053. (toolBarType == WEKAWRAPPER_TOOLBAR), tempBeanCompName, "");
  1054. if (tempBean != null) {
  1055. // set tool tip text (if any)
  1056. // setToolTipText(tempBean)
  1057. holderPanel.add(tempBean);
  1058. }
  1059. }
  1060. tempToolBar.add(holderPanel);
  1061. }
  1062. JScrollPane tempJScrollPane = createScrollPaneForToolBar(tempToolBar);
  1063. // ok, now create tabbed pane to hold this toolbar
  1064. m_toolBars.addTab(tempBarName, null, tempJScrollPane, tempBarName);
  1065. }
  1066. // Any plugin components to process?
  1067. if (BEAN_PLUGINS_PROPERTIES != null && BEAN_PLUGINS_PROPERTIES.size() > 0) {
  1068. for (int i = 0; i < BEAN_PLUGINS_PROPERTIES.size(); i++) {
  1069. Properties tempP = BEAN_PLUGINS_PROPERTIES.get(i);
  1070. JPanel tempBean = null;
  1071. String components = tempP
  1072. .getProperty("weka.gui.beans.KnowledgeFlow.Plugins");
  1073. StringTokenizer st2 = new StringTokenizer(components, ", ");
  1074. while (st2.hasMoreTokens()) {
  1075. String tempBeanCompName = st2.nextToken().trim();
  1076. tempBean = instantiateToolBarBean(false, tempBeanCompName, "");
  1077. if (m_pluginsToolBar == null) {
  1078. // need to create the plugins tab and toolbar
  1079. setUpPluginsToolBar();
  1080. }
  1081. m_pluginsBoxPanel.add(tempBean);
  1082. }
  1083. }
  1084. }
  1085. toolBarPanel.add(m_toolBars, BorderLayout.CENTER);
  1086. // add(m_toolBars, BorderLayout.NORTH);
  1087. add(toolBarPanel, BorderLayout.NORTH);
  1088. }
  1089. private void stopFlow() {
  1090. Vector components = BeanInstance.getBeanInstances();
  1091. for (int i = 0; i < components.size(); i++) {
  1092. Object temp = ((BeanInstance) components.elementAt(i)).getBean();
  1093. if (temp instanceof BeanCommon) {
  1094. ((BeanCommon) temp).stop();
  1095. }
  1096. }
  1097. }
  1098. private JScrollPane createScrollPaneForToolBar(JToolBar tb) {
  1099. JScrollPane tempJScrollPane = new JScrollPane(tb,
  1100. JScrollPane.VERTICAL_SCROLLBAR_NEVER,
  1101. JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  1102. Dimension d = tb.getPreferredSize();
  1103. tempJScrollPane.setMinimumSize(new Dimension((int) d.getWidth(), (int) (d
  1104. .getHeight() + 15)));
  1105. tempJScrollPane.setPreferredSize(new Dimension((int) d.getWidth(), (int) (d
  1106. .getHeight() + 15)));
  1107. tempJScrollPane.getHorizontalScrollBar().setUnitIncrement(
  1108. m_ScrollBarIncrementComponents);
  1109. return tempJScrollPane;
  1110. }
  1111. private void processPackage(JComponent holderPanel, String tempBeanCompName,
  1112. weka.gui.HierarchyPropertyParser hpp) {
  1113. if (hpp.isLeafReached()) {
  1114. // instantiate a bean and add it to the holderPanel
  1115. // System.err.println("Would add "+hpp.fullValue());
  1116. String algName = hpp.fullValue();
  1117. JPanel tempBean = instantiateToolBarBean(true, tempBeanCompName, algName);
  1118. if (tempBean != null) {
  1119. holderPanel.add(tempBean);
  1120. }
  1121. hpp.goToParent();
  1122. return;
  1123. }
  1124. String[] children = hpp.childrenValues();
  1125. for (int i = 0; i < children.length; i++) {
  1126. hpp.goToChild(children[i]);
  1127. processPackage(holderPanel, tempBeanCompName, hpp);
  1128. }
  1129. hpp.goToParent();
  1130. }
  1131. /**
  1132. * Instantiates a bean for display in the toolbars
  1133. *
  1134. * @param wekawrapper true if the bean to be instantiated is a wekawrapper
  1135. * @param tempBeanCompName the name of the bean to instantiate
  1136. * @param algName holds the name of a weka algorithm to configure the bean
  1137. * with if it is a wekawrapper bean
  1138. * @return a JPanel holding the instantiated (and configured bean)
  1139. */
  1140. private JPanel instantiateToolBarBean(boolean wekawrapper,
  1141. String tempBeanCompName, String algName) {
  1142. Object tempBean;
  1143. if (wekawrapper) {
  1144. try {
  1145. // modifications by Zerbetto
  1146. // tempBean = Beans.instantiate(null, tempBeanCompName);
  1147. tempBean = Beans.instantiate(this.getClass().getClassLoader(),
  1148. tempBeanCompName);
  1149. // end modifications by Zerbetto
  1150. } catch (Exception ex) {
  1151. System.err.println(Messages.getInstance().getString(
  1152. "KnowledgeFlowApp_InstantiateToolBarBean_Error_Text_First")
  1153. + tempBeanCompName + "KnowledgeFlowApp.instantiateToolBarBean()");
  1154. return null;
  1155. }
  1156. if (tempBean instanceof WekaWrapper) {
  1157. // algName = (String)tempBarSpecs.elementAt(j);
  1158. Class c = null;
  1159. try {
  1160. c = Class.forName(algName);
  1161. } catch (Exception ex) {
  1162. System.err.println(Messages.getInstance().getString(
  1163. "KnowledgeFlowApp_InstantiateToolBarBean_Error_Text_Third")
  1164. + algName);
  1165. return null;
  1166. }
  1167. try {
  1168. Object o = c.newInstance();
  1169. ((WekaWrapper) tempBean).setWrappedAlgorithm(o);
  1170. } catch (Exception ex) {
  1171. System.err.println(Messages.getInstance().getString(
  1172. "KnowledgeFlowApp_InstantiateToolBarBean_Error_Text_Fourth")
  1173. + tempBeanCompName
  1174. + Messages.getInstance().getString(
  1175. "KnowledgeFlowApp_InstantiateToolBarBean_Error_Text_Fifth")
  1176. + algName);
  1177. return null;
  1178. }
  1179. }
  1180. } else {
  1181. try {
  1182. // modifications by Zerbetto
  1183. // tempBean = Beans.instantiate(null, tempBeanCompName);
  1184. tempBean = Beans.instantiate(this.getClass().getClassLoader(),
  1185. tempBeanCompName);
  1186. // end modifications
  1187. } catch (Exception ex) {
  1188. ex.printStackTrace();
  1189. System.err.println(Messages.getInstance().getString(
  1190. "KnowledgeFlowApp_InstantiateToolBarBean_Error_Text_Sixth")
  1191. + tempBeanCompName + "KnowledgeFlowApp.setUpToolBars()");
  1192. return null;
  1193. }
  1194. }
  1195. if (tempBean instanceof BeanContextChild) {
  1196. m_bcSupport.add(tempBean);
  1197. }
  1198. if (tempBean instanceof Visible) {
  1199. ((Visible) tempBean).getVisual().scale(3);
  1200. }
  1201. return makeHolderPanelForToolBarBean(tempBeanCompName, tempBean,
  1202. wekawrapper, algName, false);
  1203. }
  1204. /**
  1205. * Instantiates (by making a serialized copy) the supplied template meta bean
  1206. * for display in the user tool bar
  1207. *
  1208. * @param bean the prototype MetaBean to display in the toolbar
  1209. */
  1210. private JPanel instantiateToolBarMetaBean(MetaBean bean) {
  1211. // copy the bean via serialization
  1212. ((Visible) bean).getVisual().removePropertyChangeListener(this);
  1213. bean.removePropertyChangeListenersSubFlow(this);
  1214. Object copy = null;
  1215. try {
  1216. SerializedObject so = new SerializedObject(bean);
  1217. copy = so.getObject();
  1218. } catch (Exception ex) {
  1219. ex.printStackTrace();
  1220. return null;
  1221. }
  1222. ((Visible) bean).getVisual().addPropertyChangeListener(this);
  1223. bean.addPropertyChangeListenersSubFlow(this);
  1224. String displayName = "";
  1225. //
  1226. if (copy instanceof Visible) {
  1227. ((Visible) copy).getVisual().scale(3);
  1228. displayName = ((Visible) copy).getVisual().getText();
  1229. }
  1230. return makeHolderPanelForToolBarBean(displayName, copy, false, null, true);
  1231. }
  1232. private JPanel makeHolderPanelForToolBarBean(final String tempName,
  1233. Object tempBean, boolean wekawrapper, String algName,
  1234. final boolean metabean) {
  1235. // ---------------------------------------
  1236. JToggleButton tempButton;
  1237. final JPanel tempP = new JPanel();
  1238. JLabel tempL = new JLabel();
  1239. tempL.setFont(new Font(null, Font.PLAIN, 9));
  1240. String labelName = (wekawrapper == true) ? algName : tempName;
  1241. labelName = labelName.substring(labelName.lastIndex