gui.java
来自「Weka」· Java 代码 · 共 2,050 行 · 第 1/5 页
JAVA
2,050 行
a_zoomin.setEnabled(true);
m_jTfZoom.setText(m_nZoomPercents[22] + "%");
m_fScale = m_nZoomPercents[22] / 100D;
}
setAppropriateSize();
m_GraphPanel.repaint();
m_GraphPanel.invalidate();
m_jScrollPane.revalidate();
m_jStatusBar.setText("Zooming out");
}
} // class ActionZoomOut
class ActionLayout extends MyAction {
/** for serialization */
private static final long serialVersionUID = -203891108593551L;
public ActionLayout() {
super("Layout", "Layout Graph", "layout", "ctrl L");
} // c'tor
JDialog dlg = null;
public void actionPerformed(ActionEvent ae) {
if (dlg == null) {
dlg = new JDialog();
dlg.setTitle("Graph Layout Options");
final JCheckBox jCbCustomNodeSize = new JCheckBox("Custom Node Size");
final JLabel jLbNodeWidth = new JLabel("Width");
final JLabel jLbNodeHeight = new JLabel("Height");
m_jTfNodeWidth.setHorizontalAlignment(JTextField.CENTER);
m_jTfNodeWidth.setText("" + m_nNodeWidth);
m_jTfNodeHeight.setHorizontalAlignment(JTextField.CENTER);
m_jTfNodeHeight.setText("" + m_nNodeHeight);
jLbNodeWidth.setEnabled(false);
m_jTfNodeWidth.setEnabled(false);
jLbNodeHeight.setEnabled(false);
m_jTfNodeHeight.setEnabled(false);
jCbCustomNodeSize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (((JCheckBox) ae.getSource()).isSelected()) {
jLbNodeWidth.setEnabled(true);
m_jTfNodeWidth.setEnabled(true);
jLbNodeHeight.setEnabled(true);
m_jTfNodeHeight.setEnabled(true);
} else {
jLbNodeWidth.setEnabled(false);
m_jTfNodeWidth.setEnabled(false);
jLbNodeHeight.setEnabled(false);
m_jTfNodeHeight.setEnabled(false);
setAppropriateSize();
setAppropriateNodeSize();
}
}
});
JButton jBtLayout;
jBtLayout = new JButton("Layout Graph");
jBtLayout.setMnemonic('L');
jBtLayout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int tmpW, tmpH;
if (jCbCustomNodeSize.isSelected()) {
try {
tmpW = Integer.parseInt(m_jTfNodeWidth.getText());
} catch (NumberFormatException ne) {
JOptionPane.showMessageDialog(GUI.this.getParent(),
"Invalid integer entered for node width.", "Error", JOptionPane.ERROR_MESSAGE);
tmpW = m_nNodeWidth;
m_jTfNodeWidth.setText("" + m_nNodeWidth);
}
try {
tmpH = Integer.parseInt(m_jTfNodeHeight.getText());
} catch (NumberFormatException ne) {
JOptionPane.showMessageDialog(GUI.this.getParent(),
"Invalid integer entered for node height.", "Error", JOptionPane.ERROR_MESSAGE);
tmpH = m_nNodeHeight;
m_jTfNodeWidth.setText("" + m_nNodeHeight);
}
if (tmpW != m_nNodeWidth || tmpH != m_nNodeHeight) {
m_nNodeWidth = tmpW;
m_nPaddedNodeWidth = m_nNodeWidth + PADDING;
m_nNodeHeight = tmpH;
}
}
// JButton bt = (JButton) ae.getSource();
// bt.setEnabled(false);
dlg.setVisible(false);
updateStatus();
layoutGraph();
m_jStatusBar.setText("Laying out Bayes net");
}
});
JButton jBtCancel;
jBtCancel = new JButton("Cancel");
jBtCancel.setMnemonic('C');
jBtCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dlg.setVisible(false);
}
});
GridBagConstraints gbc = new GridBagConstraints();
dlg.setLayout(new GridBagLayout());
//dlg.add(m_le.getControlPanel());
Container c = new Container();
c.setLayout(new GridBagLayout());
gbc.gridwidth = 1;
gbc.insets = new Insets(8, 0, 0, 0);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
c.add(jCbCustomNodeSize, gbc);
gbc.gridwidth = GridBagConstraints.RELATIVE;
c.add(jLbNodeWidth, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
c.add(m_jTfNodeWidth, gbc);
gbc.gridwidth = GridBagConstraints.RELATIVE;
c.add(jLbNodeHeight, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
c.add(m_jTfNodeHeight, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
dlg.add(c, gbc);
dlg.add(jBtLayout);
gbc.gridwidth = GridBagConstraints.REMAINDER;
dlg.add(jBtCancel);
}
dlg.setLocation(100, 100);
dlg.setVisible(true);
dlg.setSize(dlg.getPreferredSize());
dlg.setVisible(false);
dlg.setVisible(true);
dlg.repaint();
}
} // class ActionLayout
/**
* Constructor<br>
* Sets up the gui and initializes all the other previously uninitialized
* variables.
*/
public GUI() {
m_GraphPanel = new GraphPanel();
m_jScrollPane = new JScrollPane(m_GraphPanel);
// creating a new layout engine and adding this class as its listener
// to receive layoutComplete events
m_jTfZoom = new JTextField("100%");
m_jTfZoom.setMinimumSize(m_jTfZoom.getPreferredSize());
m_jTfZoom.setHorizontalAlignment(JTextField.CENTER);
m_jTfZoom.setToolTipText("Zoom");
m_jTfZoom.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JTextField jt = (JTextField) ae.getSource();
try {
int i = -1;
i = jt.getText().indexOf('%');
if (i == -1)
i = Integer.parseInt(jt.getText());
else
i = Integer.parseInt(jt.getText().substring(0, i));
if (i <= 999)
m_fScale = i / 100D;
jt.setText((int) (m_fScale * 100) + "%");
if (m_fScale > 0.1) {
if (!a_zoomout.isEnabled())
a_zoomout.setEnabled(true);
} else
a_zoomout.setEnabled(false);
if (m_fScale < 9.99) {
if (!a_zoomin.isEnabled())
a_zoomin.setEnabled(true);
} else
a_zoomin.setEnabled(false);
setAppropriateSize();
// m_GraphPanel.clearBuffer();
m_GraphPanel.repaint();
m_GraphPanel.invalidate();
m_jScrollPane.revalidate();
} catch (NumberFormatException ne) {
JOptionPane.showMessageDialog(GUI.this.getParent(),
"Invalid integer entered for zoom.", "Error", JOptionPane.ERROR_MESSAGE);
jt.setText((m_fScale * 100) + "%");
}
}
});
GridBagConstraints gbc = new GridBagConstraints();
final JPanel p = new JPanel(new GridBagLayout());
p.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("ExtraControls"), BorderFactory
.createEmptyBorder(4, 4, 4, 4)));
p.setPreferredSize(new Dimension(0, 0));
m_jTbTools = new JToolBar();
m_jTbTools.setFloatable(false);
m_jTbTools.setLayout(new GridBagLayout());
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(0, 0, 0, 0);
m_jTbTools.add(p, gbc);
gbc.gridwidth = 1;
m_jTbTools.add(a_new);
m_jTbTools.add(a_save);
m_jTbTools.add(a_load);
m_jTbTools.addSeparator(new Dimension(2, 2));
m_jTbTools.add(a_cutnode);
m_jTbTools.add(a_copynode);
m_jTbTools.add(a_pastenode);
m_jTbTools.addSeparator(new Dimension(2, 2));
m_jTbTools.add(a_undo);
m_jTbTools.add(a_redo);
m_jTbTools.addSeparator(new Dimension(2, 2));
m_jTbTools.add(a_alignleft);
m_jTbTools.add(a_alignright);
m_jTbTools.add(a_aligntop);
m_jTbTools.add(a_alignbottom);
m_jTbTools.add(a_centerhorizontal);
m_jTbTools.add(a_centervertical);
m_jTbTools.add(a_spacehorizontal);
m_jTbTools.add(a_spacevertical);
m_jTbTools.addSeparator(new Dimension(2, 2));
m_jTbTools.add(a_zoomin);
gbc.fill = GridBagConstraints.VERTICAL;
gbc.weighty = 1;
JPanel p2 = new JPanel(new BorderLayout());
p2.setPreferredSize(m_jTfZoom.getPreferredSize());
p2.setMinimumSize(m_jTfZoom.getPreferredSize());
p2.add(m_jTfZoom, BorderLayout.CENTER);
m_jTbTools.add(p2, gbc);
gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
m_jTbTools.add(a_zoomout);
m_jTbTools.addSeparator(new Dimension(2, 2));
// jTbTools.add(jBtExtraControls, gbc);
m_jTbTools.add(a_layout);
m_jTbTools.addSeparator(new Dimension(4, 2));
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
//jTbTools.add(m_layoutEngine.getProgressBar(), gbc);
m_jStatusBar = new JLabel("Status bar");
this.setLayout(new BorderLayout());
this.add(m_jTbTools, BorderLayout.NORTH);
this.add(m_jScrollPane, BorderLayout.CENTER);
this.add(m_jStatusBar, BorderLayout.SOUTH);
updateStatus();
a_datagenerator.setEnabled(false);
}
/**
* This method sets the node size that is appropriate considering the
* maximum label size that is present. It is used internally when custom
* node size checkbox is unchecked.
*/
protected void setAppropriateNodeSize() {
int strWidth;
FontMetrics fm = this.getFontMetrics(this.getFont());
int nMaxStringWidth = DEFAULT_NODE_WIDTH;
if (nMaxStringWidth == 0)
for (int iNode = 0; iNode < m_BayesNet.getNrOfNodes(); iNode++) {
strWidth = fm.stringWidth(m_BayesNet.getNodeName(iNode));
if (strWidth > nMaxStringWidth)
nMaxStringWidth = strWidth;
}
m_nNodeWidth = nMaxStringWidth + 4;
m_nPaddedNodeWidth = m_nNodeWidth + PADDING;
m_jTfNodeWidth.setText("" + m_nNodeWidth);
m_nNodeHeight = 2 * fm.getHeight();
m_jTfNodeHeight.setText("" + m_nNodeHeight);
}
/**
* Sets the preferred size for m_GraphPanel GraphPanel to the minimum size that is
* neccessary to display the graph.
*/
public void setAppropriateSize() {
int maxX = 0, maxY = 0;
m_GraphPanel.setScale(m_fScale, m_fScale);
for (int iNode = 0; iNode < m_BayesNet.getNrOfNodes(); iNode++) {
int nPosX = m_BayesNet.getPositionX(iNode);
int nPosY = m_BayesNet.getPositionY(iNode);
if (maxX < nPosX)
maxX = nPosX + 100;
if (maxY < nPosY)
maxY = nPosY;
}
m_GraphPanel.setPreferredSize(new Dimension((int) ((maxX + m_nPaddedNodeWidth + 2) * m_fScale),
(int) ((maxY + m_nNodeHeight + 2) * m_fScale)));
m_GraphPanel.revalidate();
} // setAppropriateSize
/**
* This method is an implementation for LayoutCompleteEventListener class.
* It sets the size appropriate for m_GraphPanel GraphPanel and and revalidates it's
* container JScrollPane once a LayoutCompleteEvent is received from the
* LayoutEngine. Also, it updates positions of the Bayesian network stored
* in m_BayesNet.
*/
public void layoutCompleted(LayoutCompleteEvent le) {
LayoutEngine layoutEngine = m_layoutEngine; // (LayoutEngine) le.getSource();
FastVector nPosX = new FastVector(m_BayesNet.getNrOfNodes());
FastVector nPosY = new FastVector(m_BayesNet.getNrOfNodes());
for (int iNode = 0; iNode < layoutEngine.getNodes().size(); iNode++) {
GraphNode gNode = (GraphNode) layoutEngine.getNodes().elementAt(iNode);
if (gNode.nodeType == GraphNode.NORMAL) {
nPosX.addElement(gNode.x);
nPosY.addElement(gNode.y);
}
}
m_BayesNet.layoutGraph(nPosX, nPosY);
m_jStatusBar.setText("Graph layed out");
a_undo.setEnabled(true);
a_redo.setEnabled(false);
setAppropriateSize();
m_GraphPanel.invalidate();
m_jScrollPane.revalidate();
m_GraphPanel.repaint();
} // layoutCompleted
/**
* BIF reader<br>
* Reads a graph description in XMLBIF03 from an file
* with name sFileName
*/
public void readBIFFromFile(String sFileName) throws BIFFormatException, IOException {
m_sFileName = sFileName;
try {
BIFReader bayesNet = new BIFReader();
bayesNet.processFile(sFileName);
m_BayesNet = new EditableBayesNet(bayesNet);
updateStatus();
a_datagenerator.setEnabled(m_BayesNet.getNrOfNodes() > 0);
m_BayesNet.clearUndoStack();
} catch (Exception ex) {
ex.printStackTrace();
return;
}
setAppropriateNodeSize();
setAppropriateSize();
} // readBIFFromFile
/* read arff file from file sFileName
* and start new Bayesian network with nodes
* representing attributes in data set.
*/
void initFromArffFile(String sFileName) {
try {
Instances instances = new Instances(new FileReader(sFileName));
m_BayesNet = new EditableBayesNet(instances);
m_Instances = instances;
a_learn.setEnabled(true);
a_learnCPT.setEnabled(true);
setAppropriateNodeSize();
setAppropriateSize();
} catch (Exception ex) {
ex.printStackTrace();
return;
}
} // initFromArffFile
/**
* The panel which contains the actual Bayeian network.
*/
private class GraphPanel extends PrintablePanel implements Printable {
/** for serialization */
private static final long serialVersionUID = -3562813603236753173L;
/** node drawing modes */
final static int HIGHLIGHTED = 1;
final static int NORMAL = 0;
public GraphPanel() {
super();
this.addMouseListener(new GraphVisualizerMouseListener());
this.addMouseMotionListener(new GraphVisualizerMouseMotionListener());
this.setToolTipText("");
} // c'tor
/* For showing instructions when hovering over a node
* (non-Javadoc)
* @see javax.swing.JComponent#getToolTipText(java.awt.event.MouseEvent)
*/
public String getToolTipText(MouseEvent me) {
int x, y;
Rectangle r;
x = y
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?