1 /*
2  * HomeApplication.java 1 sept. 2006
3  *
4  * Sweet Home 3D, Copyright (c) 2006 Emmanuel PUYBARET / eTeks <info@eteks.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 package com.eteks.sweethome3d.model;
21 
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 
26 /**
27  * Application managing a list of homes displayed at screen.
28  * @author Emmanuel Puybaret
29  */
30 public abstract class HomeApplication {
31   private List<Home> homes = new ArrayList<Home>();
32   private final CollectionChangeSupport<Home> homesChangeSupport =
33                              new CollectionChangeSupport<Home>(this);
34 
35   /**
36    * Adds the home <code>listener</code> in parameter to this application.
37    */
addHomesListener(CollectionListener<Home> listener)38   public void addHomesListener(CollectionListener<Home> listener) {
39     this.homesChangeSupport.addCollectionListener(listener);
40   }
41 
42   /**
43    * Removes the home <code>listener</code> in parameter from this application.
44    */
removeHomesListener(CollectionListener<Home> listener)45   public void removeHomesListener(CollectionListener<Home> listener) {
46     this.homesChangeSupport.removeCollectionListener(listener);
47   }
48 
49   /**
50    * Returns a new home.
51    * @return a new home with wall heights equal to the one in user preferences.
52    * @since 2.2
53    */
createHome()54   public Home createHome() {
55     return new Home(getUserPreferences().getNewWallHeight());
56   }
57 
58   /**
59    * Returns an unmodifiable collection of the homes of this application.
60    */
getHomes()61   public List<Home> getHomes() {
62     return Collections.unmodifiableList(this.homes);
63   }
64 
65   /**
66    * Adds a given <code>home</code> to the homes list of this application.
67    * Once the <code>home</code> is added, home listeners added
68    * to this application will receive a
69    * {@link CollectionListener#collectionChanged(CollectionEvent) collectionChanged}
70    * notification, with an {@link CollectionEvent#getType() event type}
71    * equal to {@link CollectionEvent.Type#ADD ADD}.
72    */
addHome(Home home)73   public void addHome(Home home) {
74     this.homes = new ArrayList<Home>(this.homes);
75     this.homes.add(home);
76     this.homesChangeSupport.fireCollectionChanged(home, this.homes.size() - 1, CollectionEvent.Type.ADD);
77   }
78 
79   /**
80    * Removes a given <code>home</code> from the homes list  of this application.
81    * Once the <code>home</code> is removed, home listeners added
82    * to this application will receive a
83    * {@link CollectionListener#collectionChanged(CollectionEvent) collectionChanged}
84    * notification, with an {@link CollectionEvent#getType() event type}
85    * equal to {@link CollectionEvent.Type#DELETE DELETE}.
86    */
deleteHome(Home home)87   public void deleteHome(Home home) {
88     int index = this.homes.indexOf(home);
89     if (index != -1) {
90       this.homes = new ArrayList<Home>(this.homes);
91       this.homes.remove(index);
92       this.homesChangeSupport.fireCollectionChanged(home, index, CollectionEvent.Type.DELETE);
93     }
94   }
95 
96   /**
97    * Returns the default recorder able to write and read homes.
98    */
getHomeRecorder()99   public abstract HomeRecorder getHomeRecorder();
100 
101   /**
102    * Returns a recorder of a given <code>type</code> able to write and read homes.
103    * Subclasses may override this method to return a recorder matching <code>type</code>.
104    * @param type  a hint for the application to choose the returned recorder.
105    * @return the default recorder able to write and read homes.
106    * @since 1.8
107    */
getHomeRecorder(HomeRecorder.Type type)108   public HomeRecorder getHomeRecorder(HomeRecorder.Type type) {
109     return getHomeRecorder();
110   }
111 
112   /**
113    * Returns user preferences.
114    */
getUserPreferences()115   public abstract UserPreferences getUserPreferences();
116 
117   /**
118    * Returns the name of this application. Default implementation returns <i>Sweet Home 3D</i>.
119    * @since 1.6
120    */
getName()121   public String getName() {
122     return "Sweet Home 3D";
123   }
124 
125   /**
126    * Returns information about the version of this application.
127    * Default implementation returns an empty string.
128    * @since 1.6
129    */
getVersion()130   public String getVersion() {
131     return "";
132   }
133 
134   /**
135    * Returns the id of this application.
136    * Default implementation returns null.
137    * @since 4.0
138    */
getId()139   public String getId() {
140     return null;
141   }
142 }
143