View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/devgui/ProjectData.java,v 1.14 2004/10/11 08:54:59 hkollmann Exp $
3    * $Revision: 1.14 $
4    * $Date: 2004/10/11 08:54:59 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  /*
25   *  Project.java
26   *
27   *  Created on 26. April 2001, 15:14
28   */
29  package org.dbforms.devgui;
30  
31  import java.io.*;
32  
33  import java.util.*;
34  
35  
36  
37  /***
38   * DOCUMENT ME!
39   *
40   * @author Joe Peer Changes: - 2002-03-4: usage of default property and
41   *         constant string values via new Interface Propertynames (dikr)
42   *
43   */
44  public class ProjectData implements Serializable, PropertyNames {
45     private static Properties defaultProps;
46  
47     // contains default values for properties
48     // set default property values:
49     static {
50        defaultProps = new Properties();
51  
52        defaultProps.setProperty(INCLUDE_SCHEMANAME, FALSESTRING);
53  
54        defaultProps.setProperty(INCLUDE_CATALOGNAME, FALSESTRING);
55  
56        defaultProps.setProperty(AUTOCOMMIT_MODE, TRUESTRING);
57  
58        defaultProps.setProperty(CATALOG_SELECTION, ALL);
59  
60        defaultProps.setProperty(SCHEMA_SELECTION, ALL);
61  
62        defaultProps.setProperty(TABLE_SELECTION, ALL);
63  
64        defaultProps.setProperty(EXAMINE_TABLES, TRUESTRING);
65  
66        defaultProps.setProperty(EXAMINE_VIEWS, TRUESTRING);
67  
68        defaultProps.setProperty(EXAMINE_SYSTABS, FALSESTRING);
69  
70        defaultProps.setProperty(WRITE_STD_TYPENAMES, FALSESTRING);
71  
72        defaultProps.setProperty(FOREIGNKEY_DETECTION, USE_GETIMPORTEDKEYS);
73     }
74  
75     private File       file;
76     private Properties props;
77     private boolean    unsavedChanges = false;
78  
79     /***
80      * Creates new Project
81      */
82     public ProjectData() {
83        this.props = new Properties(defaultProps);
84  
85        String dbFormsHomeStr = System.getProperty("DBFORMS_HOME");
86  
87        if (dbFormsHomeStr != null) {
88           setProperty(STYLESHEET_DIR,
89                       dbFormsHomeStr
90                       + System.getProperties().getProperty("file.separator")
91                       + "xsl-stylesheets");
92        }
93  
94        unsavedChanges = false;
95     }
96  
97  
98     /***
99      * Constructor for the ProjectData object
100     *
101     * @param props Description of the Parameter
102     */
103    public ProjectData(Properties props) {
104       this.props = props;
105 
106       unsavedChanges = false;
107 
108       // If a user hasn't specified a STYLESHEET_DIR, then guess at one!
109       String dbFormsHomeStr = props.getProperty(STYLESHEET_DIR);
110 
111       if ("".equals(dbFormsHomeStr)) {
112          dbFormsHomeStr = System.getProperty("DBFORMS_HOME");
113 
114          if (dbFormsHomeStr != null) {
115             setProperty(STYLESHEET_DIR,
116                         dbFormsHomeStr
117                         + System.getProperties().getProperty("file.separator")
118                         + "xsl-stylesheets");
119 
120             unsavedChanges = true;
121          }
122       }
123    }
124 
125    /***
126     * Sets the file attribute of the ProjectData object
127     *
128     * @param file The new file value
129     */
130    public void setFile(File file) {
131       this.file = file;
132    }
133 
134 
135    /***
136     * Gets the file attribute of the ProjectData object
137     *
138     * @return The file value
139     */
140    public File getFile() {
141       return file;
142    }
143 
144 
145    /***
146     * Sets the property attribute of the ProjectData object
147     *
148     * @param key The new property value
149     * @param value The new property value
150     */
151    public void setProperty(String key,
152                            String value) {
153       String oldValue = getProperty(key);
154 
155       if (!oldValue.equals(value)) {
156          props.setProperty(key, value);
157 
158          this.unsavedChanges = true;
159 
160          System.out.println("unsaved state look like this:" + this.toString());
161       }
162    }
163 
164 
165    /***
166     * Gets the property attribute of the ProjectData object
167     *
168     * @param prop Description of the Parameter
169     *
170     * @return The property value
171     */
172    public String getProperty(String prop) {
173       return props.getProperty(prop, "");
174 
175       // return default value "" instead of null if not found
176    }
177 
178 
179    /***
180     * Gets the unsaved attribute of the ProjectData object
181     *
182     * @return The unsaved value
183     */
184    public boolean isUnsaved() {
185       return unsavedChanges;
186    }
187 
188 
189    /***
190     * Description of the Method
191     *
192     * @param f Description of the Parameter
193     *
194     * @return Description of the Return Value
195     *
196     * @exception IOException Description of the Exception
197     */
198    public static ProjectData loadFromDisc(File f) throws IOException {
199       Properties l_props = new Properties(defaultProps);
200 
201       l_props.load(new FileInputStream(f));
202 
203       ProjectData pd = new ProjectData(l_props);
204 
205       pd.setFile(f);
206 
207       return pd;
208    }
209 
210 
211    /***
212     * Description of the Method
213     *
214     * @param f Description of the Parameter
215     *
216     * @exception IOException Description of the Exception
217     */
218    public void storeToDisc(File f) throws IOException {
219       this.props.store(new FileOutputStream(f), "DbForms DevGUI Property File");
220 
221       this.file = f;
222 
223       this.unsavedChanges = false;
224    }
225 
226 
227    /***
228     * Description of the Method
229     *
230     * @return Description of the Return Value
231     */
232    public String toString() {
233       StringBuffer buf = new StringBuffer();
234 
235       Enumeration  e = props.propertyNames();
236 
237       boolean      first = true;
238 
239       while (e.hasMoreElements()) {
240          if (first) {
241             first = false;
242          } else {
243             buf.append(", ");
244          }
245 
246          String aPropsName = (String) e.nextElement();
247 
248          buf.append(aPropsName);
249 
250          buf.append("=");
251 
252          buf.append(this.getProperty(aPropsName));
253       }
254 
255       return buf.toString();
256    }
257 }