KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > wizards > PsfFilenameStore


1 /*******************************************************************************
2  * Copyright (c) 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.team.internal.ui.wizards;
12
13 import java.util.Vector JavaDoc;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IWorkspace;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.team.internal.ui.TeamUIPlugin;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.IWorkbenchWindow;
26
27 public class PsfFilenameStore {
28     // Most recently used filename is first in the array.
29
// Least recently used filename is at the end of the list.
30
// When the list overflows, items drop off the end.
31
private static final int HISTORY_LENGTH = 10;
32
33     private static final String JavaDoc STORE_SECTION = "ImportPSFDialog"; //$NON-NLS-1$
34
private static final String JavaDoc FILENAMES = "filenames"; //$NON-NLS-1$
35
private static final String JavaDoc PREVIOUS = "previous"; //$NON-NLS-1$
36

37     // If a PSF file was selected when the wizard was opened, then this is it.
38
// This is only a cache; it is not part of the history until the user has used it.
39
private static String JavaDoc _selectedFilename = null;
40
41     private static IDialogSettings _section;
42
43     private PsfFilenameStore() {
44         // All-static
45
}
46
47     public static void setDefaultFromSelection(IWorkbench workbench) {
48         // Scan the workbench for a selected PSF file
49
IWorkbenchWindow wnd = workbench.getActiveWorkbenchWindow();
50         IWorkbenchPage pg = wnd.getActivePage();
51         ISelection sel = pg.getSelection();
52
53         if (!(sel instanceof IStructuredSelection)) {
54             return;
55         }
56         IStructuredSelection selection = (IStructuredSelection)sel;
57
58         Object JavaDoc firstElement = selection.getFirstElement();
59         if (!(firstElement instanceof IAdaptable)) {
60             return;
61         }
62         Object JavaDoc o = ((IAdaptable) firstElement).getAdapter(IResource.class);
63         if (o == null) {
64             return;
65         }
66         IResource resource = (IResource) o;
67
68         if (resource.getType() != IResource.FILE) {
69             return;
70         }
71
72         if (!resource.isAccessible()) {
73             return;
74         }
75
76         String JavaDoc extension = resource.getFileExtension();
77         if (extension == null || !extension.equalsIgnoreCase("psf")) { //$NON-NLS-1$
78
return;
79         }
80
81         IWorkspace workspace = resource.getWorkspace();
82         workspace.getRoot().getFullPath();
83
84         IPath path = resource.getLocation();
85         _selectedFilename = path.toOSString();
86     }
87
88     public static String JavaDoc getSuggestedDefault() {
89         if (_selectedFilename != null) {
90             return _selectedFilename;
91         }
92         return getPrevious();
93     }
94
95     private static String JavaDoc getPrevious() {
96         IDialogSettings section = getSettingsSection();
97         String JavaDoc retval = section.get(PREVIOUS);
98         if (retval == null) {
99             retval = ""; //$NON-NLS-1$
100
}
101         return retval;
102     }
103
104     public static String JavaDoc[] getHistory() {
105         IDialogSettings section = getSettingsSection();
106         String JavaDoc[] arr = section.getArray(FILENAMES);
107         if (arr == null) {
108             arr = new String JavaDoc[0];
109         }
110         return arr;
111     }
112
113     public static void remember(String JavaDoc filename) {
114         Vector JavaDoc filenames = createVector(getHistory());
115         if (filenames.contains(filename)) {
116             // The item is in the list. Remove it and add it back at the
117
// beginning. If it already was at the beginning this will be a
118
// waste of time, but it's not even measurable so I don't care.
119
filenames.remove(filename);
120         }
121         // Most recently used filename goes to the beginning of the list
122
filenames.add(0, filename);
123
124         // Forget any overflowing items
125
while (filenames.size() > HISTORY_LENGTH) {
126             filenames.remove(HISTORY_LENGTH);
127         }
128
129         // Make it an array
130
String JavaDoc[] arr = (String JavaDoc[]) filenames.toArray(new String JavaDoc[filenames.size()]);
131
132         IDialogSettings section = getSettingsSection();
133         section.put(FILENAMES, arr);
134         section.put(PREVIOUS, filename);
135     }
136
137     private static Vector JavaDoc createVector(Object JavaDoc[] arr) {
138         Vector JavaDoc v = new Vector JavaDoc();
139         for (int ix = 0; ix < arr.length; ++ix) {
140             v.add(ix, arr[ix]);
141         }
142         return v;
143     }
144
145     private static IDialogSettings getSettingsSection() {
146         if (_section != null)
147             return _section;
148
149         IDialogSettings settings = TeamUIPlugin.getPlugin().getDialogSettings();
150         _section = settings.getSection(STORE_SECTION);
151         if (_section != null)
152             return _section;
153
154         _section = settings.addNewSection(STORE_SECTION);
155         return _section;
156     }
157 }
158
Popular Tags