KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > rows > ResourceAttributeRow


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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 /*
12  * Created on Jan 30, 2004
13  *
14  * To change the template for this generated file go to
15  * Window - Preferences - Java - Code Generation - Code and Comments
16  */

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     /*
48      * (non-Javadoc)
49      *
50      * @see org.eclipse.pde.internal.ui.neweditor.plugin.ReferenceAttributeRow#openReference()
51      */

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 JavaDoc 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 JavaDoc value = text.getText();
106         if (value.length() <= 5 || !value.startsWith("$nl$/"))return null; //$NON-NLS-1$
107
IPath path = getProject().getFullPath().append(value.substring(5));
108         return getProject().getWorkspace().getRoot().getFile(path);
109     }
110     /*
111      * (non-Javadoc)
112      *
113      * @see org.eclipse.pde.internal.ui.neweditor.plugin.ReferenceAttributeRow#browse()
114      */

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 JavaDoc parentElement,
127                     Object JavaDoc 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 JavaDoc[] 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); //$NON-NLS-1$
144

145                 return new Status(IStatus.ERROR, PDEPlugin.getPluginId(),
146                         IStatus.ERROR, "", null); //$NON-NLS-1$
147
}
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 JavaDoc value = path.toString();
155             text.setText(value);
156         }
157     }
158 }
159
Popular Tags