add project manager
[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          * Creates a new core.
48          */
49         public Core() {
50         }
51
52         //
53         // LISTENER MANAGEMENT
54         //
55
56         /**
57          * Adds the given listener to the list of registered listeners.
58          * 
59          * @param coreListener
60          *            The listener to add
61          */
62         public void addCoreListener(CoreListener coreListener) {
63                 coreListeners.add(coreListener);
64         }
65
66         /**
67          * Removes the given listener from the list of registered listeners.
68          * 
69          * @param coreListener
70          *            The listener to remove
71          */
72         public void removeCoreListener(CoreListener coreListener) {
73                 coreListeners.remove(coreListener);
74         }
75
76         /**
77          * Notifies all core listeners that loading the projects from the given
78          * directory has failed.
79          * 
80          * @param directory
81          *            The directory the projects were tried to load from
82          */
83         private void fireLoadingProjectsFailed(String directory) {
84                 for (CoreListener coreListener: coreListeners) {
85                         coreListener.loadingProjectsFailed(directory);
86                 }
87         }
88
89         /**
90          * Notifies all core listeners that the core has loaded and is ready to run.
91          */
92         private void fireCoreLoaded() {
93                 for (CoreListener coreListener: coreListeners) {
94                         coreListener.coreLoaded();
95                 }
96         }
97
98         //
99         // ACCESSORS
100         //
101
102         /**
103          * Returns the project manager.
104          * 
105          * @return The project manager
106          */
107         public ProjectManager getProjectManager() {
108                 return projectManager;
109         }
110
111         /**
112          * Sets the project manager to use.
113          * 
114          * @param projectManager
115          *            The project manager to use
116          */
117         public void setProjectManager(ProjectManager projectManager) {
118                 this.projectManager = projectManager;
119         }
120
121         /**
122          * Returns the list of all configured nodes.
123          * 
124          * @return All configured nodes
125          */
126         public List<Node> getNodes() {
127                 return configuredNodes;
128         }
129
130         /**
131          * Returns whether the core is currently connected to the given node.
132          * 
133          * @param node
134          *            The node to check
135          * @return <code>true</code> if the core is currently connected to the
136          *         node, <code>false</code> otherwise
137          */
138         public boolean isNodeConnected(Node node) {
139                 return connectedNodes.contains(node);
140         }
141
142         //
143         // ACTIONS
144         //
145
146         /**
147          * Starts the core.
148          */
149         public void start() {
150                 try {
151                         projectManager.load();
152                 } catch (IOException ioe1) {
153                         fireLoadingProjectsFailed(projectManager.getDirectory());
154                 }
155                 fireCoreLoaded();
156         }
157
158         /**
159          * Connects to the given node.
160          * 
161          * @param node
162          *            The node to connect to
163          */
164         public void connectToNode(Node node) {
165         }
166
167 }