1// BEGIN LICENSE BLOCK 2// Version: CMPL 1.1 3// 4// The contents of this file are subject to the Cisco-style Mozilla Public 5// License Version 1.1 (the "License"); you may not use this file except 6// in compliance with the License. You may obtain a copy of the License 7// at www.eclipse-clp.org/license. 8// 9// Software distributed under the License is distributed on an "AS IS" 10// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 11// the License for the specific language governing rights and limitations 12// under the License. 13// 14// The Original Code is The ECLiPSe Constraint Logic Programming System. 15// The Initial Developer of the Original Code is Cisco Systems, Inc. 16// Portions created by the Initial Developer are 17// Copyright (C) 2006 Cisco Systems, Inc. All Rights Reserved. 18// 19// Contributor(s): 20// 21// END LICENSE BLOCK 22 23package com.parctechnologies.eclipse.jdotview; 24 25import java.awt.event.*; 26import java.io.*; 27import java.util.*; 28import javax.swing.*; 29 30 31public class FileWatcher extends Thread { 32 File file; 33 String filename; 34 long lastModified; 35 ArrayList listenerList; 36 long SLEEPTIME; 37 38 public FileWatcher(String filename, long SLEEPTIME) { 39 this.setDaemon(true); 40 this.filename=filename; 41 this.file = new File(filename); 42 this.SLEEPTIME=SLEEPTIME; 43 this.lastModified = 0; 44 this.listenerList = new ArrayList(); 45 } 46 47 public void addActionListener(ActionListener al) { 48 listenerList.add(al); 49 } 50 51 public void run() { 52 while(true) { 53 try { 54 long lm = file.lastModified(); 55 if (lm > lastModified ) { 56 lastModified = lm; 57 Object[] listeners = listenerList.toArray(); 58 // Process the listeners last to first, notifying 59 // those that are interested in this event 60 for (int i = listeners.length-1; i>=0; i--) { 61 ((ActionListener)listeners[i]). 62 actionPerformed(new ActionEvent( this, 0, filename)); 63 } 64 } 65 sleep(SLEEPTIME); 66 } catch(InterruptedException ie) { 67 // no need to do anything 68 } 69 } 70 } 71} 72