📄 preprocesspanel.java
字号:
* @param inst a set of Instances */ public void setInstances(Instances inst) { m_Instances = inst; try { Runnable r = new Runnable() { public void run() { m_InstSummaryPanel.setInstances(m_Instances); m_AttPanel.setInstances(m_Instances); m_RemoveButton.setEnabled(true); m_AttSummaryPanel.setInstances(m_Instances); m_AttVisualizePanel.setInstances(m_Instances); // select the first attribute in the list m_AttPanel.getSelectionModel().setSelectionInterval(0, 0); m_AttSummaryPanel.setAttribute(0); m_AttVisualizePanel.setAttribute(0); m_ApplyFilterBut.setEnabled(true); m_Log.logMessage("Base relation is now " + m_Instances.relationName() + " (" + m_Instances.numInstances() + " instances)"); m_SaveBut.setEnabled(true); m_EditBut.setEnabled(true); m_Log.statusMessage("OK"); // Fire a propertychange event m_Support.firePropertyChange("", null, null); // notify GOEs about change try { if (ExplorerDefaults.getInitGenericObjectEditorFilter()) getExplorer().notifyCapabilitiesFilterListener( Capabilities.forInstances(m_Instances)); else getExplorer().notifyCapabilitiesFilterListener( Capabilities.forInstances(new Instances(m_Instances, 0))); } catch (Exception e) { e.printStackTrace(); m_Log.logMessage(e.toString()); } } }; if (SwingUtilities.isEventDispatchThread()) { r.run(); } else { SwingUtilities.invokeAndWait(r); } } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Problem setting base instances:\n" + ex, "Instances", JOptionPane.ERROR_MESSAGE); } } /** * Gets the working set of instances. * * @return the working instances */ public Instances getInstances() { return m_Instances; } /** * Adds a PropertyChangeListener who will be notified of value changes. * * @param l a value of type 'PropertyChangeListener' */ public void addPropertyChangeListener(PropertyChangeListener l) { m_Support.addPropertyChangeListener(l); } /** * Removes a PropertyChangeListener. * * @param l a value of type 'PropertyChangeListener' */ public void removePropertyChangeListener(PropertyChangeListener l) { m_Support.removePropertyChangeListener(l); } /** * Passes the dataset through the filter that has been configured for use. * * @param filter the filter to apply */ protected void applyFilter(final Filter filter) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { if (filter != null) { if (m_Log instanceof TaskLogger) { ((TaskLogger)m_Log).taskStarted(); } m_Log.statusMessage("Passing dataset through filter " + filter.getClass().getName()); String cmd = filter.getClass().getName(); if (filter instanceof OptionHandler) cmd += " " + Utils.joinOptions(((OptionHandler) filter).getOptions()); m_Log.logMessage("Command: " + cmd); int classIndex = m_AttVisualizePanel.getColoringIndex(); if ((classIndex < 0) && (filter instanceof SupervisedFilter)) { throw new IllegalArgumentException("Class (colour) needs to " + "be set for supervised " + "filter."); } Instances copy = new Instances(m_Instances); copy.setClassIndex(classIndex); filter.setInputFormat(copy); Instances newInstances = Filter.useFilter(copy, filter); if (newInstances == null || newInstances.numAttributes() < 1) { throw new Exception("Dataset is empty."); } m_Log.statusMessage("Saving undo information"); addUndoPoint(); m_AttVisualizePanel.setColoringIndex(copy.classIndex()); // if class was not set before, reset it again after use of filter if (m_Instances.classIndex() < 0) newInstances.setClassIndex(-1); m_Instances = newInstances; setInstances(m_Instances); if (m_Log instanceof TaskLogger) { ((TaskLogger)m_Log).taskFinished(); } } } catch (Exception ex) { if (m_Log instanceof TaskLogger) { ((TaskLogger)m_Log).taskFinished(); } // Pop up an error optionpane JOptionPane.showMessageDialog(PreprocessPanel.this, "Problem filtering instances:\n" + ex.getMessage(), "Apply Filter", JOptionPane.ERROR_MESSAGE); m_Log.logMessage("Problem filtering instances: " + ex.getMessage()); m_Log.statusMessage("Problem filtering instances"); } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } else { JOptionPane.showMessageDialog(this, "Can't apply filter at this time,\n" + "currently busy with other IO", "Apply Filter", JOptionPane.WARNING_MESSAGE); } } /** * Queries the user for a file to save instances as, then saves the * instances in a background process. This is done in the IO * thread, and an error message is popped up if the IO thread is busy. */ public void saveWorkingInstancesToFileQ() { if (m_IOThread == null) { m_FileChooser.setCapabilitiesFilter(m_FilterEditor.getCapabilitiesFilter()); m_FileChooser.setAcceptAllFileFilterUsed(false); int returnVal = m_FileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { Instances inst = new Instances(m_Instances); inst.setClassIndex(m_AttVisualizePanel.getColoringIndex()); saveInstancesToFile(m_FileChooser.getSaver(), inst); } FileFilter temp = m_FileChooser.getFileFilter(); m_FileChooser.setAcceptAllFileFilterUsed(true); m_FileChooser.setFileFilter(temp); } else { JOptionPane.showMessageDialog(this, "Can't save at this time,\n" + "currently busy with other IO", "Save Instances", JOptionPane.WARNING_MESSAGE); } } /** * saves the data with the specified saver * * @param saver the saver to use for storing the data * @param inst the data to save */ public void saveInstancesToFile(final AbstractFileSaver saver, final Instances inst) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { m_Log.statusMessage("Saving to file..."); saver.setInstances(inst); saver.writeBatch(); m_Log.statusMessage("OK"); } catch (Exception ex) { ex.printStackTrace(); m_Log.logMessage(ex.getMessage()); } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } else { JOptionPane.showMessageDialog(this, "Can't save at this time,\n" + "currently busy with other IO", "Saving instances", JOptionPane.WARNING_MESSAGE); } } /** * Queries the user for a file to load instances from, then loads the * instances in a background process. This is done in the IO * thread, and an error message is popped up if the IO thread is busy. */ public void setInstancesFromFileQ() { if (m_IOThread == null) { int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { addUndoPoint(); } catch (Exception ignored) { // ignored } if (m_FileChooser.getLoader() == null) { JOptionPane.showMessageDialog(this, "Cannot determine file loader automatically, please choose one.", "Load Instances", JOptionPane.ERROR_MESSAGE); converterQuery(m_FileChooser.getSelectedFile()); } else { setInstancesFromFile(m_FileChooser.getLoader()); } } } else { JOptionPane.showMessageDialog(this, "Can't load at this time,\n" + "currently busy with other IO", "Load Instances", JOptionPane.WARNING_MESSAGE); } } /** * Loads instances from an SQL query the user provided with the * SqlViewerDialog, then loads the instances in a background process. This is * done in the IO thread, and an error message is popped up if the IO thread * is busy. * @param url the database URL * @param user the user to connect as * @param pw the password of the user * @param query the query for retrieving instances from */ public void setInstancesFromDBQ(String url, String user, String pw, String query) { if (m_IOThread == null) { try { InstanceQuery InstQ = new InstanceQuery(); InstQ.setDatabaseURL(url); InstQ.setUsername(user); InstQ.setPassword(pw); InstQ.setQuery(query); // we have to disconnect, otherwise we can't change the DB! if (InstQ.isConnected()) InstQ.disconnectFromDatabase(); InstQ.connectToDatabase(); try { addUndoPoint(); } catch (Exception ignored) {} setInstancesFromDB(InstQ); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Problem connecting to database:\n" + ex.getMessage(), "Load Instances", JOptionPane.ERROR_MESSAGE); } } else { JOptionPane.showMessageDialog(this, "Can't load at this time,\n" + "currently busy with other IO", "Load Instances", JOptionPane.WARNING_MESSAGE); } } /** * Queries the user for a URL to load instances from, then loads the * instances in a background process. This is done in the IO * thread, and an error message is popped up if the IO thread is busy. */ public void setInstancesFromURLQ() { if (m_IOThread == null) { try { String urlName = (String) JOptionPane.showInputDialog(this, "Enter the source URL", "Load Instances", JOptionPane.QUESTION_MESSAGE, null, null, m_LastURL); if (urlName != null) { m_LastURL = urlName; URL url = new URL(urlName); try { addUndoPoint(); } catch (Exception ignored) {} setInstancesFromURL(url); } } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Problem with URL:\n" + ex.getMessage(), "Load Instances", JOptionPane.ERROR_MESSAGE); } } else { JOptionPane.showMessageDialog(this, "Can't load at this time,\n" + "currently busy with other IO", "Load Instances", JOptionPane.WARNING_MESSAGE); } } /** * sets Instances generated via DataGenerators (pops up a Dialog) */ public void generateInstances() { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { // create dialog final DataGeneratorPanel generatorPanel = new DataGeneratorPanel(); final JDialog dialog = new JDialog(); final JButton generateButton = new JButton("Generate"); final JCheckBox showOutputCheckBox = new JCheckBox("Show generated data as text, incl. comments"); showOutputCheckBox.setMnemonic('S'); generatorPanel.setLog(m_Log); generatorPanel.setGenerator(m_DataGenerator); generatorPanel.setPreferredSize( new Dimension( 300, (int) generatorPanel.getPreferredSize().getHeight())); generateButton.setMnemonic('G'); generateButton.setToolTipText("Generates the dataset according the settings."); generateButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ // generate generatorPanel.execute(); boolean generated = (generatorPanel.getInstances() != null); if (generated) setInstances(generatorPanel.getInstances()); // close dialog dialog.dispose(); // get last generator m_DataGenerator = generatorPanel.getGenerator(); // display output? if ( (generated) && (showOutputCheckBox.isSelected()) ) showGeneratedInstances(generatorPanel.getOutput()); } }); dialog.setTitle("DataGenerator"); dialog.getContentPane().add(generatorPanel, BorderLayout.CENTER); dialog.getContentPane().add(generateButton, BorderLayout.EAST); dialog.getContentPane().add(showOutputCheckBox, BorderLayout.SOUTH); dialog.pack(); // display dialog dialog.setVisible(true); } catch (Exception ex) { ex.printStackTrace(); m_Log.logMessage(ex.getMessage()); } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } else { JOptionPane.showMessageDialog(this, "Can't generate data at this time,\n" + "currently busy with other IO", "Generate Data", JOptionPane.WARNING_MESSAGE); } } /** * displays a dialog with the generated instances from the DataGenerator * * @param data the data to display */ protected void showGeneratedInstances(String data) { final JDialog dialog = new JDialog(); final JButton saveButton = new JButton("Save"); final JButton closeButton = new JButton("Close"); final JTextArea textData = new JTextArea(data);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -