1 11 package org.eclipse.ui.internal.registry; 12 13 import java.io.BufferedReader ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.InputStreamReader ; 19 import java.io.Reader ; 20 import java.io.StringReader ; 21 import java.io.StringWriter ; 22 import java.io.Writer ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.eclipse.core.runtime.IConfigurationElement; 28 import org.eclipse.core.runtime.IExtension; 29 import org.eclipse.core.runtime.IPath; 30 import org.eclipse.core.runtime.IStatus; 31 import org.eclipse.core.runtime.Platform; 32 import org.eclipse.core.runtime.Status; 33 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 34 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 35 import org.eclipse.jface.dialogs.IDialogSettings; 36 import org.eclipse.jface.preference.IPreferenceStore; 37 import org.eclipse.jface.resource.StringConverter; 38 import org.eclipse.jface.util.IPropertyChangeListener; 39 import org.eclipse.jface.util.PropertyChangeEvent; 40 import org.eclipse.ui.IMemento; 41 import org.eclipse.ui.IPerspectiveDescriptor; 42 import org.eclipse.ui.IPerspectiveRegistry; 43 import org.eclipse.ui.IWorkbenchPage; 44 import org.eclipse.ui.IWorkbenchPreferenceConstants; 45 import org.eclipse.ui.IWorkbenchWindow; 46 import org.eclipse.ui.PlatformUI; 47 import org.eclipse.ui.WorkbenchException; 48 import org.eclipse.ui.XMLMemento; 49 import org.eclipse.ui.internal.ClosePerspectiveAction; 50 import org.eclipse.ui.internal.IPreferenceConstants; 51 import org.eclipse.ui.internal.Workbench; 52 import org.eclipse.ui.internal.WorkbenchMessages; 53 import org.eclipse.ui.internal.WorkbenchPage; 54 import org.eclipse.ui.internal.WorkbenchPlugin; 55 import org.eclipse.ui.internal.misc.StatusUtil; 56 import org.eclipse.ui.internal.util.PrefUtil; 57 import org.eclipse.ui.statushandlers.StatusManager; 58 59 62 public class PerspectiveRegistry implements IPerspectiveRegistry, 63 IExtensionChangeHandler { 64 private String defaultPerspID; 65 66 private static final String EXT = "_persp.xml"; 68 private static final String ID_DEF_PERSP = "PerspectiveRegistry.DEFAULT_PERSP"; 70 private static final String PERSP = "_persp"; 72 private static final char SPACE_DELIMITER = ' '; 73 74 private List perspectives = new ArrayList (10); 75 76 private ArrayList perspToRemove = new ArrayList (5); 78 79 private IPropertyChangeListener preferenceListener; 80 81 84 public PerspectiveRegistry() { 85 IExtensionTracker tracker = PlatformUI.getWorkbench() 86 .getExtensionTracker(); 87 tracker.registerHandler(this, null); 88 89 initializePreferenceChangeListener(); 90 WorkbenchPlugin.getDefault().getPreferenceStore() 91 .addPropertyChangeListener(preferenceListener); 92 93 } 94 95 98 private void initializePreferenceChangeListener() { 99 preferenceListener = new IPropertyChangeListener() { 100 public void propertyChange(PropertyChangeEvent event) { 101 106 if (event.getProperty().endsWith(PERSP)) { 107 108 mergePerspectives(event); 109 } else if (event.getProperty().equals( 110 IPreferenceConstants.PERSPECTIVES)) { 111 112 updatePreferenceList((IPreferenceStore) event.getSource()); 113 } 114 } 115 116 private void mergePerspectives(PropertyChangeEvent event) { 117 IPreferenceStore store = (IPreferenceStore) event.getSource(); 118 if (event.getNewValue() == null 119 || event.getNewValue().equals("")) { 125 126 IPerspectiveDescriptor[] perspectiveList = getPerspectives(); 128 for (int i = 0; i < perspectiveList.length; i++) { 129 String id = perspectiveList[i].getId(); 130 if (event.getProperty().equals(id + PERSP)) { if (!perspToRemove.contains(id)) { store.setValue(id + PERSP, (String ) event 136 .getOldValue()); 137 } else { perspToRemove.remove(id); 139 } 140 } 141 } 142 } else if ((event.getOldValue() == null || event.getOldValue() 143 .equals(""))) { 145 149 150 String id = event.getProperty().substring(0, 151 event.getProperty().lastIndexOf(PERSP)); 152 if (findPerspectiveWithId(id) == null) { 153 PerspectiveDescriptor desc = new PerspectiveDescriptor( 156 null, null, null); 157 StringReader reader = new StringReader ((String ) event 158 .getNewValue()); 159 try { 160 XMLMemento memento = XMLMemento 161 .createReadRoot(reader); 162 desc.restoreState(memento); 163 addPerspective(desc); 164 } catch (WorkbenchException e) { 165 unableToLoadPerspective(e.getStatus()); 166 } 167 } 168 } 169 170 updatePreferenceList(store); 171 } 172 173 179 private void updatePreferenceList(IPreferenceStore store) { 180 IPerspectiveDescriptor[] perspectiveList = getPerspectives(); 181 StringBuffer perspBuffer = new StringBuffer (); 182 for (int i = 0; i < perspectiveList.length; i++) { 183 PerspectiveDescriptor desc = (PerspectiveDescriptor) perspectiveList[i]; 184 if (hasCustomDefinition(desc)) { 185 perspBuffer.append(desc.getId()) 186 .append(SPACE_DELIMITER); 187 } 188 } 189 String newList = perspBuffer.toString().trim(); 190 store.setValue(IPreferenceConstants.PERSPECTIVES, newList); 191 } 192 }; 193 } 194 195 200 public void addPerspective(PerspectiveDescriptor desc) { 201 if (desc == null) { 202 return; 203 } 204 add(desc); 205 } 206 207 210 private void add(PerspectiveDescriptor desc) { 211 perspectives.add(desc); 212 IConfigurationElement element = desc.getConfigElement(); 213 if (element != null) { 214 PlatformUI.getWorkbench().getExtensionTracker().registerObject( 215 element.getDeclaringExtension(), desc, 216 IExtensionTracker.REF_WEAK); 217 } 218 } 219 220 230 public PerspectiveDescriptor createPerspective(String label, 231 PerspectiveDescriptor originalDescriptor) { 232 if (!validateLabel(label)) { 234 return null; 235 } 236 if (findPerspectiveWithLabel(label) != null) { 237 return null; 238 } 239 240 String id = label.replace(' ', '_'); 242 id = id.trim(); 243 244 PerspectiveDescriptor desc = new PerspectiveDescriptor(id, label, 246 originalDescriptor); 247 add(desc); 248 return desc; 249 } 250 251 256 public void revertPerspectives(ArrayList perspToRevert) { 257 for (int i = 0; i < perspToRevert.size(); i++) { 259 PerspectiveDescriptor desc = (PerspectiveDescriptor) perspToRevert 260 .get(i); 261 perspToRemove.add(desc.getId()); 262 desc.revertToPredefined(); 263 } 264 } 265 266 271 public void deletePerspectives(ArrayList perspToDelete) { 272 for (int i = 0; i < perspToDelete.size(); i++) { 273 deletePerspective((IPerspectiveDescriptor) perspToDelete.get(i)); 274 } 275 } 276 277 283 public void deletePerspective(IPerspectiveDescriptor in) { 284 PerspectiveDescriptor desc = (PerspectiveDescriptor) in; 285 if (!desc.isPredefined()) { 287 perspToRemove.add(desc.getId()); 288 perspectives.remove(desc); 289 desc.deleteCustomDefinition(); 290 verifyDefaultPerspective(); 291 } 292 } 293 294 302 private void internalDeletePerspective(PerspectiveDescriptor desc) { 303 perspToRemove.add(desc.getId()); 304 perspectives.remove(desc); 305 desc.deleteCustomDefinition(); 306 verifyDefaultPerspective(); 307 } 308 309 314 315 void deleteCustomDefinition(PerspectiveDescriptor desc) { 316 IPreferenceStore store = WorkbenchPlugin.getDefault() 318 .getPreferenceStore(); 319 320 325 store.setToDefault(desc.getId() + PERSP); 326 327 } 328 329 334 335 boolean hasCustomDefinition(PerspectiveDescriptor desc) { 336 IPreferenceStore store = WorkbenchPlugin.getDefault() 337 .getPreferenceStore(); 338 return store.contains(desc.getId() + PERSP); 339 } 340 341 346 public IPerspectiveDescriptor findPerspectiveWithId(String id) { 347 for (Iterator i = perspectives.iterator(); i.hasNext();) { 348 PerspectiveDescriptor desc = (PerspectiveDescriptor) i.next(); 349 if (desc.getId().equals(id)) { 350 return desc; 351 } 352 } 353 354 return null; 355 } 356 357 362 public IPerspectiveDescriptor findPerspectiveWithLabel(String label) { 363 for (Iterator i = perspectives.iterator(); i.hasNext();) { 364 PerspectiveDescriptor desc = (PerspectiveDescriptor) i.next(); 365 if (desc.getLabel().equals(label)) { 366 return desc; 367 } 368 } 369 return null; 370 } 371 372 375 public String getDefaultPerspective() { 376 return defaultPerspID; 377 } 378 379 384 public IPerspectiveDescriptor[] getPerspectives() { 385 return (IPerspectiveDescriptor[]) perspectives 386 .toArray(new IPerspectiveDescriptor[perspectives.size()]); 387 } 388 389 392 public void load() { 393 loadPredefined(); 395 loadCustom(); 396 397 IDialogSettings dialogSettings = WorkbenchPlugin.getDefault() 400 .getDialogSettings(); 401 String str = dialogSettings.get(ID_DEF_PERSP); 402 if (str != null && str.length() > 0) { 403 setDefaultPerspective(str); 404 dialogSettings.put(ID_DEF_PERSP, ""); } 406 verifyDefaultPerspective(); 407 } 408 409 412 private void loadCustom() { 413 Reader reader = null; 414 415 416 IPreferenceStore store = WorkbenchPlugin.getDefault() 417 .getPreferenceStore(); 418 419 420 String customPerspectives = store 421 .getString(IPreferenceConstants.PERSPECTIVES); 422 String [] perspectivesList = StringConverter.asArray(customPerspectives); 423 424 for (int i = 0; i < perspectivesList.length; i++) { 425 try { 426 String xmlString = store.getString(perspectivesList[i] + PERSP); 427 if (xmlString != null && xmlString.length() != 0) { 428 reader = new StringReader (xmlString); 429 } 430 431 XMLMemento memento = XMLMemento.createReadRoot(reader); 433 PerspectiveDescriptor newPersp = new PerspectiveDescriptor( 434 null, null, null); 435 newPersp.restoreState(memento); 436 String id = newPersp.getId(); 437 IPerspectiveDescriptor oldPersp = findPerspectiveWithId(id); 438 if (oldPersp == null) { 439 add(newPersp); 440 } 441 reader.close(); 442 } catch (IOException e) { 443 unableToLoadPerspective(null); 444 } catch (WorkbenchException e) { 445 unableToLoadPerspective(e.getStatus()); 446 } 447 } 448 449 IPath path = WorkbenchPlugin.getDefault().getDataLocation(); 453 if (path == null) { 454 return; 455 } 456 457 File folder = path.toFile(); 458 459 if (folder.isDirectory()) { 460 File [] fileList = folder.listFiles(); 461 int nSize = fileList.length; 462 for (int nX = 0; nX < nSize; nX++) { 463 File file = fileList[nX]; 464 if (file.getName().endsWith(EXT)) { 465 InputStream stream = null; 467 try { 468 stream = new FileInputStream (file); 469 reader = new BufferedReader (new InputStreamReader ( 470 stream, "utf-8")); 472 XMLMemento memento = XMLMemento.createReadRoot(reader); 474 PerspectiveDescriptor newPersp = new PerspectiveDescriptor( 475 null, null, null); 476 newPersp.restoreState(memento); 477 IPerspectiveDescriptor oldPersp = findPerspectiveWithId(newPersp 478 .getId()); 479 if (oldPersp == null) { 480 add(newPersp); 481 } 482 483 saveCustomPersp(newPersp, memento); 485 486 file.delete(); 488 489 reader.close(); 490 stream.close(); 491 } catch (IOException e) { 492 unableToLoadPerspective(null); 493 } catch (WorkbenchException e) { 494 unableToLoadPerspective(e.getStatus()); 495 } 496 } 497 } 498 } 499 } 500 501 504 private void unableToLoadPerspective(IStatus status) { 505 String msg = WorkbenchMessages.Perspective_errorLoadingState; 506 if (status == null) { 507 IStatus errStatus = new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, msg); 508 StatusManager.getManager().handle(errStatus, StatusManager.SHOW); 509 } else { 510 IStatus errStatus = StatusUtil.newStatus(status, msg); 511 StatusManager.getManager().handle(errStatus, StatusManager.SHOW); 512 } 513 } 514 515 524 public void saveCustomPersp(PerspectiveDescriptor desc, XMLMemento memento) 525 throws IOException { 526 527 IPreferenceStore store = WorkbenchPlugin.getDefault() 528 .getPreferenceStore(); 529 530 Writer writer = new StringWriter (); 532 533 memento.save(writer); 534 writer.close(); 535 store.setValue(desc.getId() + PERSP, writer.toString()); 536 537 } 538 539 549 public IMemento getCustomPersp(String id) throws WorkbenchException, 550 IOException { 551 Reader reader = null; 552 553 IPreferenceStore store = WorkbenchPlugin.getDefault() 554 .getPreferenceStore(); 555 String xmlString = store.getString(id + PERSP); 556 if (xmlString != null && xmlString.length() != 0) { reader = new StringReader (xmlString); 558 } 559 XMLMemento memento = XMLMemento.createReadRoot(reader); 560 reader.close(); 561 return memento; 562 } 563 564 567 private void loadPredefined() { 568 PerspectiveRegistryReader reader = new PerspectiveRegistryReader(this); 569 reader.readPerspectives(Platform.getExtensionRegistry()); 570 } 571 572 575 public void setDefaultPerspective(String id) { 576 IPerspectiveDescriptor desc = findPerspectiveWithId(id); 577 if (desc != null) { 578 defaultPerspID = id; 579 PrefUtil.getAPIPreferenceStore().setValue( 580 IWorkbenchPreferenceConstants.DEFAULT_PERSPECTIVE_ID, id); 581 } 582 } 583 584 593 public boolean validateLabel(String label) { 594 label = label.trim(); 595 if (label.length() <= 0) { 596 return false; 597 } 598 return true; 599 } 600 601 605 private void verifyDefaultPerspective() { 606 IPerspectiveDescriptor desc = null; 608 if (defaultPerspID != null) { 609 desc = findPerspectiveWithId(defaultPerspID); 610 } 611 if (desc != null) { 612 return; 613 } 614 615 String str = PrefUtil.getAPIPreferenceStore().getString( 617 IWorkbenchPreferenceConstants.DEFAULT_PERSPECTIVE_ID); 618 if (str != null && str.length() > 0) { 619 desc = findPerspectiveWithId(str); 620 } 621 if (desc != null) { 622 defaultPerspID = str; 623 return; 624 } 625 626 defaultPerspID = Workbench.getInstance().getDefaultPerspectiveId(); 628 } 629 630 636 public IPerspectiveDescriptor clonePerspective(String id, String label, 637 IPerspectiveDescriptor originalDescriptor) { 638 639 if (label == null || !(label.trim().length() > 0)) { 641 throw new IllegalArgumentException (); 642 } 643 644 IPerspectiveDescriptor desc = findPerspectiveWithId(id); 646 if (desc != null) { 647 throw new IllegalArgumentException (); 648 } 649 650 desc = new PerspectiveDescriptor(id, label, 652 (PerspectiveDescriptor) originalDescriptor); 653 add((PerspectiveDescriptor) desc); 654 return desc; 655 } 656 657 662 public void revertPerspective(IPerspectiveDescriptor perspToRevert) { 663 PerspectiveDescriptor desc = (PerspectiveDescriptor) perspToRevert; 664 perspToRemove.add(desc.getId()); 665 desc.revertToPredefined(); 666 } 667 668 671 public void dispose() { 672 PlatformUI.getWorkbench().getExtensionTracker().unregisterHandler(this); 673 WorkbenchPlugin.getDefault().getPreferenceStore() 674 .removePropertyChangeListener(preferenceListener); 675 } 676 677 683 public void removeExtension(IExtension source, Object [] objects) { 684 for (int i = 0; i < objects.length; i++) { 685 if (objects[i] instanceof PerspectiveDescriptor) { 686 IWorkbenchWindow[] windows = PlatformUI.getWorkbench() 688 .getWorkbenchWindows(); 689 PerspectiveDescriptor desc = (PerspectiveDescriptor) objects[i]; 690 for (int w = 0; w < windows.length; ++w) { 691 IWorkbenchWindow window = windows[w]; 692 IWorkbenchPage[] pages = window.getPages(); 693 for (int p = 0; p < pages.length; ++p) { 694 WorkbenchPage page = (WorkbenchPage) pages[p]; 695 ClosePerspectiveAction.closePerspective(page, page 696 .findPerspective(desc)); 697 } 698 } 699 700 702 internalDeletePerspective(desc); 703 } 704 705 } 706 } 707 708 714 public void addExtension(IExtensionTracker tracker, 715 IExtension addedExtension) { 716 IConfigurationElement[] addedElements = addedExtension 717 .getConfigurationElements(); 718 for (int i = 0; i < addedElements.length; i++) { 719 PerspectiveRegistryReader reader = new PerspectiveRegistryReader( 720 this); 721 reader.readElement(addedElements[i]); 722 } 723 } 724 } 725 | Popular Tags |