📄 guimanager.java
字号:
/* jahmm package - v0.3.1 *//* * Copyright (c) 2004, Jean-Marc Francois. * * This file is part of Jahmm. * Jahmm is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Jahmm is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jahmm; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package be.ac.ulg.montefiore.run.jahmm.apps;import java.io.*;import java.util.*;import java.awt.*;import java.text.*;import java.awt.event.*;import javax.swing.*;import java.lang.reflect.Array;import java.beans.*;import be.ac.ulg.montefiore.run.jahmm.*;import be.ac.ulg.montefiore.run.jahmm.io.*;import be.ac.ulg.montefiore.run.jahmm.gui.*;/* * This class performs GUI-related operations. */public class GuiManager implements ActionListener, WindowListener { static private GuiManager instance = null; final protected Dialogs dialogs; final protected JFrame mainFrame; final protected MainPanel mainPanel; final protected Map<Window,GuiComponent> map; final protected Map<Object,Window> rmap; private GuiManager() { mainFrame = new JFrame("JahmmViz " + JahmmViz.VERSION); dialogs = new Dialogs(mainFrame); mainPanel = new MainPanel(this); mainFrame.getContentPane().add(mainPanel); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.pack(); mainFrame.setVisible(true); mainFrame.setResizable(false); map = new HashMap<Window,GuiComponent>(); rmap = new HashMap<Object,Window>(); } /* * Returns the (only) instance of this class. */ static public GuiManager instance() { if (instance == null) instance = new GuiManager(); return instance; } /* * Draws a new window depicting a set of sequences. */ public void newWindow(ObservationSequences sequences) { JFrame sFrame = new JFrame(sequences.name); ObservationSequencesComponent observationSequencesComponent = new ObservationSequencesComponent(sequences); map.put(sFrame, observationSequencesComponent); rmap.put(sequences, sFrame); sFrame.getContentPane().add(new JScrollPane(observationSequencesComponent)); sFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); sFrame.addWindowListener(this); sFrame.pack(); sFrame.setVisible(true); } /* * Draws a new window depicting a set of sequences. */ public void newWindow(StateSequence sequence) { JFrame sFrame = new JFrame(sequence.name); StateSequenceComponent stateSequenceComponent = new StateSequenceComponent(sequence); map.put(sFrame, stateSequenceComponent); rmap.put(sequence, sFrame); sFrame.getContentPane().add(stateSequenceComponent); sFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); sFrame.addWindowListener(this); sFrame.pack(); sFrame.setVisible(true); } /* * Draws a new window depicting a set of sequences. */ public void newWindow(Hmm hmm) { JFrame hFrame = new JFrame(hmm.name); HmmComponent hmmComponent = new HmmComponent(hmm); map.put(hFrame, hmmComponent); rmap.put(hmm, hFrame); hFrame.getContentPane().add(hmmComponent); hFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); hFrame.addWindowListener(this); hFrame.pack(); hFrame.setVisible(true); } /* * Responds to button press. */ public void actionPerformed(ActionEvent e) { if ("Read sequences".equals(e.getActionCommand())) { readSequences(); } else if ("Write sequences".equals(e.getActionCommand())) { showMessage("This has not been implemented yet."); } else if ("Read HMM".equals(e.getActionCommand())) { readHmm(); } else if ("Write HMM".equals(e.getActionCommand())) { if (OperationsManager.instance().hmms().size() == 0) { showMessage("No HMM available."); return; } Hmm hmm = dialogs.chooseOpenHmm(); if (hmm != null) writeHmm(hmm); } else if ("Learn".equals(e.getActionCommand())) { ObservationSequences sequences = dialogs.chooseOpenSequences(); if (sequences != null) { LearningParametersDialog.LearningParameters parameters = dialogs.chooseLearningParameters(sequences.type, sequences.dimension); if (parameters != null) { OpdfFactory opdfFactory = (sequences.type == ObservationSequences.INTEGER) ? new OpdfIntegerFactory(sequences.dimension) : new OpdfMultiGaussianFactory(sequences.dimension); Hmm hmm; try { hmm = ComputationManager.learn("Model of " + sequences.name, sequences, opdfFactory, parameters.nbStates); } catch(Exception exception) { showMessage("HMM learning has triggered an " + "exception: \n" + exception + "\n\n Learning canceled."); return; } OperationsManager.instance().addObject(hmm); } } } else if ("Most Likely State Sequence".equals(e.getActionCommand())) { ObservationSequences sequences; Hmm hmm; if ((sequences = dialogs.chooseOpenSequences()) == null || (hmm = dialogs.chooseOpenHmm()) == null) return; int[] selectedSequences = ((ObservationSequencesComponent) map. get(rmap.get(sequences))).selected(); if (selectedSequences.length != 1) { showMessage("One (and only one) sequence must be selected."); return; } StateSequence stateSequence = new StateSequence("MLSS of " + sequences.name + "[" + selectedSequences[0] + "] for " + hmm, ComputationManager. mostLikelySequence(hmm, sequences. get(selectedSequences[0]))); OperationsManager.instance().addObject(stateSequence); } else if ("Probability".equals(e.getActionCommand())) { ObservationSequences sequences; Hmm hmm; if ((sequences = dialogs.chooseOpenSequences()) == null || (hmm = dialogs.chooseOpenHmm()) == null) return; int[] selectedSequences = ((ObservationSequencesComponent) map. get(rmap.get(sequences))).selected(); if (selectedSequences.length != 1) { showMessage("One (and only one) sequence must be selected."); return; } double p, lnp; try { p = ComputationManager. probability(hmm, sequences.get(selectedSequences[0])); lnp = ComputationManager. lnProbability(hmm, sequences.get(selectedSequences[0])); } catch(Exception exception) { showMessage("An exception has been triggered while computing " + "sequence probability :\n" + exception); return; } showMessage("Let p denote de probability of the observation\n" + "sequence for the chosen HMM.\n\n" + "p = " + p + "\nln(p) = " + lnp); } else if ("Generate Sequences".equals(e.getActionCommand())) { Hmm hmm = dialogs.chooseOpenHmm(); if (hmm == null) return; ObservationSequences sequences = ComputationManager. generateSequences("Sequences generated by " + hmm.name, hmm, 5, 20); OperationsManager.instance().addObject(sequences); } return; } private void readSequences() { File file = dialogs.chooseFileToOpen(); if (file != null) try { ObservationDescriptionDialog.ObservationDescription description; if ((description = dialogs.observationDescription()) == null) return; FileInputStream fis = new FileInputStream(file); OperationsManager.instance(). readSequences(file.toString(), description.type, description.dimension, fis); fis.close(); } catch(FileNotFoundException fnfe) { showMessage("File not found."); return; } catch(IOException ioe) { showMessage("Error reading file (" + ioe + ")."); return; } } private void readHmm() { File file = dialogs.chooseFileToOpen(); if (file != null) { Hmm hmm; try { FileInputStream fis = new FileInputStream(file); OperationsManager.instance().readHmm(file.toString(), fis); fis.close(); } catch(FileNotFoundException fnfe) { showMessage("File not found."); return; } catch(IOException ioe) { showMessage("Error reading file (" + ioe + ")."); return; } } } private void writeHmm(Hmm hmm) { File file = dialogs.chooseFileToSave(); if (file != null) { try { FileOutputStream fos = new FileOutputStream(file); OperationsManager.instance().writeHmm(fos, hmm); fos.close(); } catch(FileNotFoundException fnfe) { showMessage("File not found."); return; } catch(IOException ioe) { showMessage("Error saving file (" + ioe + ")."); return; } } } /* * Called whenever a window closes. */ public void windowClosing(WindowEvent e) { GuiComponent component = map.remove(e.getWindow()); if (component instanceof HmmComponent) { Hmm hmm = (Hmm) ((HmmComponent) component).embeddedObject(); rmap.remove(hmm); OperationsManager.instance().removeObject(hmm); } else if (component instanceof StateSequenceComponent) { StateSequence sequence = (StateSequence) ((StateSequenceComponent) component).embeddedObject(); rmap.remove(sequence); OperationsManager.instance().removeObject(sequence); } else if (component instanceof ObservationSequencesComponent) { ObservationSequences sequence = (ObservationSequences) ((ObservationSequencesComponent) component).embeddedObject(); rmap.remove(component); OperationsManager.instance().removeObject(sequence); } else throw new RuntimeException("Internal error"); } public void windowClosed(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public void showMessage(String message) { dialogs.showMessage(message); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -