KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > ScopeSet


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.ui.internal.views;
12
13 import java.io.*;
14
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.help.ui.internal.*;
17 import org.eclipse.jface.preference.*;
18
19 /**
20  * Federated search scope.
21  */

22 public class ScopeSet {
23     public static final String JavaDoc SCOPE_DIR_NAME = "scope_sets"; //$NON-NLS-1$
24
private static final String JavaDoc KEY_DEFAULT = "__DEFAULT__"; //$NON-NLS-1$
25
public static final String JavaDoc EXT = ".pref"; //$NON-NLS-1$
26
private String JavaDoc name;
27     private PreferenceStore preferenceStore;
28     private boolean needsSaving;
29     private int defaultSet = -1;
30     
31     public ScopeSet() {
32         this(Messages.ScopeSet_default);
33         defaultSet = 1;
34     }
35     
36     public ScopeSet(String JavaDoc name) {
37         this.needsSaving = true;
38         this.name = name;
39     }
40     
41     public boolean isEditable() {
42         return !isDefault();
43     }
44     
45     public boolean isDefault() {
46         if (defaultSet==1)
47             return true;
48         return getPreferenceStore().getBoolean(KEY_DEFAULT);
49     }
50     
51     public boolean isImplicit() {
52         return false;
53     }
54
55     public ScopeSet(ScopeSet set) {
56         this(set.getName()+"_new"); //$NON-NLS-1$
57
copyFrom(set);
58     }
59     
60     public void copyFrom(ScopeSet set) {
61         copy((PreferenceStore)set.getPreferenceStore());
62     }
63     
64     public void dispose() {
65         File file = new File(getFileName(name));
66         if (file.exists())
67             file.delete();
68     }
69
70     public IPreferenceStore getPreferenceStore() {
71         if (preferenceStore==null) {
72             preferenceStore = new PreferenceStore(getFileName(this.name));
73             try {
74                 File file = new File(getFileName(this.name));
75                 if (file.exists()) {
76                     preferenceStore.load();
77                 }
78             }
79             catch (IOException e) {
80                 String JavaDoc message = Messages.bind(Messages.ScopeSet_errorLoading, name);
81                 HelpUIPlugin.logError(message, e);
82             }
83         }
84         return preferenceStore;
85     }
86     
87     protected String JavaDoc encodeFileName(String JavaDoc name) {
88         return name;
89     }
90
91     private String JavaDoc getFileName(String JavaDoc name) {
92         IPath location = HelpUIPlugin.getDefault().getStateLocation();
93         location = location.append(SCOPE_DIR_NAME);
94         location = location.append(encodeFileName(name)+getExtension());
95         return location.toOSString();
96     }
97     
98     protected String JavaDoc getExtension() {
99         return EXT;
100     }
101
102     private void copy(PreferenceStore store) {
103         try {
104             File file = File.createTempFile("sset", null); //$NON-NLS-1$
105
FileOutputStream fos = new FileOutputStream(file);
106             store.save(fos, ""); //$NON-NLS-1$
107
fos.close();
108             FileInputStream fis = new FileInputStream(file);
109             getPreferenceStore();
110             preferenceStore.load(fis);
111             //when we clone the defult set, we should
112
//clear the default marker
113
preferenceStore.setValue(KEY_DEFAULT, false);
114             fis.close();
115         }
116         catch (IOException e) {
117         }
118     }
119     /**
120      * @return Returns the name.
121      */

122     public String JavaDoc getName() {
123         return name;
124     }
125     /**
126      * @param name The name to set.
127      */

128     public void setName(String JavaDoc name) {
129         String JavaDoc oldFileName = getFileName(this.name);
130         File oldFile = new File(oldFileName);
131         if (oldFile.exists()) {
132             // store under the old name already exists
133
if (preferenceStore==null) {
134                 // just rename the file
135
oldFile.renameTo(new File(getFileName(name)));
136             }
137             else {
138                 // remove the old file, set the new file name,
139
// then save to create the new file
140
oldFile.delete();
141                 preferenceStore.setFilename(getFileName(name));
142                 try {
143                     preferenceStore.save();
144                 }
145                 catch (IOException e) {
146                     String JavaDoc message = Messages.bind(Messages.ScopeSet_errorSaving, name);
147                     HelpUIPlugin.logError(message, e);
148                 }
149             }
150         }
151         this.name = name;
152     }
153
154     public void save() {
155         getPreferenceStore();
156         if (preferenceStore.needsSaving() || needsSaving) {
157             try {
158                 if (defaultSet != -1)
159                     preferenceStore.setValue(KEY_DEFAULT, defaultSet>0);
160                 preferenceStore.save();
161                 needsSaving = false;
162             }
163             catch (IOException e) {
164                 String JavaDoc message = Messages.bind(Messages.ScopeSet_errorSaving, name);
165                 HelpUIPlugin.logError(message, e);
166             }
167         }
168     }
169
170     public boolean getEngineEnabled(EngineDescriptor desc) {
171         IPreferenceStore store = getPreferenceStore();
172         String JavaDoc key = getMasterKey(desc.getId());
173         if (store.contains(key))
174             return store.getBoolean(key);
175         store.setValue(key, desc.isEnabled());
176         return desc.isEnabled();
177     }
178     public void setEngineEnabled(EngineDescriptor desc, boolean value) {
179         IPreferenceStore store = getPreferenceStore();
180         String JavaDoc key = getMasterKey(desc.getId());
181         store.setValue(key, value);
182     }
183     public static String JavaDoc getMasterKey(String JavaDoc id) {
184         return id + ".master"; //$NON-NLS-1$
185
}
186     public static String JavaDoc getLabelKey(String JavaDoc id) {
187         return id+".label"; //$NON-NLS-1$
188
}
189     public static String JavaDoc getDescKey(String JavaDoc id) {
190         return id+".desc"; //$NON-NLS-1$
191
}
192 }
193
Popular Tags