knowledgeflow.java
来自「wekaUT是 university texas austin 开发的基于wek」· Java 代码 · 共 1,209 行 · 第 1/3 页
JAVA
1,209 行
* @param y the y coordinate at which to popup the menu */ private void doPopup(Point pt, final BeanInstance bi, int x, int y) { final JComponent bc = (JComponent)bi.getBean(); final int xx = x; final int yy = y; int menuItemCount = 0; JPopupMenu beanContextMenu = new JPopupMenu(); beanContextMenu.insert(new JLabel("Edit", SwingConstants.CENTER), menuItemCount); menuItemCount++; JMenuItem deleteItem = new JMenuItem("Delete"); deleteItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { BeanConnection.removeConnections(bi); bi.removeBean(m_beanLayout); revalidate(); } }); beanContextMenu.add(deleteItem); menuItemCount++; // first determine if there is a customizer for this bean try { BeanInfo compInfo = Introspector.getBeanInfo(bc.getClass()); if (compInfo == null) { System.err.println("Error"); } else { // System.err.println("Got bean info"); final Class custClass = compInfo.getBeanDescriptor().getCustomizerClass(); if (custClass != null) { // System.err.println("Got customizer class"); // popupCustomizer(custClass, bc); JMenuItem custItem = new JMenuItem("Configure..."); custItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { popupCustomizer(custClass, bc); } }); beanContextMenu.add(custItem); menuItemCount++; } else { System.err.println("No customizer class"); } EventSetDescriptor [] esds = compInfo.getEventSetDescriptors(); if (esds != null && esds.length > 0) { beanContextMenu.insert(new JLabel("Connections", SwingConstants.CENTER), menuItemCount); menuItemCount++; } for (int i = 0; i < esds.length; i++) { // System.err.println(esds[i].getName()); // add each event name to the menu JMenuItem evntItem = new JMenuItem(esds[i].getName()); final EventSetDescriptor esd = esds[i]; // Check EventConstraints (if any) here boolean ok = true; if (bc instanceof EventConstraints) { ok = ((EventConstraints) bc).eventGeneratable(esd.getName()); } if (ok) { evntItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { connectComponents(esd, bi, xx, yy); } }); } else { evntItem.setEnabled(false); } beanContextMenu.add(evntItem); menuItemCount++; } } } catch (IntrospectionException ie) { ie.printStackTrace(); } // System.err.println("Just before look for other options"); // now look for other options for this bean if (bc instanceof UserRequestAcceptor) { Enumeration req = ((UserRequestAcceptor)bc).enumerateRequests(); if (req.hasMoreElements()) { beanContextMenu.insert(new JLabel("Actions", SwingConstants.CENTER), menuItemCount); menuItemCount++; } while (req.hasMoreElements()) { final String tempS = (String)req.nextElement(); JMenuItem custItem = new JMenuItem(tempS); custItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ((UserRequestAcceptor)bc).performRequest(tempS); } }); beanContextMenu.add(custItem); menuItemCount++; } } // System.err.println("Just before showing menu"); // popup the menu if (menuItemCount > 0) { beanContextMenu.show(m_beanLayout, x, y); } } /** * Popup the customizer for this bean * * @param custClass the class of the customizer * @param bc the bean to be customized */ private void popupCustomizer(Class custClass, JComponent bc) { try { // instantiate Object customizer = custClass.newInstance(); ((Customizer)customizer).setObject(bc); final javax.swing.JFrame jf = new javax.swing.JFrame(); jf.getContentPane().setLayout(new BorderLayout()); jf.getContentPane().add((JComponent)customizer, BorderLayout.CENTER); jf.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { jf.dispose(); } }); jf.pack(); jf.setVisible(true); } catch (Exception ex) { ex.printStackTrace(); } } /** * Popup a menu giving choices for connections to delete (if any) * * @param closestConnections a vector containing 0 or more BeanConnections * @param x the x coordinate at which to popup the menu * @param y the y coordinate at which to popup the menu */ private void deleteConnectionPopup(Vector closestConnections, int x, int y) { if (closestConnections.size() > 0) { int menuItemCount = 0; JPopupMenu deleteConnectionMenu = new JPopupMenu(); deleteConnectionMenu.insert(new JLabel("Delete Connection", SwingConstants.CENTER), menuItemCount); menuItemCount++; for (int i = 0; i < closestConnections.size(); i++) { final BeanConnection bc = (BeanConnection)closestConnections.elementAt(i); String connName = bc.getSourceEventSetDescriptor().getName(); JMenuItem deleteItem = new JMenuItem(connName); deleteItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { bc.remove(); m_beanLayout.revalidate(); m_beanLayout.repaint(); } }); deleteConnectionMenu.add(deleteItem); menuItemCount++; } deleteConnectionMenu.show(m_beanLayout, x, y); } } /** * Initiates the connection process for two beans * * @param esd the EventSetDescriptor for the source bean * @param bi the source bean * @param x the x coordinate to start connecting from * @param y the y coordinate to start connecting from */ private void connectComponents(EventSetDescriptor esd, BeanInstance bi, int x, int y) { // record the event set descriptior for this event m_sourceEventSetDescriptor = esd; Class listenerClass = esd.getListenerType(); // class of the listener JComponent source = (JComponent)bi.getBean(); // now determine which (if any) of the other beans implement this // listener int targetCount = 0; Vector beanInstances = BeanInstance.getBeanInstances(); for (int i = 0; i < beanInstances.size(); i++) { JComponent bean = (JComponent)((BeanInstance)beanInstances.elementAt(i)).getBean(); boolean connectable = false; if (listenerClass.isInstance(bean) && bean != source) { if (!(bean instanceof BeanCommon)) { connectable = true; // assume this bean is happy to receive a connection } else { // give this bean a chance to veto any proposed connection via // the listener interface if (((BeanCommon)bean). connectionAllowed(esd.getName())) { connectable = true; } } if (connectable) { if (bean instanceof Visible) { targetCount++; ((Visible)bean).getVisual().setDisplayConnectors(true); } } } } // have some possible beans to connect to? if (targetCount > 0) { // System.err.println("target count "+targetCount); if (source instanceof Visible) { ((Visible)source).getVisual().setDisplayConnectors(true); } m_editElement = bi; Point closest = ((Visible)source).getVisual(). getClosestConnectorPoint(new Point(x, y)); m_startX = (int)closest.getX(); m_startY = (int)closest.getY(); m_oldX = m_startX; m_oldY = m_startY; Graphics2D gx = (Graphics2D)m_beanLayout.getGraphics(); gx.setXORMode(java.awt.Color.white); gx.drawLine(m_startX, m_startY, m_startX, m_startY); gx.dispose(); m_mode = CONNECTING; } } private void addComponent(int x, int y) { BeanInstance bi = new BeanInstance(m_beanLayout, m_toolBarBean, x, y); // addBean((JComponent)bi.getBean()); if (m_toolBarBean instanceof Visible) { ((Visible)m_toolBarBean).getVisual().addPropertyChangeListener(this); } if (m_toolBarBean instanceof BeanCommon) { ((BeanCommon)m_toolBarBean).setLog(m_logPanel); } m_toolBarBean = null; setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); m_beanLayout.repaint(); m_pointerB.setSelected(true); m_mode = NONE; } /** * Accept property change events * * @param e a <code>PropertyChangeEvent</code> value */ public void propertyChange(PropertyChangeEvent e) { revalidate(); m_beanLayout.repaint(); } /** * Load a pre-saved layout */ private void loadLayout() { m_loadB.setEnabled(false); m_saveB.setEnabled(false); int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { File oFile = m_FileChooser.getSelectedFile(); InputStream is = new FileInputStream(oFile); ObjectInputStream ois = new ObjectInputStream(is); Vector beans = (Vector) ois.readObject(); Vector connections = (Vector) ois.readObject(); ois.close(); java.awt.Color bckC = getBackground(); // register this panel as a property change listener with each // bean for (int i = 0; i < beans.size(); i++) { BeanInstance tempB = (BeanInstance)beans.elementAt(i); if (tempB.getBean() instanceof Visible) { ((Visible)(tempB.getBean())).getVisual(). addPropertyChangeListener(this); // A workaround to account for JPanel's with their default // background colour not being serializable in Apple's JRE ((Visible)(tempB.getBean())).getVisual(). setBackground(bckC); ((JComponent)(tempB.getBean())).setBackground(bckC); } if (tempB.getBean() instanceof BeanCommon) { ((BeanCommon)(tempB.getBean())).setLog(m_logPanel); } } BeanInstance.setBeanInstances(beans, m_beanLayout); BeanConnection.setConnections(connections); m_beanLayout.revalidate(); m_beanLayout.repaint(); } catch (Exception ex) { ex.printStackTrace(); } } m_loadB.setEnabled(true); m_saveB.setEnabled(true); } /** * Serialize the layout to a file */ private void saveLayout() { m_loadB.setEnabled(false); m_saveB.setEnabled(false); int returnVal = m_FileChooser.showSaveDialog(this); java.awt.Color bckC = getBackground(); if (returnVal == JFileChooser.APPROVE_OPTION) { // temporarily remove this panel as a property changle listener from // each bean Vector beans = BeanInstance.getBeanInstances(); for (int i = 0; i < beans.size(); i++) { BeanInstance tempB = (BeanInstance)beans.elementAt(i); if (tempB.getBean() instanceof Visible) { ((Visible)(tempB.getBean())).getVisual(). removePropertyChangeListener(this); // A workaround to account for JPanel's with their default // background colour not being serializable in Apple's JRE. // JComponents are rendered with a funky stripy background // under OS X using java.awt.TexturePaint - unfortunately // TexturePaint doesn't implement Serializable. ((Visible)(tempB.getBean())).getVisual(). setBackground(java.awt.Color.white); ((JComponent)(tempB.getBean())).setBackground(java.awt.Color.white); } } // now serialize components vector and connections vector try { File sFile = m_FileChooser.getSelectedFile(); OutputStream os = new FileOutputStream(sFile); ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(beans); oos.writeObject(BeanConnection.getConnections()); oos.flush(); oos.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { // restore this panel as a property change listener in the beans for (int i = 0; i < beans.size(); i++) { BeanInstance tempB = (BeanInstance)beans.elementAt(i); if (tempB.getBean() instanceof Visible) { ((Visible)(tempB.getBean())).getVisual(). addPropertyChangeListener(this); // Restore the default background colour ((Visible)(tempB.getBean())).getVisual(). setBackground(bckC); ((JComponent)(tempB.getBean())).setBackground(bckC); } } } } m_saveB.setEnabled(true); m_loadB.setEnabled(true); } /** * Main method. * * @param args a <code>String[]</code> value */ public static void main(String [] args) { try { final javax.swing.JFrame jf = new javax.swing.JFrame(); jf.getContentPane().setLayout(new java.awt.BorderLayout()); final KnowledgeFlow tm = new KnowledgeFlow(); jf.getContentPane().add(tm, java.awt.BorderLayout.CENTER); jf.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { jf.dispose(); System.exit(0); } }); jf.setSize(800,600); jf.setVisible(true); } catch (Exception ex) { ex.printStackTrace(); System.err.println(ex.getMessage()); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?