gui.java

来自「Weka」· Java 代码 · 共 2,050 行 · 第 1/5 页

JAVA
2,050
字号
				JOptionPane.showMessageDialog(null, "Select instances to learn from first (menu Tools/Set Data)");
				return;
			}
			try {
				m_BayesNet.setData(m_Instances);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, "Data set is not compatible with network.\n"+e.getMessage() + "\nChoose other instances (menu Tools/Set Data)");
				return;
			}
			try {
				m_BayesNet.estimateCPTs();
				m_BayesNet.clearUndoStack();
			} catch (Exception e) {
				e.printStackTrace();
			}
			updateStatus();
		} // actionPerformed
	} // class ActionLearnCPT

	class ActionSetData extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -2038911085935519L;

		public ActionSetData() {
			super("Set Data", "Set Data File", "setdata", "ctrl A");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
			ExtensionFileFilter ef1 = new ExtensionFileFilter(".arff", "ARFF files");
			fc.addChoosableFileFilter(ef1);
			fc.setDialogTitle("Set Data File");
			int rval = fc.showOpenDialog(GUI.this);

			if (rval == JFileChooser.APPROVE_OPTION) {
				String filename = fc.getSelectedFile().toString();
				try {
					m_Instances = new Instances(new FileReader(filename));
					m_Instances.setClassIndex(m_Instances.numAttributes() - 1);
					a_learn.setEnabled(true);
					a_learnCPT.setEnabled(true);
					repaint();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	} // class ActionSetData

	class ActionUndo extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -3038910085935519L;

		public ActionUndo() {
			super("Undo", "Undo", "undo", "ctrl Z");
			setEnabled(false);
		} // c'tor

		public boolean isEnabled() {
			return m_BayesNet.canUndo();
		}

		public void actionPerformed(ActionEvent ae) {
			String sMsg = m_BayesNet.undo();
			m_jStatusBar.setText("Undo action performed: " + sMsg);
			//if (!sMsg.equals("")) {
			//	JOptionPane.showMessageDialog(null, sMsg, "Undo action successful", JOptionPane.INFORMATION_MESSAGE);
			//}
			a_redo.setEnabled(m_BayesNet.canRedo());
			a_undo.setEnabled(m_BayesNet.canUndo());
			m_Selection.clear();
			updateStatus();
			repaint();
		}
	} // ActionUndo
	
	class ActionRedo extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -4038910085935519L;

		public ActionRedo() {
			super("Redo", "Redo", "redo", "ctrl Y");
			setEnabled(false);
		} // c'tor

		public boolean isEnabled() {
			return m_BayesNet.canRedo();
		}

		public void actionPerformed(ActionEvent ae) {
			String sMsg = m_BayesNet.redo();
			m_jStatusBar.setText("Redo action performed: " + sMsg);
			//if (!sMsg.equals("")) {
			//	JOptionPane.showMessageDialog(null, sMsg, "Redo action successful", JOptionPane.INFORMATION_MESSAGE);
			//}
			m_Selection.clear();
			updateStatus();
			repaint();
		}
	} // ActionRedo

	class ActionAddNode extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -2038910085935519L;

		public ActionAddNode() {
			super("Add Node", "Add Node", "addnode", "");
		} // c'tor

		JDialog dlg = null;

		JTextField jTfName = new JTextField(20);

		JTextField jTfCard = new JTextField(3);

		int m_X = Integer.MAX_VALUE;
		int m_Y;
		public void addNode(int nX, int nY) {
			m_X = nX;
			m_Y = nY;
			addNode();
		} // addNode
		
		void addNode() {
			if (dlg == null) {
				dlg = new JDialog();
				dlg.setTitle("Add node");
				JLabel jLbName = new JLabel("Name");
				jTfName.setHorizontalAlignment(JTextField.CENTER);
				JLabel jLbCard = new JLabel("Cardinality");
				jTfCard.setHorizontalAlignment(JTextField.CENTER);
				jTfCard.setText("2");

				JButton jBtCancel;
				jBtCancel = new JButton("Cancel");
				jBtCancel.setMnemonic('C');
				jBtCancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						dlg.setVisible(false);
					}
				});
				JButton jBtOk = new JButton("Ok");
				jBtOk.setMnemonic('O');
				jBtOk.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						String sName = jTfName.getText();
						if (sName.length() <= 0) {
							JOptionPane.showMessageDialog(null, "Name should have at least one character");
							return;
						}
						int nCard = new Integer(jTfCard.getText()).intValue();
						if (nCard <= 1) {
							JOptionPane.showMessageDialog(null, "Cardinality should be larger than 1");
							return;
						}
						try {
							if (m_X < Integer.MAX_VALUE) {
								m_BayesNet.addNode(sName, nCard, m_X, m_Y);
							} else {
								m_BayesNet.addNode(sName, nCard);
							}
						    m_jStatusBar.setText(m_BayesNet.lastActionMsg());
							a_undo.setEnabled(true);
							a_redo.setEnabled(false);
							//GraphNode n = new GraphNode("id" + m_nodes.size(), sName);
							//n.probs = m_BayesNet.getDistribution(sName);
							//n.outcomes = m_BayesNet.getValues(sName);
							//n.x = 100 + m_nodes.size() * 10;
							//n.y = 100 + m_nodes.size() * 10;
							//m_nodes.addElement(n);
						} catch (Exception e) {
							e.printStackTrace();
						}
						repaint();
						dlg.setVisible(false);
					}
				});
				dlg.setLayout(new GridLayout(3, 2, 10, 10));
				dlg.add(jLbName);
				dlg.add(jTfName);
				dlg.add(jLbCard);
				dlg.add(jTfCard);
				dlg.add(jBtOk);
				dlg.add(jBtCancel);
				dlg.setSize(dlg.getPreferredSize());
			}
			jTfName.setText("Node" + (m_BayesNet.getNrOfNodes() + 1));
			dlg.setVisible(true);
		} // addNode
		
		public void actionPerformed(ActionEvent ae) {
			m_X = Integer.MAX_VALUE;
			addNode();
		}
	} // class ActionAddNode

	class ActionDeleteNode extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -2038912085935519L;

		public ActionDeleteNode() {
			super("Delete Node", "Delete Node", "delnode", "DELETE");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			if (m_Selection.getSelected().size() > 0) {
				m_BayesNet.deleteSelection(m_Selection.getSelected());
			    m_jStatusBar.setText(m_BayesNet.lastActionMsg());
				m_Selection.clear();
				updateStatus();
				repaint();
			} else {
				String[] options = new String[m_BayesNet.getNrOfNodes()];
				for (int i = 0; i < options.length; i++) {
					options[i] = m_BayesNet.getNodeName(i);
				}
				String sResult = (String) JOptionPane.showInputDialog(null, "Select node to delete", "Nodes", 0, null,
						options, options[0]);
				if (sResult != null && !sResult.equals("")) {
					int iNode = m_BayesNet.getNode2(sResult);
					deleteNode(iNode);
				}
			}
		}
	} // class ActionDeleteNode

	class ActionCopyNode extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -2038732085935519L;

		public ActionCopyNode() {
			super("Copy", "Copy Nodes", "copy", "ctrl C");
		} // c'tor
		
		public ActionCopyNode(String sName, String sToolTipText, String sIcon, String sAcceleratorKey) {
			super(sName, sToolTipText, sIcon, sAcceleratorKey);
		} // c'rot
		
		public void actionPerformed(ActionEvent ae) {
			copy();
		}
		
		public void copy() {
			String sXML = m_BayesNet.toXMLBIF03(m_Selection.getSelected());
			m_clipboard.setText(sXML);
		} // copy
	} // class ActionCopyNode

	class ActionCutNode extends ActionCopyNode {
		/** for serialization */
		private static final long serialVersionUID = -2038822085935519L;

		public ActionCutNode() {
			super("Cut", "Cut Nodes", "cut", "ctrl X");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			copy();
			m_BayesNet.deleteSelection(m_Selection.getSelected());
		    m_jStatusBar.setText(m_BayesNet.lastActionMsg());
			m_Selection.clear();
			a_undo.setEnabled(true);
			a_redo.setEnabled(false);
			repaint();
		}
	} // class ActionCutNode

	class ActionPasteNode extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -2038732085935519L;

		public ActionPasteNode() {
			super("Paste", "Paste Nodes", "paste", "ctrl V");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			try {
				m_BayesNet.paste(m_clipboard.getText());
				updateStatus();
			    m_jStatusBar.setText(m_BayesNet.lastActionMsg());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		public boolean isEnabled() {
			return m_clipboard.hasText();
		}
	} // class ActionPasteNode

	class ActionSelectAll extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -2038642085935519L;

		public ActionSelectAll() {
			super("Select All", "Select All Nodes", "selectall", "ctrl A");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			m_Selection.selectAll();
			repaint();
		}
	} // class ActionSelectAll

	class ActionExport extends MyAction {
		boolean m_bIsExporting = false;		
		/** for serialization */
		private static final long serialVersionUID = -3027642085935519L;

		public ActionExport() {
			super("Export", "Export to graphics file", "export", "");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			m_bIsExporting = true;
			m_GraphPanel.saveComponent();
			m_bIsExporting = false;
			repaint();
		}
		public boolean isExporting() {return m_bIsExporting;}
	} // class ActionExport

	class ActionAlignLeft extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -3138642085935519L;

		public ActionAlignLeft() {
			super("Align Left", "Align Left", "alignleft", "");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			m_BayesNet.alignLeft(m_Selection.getSelected());
		    m_jStatusBar.setText(m_BayesNet.lastActionMsg());
			a_undo.setEnabled(true);
			a_redo.setEnabled(false);
			repaint();
		}
	} // class ActionAlignLeft
	
	class ActionAlignRight extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -4238642085935519L;

		public ActionAlignRight() {
			super("Align Right", "Align Right", "alignright", "");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			m_BayesNet.alignRight(m_Selection.getSelected());
		    m_jStatusBar.setText(m_BayesNet.lastActionMsg());
			a_undo.setEnabled(true);
			a_redo.setEnabled(false);
			repaint();
		}
	} // class ActionAlignRight

	class ActionAlignTop extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -5338642085935519L;

		public ActionAlignTop() {
			super("Align Top", "Align Top", "aligntop", "");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			m_BayesNet.alignTop(m_Selection.getSelected());
		    m_jStatusBar.setText(m_BayesNet.lastActionMsg());
			a_undo.setEnabled(true);
			a_redo.setEnabled(false);
			repaint();
		}
	} // class ActionAlignTop

	class ActionAlignBottom extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -6438642085935519L;

		public ActionAlignBottom() {
			super("Align Bottom", "Align Bottom", "alignbottom", "");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			m_BayesNet.alignBottom(m_Selection.getSelected());
		    m_jStatusBar.setText(m_BayesNet.lastActionMsg());
			a_undo.setEnabled(true);
			a_redo.setEnabled(false);
			repaint();
		}
	} // class ActionAlignBottom

	class ActionCenterHorizontal extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -7538642085935519L;

		public ActionCenterHorizontal() {
			super("Center Horizontal", "Center Horizontal", "centerhorizontal", "");
		} // c'tor

		public void actionPerformed(ActionEvent ae) {
			m_BayesNet.centerHorizontal(m_Selection.getSelected());
		    m_jStatusBar.setText(m_BayesNet.lastActionMsg());
			a_undo.setEnabled(true);
			a_redo.setEnabled(false);
			repaint();
		}
	} // class ActionCenterHorizontal

	class ActionCenterVertical extends MyAction {
		/** for serialization */
		private static final long serialVersionUID = -8638642085935519L;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?