1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.core.runtime.IPath; 14 import org.eclipse.core.runtime.Path; 15 import org.eclipse.core.runtime.SafeRunner; 16 import org.eclipse.jface.action.ContributionItem; 17 import org.eclipse.jface.action.IMenuListener; 18 import org.eclipse.jface.action.IMenuManager; 19 import org.eclipse.jface.action.MenuManager; 20 import org.eclipse.jface.dialogs.MessageDialog; 21 import org.eclipse.jface.util.SafeRunnable; 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.osgi.util.TextProcessor; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.events.SelectionAdapter; 26 import org.eclipse.swt.events.SelectionEvent; 27 import org.eclipse.swt.widgets.Menu; 28 import org.eclipse.swt.widgets.MenuItem; 29 import org.eclipse.ui.IEditorDescriptor; 30 import org.eclipse.ui.IEditorInput; 31 import org.eclipse.ui.IWorkbenchPage; 32 import org.eclipse.ui.IWorkbenchWindow; 33 import org.eclipse.ui.PartInitException; 34 35 38 public class ReopenEditorMenu extends ContributionItem { 39 private IWorkbenchWindow window; 40 41 private EditorHistory history; 42 43 private boolean showSeparator; 44 45 private boolean dirty = true; 46 47 private IMenuListener menuListener = new IMenuListener() { 48 public void menuAboutToShow(IMenuManager manager) { 49 manager.markDirty(); 50 dirty = true; 51 } 52 }; 53 54 private static final int MAX_TEXT_LENGTH = 40; 56 57 private static final int MAX_MNEMONIC_SIZE = 9; 59 60 66 public ReopenEditorMenu(IWorkbenchWindow window, String id, 67 boolean showSeparator) { 68 super(id); 69 this.window = window; 70 this.showSeparator = showSeparator; 71 history = ((Workbench) window.getWorkbench()).getEditorHistory(); 72 } 73 74 78 private String calcText(int index, EditorHistoryItem item) { 79 StringBuffer sb = new StringBuffer (); 80 81 int mnemonic = index + 1; 82 sb.append(mnemonic); 83 if (mnemonic <= MAX_MNEMONIC_SIZE) { 84 sb.insert(sb.length() - (mnemonic + "").length(), '&'); } 86 sb.append(" "); 88 String fileName = item.getName(); 93 String pathName = item.getToolTipText(); 94 if (pathName.equals(fileName)) { 95 pathName = ""; } 99 IPath path = new Path(pathName); 100 if (path.segmentCount() > 1 102 && path.segment(path.segmentCount() - 1).equals(fileName)) { 103 path = path.removeLastSegments(1); 104 pathName = path.toString(); 105 } 106 107 if ((fileName.length() + pathName.length()) <= (MAX_TEXT_LENGTH - 4)) { 108 sb.append(fileName); 110 if (pathName.length() > 0) { 111 sb.append(" ["); sb.append(pathName); 113 sb.append("]"); } 115 } else { 116 int length = fileName.length(); 118 if (length > MAX_TEXT_LENGTH) { 119 sb.append(fileName.substring(0, MAX_TEXT_LENGTH - 3)); 121 sb.append("..."); } else if (length > MAX_TEXT_LENGTH - 7) { 123 sb.append(fileName); 124 } else { 125 sb.append(fileName); 126 int segmentCount = path.segmentCount(); 127 if (segmentCount > 0) { 128 length += 7; 130 sb.append(" ["); 132 int i = 0; 134 while (i < segmentCount && length < MAX_TEXT_LENGTH) { 135 String segment = path.segment(i); 136 if (length + segment.length() < MAX_TEXT_LENGTH) { 137 sb.append(segment); 138 sb.append(IPath.SEPARATOR); 139 length += segment.length() + 1; 140 i++; 141 } else if (i == 0) { 142 sb.append(segment.substring(0, MAX_TEXT_LENGTH 144 - length)); 145 length = MAX_TEXT_LENGTH; 146 break; 147 } else { 148 break; 149 } 150 } 151 152 sb.append("..."); 154 i = segmentCount - 1; 155 while (i > 0 && length < MAX_TEXT_LENGTH) { 157 String segment = path.segment(i); 158 if (length + segment.length() < MAX_TEXT_LENGTH) { 159 sb.append(IPath.SEPARATOR); 160 sb.append(segment); 161 length += segment.length() + 1; 162 i--; 163 } else { 164 break; 165 } 166 } 167 168 sb.append("]"); } 170 } 171 } 172 return TextProcessor.process(sb.toString(), TextProcessor.getDefaultDelimiters() + "[]"); } 174 175 179 public void fill(final Menu menu, int index) { 180 if (window.getActivePage() == null 181 || window.getActivePage().getPerspective() == null) { 182 return; 183 } 184 185 if (getParent() instanceof MenuManager) { 186 ((MenuManager) getParent()).addMenuListener(menuListener); 187 } 188 189 int itemsToShow = WorkbenchPlugin.getDefault().getPreferenceStore() 190 .getInt(IPreferenceConstants.RECENT_FILES); 191 if (itemsToShow == 0) { 192 return; 193 } 194 195 EditorHistoryItem[] historyItems = history.getItems(); 197 198 int n = Math.min(itemsToShow, historyItems.length); 199 if (n <= 0) { 200 return; 201 } 202 203 if (showSeparator) { 204 new MenuItem(menu, SWT.SEPARATOR, index); 205 ++index; 206 } 207 208 final int menuIndex[] = new int[] { index }; 209 210 for (int i = 0; i < n; i++) { 211 final EditorHistoryItem item = historyItems[i]; 212 final int historyIndex = i; 213 SafeRunner.run(new SafeRunnable() { 214 public void run() throws Exception { 215 String text = calcText(historyIndex, item); 216 MenuItem mi = new MenuItem(menu, SWT.PUSH, menuIndex[0]); 217 ++menuIndex[0]; 218 mi.setText(text); 219 mi.addSelectionListener(new SelectionAdapter() { 220 public void widgetSelected(SelectionEvent e) { 221 open(item); 222 } 223 }); 224 } 225 226 public void handleException(Throwable e) { 227 WorkbenchPlugin.log(getClass(), "fill", e); } 231 }); 232 } 233 dirty = false; 234 } 235 236 239 public boolean isDirty() { 240 return dirty; 241 } 242 243 246 public boolean isDynamic() { 247 return true; 248 } 249 250 253 private void open(EditorHistoryItem item) { 254 IWorkbenchPage page = window.getActivePage(); 255 if (page != null) { 256 try { 257 String itemName = item.getName(); 258 if (!item.isRestored()) { 259 item.restoreState(); 260 } 261 IEditorInput input = item.getInput(); 262 IEditorDescriptor desc = item.getDescriptor(); 263 if (input == null || desc == null) { 264 String title = WorkbenchMessages.OpenRecent_errorTitle; 265 String msg = NLS.bind(WorkbenchMessages.OpenRecent_unableToOpen, itemName ); 266 MessageDialog.openWarning(window.getShell(), title, msg); 267 history.remove(item); 268 } else { 269 page.openEditor(input, desc.getId()); 270 } 271 } catch (PartInitException e2) { 272 String title = WorkbenchMessages.OpenRecent_errorTitle; 273 MessageDialog.openWarning(window.getShell(), title, e2 274 .getMessage()); 275 history.remove(item); 276 } 277 } 278 } 279 280 } 281 | Popular Tags |