001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.cnd.makeproject.api.compilers;
042:
043: import java.io.File;
044: import java.io.FileInputStream;
045: import java.io.FileOutputStream;
046: import java.io.IOException;
047: import java.io.ObjectInputStream;
048: import java.io.ObjectOutputStream;
049: import java.io.Serializable;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.Vector;
053:
054: public class PersistentList extends Vector implements Serializable {
055: private static final long serialVersionUID = -8893123456464434693L;
056:
057: /** Creates a new instance of PersistentList */
058: public PersistentList() {
059: }
060:
061: public PersistentList(List values) {
062: super (values);
063: }
064:
065: private static String getRoot() {
066: String dir = System.getProperty("netbeans.user")
067: + "/config/cnd-compilers/"; // NOI18N
068: return dir;
069: }
070:
071: /*
072: * Add a string only if not already in list
073: */
074: public void addUnique(String string) {
075: if (!inList(string)) {
076: super .add(string);
077: }
078:
079: }
080:
081: protected boolean inList(String path) {
082: if (path == null) {
083: return false;
084: }
085: Iterator iterator = iterator();
086: while (iterator.hasNext()) {
087: if (path.equals((String) iterator.next())) {
088: return true;
089: }
090: }
091: return false;
092: }
093:
094: /**
095: * For serialization
096: */
097: public void saveList(ObjectOutputStream out) {
098: try {
099: out.writeObject(this );
100: } catch (IOException ioe) {
101: System.out
102: .println("PersistentList - saveList - ioe " + ioe); // NOI18N
103: }
104: }
105:
106: public void saveList(String name) {
107: File dirfile = new File(getRoot());
108: if (!dirfile.exists()) {
109: dirfile.mkdirs();
110: }
111:
112: try {
113: ObjectOutputStream oos = new ObjectOutputStream(
114: new FileOutputStream(getRoot() + name));
115: saveList(oos);
116: oos.flush();
117: oos.close();
118: } catch (Exception e) {
119: System.out.println("e " + e); // NOI18N
120: }
121: }
122:
123: /**
124: * For serialization
125: */
126: public static PersistentList restoreList(ObjectInputStream in)
127: throws Exception {
128: PersistentList list = null;
129: try {
130: list = (PersistentList) in.readObject();
131: } catch (Exception e) {
132: System.err.println("PersistentList - restorePicklist - e "
133: + e); // NOI18N
134: throw e;
135: }
136: return list;
137: }
138:
139: public static PersistentList restoreList(String name) {
140: PersistentList ret = null;
141: File file = new File(getRoot() + File.separator + name);
142: if (!file.exists()) {
143: ; // nothing
144: } else {
145: try {
146: ObjectInputStream ois = new ObjectInputStream(
147: new FileInputStream(getRoot() + name));
148: ret = restoreList(ois);
149: ois.close();
150: } catch (Exception e) {
151: System.err.println("PersistentList - restoreList - e"
152: + e); // NOI18N
153: System.err.println(getRoot() + name + " deleted"); // NOI18N
154: file.delete();
155: }
156: }
157: return ret;
158: }
159:
160: // private void writeObject(java.io.ObjectOutputStream out) throws IOException {
161: // try {
162: // out.writeObject(new Integer(size()));
163: // for (int i = 0; i < size(); i++)
164: // out.writeObject(elementAt(i));
165: // }
166: // catch (IOException ioe) {
167: // System.err.println("DefaultPicklistModel - writeObject - ioe " + ioe); // NOI18N
168: // throw ioe;
169: // }
170: // }
171:
172: // private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
173: // try {
174: // int size = ((Integer)in.readObject()).intValue();
175: // for (int i = 0; i < size; i++)
176: // add(((String)in.readObject()));
177: // }
178: // catch (IOException e) {
179: // System.err.println("DefaultPicklistModel - readObject - e " + e); // NOI18N
180: // throw e;
181: // }
182: // catch (ClassNotFoundException e) {
183: // System.err.println("DefaultPicklistModel - readObject - e " + e); // NOI18N
184: // throw e;
185: // }
186: // }
187: }
|