add more events to core listener
[jSite2.git] / src / net / pterodactylus / jsite / core / Core.java
1 /*
2  * jSite2 - Core.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.core;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 /**
27  * The core of jSite.
28  *
29  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
30  * @version $Id$
31  */
32 public class Core {
33
34         /** The core listeners. */
35         private final List<CoreListener> coreListeners = new ArrayList<CoreListener>();
36
37         /** The project manager. */
38         private ProjectManager projectManager;
39
40         /** The node list. */
41         private List<Node> configuredNodes = new ArrayList<Node>();
42
43         /** List of currently connected nodes. */
44         private List<Node> connectedNodes = new ArrayList<Node>();
45
46         //
47         // LISTENER MANAGEMENT
48         //
49
50         /**
51          * Adds the given listener to the list of registered listeners.
52          *
53          * @param coreListener
54          *            The listener to add
55          */
56         public void addCoreListener(CoreListener coreListener) {
57                 coreListeners.add(coreListener);
58         }
59
60         /**
61          * Removes the given listener from the list of registered listeners.
62          *
63          * @param coreListener
64          *            The listener to remove
65          */
66         public void removeCoreListener(CoreListener coreListener) {
67                 coreListeners.remove(coreListener);
68         }
69
70         /**
71          * Notifies all core listeners that loading the projects from the given
72          * directory has failed.
73          *
74          * @param directory
75          *            The directory the projects were tried to load from
76          * @param throwable
77          *            The exception that occured when loading projects
78          */
79         private void fireLoadingProjectsFailed(String directory, Throwable throwable) {
80                 for (CoreListener coreListener: coreListeners) {
81                         coreListener.loadingProjectsFailed(directory, throwable);
82                 }
83         }
84
85         /**
86          * Notifies all listeners that the projects were successfully saved.
87          *
88          * @param directory
89          *            The directory the projects were saved to
90          */
91         private void fireSavingProjectsDone(String directory) {
92                 for (CoreListener coreListener: coreListeners) {
93                         coreListener.savingProjectsDone(directory);
94                 }
95         }
96
97         /**
98          * Notifies all listeners that the projects could not be saved.
99          *
100          * @param directory
101          *            The directory the projects were to be saved to
102          * @param throwable
103          *            The exception that occured when saving the projects
104          */
105         private void fireSavingProjectsFailed(String directory, Throwable throwable) {
106                 for (CoreListener coreListener: coreListeners) {
107                         coreListener.savingProjectsFailed(directory, throwable);
108                 }
109         }
110
111         /**
112          * Notifies all core listeners that the core has loaded and is ready to run.
113          */
114         private void fireCoreLoaded() {
115                 for (CoreListener coreListener: coreListeners) {
116                         coreListener.coreLoaded();
117                 }
118         }
119
120         /**
121          * Notifies all listeners that the core was stopped.
122          */
123         private void fireCoreStopped() {
124                 for (CoreListener coreListener: coreListeners) {
125                         coreListener.coreStopped();
126                 }
127         }
128
129         //
130         // ACCESSORS
131         //
132
133         /**
134          * Returns the project manager.
135          *
136          * @return The project manager
137          */
138         public ProjectManager getProjectManager() {
139                 return projectManager;
140         }
141
142         /**
143          * Sets the project manager to use.
144          *
145          * @param projectManager
146          *            The project manager to use
147          */
148         public void setProjectManager(ProjectManager projectManager) {
149                 this.projectManager = projectManager;
150         }
151
152         /**
153          * Returns the list of all configured nodes.
154          *
155          * @return All configured nodes
156          */
157         public List<Node> getNodes() {
158                 return configuredNodes;
159         }
160
161         /**
162          * Returns whether the core is currently connected to the given node.
163          *
164          * @param node
165          *            The node to check
166          * @return <code>true</code> if the core is currently connected to the
167          *         node, <code>false</code> otherwise
168          */
169         public boolean isNodeConnected(Node node) {
170                 return connectedNodes.contains(node);
171         }
172
173         //
174         // ACTIONS
175         //
176
177         /**
178          * Starts the core.
179          */
180         public void start() {
181                 try {
182                         projectManager.load();
183                 } catch (IOException ioe1) {
184                         fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
185                 }
186                 fireCoreLoaded();
187         }
188
189         /**
190          * Stops the core.
191          */
192         public void stop() {
193                 try {
194                         projectManager.save();
195                         fireSavingProjectsDone(projectManager.getDirectory());
196                 } catch (IOException ioe1) {
197                         fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
198                 }
199                 fireCoreStopped();
200         }
201
202         /**
203          * Connects to the given node.
204          *
205          * @param node
206          *            The node to connect to
207          */
208         public void connectToNode(Node node) {
209                 /* TODO */
210         }
211
212         //
213         // PRIVATE METHODS
214         //
215
216         /**
217          * Loads the configuration.
218          */
219         @SuppressWarnings("unused")
220         private void loadConfig() {
221                 /* TODO */
222         }
223
224         /**
225          * Saves the configuration.
226          */
227         @SuppressWarnings("unused")
228         private void saveConfig() {
229                 /* TODO */
230         }
231
232 }