1 11 package org.eclipse.team.internal.ui.wizards; 12 13 import java.util.Vector ; 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 private static final int HISTORY_LENGTH = 10; 32 33 private static final String STORE_SECTION = "ImportPSFDialog"; private static final String FILENAMES = "filenames"; private static final String PREVIOUS = "previous"; 37 private static String _selectedFilename = null; 40 41 private static IDialogSettings _section; 42 43 private PsfFilenameStore() { 44 } 46 47 public static void setDefaultFromSelection(IWorkbench workbench) { 48 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 firstElement = selection.getFirstElement(); 59 if (!(firstElement instanceof IAdaptable)) { 60 return; 61 } 62 Object 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 extension = resource.getFileExtension(); 77 if (extension == null || !extension.equalsIgnoreCase("psf")) { 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 getSuggestedDefault() { 89 if (_selectedFilename != null) { 90 return _selectedFilename; 91 } 92 return getPrevious(); 93 } 94 95 private static String getPrevious() { 96 IDialogSettings section = getSettingsSection(); 97 String retval = section.get(PREVIOUS); 98 if (retval == null) { 99 retval = ""; } 101 return retval; 102 } 103 104 public static String [] getHistory() { 105 IDialogSettings section = getSettingsSection(); 106 String [] arr = section.getArray(FILENAMES); 107 if (arr == null) { 108 arr = new String [0]; 109 } 110 return arr; 111 } 112 113 public static void remember(String filename) { 114 Vector filenames = createVector(getHistory()); 115 if (filenames.contains(filename)) { 116 filenames.remove(filename); 120 } 121 filenames.add(0, filename); 123 124 while (filenames.size() > HISTORY_LENGTH) { 126 filenames.remove(HISTORY_LENGTH); 127 } 128 129 String [] arr = (String []) filenames.toArray(new String [filenames.size()]); 131 132 IDialogSettings section = getSettingsSection(); 133 section.put(FILENAMES, arr); 134 section.put(PREVIOUS, filename); 135 } 136 137 private static Vector createVector(Object [] arr) { 138 Vector v = new Vector (); 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 |