/**
  * Walk through tall Files and remove the ones for the current device.
  *
  * @param dirsToRefresh list of the directory to refresh, null if all of them
  * @return true if there was any file removed.
  */
 private boolean cleanFiles(List<Directory> dirsToRefresh) {
   boolean bChanges = false;
   final List<org.jajuk.base.File> files = FileManager.getInstance().getFiles();
   for (final org.jajuk.base.File file : files) {
     // check if it is a file located inside refreshed directory
     if (dirsToRefresh != null) {
       boolean checkIt = false;
       for (Directory directory : dirsToRefresh) {
         if (file.hasAncestor(directory)) {
           checkIt = true;
         }
       }
       // This item is not in given directories, just continue
       if (!checkIt) {
         continue;
       }
     }
     if (!ExitService.isExiting()
         && file.getDirectory().getDevice().equals(this)
         && file.isReady()
         &&
         // Remove file if it doesn't exist any more or if it is a iTunes
         // file (useful for jajuk < 1.4)
         (!file.getFIO().exists() || file.getName().startsWith("._"))) {
       FileManager.getInstance().removeFile(file);
       Log.debug("Removed: " + file);
       bChanges = true;
       if (reporter != null) {
         reporter.notifyFileOrPlaylistDropped();
       }
     }
   }
   return bChanges;
 }
 /**
  * Test device accessibility.
  *
  * @return true if the device is available
  */
 public boolean test() {
   UtilGUI.waiting(); // waiting cursor
   boolean bOK = false;
   boolean bWasMounted = bMounted; // store mounted state of device before
   // mount test
   try {
     if (!bMounted) {
       mount(true);
     }
   } catch (final Exception e) {
     UtilGUI.stopWaiting();
     return false;
   }
   if (getLongValue(Const.XML_TYPE) != 5) { // not a remote device
     final File file = new File(sUrl);
     if (file.exists() && file.canRead()) { // see if the url exists
       // and is readable
       // check if this device was void
       boolean bVoid = true;
       for (org.jajuk.base.File f : FileManager.getInstance().getFiles()) {
         if (f.getDirectory().getDevice().equals(this)) {
           // at least one field in this device
           bVoid = false;
           break;
         }
       }
       if (!bVoid) { // if the device is not supposed to be void,
         // check if it is the case, if no, the device
         // must not be unix-mounted
         if (file.list().length > 0) {
           bOK = true;
         }
       } else { // device is void, OK we assume it is accessible
         bOK = true;
       }
     }
   } else {
     bOK = false; // TBI
   }
   // unmount the device if it was mounted only for the test
   if (!bWasMounted) {
     try {
       unmount(false, false);
     } catch (final Exception e1) {
       Log.error(e1);
     }
   }
   UtilGUI.stopWaiting();
   return bOK;
 }
 /**
  * Sets the url.
  *
  * @param url The sUrl to set.
  */
 public void setUrl(final String url) {
   sUrl = url;
   setProperty(Const.XML_URL, url);
   fio = new File(url);
   /** Reset files */
   for (final org.jajuk.base.File file : FileManager.getInstance().getFiles()) {
     file.reset();
   }
   /** Reset playlists */
   for (final Playlist plf : PlaylistManager.getInstance().getPlaylists()) {
     plf.reset();
   }
   /** Reset directories */
   for (final Directory dir : DirectoryManager.getInstance().getDirectories()) {
     dir.reset();
   }
   // Reset the root dir
   rootDir = null;
 }
예제 #4
0
public class CAMenuControl extends JFrame {

  JButton randomB, loadFileB, helpB, exitB;
  JComboBox populationCB, rulesetCB, filenameCB;
  JTextField pathTF;
  JLabel randomL, loadL, populationL, rulesetL, pathL;
  JSeparator sep1, sep2;
  JPanel buttonP;
  ActionListener handler = new ButtonHandler();
  FileManager fm = FileManager.getInstance();

  public CAMenuControl(RuleSet[] rules, String[] files) {

    super();

    // buttons
    randomB = new JButton("Create Simulation");
    randomB.addActionListener(handler);
    loadFileB = new JButton("Load Simulation");
    loadFileB.addActionListener(handler);
    helpB = new JButton("Help");
    helpB.addActionListener(handler);
    exitB = new JButton("Exit");
    exitB.addActionListener(handler);

    // comboboxes
    Integer[] populations = {10, 20, 30, 40, 50, 60, 70, 80, 90};
    populationCB = new JComboBox<Integer>(populations);
    rulesetCB = new JComboBox<RuleSet>(rules);
    rulesetCB.addActionListener(handler);
    filenameCB = new JComboBox<String>(files);

    // labels
    Font title = new Font("Ariel", Font.BOLD, 14);
    randomL = new JLabel("Create a simulation:", JLabel.CENTER);
    randomL.setFont(title);
    populationL = new JLabel("Population (%): ", JLabel.RIGHT);
    rulesetL = new JLabel("Ruleset to use: ", JLabel.RIGHT);

    loadL = new JLabel("Load simulation from xml file:", JLabel.CENTER);
    loadL.setFont(title);
    pathL = new JLabel("Select file: ", JLabel.RIGHT);

    sep1 = new JSeparator(JSeparator.HORIZONTAL);
    sep1.setPreferredSize(new Dimension(50, 5));
    sep1.setBackground(Color.DARK_GRAY);

    sep2 = new JSeparator(JSeparator.HORIZONTAL);
    sep2.setPreferredSize(new Dimension(50, 5));
    sep2.setBackground(Color.DARK_GRAY);

    buttonP = new JPanel();
    buttonP.setLayout(new GridLayout(1, 2, 1, 1));
    buttonP.add(helpB);
    buttonP.add(exitB);

    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    // row1
    c.gridwidth = 2;
    add(randomL, c);
    // row2
    c.gridwidth = 1;
    c.gridy = 1;
    c.gridx = 0;
    add(rulesetL, c);
    c.gridx = 1;
    add(rulesetCB, c);
    // row3
    c.gridwidth = 1;
    c.gridy++;
    c.gridx = 0;
    add(populationL, c);
    c.gridx++;
    add(populationCB, c);
    c.gridy++;
    c.gridx = 1;
    add(randomB, c);
    // sep
    c.gridy++;
    c.gridx = 0;
    c.gridwidth = 2;
    add(sep1, c);
    // row4
    c.gridwidth = 2;
    c.gridy++;
    c.gridx = 0;
    add(loadL, c);
    // row5
    c.gridwidth = 1;
    c.gridy++;
    c.gridx = 0;
    add(pathL, c);
    c.gridx = 1;
    add(filenameCB, c);
    c.gridy++;
    c.gridx = 1;
    add(loadFileB, c);
    // sep
    c.gridy++;
    c.gridx = 0;
    c.gridwidth = 3;
    add(sep2, c);
    // row6
    c.gridy++;
    c.gridx = 0;
    c.gridwidth = 3;
    add(buttonP, c);

    setSize(500, 300);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
  }

  private Simulation loadAnt() {
    return fm.loadXML("LANGTONS_ANT.xml");
  }

  class ButtonHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
      if (e.getSource() == randomB) {
        Simulation sim;
        if (rulesetCB.getSelectedItem().getClass().getName().equals("CS415.LangtonsAnt")) {
          sim = loadAnt();
        } else {
          sim =
              new Simulation(
                  (RuleSet) rulesetCB.getSelectedItem(),
                  50,
                  50,
                  (int) populationCB.getSelectedItem());
        }

        if (sim != null) {
          CAApplication.control = new SimControl(sim);
          setVisible(false);
        }
      } else if (e.getSource() == loadFileB) {
        Simulation sim = fm.loadXML(filenameCB.getSelectedItem().toString());
        new SimControl(sim);
        setVisible(false);
      } else if (e.getSource() == exitB) {
        JOptionPane op = new JOptionPane();
        op.showMessageDialog(null, "Your application is awesome, and ran perfectly!");
        System.exit(0);
      } else if (e.getSource() == rulesetCB) {
        if (rulesetCB.getSelectedItem().getClass().getName().equals("CS415.LangtonsAnt")) {
          populationL.setVisible(false);
          populationCB.setVisible(false);
        } else {
          populationL.setVisible(true);
          populationCB.setVisible(true);
        }
      }
    }
  }
}
 /**
  * The refresh itself.
  *
  * @param bDeepScan whether it is a deep refresh request or only fast
  * @param bManual whether it is a manual refresh or auto
  * @param dirsToRefresh list of the directory to refresh, null if all of them
  * @return true if some changes occurred in device
  */
 boolean refreshCommand(
     final boolean bDeepScan, final boolean bManual, List<Directory> dirsToRefresh) {
   try {
     // Check if this device is mounted (useful when called by
     // automatic refresh)
     if (!isMounted()) {
       return false;
     }
     // Check that device is still available
     boolean readyToMount = checkDevice();
     if (!readyToMount) {
       return false;
     }
     bAlreadyRefreshing = true;
     // reporter is already set in case of manual refresh
     if (reporter == null) {
       reporter = new RefreshReporter(this);
     }
     // Notify the reporter of the actual refresh startup
     reporter.refreshStarted();
     lDateLastRefresh = System.currentTimeMillis();
     // check Jajuk is not exiting because a refresh cannot start in
     // this state
     if (ExitService.isExiting()) {
       return false;
     }
     int iNbFilesBeforeRefresh = FileManager.getInstance().getElementCount();
     int iNbDirsBeforeRefresh = DirectoryManager.getInstance().getElementCount();
     int iNbPlaylistsBeforeRefresh = PlaylistManager.getInstance().getElementCount();
     if (bDeepScan && Log.isDebugEnabled()) {
       Log.debug("Starting refresh of device : " + this);
     }
     // Create a directory for device itself and scan files to allow
     // files at the root of the device
     final Directory top = DirectoryManager.getInstance().registerDirectory(this);
     if (!getDirectories().contains(top)) {
       addDirectory(top);
     }
     // Start actual scan
     List<Directory> dirs = null;
     if (dirsToRefresh == null) {
       // No directory specified ? refresh the top directory
       dirs = new ArrayList<Directory>(1);
       dirs.add(top);
     } else {
       dirs = dirsToRefresh;
     }
     for (Directory dir : dirs) {
       scanRecursively(dir, bDeepScan);
     }
     // Force a GUI refresh if new files or directories discovered or have been
     // removed
     if (((FileManager.getInstance().getElementCount() - iNbFilesBeforeRefresh) != 0)
         || ((DirectoryManager.getInstance().getElementCount() - iNbDirsBeforeRefresh) != 0)
         || ((PlaylistManager.getInstance().getElementCount() - iNbPlaylistsBeforeRefresh) != 0)) {
       return true;
     }
     return false;
   } catch (final Exception e) {
     // and regular ones logged
     Log.error(e);
     return false;
   } finally {
     // make sure to unlock refreshing even if an error occurred
     bAlreadyRefreshing = false;
     // reporter is null if mount is not mounted due to early return
     if (reporter != null) {
       // Notify the reporter of the actual refresh startup
       reporter.done();
       // Reset the reporter as next time, it could be another type
       reporter = null;
     }
   }
 }
예제 #6
0
 @Before
 public void setUp() {
   manager = FileManager.getInstance();
   root = manager.getFileSystem().getRoot();
 }