~ubuntu-branches/ubuntu/raring/weka/raring

« back to all changes in this revision

Viewing changes to weka/gui/experiment/DatasetListPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner, Soeren Sonnenburg, Torsten Werner
  • Date: 2008-08-10 21:27:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080810212705-tr8etpnkdx2ziktp
Tags: 3.5.8-1
[ Soeren Sonnenburg ]
* Bump Standards Version to 3.8.0.
* Remove references to non-free Java in debian/copyright.

[ Torsten Werner ]
* new upstream release
* Switch to openjdk-6.
* Move package to main.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import weka.core.converters.ConverterUtils;
27
27
import weka.core.converters.Saver;
28
28
import weka.core.converters.ConverterUtils.DataSource;
 
29
import weka.core.Utils;
29
30
import weka.experiment.Experiment;
30
31
import weka.gui.ConverterFileChooser;
31
32
import weka.gui.JListHelper;
63
64
 * iterate over.
64
65
 *
65
66
 * @author Len Trigg (trigg@cs.waikato.ac.nz)
66
 
 * @version $Revision: 1.26 $
 
67
 * @version $Revision: 1.27 $
67
68
 */
68
69
public class DatasetListPanel
69
70
  extends JPanel
97
98
  protected JCheckBox m_relativeCheck = new JCheckBox("Use relative paths");
98
99
 
99
100
  /** The user (start) directory. */
100
 
  protected File m_UserDir = new File(System.getProperty("user.dir"));
 
101
  //  protected File m_UserDir = new File(System.getProperty("user.dir"));
101
102
 
102
103
  /** The file chooser component. */
103
104
  protected ConverterFileChooser m_FileChooser = 
261
262
      System.err.println("IOError occured when reading list of files");
262
263
    }
263
264
  }
264
 
 
265
 
  /**
266
 
   * Converts a File's absolute path to a path relative to the user
267
 
   * (ie start) directory.
268
 
   * 
269
 
   * @param absolute the File to convert to relative path
270
 
   * @return a File with a path that is relative to the user's directory
271
 
   * @exception Exception if the path cannot be constructed
272
 
   */
273
 
  protected File createRelativePath(File absolute) throws Exception {
274
 
    String userPath = m_UserDir.getAbsolutePath() + File.separator;
275
 
    String targetPath = (new File(absolute.getParent())).getPath() 
276
 
      + File.separator;
277
 
    String fileName = absolute.getName();
278
 
    StringBuffer relativePath = new StringBuffer();
279
 
    relativePath.append("."+File.separator);
280
 
    //    System.err.println("User dir "+userPath);
281
 
    //    System.err.println("Target path "+targetPath);
282
 
    
283
 
    // file is in user dir (or subdir)
284
 
    int subdir = targetPath.indexOf(userPath);
285
 
    if (subdir == 0) {
286
 
      if (userPath.length() == targetPath.length()) {
287
 
        relativePath.append(fileName);
288
 
      } else {
289
 
        int ll = userPath.length();
290
 
        relativePath.append(targetPath.substring(ll));
291
 
        relativePath.append(fileName);
292
 
      }
293
 
    } else {
294
 
      int sepCount = 0;
295
 
      String temp = new String(userPath);
296
 
      while (temp.indexOf(File.separator) != -1) {
297
 
        int ind = temp.indexOf(File.separator);
298
 
        sepCount++;
299
 
        temp = temp.substring(ind+1, temp.length());
300
 
      }
301
 
      
302
 
      String targetTemp = new String(targetPath);
303
 
      String userTemp = new String(userPath);
304
 
      int tcount = 0;
305
 
      while (targetTemp.indexOf(File.separator) != -1) {
306
 
        int ind = targetTemp.indexOf(File.separator);
307
 
        int ind2 = userTemp.indexOf(File.separator);
308
 
        String tpart = targetTemp.substring(0,ind+1);
309
 
        String upart = userTemp.substring(0,ind2+1);
310
 
        if (tpart.compareTo(upart) != 0) {
311
 
          if (tcount == 0) {
312
 
            tcount = -1;
313
 
          }
314
 
          break;
315
 
        }
316
 
        tcount++;
317
 
        targetTemp = targetTemp.substring(ind+1, targetTemp.length());
318
 
        userTemp = userTemp.substring(ind2+1, userTemp.length());
319
 
      }
320
 
      if (tcount == -1) {
321
 
        // then target file is probably on another drive (under windows)
322
 
        throw new Exception("Can't construct a path to file relative to user "
323
 
                            +"dir.");
324
 
      }
325
 
      if (targetTemp.indexOf(File.separator) == -1) {
326
 
        targetTemp = "";
327
 
      }
328
 
      for (int i = 0; i < sepCount - tcount; i++) {
329
 
        relativePath.append(".."+File.separator);
330
 
      }
331
 
      relativePath.append(targetTemp + fileName);
332
 
    }
333
 
    //    System.err.println("new path : "+relativePath.toString());
334
 
    return new File(relativePath.toString());
335
 
  }
336
 
 
337
 
  /**
338
 
   * Converts a File's absolute path to a path relative to the user
339
 
   * (ie start) directory. Includes an additional workaround for Cygwin, which
340
 
   * doesn't like upper case drive letters.
341
 
   * @param absolute the File to convert to relative path
342
 
   * @return a File with a path that is relative to the user's directory
343
 
   * @exception Exception if the path cannot be constructed
344
 
   */
345
 
  protected File convertToRelativePath(File absolute) throws Exception {
346
 
    File        result;
347
 
    String      fileStr;
348
 
    
349
 
    result = null;
350
 
    
351
 
    // if we're running windows, it could be Cygwin
352
 
    if (File.separator.equals("\\")) {
353
 
      // Cygwin doesn't like upper case drives -> try lower case drive
354
 
      try {
355
 
        fileStr = absolute.getPath();
356
 
        fileStr =   fileStr.substring(0, 1).toLowerCase() 
357
 
                  + fileStr.substring(1);
358
 
        result = createRelativePath(new File(fileStr));
359
 
      }
360
 
      catch (Exception e) {
361
 
        // no luck with Cygwin workaround, convert it like it is
362
 
        result = createRelativePath(absolute);
363
 
      }
364
 
    }
365
 
    else {
366
 
      result = createRelativePath(absolute);
367
 
    }
368
 
 
369
 
    return result;
370
 
  }
371
265
  
372
266
  /**
373
267
   * Handle actions when buttons get pressed.
395
289
                File temp = (File)files.elementAt(j);
396
290
                if (useRelativePaths) {
397
291
                  try {
398
 
                    temp = convertToRelativePath(temp);
 
292
                    temp = Utils.convertToRelativePath(temp);
399
293
                  } catch (Exception ex) {
400
294
                    ex.printStackTrace();
401
295
                  }
406
300
              File temp = selected[i];
407
301
              if (useRelativePaths) {
408
302
                try {
409
 
                  temp = convertToRelativePath(temp);
 
303
                  temp = Utils.convertToRelativePath(temp);
410
304
                } catch (Exception ex) {
411
305
                  ex.printStackTrace();
412
306
                }
427
321
              File temp = (File)files.elementAt(j);
428
322
              if (useRelativePaths) {
429
323
                try {
430
 
                  temp = convertToRelativePath(temp);
 
324
                  temp = Utils.convertToRelativePath(temp);
431
325
                } catch (Exception ex) {
432
326
                  ex.printStackTrace();
433
327
                }
438
332
            File temp = m_FileChooser.getSelectedFile();
439
333
            if (useRelativePaths) {
440
334
              try {
441
 
                temp = convertToRelativePath(temp);
 
335
                temp = Utils.convertToRelativePath(temp);
442
336
              } catch (Exception ex) {
443
337
                ex.printStackTrace();
444
338
              }