Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / obfuscator / ObfuscatorIgnores.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpeclipse.obfuscator;
6
7 import java.io.File;
8 import java.io.InputStream;
9
10 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
11
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.jface.dialogs.ErrorDialog;
16
17 /**
18  * <code>ObfuscatorIgnores</code> gives access to the available templates.
19  */
20 public class ObfuscatorIgnores extends ObfuscatorIgnoreSet {
21
22   private static final String DEFAULT_FILE = "default-obfuscator.xml"; //$NON-NLS-1$
23   private static final String TEMPLATE_FILE = "obfuscator.xml"; //$NON-NLS-1$
24
25   /** Singleton. */
26   private static ObfuscatorIgnores fgIgnores;
27         private IProject fProject;
28
29   public ObfuscatorIgnores(IProject project) {
30         fProject = project;
31     try {
32       File templateFile = getTemplateFile();
33       if (templateFile.exists()) {
34         addFromFile(templateFile);
35       } else {
36         addFromStream(getDefaultsAsStream());
37         saveToFile(templateFile);
38       }
39
40     } catch (CoreException e) {
41       PHPeclipsePlugin.log(e);
42       ErrorDialog.openError(null, ObfuscatorMessages.getString("Obfuscator.error.title"), //$NON-NLS-1$
43       e.getMessage(), e.getStatus());
44
45       clear();
46     }
47   }
48   /**
49    * Returns an instance of templates.
50    */
51 //  public static ObfuscatorIgnores getInstance() {
52 //    if (fgIgnores == null)
53 //      fgIgnores = create();
54 //
55 //    return fgIgnores;
56 //  }
57 //
58 //  private static ObfuscatorIgnores create() {
59 //    ObfuscatorIgnores templates = new ObfuscatorIgnores();
60 //
61 //    try {
62 //      File templateFile = getTemplateFile();
63 //      if (templateFile.exists()) {
64 //        templates.addFromFile(templateFile);
65 //      } else {
66 //        templates.addFromStream(getDefaultsAsStream());
67 //        templates.saveToFile(templateFile);
68 //      }
69 //
70 //    } catch (CoreException e) {
71 //      PHPeclipsePlugin.log(e);
72 //      ErrorDialog.openError(null, ObfuscatorMessages.getString("Templates.error.title"), //$NON-NLS-1$
73 //      e.getMessage(), e.getStatus());
74 //
75 //      templates.clear();
76 //    }
77 //
78 //    return templates;
79 //  }
80
81   /**
82    * Resets the template set.
83    */
84   public void reset() throws CoreException {
85     clear();
86     addFromFile(getTemplateFile());
87   }
88
89   /**
90    * Resets the template set with the default templates.
91    */
92   public void restoreDefaults() throws CoreException {
93     clear();
94     addFromStream(getDefaultsAsStream());
95   }
96
97   /**
98    * Saves the template set.
99    */
100   public void save() throws CoreException {
101     saveToFile(getTemplateFile());
102   }
103
104   private InputStream getDefaultsAsStream() {
105     return ObfuscatorIgnores.class.getResourceAsStream(DEFAULT_FILE);
106   }
107
108   private File getTemplateFile() {
109     IPath path = fProject.getLocation(); 
110     // PHPeclipsePlugin.getDefault().getStateLocation();
111     path = path.append(TEMPLATE_FILE);
112
113     return path.toFile();
114   }
115 }