📄 datasetlistpanel.java
字号:
+ File.separator; String fileName = absolute.getName(); StringBuffer relativePath = new StringBuffer(); relativePath.append("."+File.separator); // System.err.println("User dir "+userPath); // System.err.println("Target path "+targetPath); // file is in user dir (or subdir) int subdir = targetPath.indexOf(userPath); if (subdir == 0) { if (userPath.length() == targetPath.length()) { relativePath.append(fileName); } else { int ll = userPath.length(); relativePath.append(targetPath.substring(ll)); relativePath.append(fileName); } } else { int sepCount = 0; String temp = new String(userPath); while (temp.indexOf(File.separator) != -1) { int ind = temp.indexOf(File.separator); sepCount++; temp = temp.substring(ind+1, temp.length()); } String targetTemp = new String(targetPath); String userTemp = new String(userPath); int tcount = 0; while (targetTemp.indexOf(File.separator) != -1) { int ind = targetTemp.indexOf(File.separator); int ind2 = userTemp.indexOf(File.separator); String tpart = targetTemp.substring(0,ind+1); String upart = userTemp.substring(0,ind2+1); if (tpart.compareTo(upart) != 0) { if (tcount == 0) { tcount = -1; } break; } tcount++; targetTemp = targetTemp.substring(ind+1, targetTemp.length()); userTemp = userTemp.substring(ind2+1, userTemp.length()); } if (tcount == -1) { // then target file is probably on another drive (under windows) throw new Exception("Can't construct a path to file relative to user " +"dir."); } if (targetTemp.indexOf(File.separator) == -1) { targetTemp = ""; } for (int i = 0; i < sepCount - tcount; i++) { relativePath.append(".."+File.separator); } relativePath.append(targetTemp + fileName); } // System.err.println("new path : "+relativePath.toString()); return new File(relativePath.toString()); } /** * Converts a File's absolute path to a path relative to the user * (ie start) directory. Includes an additional workaround for Cygwin, which * doesn't like upper case drive letters. * @param absolute the File to convert to relative path * @return a File with a path that is relative to the user's directory * @exception Exception if the path cannot be constructed */ protected File convertToRelativePath(File absolute) throws Exception { File result; String fileStr; result = null; // if we're running windows, it could be Cygwin if (File.separator.equals("\\")) { // Cygwin doesn't like upper case drives -> try lower case drive try { fileStr = absolute.getPath(); fileStr = fileStr.substring(0, 1).toLowerCase() + fileStr.substring(1); result = createRelativePath(new File(fileStr)); } catch (Exception e) { // no luck with Cygwin workaround, convert it like it is result = createRelativePath(absolute); } } else { result = createRelativePath(absolute); } return result; } /** * Handle actions when buttons get pressed. * * @param e a value of type 'ActionEvent' */ public void actionPerformed(ActionEvent e) { boolean useRelativePaths = m_relativeCheck.isSelected(); if (e.getSource() == m_AddBut) { // Let the user select an arff file from a file chooser int returnVal = m_FileChooser.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION) { if (m_FileChooser.isMultiSelectionEnabled()) { File [] selected = m_FileChooser.getSelectedFiles(); for (int i = 0; i < selected.length; i++) { if (selected[i].isDirectory()) { Vector files = new Vector(); getFilesRecursively(selected[i], files); // sort the result Collections.sort(files, new ClassDiscovery().new StringCompare()); for (int j = 0; j < files.size(); j++) { File temp = (File)files.elementAt(j); if (useRelativePaths) { try { temp = convertToRelativePath(temp); } catch (Exception ex) { ex.printStackTrace(); } } m_Exp.getDatasets().addElement(temp); } } else { File temp = selected[i]; if (useRelativePaths) { try { temp = convertToRelativePath(temp); } catch (Exception ex) { ex.printStackTrace(); } } m_Exp.getDatasets().addElement(temp); } } setButtons(null); } else { if (m_FileChooser.getSelectedFile().isDirectory()) { Vector files = new Vector(); getFilesRecursively(m_FileChooser.getSelectedFile(), files); // sort the result Collections.sort(files, new ClassDiscovery().new StringCompare()); for (int j = 0; j < files.size(); j++) { File temp = (File)files.elementAt(j); if (useRelativePaths) { try { temp = convertToRelativePath(temp); } catch (Exception ex) { ex.printStackTrace(); } } m_Exp.getDatasets().addElement(temp); } } else { File temp = m_FileChooser.getSelectedFile(); if (useRelativePaths) { try { temp = convertToRelativePath(temp); } catch (Exception ex) { ex.printStackTrace(); } } m_Exp.getDatasets().addElement(temp); } setButtons(null); } } } else if (e.getSource() == m_DeleteBut) { // Delete the selected files int [] selected = m_List.getSelectedIndices(); if (selected != null) { for (int i = selected.length - 1; i >= 0; i--) { int current = selected[i]; m_Exp.getDatasets().removeElementAt(current); if (m_Exp.getDatasets().size() > current) { m_List.setSelectedIndex(current); } else { m_List.setSelectedIndex(current - 1); } } } setButtons(null); } else if (e.getSource() == m_EditBut) { // Delete the selected files int selected = m_List.getSelectedIndex(); if (selected != -1) { ViewerDialog dialog = new ViewerDialog(null); String filename = m_List.getSelectedValue().toString(); int result; try { DataSource source = new DataSource(filename); result = dialog.showDialog(source.getDataSet()); // nasty workaround for Windows regarding locked files: // if file Reader in Loader is not closed explicitly, we cannot // overwrite the file. source = null; System.gc(); // workaround end if ((result == ViewerDialog.APPROVE_OPTION) && (dialog.isChanged())) { result = JOptionPane.showConfirmDialog( this, "File was modified - save changes?"); if (result == JOptionPane.YES_OPTION) { Saver saver = ConverterUtils.getSaverForFile(filename); saver.setFile(new File(filename)); saver.setInstances(dialog.getInstances()); saver.writeBatch(); } } } catch (Exception ex) { JOptionPane.showMessageDialog( this, "Error loading file '" + filename + "':\n" + ex.toString(), "Error loading file", JOptionPane.INFORMATION_MESSAGE); } } setButtons(null); } else if (e.getSource() == m_UpBut) { JListHelper.moveUp(m_List); } else if (e.getSource() == m_DownBut) { JListHelper.moveDown(m_List); } } /** * Tests out the dataset list panel from the command line. * * @param args ignored */ public static void main(String [] args) { try { final JFrame jf = new JFrame("Dataset List Editor"); jf.getContentPane().setLayout(new BorderLayout()); DatasetListPanel dp = new DatasetListPanel(); jf.getContentPane().add(dp, BorderLayout.CENTER); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { jf.dispose(); System.exit(0); } }); jf.pack(); jf.setVisible(true); System.err.println("Short nap"); Thread.currentThread().sleep(3000); System.err.println("Done"); dp.setExperiment(new Experiment()); } catch (Exception ex) { ex.printStackTrace(); System.err.println(ex.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -