applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SessionFileWatcherElement.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.client.simpleclient;\r
23 \r
24 /**\r
25  * Element of the VamsasFileWatcherThread event generator chain. Keep a\r
26  * reference to this element before adding it to the chain in order to control\r
27  * the generation of events with halt and enable.\r
28  * \r
29  * doWatch will do nothing if the element is not enabled.\r
30  * \r
31  */\r
32 public class SessionFileWatcherElement extends WatcherElement {\r
33   SessionFile watched = null;\r
34 \r
35   /**\r
36    * create a new, enabled watch element\r
37    * \r
38    * @param watcher\r
39    *          file being watched\r
40    * @param handler\r
41    *          handler to call on state change\r
42    */\r
43   public SessionFileWatcherElement(SessionFile watcher, WatcherCallBack handler) {\r
44     this(watcher, handler, true);\r
45   }\r
46 \r
47   protected void initWatch() {\r
48     if (watched == null)\r
49       return;\r
50     if (watcher == null || !watcher.getSubject().equals(watched.sessionFile))\r
51       watcher = new FileWatcher(watched.sessionFile);\r
52     else\r
53       watcher.setState();\r
54   }\r
55 \r
56   protected void endWatch() {\r
57     // leaves watcher in state its in.\r
58   }\r
59 \r
60   /**\r
61    * new watcher with flag to initially skip watching this sessionfile\r
62    * \r
63    * @param watcher\r
64    * @param handler\r
65    * @param enableWatching\r
66    */\r
67   public SessionFileWatcherElement(SessionFile watcher,\r
68       WatcherCallBack handler, boolean enableWatching) {\r
69     super(handler);\r
70     this.watched = watcher;\r
71     if (enableWatching)\r
72       enableWatch();\r
73     else\r
74       haltWatch();\r
75   }\r
76 \r
77   /**\r
78    * @return the watched\r
79    */\r
80   public SessionFile getWatched() {\r
81     return watched;\r
82   }\r
83 \r
84   protected String getSubject() {\r
85     return watched.sessionFile.toString();\r
86   }\r
87 }\r