📄 knowledgeflow.java
字号:
private void processPackage(JComponent holderPanel,
String tempBeanCompName,
weka.gui.HierarchyPropertyParser hpp) {
if (hpp.isLeafReached()) {
// instantiate a bean and add it to the holderPanel
// System.err.println("Would add "+hpp.fullValue());
String algName = hpp.fullValue();
JPanel tempBean =
instantiateToolBarBean(true, tempBeanCompName, algName);
if (tempBean != null) {
holderPanel.add(tempBean);
}
hpp.goToParent();
return;
}
String [] children = hpp.childrenValues();
for (int i = 0; i < children.length; i++) {
hpp.goToChild(children[i]);
processPackage(holderPanel, tempBeanCompName, hpp);
}
hpp.goToParent();
}
/**
* Instantiates a bean for display in the toolbars
*
* @param wekawrapper true if the bean to be instantiated is a wekawrapper
* @param tempBeanCompName the name of the bean to instantiate
* @param algName holds the name of a weka algorithm to configure the
* bean with if it is a wekawrapper bean
* @return a JPanel holding the instantiated (and configured bean)
*/
private JPanel instantiateToolBarBean(boolean wekawrapper,
String tempBeanCompName,
String algName) {
Object tempBean;
if (wekawrapper) {
try {
tempBean = Beans.instantiate(null, tempBeanCompName);
} catch (Exception ex) {
System.err.println("Failed to instantiate :"+tempBeanCompName
+"KnowledgeFlow.instantiateToolBarBean()");
return null;
}
if (tempBean instanceof WekaWrapper) {
// algName = (String)tempBarSpecs.elementAt(j);
Class c = null;
try {
c = Class.forName(algName);
} catch (Exception ex) {
System.err.println("Can't find class called: "+algName);
return null;
}
try {
Object o = c.newInstance();
((WekaWrapper)tempBean).setWrappedAlgorithm(o);
} catch (Exception ex) {
System.err.println("Failed to configure "+tempBeanCompName
+" with "+algName);
return null;
}
}
} else {
try {
tempBean = Beans.instantiate(null, tempBeanCompName);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("Failed to instantiate :"+tempBeanCompName
+"KnowledgeFlow.setUpToolBars()");
return null;
}
}
if (tempBean instanceof BeanContextChild) {
m_bcSupport.add(tempBean);
}
if (tempBean instanceof Visible) {
((Visible)tempBean).getVisual().scale(3);
}
// ---------------------------------------
JToggleButton tempButton;
JPanel tempP = new JPanel();
JLabel tempL = new JLabel();
tempL.setFont(new Font("Monospaced", Font.PLAIN, 10));
String labelName = (wekawrapper == true)
? algName
: tempBeanCompName;
labelName = labelName.substring(labelName.lastIndexOf('.')+1,
labelName.length());
tempL.setText(" "+labelName+" ");
tempL.setHorizontalAlignment(JLabel.CENTER);
tempP.setLayout(new BorderLayout());
if (tempBean instanceof Visible) {
BeanVisual bv = ((Visible)tempBean).getVisual();
tempButton =
new JToggleButton(bv.getStaticIcon());
int width = bv.getStaticIcon().getIconWidth();
int height = bv.getStaticIcon().getIconHeight();
JPanel labelPanel =
multiLineLabelPanel(labelName, width);
tempP.add(labelPanel, BorderLayout.SOUTH);
} else {
tempButton = new JToggleButton();
tempP.add(tempL, BorderLayout.SOUTH);
}
tempP.add(tempButton, BorderLayout.NORTH);
// tempP.add(tempL, BorderLayout.SOUTH);
// holderPanel.add(tempP);
// tempToolBar.add(tempP);
m_toolBarGroup.add(tempButton);
// add an action listener for the button here
final String tempName = tempBeanCompName;
final Object tempBN = tempBean;
// final JToggleButton tempButton2 = tempButton;
tempButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
m_toolBarBean = null;
m_toolBarBean = Beans.instantiate(null, tempName);
if (m_toolBarBean instanceof WekaWrapper) {
Object wrappedAlg =
((WekaWrapper)tempBN).getWrappedAlgorithm();
((WekaWrapper)m_toolBarBean).setWrappedAlgorithm(wrappedAlg.getClass().newInstance());
// tempButton2.setSelected(false);
}
setCursor(Cursor.
getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
m_mode = ADDING;
} catch (Exception ex) {
System.err.
println("Problem adding bean to data flow layout");
}
}
});
// set tool tip text from global info if supplied
String summary = getGlobalInfo(tempBean);
if (summary != null) {
int ci = summary.indexOf('.');
if (ci != -1) {
summary = summary.substring(0, ci + 1);
}
tempButton.setToolTipText(summary);
}
//return tempBean;
return tempP;
}
private JPanel multiLineLabelPanel(String sourceL,
int splitWidth) {
JPanel jp = new JPanel();
Vector v = new Vector();
int labelWidth = m_fontM.stringWidth(sourceL);
if (labelWidth < splitWidth) {
v.addElement(sourceL);
} else {
// find mid point
int mid = sourceL.length() / 2;
// look for split point closest to the mid
int closest = sourceL.length();
int closestI = -1;
for (int i = 0; i < sourceL.length(); i++) {
if (sourceL.charAt(i) < 'a') {
if (Math.abs(mid - i) < closest) {
closest = Math.abs(mid - i);
closestI = i;
}
}
}
if (closestI != -1) {
String left = sourceL.substring(0, closestI);
String right = sourceL.substring(closestI, sourceL.length());
if (left.length() > 1 && right.length() > 1) {
v.addElement(left);
v.addElement(right);
} else {
v.addElement(sourceL);
}
} else {
v.addElement(sourceL);
}
}
jp.setLayout(new GridLayout(v.size(), 1));
for (int i = 0; i < v.size(); i++) {
JLabel temp = new JLabel();
temp.setFont(new Font("Monospaced", Font.PLAIN, 10));
temp.setText(" "+((String)v.elementAt(i))+" ");
temp.setHorizontalAlignment(JLabel.CENTER);
jp.add(temp);
}
return jp;
}
/**
* Pop up a help window
*/
private void popupHelp() {
final JButton tempB = m_helpB;
try {
tempB.setEnabled(false);
InputStream inR =
ClassLoader.getSystemResourceAsStream("weka/gui/beans/README_KnowledgeFlow");
StringBuffer helpHolder = new StringBuffer();
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(inR));
String line;
while ((line = lnr.readLine()) != null) {
helpHolder.append(line+"\n");
}
lnr.close();
final javax.swing.JFrame jf = new javax.swing.JFrame();
jf.getContentPane().setLayout(new java.awt.BorderLayout());
final JTextArea ta = new JTextArea(helpHolder.toString());
ta.setFont(new Font("Monospaced", Font.PLAIN, 12));
ta.setEditable(false);
final JScrollPane sp = new JScrollPane(ta);
jf.getContentPane().add(sp, java.awt.BorderLayout.CENTER);
jf.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
tempB.setEnabled(true);
jf.dispose();
}
});
jf.setSize(600,600);
jf.setVisible(true);
} catch (Exception ex) {
tempB.setEnabled(true);
}
}
/**
* Popup a context sensitive menu for the bean component
*
* @param pt holds the panel coordinates for the component
* @param bi the bean component over which the user right clicked the mouse
* @param x the x coordinate at which to popup the menu
* @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).emerateRequests();
if (req.hasMoreElements()) {
beanContextMenu.insert(new JLabel("Actions",
SwingConstants.CENTER),
menuItemCount);
menuItemCount++;
}
while (req.hasMoreElements()) {
String tempS = (String)req.nextElement();
boolean disabled = false;
// check to see if this item is currently disabled
if (tempS.charAt(0) == '$') {
tempS = tempS.substring(1, tempS.length());
disabled = true;
}
final String tempS2 = tempS;
JMenuItem custItem = new JMenuItem(tempS2);
custItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
((UserRequestAcceptor)bc).performRequest(tempS2);
}
});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -