1 11 17 package org.eclipse.pde.internal.ui.editor.plugin.rows; 18 import org.eclipse.core.resources.IContainer; 19 import org.eclipse.core.resources.IFile; 20 import org.eclipse.core.resources.IProject; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Status; 25 import org.eclipse.jdt.ui.actions.ShowInNavigatorViewAction; 26 import org.eclipse.jface.viewers.Viewer; 27 import org.eclipse.jface.viewers.ViewerFilter; 28 import org.eclipse.jface.window.Window; 29 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute; 30 import org.eclipse.pde.internal.ui.PDEPlugin; 31 import org.eclipse.pde.internal.ui.PDEUIMessages; 32 import org.eclipse.pde.internal.ui.editor.IContextPart; 33 import org.eclipse.swt.widgets.Display; 34 import org.eclipse.ui.IPageLayout; 35 import org.eclipse.ui.IViewPart; 36 import org.eclipse.ui.PartInitException; 37 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 38 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 39 import org.eclipse.ui.ide.IDE; 40 import org.eclipse.ui.model.WorkbenchContentProvider; 41 import org.eclipse.ui.model.WorkbenchLabelProvider; 42 43 public class ResourceAttributeRow extends ButtonAttributeRow { 44 public ResourceAttributeRow(IContextPart part, ISchemaAttribute att) { 45 super(part, att); 46 } 47 52 protected boolean isReferenceModel() { 53 return !part.getPage().getModel().isEditable(); 54 } 55 protected void openReference() { 56 IResource file = getFile(); 57 boolean successful = false; 58 if (file instanceof IFile) 59 successful = openFile((IFile)file); 60 else if (file instanceof IContainer) 61 successful = openContainer((IContainer)file); 62 if (!successful) 63 Display.getCurrent().beep(); 64 } 65 private boolean openFile(IFile file) { 66 if (file!=null && file.exists()) { 67 try { 68 IDE.openEditor(PDEPlugin.getActivePage(), file, true); 69 } catch (PartInitException e) { 70 PDEPlugin.logException(e); 71 return false; 72 } 73 return true; 74 } 75 file = getNLFile(); 76 if (file != null && file.exists()) { 77 try { 78 IDE.openEditor(PDEPlugin.getActivePage(), file, true); 79 } catch (PartInitException e) { 80 PDEPlugin.logException(e); 81 return false; 82 } 83 return true; 84 } 85 return false; 86 } 87 private boolean openContainer(IContainer container) { 88 if (container != null && container.exists()) 89 try { 90 IViewPart part = PDEPlugin.getActivePage().showView(IPageLayout.ID_RES_NAV); 91 ShowInNavigatorViewAction action = new ShowInNavigatorViewAction(part.getSite()); 92 action.run(container); 93 } catch (PartInitException e) { 94 return false; 95 } 96 return true; 97 } 98 private IResource getFile() { 99 String value = text.getText(); 100 if (value.length()==0) return null; 101 IPath path = getProject().getFullPath().append(value); 102 return getProject().getWorkspace().getRoot().findMember(path); 103 } 104 private IFile getNLFile() { 105 String value = text.getText(); 106 if (value.length() <= 5 || !value.startsWith("$nl$/"))return null; IPath path = getProject().getFullPath().append(value.substring(5)); 108 return getProject().getWorkspace().getRoot().getFile(path); 109 } 110 115 protected void browse() { 116 final IProject project = part.getPage().getPDEEditor() 117 .getCommonProject(); 118 ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog( 119 PDEPlugin.getActiveWorkbenchShell(), 120 new WorkbenchLabelProvider(), new WorkbenchContentProvider()); 121 dialog.setInput(project.getWorkspace()); 122 IResource resource = getFile(); 123 if (resource!=null) 124 dialog.setInitialSelection(resource); 125 dialog.addFilter(new ViewerFilter() { 126 public boolean select(Viewer viewer, Object parentElement, 127 Object element) { 128 if (element instanceof IProject) 129 return ((IProject) element).equals(project); 130 return true; 131 } 132 }); 133 dialog.setAllowMultiple(false); 134 dialog 135 .setTitle(PDEUIMessages.ResourceAttributeCellEditor_title); 136 dialog 137 .setMessage(PDEUIMessages.ResourceAttributeCellEditor_message); 138 dialog.setValidator(new ISelectionStatusValidator() { 139 public IStatus validate(Object [] selection) { 140 if (selection != null && selection.length > 0 141 && (selection[0] instanceof IFile || selection[0] instanceof IContainer)) 142 return new Status(IStatus.OK, PDEPlugin.getPluginId(), 143 IStatus.OK, "", null); 145 return new Status(IStatus.ERROR, PDEPlugin.getPluginId(), 146 IStatus.ERROR, "", null); } 148 }); 149 if (dialog.open() == Window.OK) { 150 IResource res = (IResource) dialog.getFirstResult(); 151 IPath path = res.getProjectRelativePath(); 152 if (res instanceof IContainer) 153 path = path.addTrailingSeparator(); 154 String value = path.toString(); 155 text.setText(value); 156 } 157 } 158 } 159 | Popular Tags |