📄 preprocesspanel.java
字号:
final JPanel panel = new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.RIGHT)); textData.setEditable(false); textData.setFont( new Font("Monospaced", Font.PLAIN, textData.getFont().getSize())); saveButton.setMnemonic('S'); saveButton.setToolTipText("Saves the output to a file"); saveButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ JFileChooser filechooser = new JFileChooser(); int result = filechooser.showSaveDialog(dialog); if (result == JFileChooser.APPROVE_OPTION) { try { BufferedWriter writer = new BufferedWriter( new FileWriter( filechooser.getSelectedFile())); writer.write(textData.getText()); writer.flush(); writer.close(); JOptionPane.showMessageDialog( dialog, "Output successfully saved to file '" + filechooser.getSelectedFile() + "'!", "Information", JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { e.printStackTrace(); } dialog.dispose(); } } }); closeButton.setMnemonic('C'); closeButton.setToolTipText("Closes the dialog"); closeButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ dialog.dispose(); } }); panel.add(saveButton); panel.add(closeButton); dialog.setTitle("Generated Instances (incl. comments)"); dialog.getContentPane().add(new JScrollPane(textData), BorderLayout.CENTER); dialog.getContentPane().add(panel, BorderLayout.SOUTH); dialog.pack(); // make sure, it's not bigger than 80% of the screen Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int width = dialog.getWidth() > screen.getWidth()*0.8 ? (int) (screen.getWidth()*0.8) : dialog.getWidth(); int height = dialog.getHeight() > screen.getHeight()*0.8 ? (int) (screen.getHeight()*0.8) : dialog.getHeight(); dialog.setSize(width, height); // display dialog dialog.setVisible(true); } /** * Pops up generic object editor with list of conversion filters * * @param f the File */ private void converterQuery(final File f) { final GenericObjectEditor convEd = new GenericObjectEditor(true); try { convEd.setClassType(weka.core.converters.Loader.class); convEd.setValue(new weka.core.converters.CSVLoader()); ((GenericObjectEditor.GOEPanel)convEd.getCustomEditor()) .addOkListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tryConverter((Loader)convEd.getValue(), f); } }); } catch (Exception ex) { } PropertyDialog pd = new PropertyDialog(convEd, 100, 100); } /** * Applies the selected converter * * @param cnv the converter to apply to the input file * @param f the input file */ private void tryConverter(final Loader cnv, final File f) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { cnv.setSource(f); Instances inst = cnv.getDataSet(); setInstances(inst); } catch (Exception ex) { m_Log.statusMessage(cnv.getClass().getName()+" failed to load " +f.getName()); JOptionPane.showMessageDialog(PreprocessPanel.this, cnv.getClass().getName()+" failed to load '" + f.getName() + "'.\n" + "Reason:\n" + ex.getMessage(), "Convert File", JOptionPane.ERROR_MESSAGE); m_IOThread = null; converterQuery(f); } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } } /** * Loads results from a set of instances retrieved with the supplied loader. * This is started in the IO thread, and a dialog is popped up * if there's a problem. * * @param loader the loader to use */ public void setInstancesFromFile(final AbstractFileLoader loader) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { m_Log.statusMessage("Reading from file..."); Instances inst = loader.getDataSet(); setInstances(inst); } catch (Exception ex) { m_Log.statusMessage( "File '" + loader.retrieveFile() + "' not recognised as an '" + loader.getFileDescription() + "' file."); m_IOThread = null; if (JOptionPane.showOptionDialog(PreprocessPanel.this, "File '" + loader.retrieveFile() + "' not recognised as an '" + loader.getFileDescription() + "' file.\n" + "Reason:\n" + ex.getMessage(), "Load Instances", 0, JOptionPane.ERROR_MESSAGE, null, new String[] {"OK", "Use Converter"}, null) == 1) { converterQuery(loader.retrieveFile()); } } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } else { JOptionPane.showMessageDialog(this, "Can't load at this time,\n" + "currently busy with other IO", "Load Instances", JOptionPane.WARNING_MESSAGE); } } /** * Loads instances from a database * * @param iq the InstanceQuery object to load from (this is assumed * to have been already connected to a valid database). */ public void setInstancesFromDB(final InstanceQuery iq) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { m_Log.statusMessage("Reading from database..."); final Instances i = iq.retrieveInstances(); SwingUtilities.invokeAndWait(new Runnable() { public void run() { setInstances(new Instances(i)); } }); iq.disconnectFromDatabase(); } catch (Exception ex) { m_Log.statusMessage("Problem executing DB query "+m_SQLQ); JOptionPane.showMessageDialog(PreprocessPanel.this, "Couldn't read from database:\n" + ex.getMessage(), "Load Instances", JOptionPane.ERROR_MESSAGE); } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } else { JOptionPane.showMessageDialog(this, "Can't load at this time,\n" + "currently busy with other IO", "Load Instances", JOptionPane.WARNING_MESSAGE); } } /** * Loads instances from a URL. * * @param u the URL to load from. */ public void setInstancesFromURL(final URL u) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { m_Log.statusMessage("Reading from URL..."); AbstractFileLoader loader = ConverterUtils.getURLLoaderForFile(u.toString()); if (loader == null) throw new Exception("No suitable URLSourcedLoader found for URL!\n" + u); ((URLSourcedLoader) loader).setURL(u.toString()); setInstances(loader.getDataSet()); } catch (Exception ex) { ex.printStackTrace(); m_Log.statusMessage("Problem reading " + u); JOptionPane.showMessageDialog(PreprocessPanel.this, "Couldn't read from URL:\n" + u + "\n" + ex.getMessage(), "Load Instances", JOptionPane.ERROR_MESSAGE); } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } else { JOptionPane.showMessageDialog(this, "Can't load at this time,\n" + "currently busy with other IO", "Load Instances", JOptionPane.WARNING_MESSAGE); } } /** * Backs up the current state of the dataset, so the changes can be undone. * * @throws Exception if an error occurs */ public void addUndoPoint() throws Exception { if (m_Instances != null) { // create temporary file File tempFile = File.createTempFile("weka", SerializedInstancesLoader.FILE_EXTENSION); tempFile.deleteOnExit(); ObjectOutputStream oos = new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream(tempFile))); oos.writeObject(m_Instances); oos.flush(); oos.close(); // update undo file list if (m_tempUndoFiles[m_tempUndoIndex] != null) { // remove undo points that are too old m_tempUndoFiles[m_tempUndoIndex].delete(); } m_tempUndoFiles[m_tempUndoIndex] = tempFile; if (++m_tempUndoIndex >= m_tempUndoFiles.length) { // wrap pointer around m_tempUndoIndex = 0; } m_UndoBut.setEnabled(true); } } /** * Reverts to the last backed up version of the dataset. */ public void undo() { if (--m_tempUndoIndex < 0) { // wrap pointer around m_tempUndoIndex = m_tempUndoFiles.length-1; } if (m_tempUndoFiles[m_tempUndoIndex] != null) { // load instances from the temporary file AbstractFileLoader loader = ConverterUtils.getLoaderForFile(m_tempUndoFiles[m_tempUndoIndex]); try { loader.setFile(m_tempUndoFiles[m_tempUndoIndex]); setInstancesFromFile(loader); } catch (Exception e) { e.printStackTrace(); m_Log.logMessage(e.toString()); JOptionPane.showMessageDialog(PreprocessPanel.this, "Cannot perform undo operation!\n" + e.toString(), "Undo", JOptionPane.ERROR_MESSAGE); } // update undo file list m_tempUndoFiles[m_tempUndoIndex] = null; } // update undo button int temp = m_tempUndoIndex-1; if (temp < 0) { temp = m_tempUndoFiles.length-1; } m_UndoBut.setEnabled(m_tempUndoFiles[temp] != null); } /** * edits the current instances object in the viewer */ public void edit() { ViewerDialog dialog; int result; Instances copy; Instances newInstances; final int classIndex = m_AttVisualizePanel.getColoringIndex(); copy = new Instances(m_Instances); copy.setClassIndex(classIndex); dialog = new ViewerDialog(null); result = dialog.showDialog(copy); if (result == ViewerDialog.APPROVE_OPTION) { try { addUndoPoint(); } catch (Exception e) { e.printStackTrace(); } // if class was not set before, reset it again after use of filter newInstances = dialog.getInstances(); if (m_Instances.classIndex() < 0) newInstances.setClassIndex(-1); setInstances(newInstances); } } /** * Sets the Explorer to use as parent frame (used for sending notifications * about changes in the data) * * @param parent the parent frame */ public void setExplorer(Explorer parent) { m_Explorer = parent; } /** * returns the parent Explorer frame * * @return the parent */ public Explorer getExplorer() { return m_Explorer; } /** * updates the capabilities filter of the GOE * * @param filter the new filter to use */ protected void updateCapabilitiesFilter(Capabilities filter) { if (filter == null) { m_FilterEditor.setCapabilitiesFilter(new Capabilities(null)); return; } // class index is never set in Explorer! filter.disable(Capability.NO_CLASS); filter.disableAllClasses(); // we don't get notified if the class gets changed in the // AttributeVisualizePanel, so we don't bother setting it here m_FilterEditor.setCapabilitiesFilter(filter); } /** * method gets called in case of a change event * * @param e the associated change event */ public void capabilitiesFilterChanged(CapabilitiesFilterChangeEvent e) { updateCapabilitiesFilter((Capabilities) e.getFilter().clone()); } /** * Tests out the instance-preprocessing panel from the command line. * * @param args ignored */ public static void main(String [] args) { try { final JFrame jf = new JFrame("Weka Explorer: Preprocess"); jf.getContentPane().setLayout(new BorderLayout()); final PreprocessPanel sp = new PreprocessPanel(); jf.getContentPane().add(sp, BorderLayout.CENTER); weka.gui.LogPanel lp = new weka.gui.LogPanel(); sp.setLog(lp); jf.getContentPane().add(lp, BorderLayout.SOUTH); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { jf.dispose(); System.exit(0); } }); jf.pack(); 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 + -