📄 knowledgeflow.java
字号:
if (disabled) {
custItem.setEnabled(false);
}
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
final 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);
if (customizer instanceof CustomizerCloseRequester) {
((CustomizerCloseRequester)customizer).setParentFrame(jf);
}
jf.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
if (customizer instanceof CustomizerClosingListener) {
((CustomizerClosingListener)customizer).customizerClosing();
}
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) {
if (m_toolBarBean instanceof BeanContextChild) {
m_bcSupport.add(m_toolBarBean);
}
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();
m_bcSupport = new BeanContextSupport();
m_bcSupport.setDesignTime(true);
// 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);
}
if (tempB.getBean() instanceof BeanContextChild) {
m_bcSupport.add(tempB.getBean());
}
}
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);
}
/**
* Utility method for grabbing the global info help (if it exists) from
* an arbitrary object
*
* @param tempBean the object to grab global info from
* @return the global help info or null if global info does not exist
*/
public static String getGlobalInfo(Object tempBean) {
// set tool tip text from global info if supplied
String gi = null;
try {
BeanInfo bi = Introspector.getBeanInfo(tempBean.getClass());
MethodDescriptor [] methods = bi.getMethodDescriptors();
for (int i = 0; i < methods.length; i++) {
String name = methods[i].getDisplayName();
Method meth = methods[i].getMethod();
if (name.equals("globalInfo")) {
if (meth.getReturnType().equals(String.class)) {
Object args[] = { };
String globalInfo = (String)(meth.invoke(tempBean, args));
gi = globalInfo;
break;
}
}
}
} catch (Exception ex) {
}
return gi;
}
/**
* Main method.
*
* @param args a <code>String[]</code> value
*/
public static void main(String [] args) {
try { // use system look & feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {}
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -