001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.schema;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.jdt.core.IJavaElement;
015: import org.eclipse.jdt.core.IJavaProject;
016: import org.eclipse.jdt.core.IType;
017: import org.eclipse.jdt.core.JavaCore;
018: import org.eclipse.jdt.core.search.IJavaSearchConstants;
019: import org.eclipse.jdt.core.search.SearchEngine;
020: import org.eclipse.jdt.ui.IJavaElementSearchConstants;
021: import org.eclipse.jdt.ui.JavaUI;
022: import org.eclipse.jface.window.Window;
023: import org.eclipse.jface.wizard.WizardDialog;
024: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
025: import org.eclipse.pde.internal.core.schema.SchemaAttribute;
026: import org.eclipse.pde.internal.ui.PDEPlugin;
027: import org.eclipse.pde.internal.ui.PDEUIMessages;
028: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
029: import org.eclipse.pde.internal.ui.editor.contentassist.TypeFieldAssistDisposer;
030: import org.eclipse.pde.internal.ui.parts.FormEntry;
031: import org.eclipse.pde.internal.ui.util.PDEJavaHelperUI;
032: import org.eclipse.pde.internal.ui.util.SWTUtil;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.ui.IActionBars;
035: import org.eclipse.ui.PartInitException;
036: import org.eclipse.ui.PlatformUI;
037: import org.eclipse.ui.dialogs.SelectionDialog;
038: import org.eclipse.ui.forms.events.HyperlinkEvent;
039: import org.eclipse.ui.forms.widgets.FormToolkit;
040:
041: public class SchemaJavaAttributeDetails extends SchemaAttributeDetails {
042: private FormEntry fClassEntry;
043: private FormEntry fInterfaceEntry;
044: private TypeFieldAssistDisposer fClassEntryFieldAssistDisposer;
045: private TypeFieldAssistDisposer fInterfaceEntryFieldAssistDisposer;
046:
047: public SchemaJavaAttributeDetails(ElementSection section) {
048: super (section);
049: }
050:
051: protected void createTypeDetails(Composite parent,
052: FormToolkit toolkit) {
053: fClassEntry = new FormEntry(parent, toolkit,
054: PDEUIMessages.SchemaAttributeDetails_extends,
055: PDEUIMessages.SchemaAttributeDetails_browseButton,
056: isEditable(), 13);
057: fInterfaceEntry = new FormEntry(parent, toolkit,
058: PDEUIMessages.SchemaAttributeDetails_implements ,
059: PDEUIMessages.SchemaAttributeDetails_browseButton,
060: isEditable(), 13);
061: fClassEntryFieldAssistDisposer = PDEJavaHelperUI
062: .addTypeFieldAssistToText(fClassEntry.getText(),
063: getPage().getPDEEditor().getCommonProject(),
064: IJavaSearchConstants.CLASS);
065: fInterfaceEntryFieldAssistDisposer = PDEJavaHelperUI
066: .addTypeFieldAssistToText(fInterfaceEntry.getText(),
067: getPage().getPDEEditor().getCommonProject(),
068: IJavaSearchConstants.INTERFACE);
069: }
070:
071: public void updateFields(ISchemaObject object) {
072: if (!(object instanceof SchemaAttribute))
073: return;
074: super .updateFields(object);
075:
076: String basedOn = getAttribute().getBasedOn();
077: if ((basedOn != null) && (basedOn.length() > 0)) {
078: int index = basedOn.indexOf(":"); //$NON-NLS-1$
079: if (index == -1) {
080: String className = basedOn.substring(basedOn
081: .lastIndexOf(".") + 1); //$NON-NLS-1$
082: if ((className.length() > 1)
083: && (className.charAt(0) == 'I')) {
084: fClassEntry.setValue("", true); //$NON-NLS-1$
085: fInterfaceEntry.setValue(basedOn, true);
086: } else {
087: fClassEntry.setValue(basedOn, true);
088: fInterfaceEntry.setValue("", true); //$NON-NLS-1$
089: }
090: } else {
091: fClassEntry.setValue(basedOn.substring(0, index), true);
092: fInterfaceEntry.setValue(basedOn.substring(index + 1),
093: true);
094: }
095: } else {
096: fClassEntry.setValue("", true); //$NON-NLS-1$
097: fInterfaceEntry.setValue("", true); //$NON-NLS-1$
098: }
099:
100: boolean editable = isEditableElement();
101: fClassEntry.setEditable(editable);
102: fInterfaceEntry.setEditable(editable);
103: }
104:
105: public void hookListeners() {
106: super .hookListeners();
107: IActionBars actionBars = getPage().getPDEEditor()
108: .getEditorSite().getActionBars();
109: fClassEntry.setFormEntryListener(new FormEntryAdapter(this ,
110: actionBars) {
111: public void textValueChanged(FormEntry entry) {
112: if (blockListeners())
113: return;
114: setBasedOn();
115: }
116:
117: public void linkActivated(HyperlinkEvent e) {
118: if (blockListeners())
119: return;
120: String value = fClassEntry.getValue();
121: value = handleLinkActivated(value, false);
122: if (value != null)
123: fClassEntry.setValue(value);
124: }
125:
126: public void browseButtonSelected(FormEntry entry) {
127: if (blockListeners())
128: return;
129: doOpenSelectionDialog(
130: IJavaElementSearchConstants.CONSIDER_CLASSES,
131: fClassEntry);
132: }
133: });
134: fInterfaceEntry.setFormEntryListener(new FormEntryAdapter(this ,
135: actionBars) {
136: public void textValueChanged(FormEntry entry) {
137: if (blockListeners())
138: return;
139: setBasedOn();
140: }
141:
142: public void linkActivated(HyperlinkEvent e) {
143: if (blockListeners())
144: return;
145: String value = fInterfaceEntry.getValue();
146: value = handleLinkActivated(value, true);
147: if (value != null)
148: fInterfaceEntry.setValue(value);
149: }
150:
151: public void browseButtonSelected(FormEntry entry) {
152: if (blockListeners())
153: return;
154: doOpenSelectionDialog(
155: IJavaElementSearchConstants.CONSIDER_INTERFACES,
156: fInterfaceEntry);
157: }
158: });
159: }
160:
161: private String handleLinkActivated(String value, boolean isInter) {
162: IProject project = getPage().getPDEEditor().getCommonProject();
163: try {
164: if (project != null
165: && project.hasNature(JavaCore.NATURE_ID)) {
166: IJavaProject javaProject = JavaCore.create(project);
167: IJavaElement element = javaProject.findType(value
168: .replace('$', '.'));
169: if (element != null)
170: JavaUI.openInEditor(element);
171: else {
172: NewClassCreationWizard wizard = new NewClassCreationWizard(
173: project, isInter, value);
174: WizardDialog dialog = new WizardDialog(PDEPlugin
175: .getActiveWorkbenchShell(), wizard);
176: dialog.create();
177: SWTUtil.setDialogSize(dialog, 400, 500);
178: if (dialog.open() == Window.OK) {
179: return wizard.getQualifiedName();
180: }
181: }
182: }
183: } catch (PartInitException e1) {
184: } catch (CoreException e1) {
185: }
186: return null;
187: }
188:
189: private void setBasedOn() {
190: String classEntry = fClassEntry.getValue().replaceAll(":", ""); //$NON-NLS-1$ //$NON-NLS-2$
191: String interfaceEntry = fInterfaceEntry.getValue().replaceAll(
192: ":", ""); //$NON-NLS-1$ //$NON-NLS-2$
193: StringBuffer sb = new StringBuffer();
194: if (classEntry.length() > 0)
195: sb.append(classEntry);
196: if (classEntry.length() > 0 || interfaceEntry.length() > 0)
197: sb.append(":"); //$NON-NLS-1$
198: if (interfaceEntry.length() > 0)
199: sb.append(interfaceEntry);
200: getAttribute().setBasedOn(
201: sb.length() > 0 ? sb.toString() : null);
202: }
203:
204: private void doOpenSelectionDialog(int scopeType, FormEntry entry) {
205: try {
206: String filter = entry.getValue();
207: filter = filter.substring(filter.lastIndexOf(".") + 1); //$NON-NLS-1$
208: SelectionDialog dialog = JavaUI.createTypeDialog(PDEPlugin
209: .getActiveWorkbenchShell(), PlatformUI
210: .getWorkbench().getProgressService(), SearchEngine
211: .createWorkspaceScope(), scopeType, false, filter); //$NON-NLS-1$
212: dialog
213: .setTitle(PDEUIMessages.GeneralInfoSection_selectionTitle);
214: if (dialog.open() == Window.OK) {
215: IType type = (IType) dialog.getResult()[0];
216: entry.setValue(type.getFullyQualifiedName('$'));
217: entry.commit();
218: }
219: } catch (CoreException e) {
220: }
221: }
222:
223: public void commit(boolean onSave) {
224: super .commit(onSave);
225: // Only required for form entries
226: fClassEntry.commit();
227: fInterfaceEntry.commit();
228: }
229:
230: public void dispose() {
231: super.dispose();
232: if (fClassEntryFieldAssistDisposer != null)
233: fClassEntryFieldAssistDisposer.dispose();
234: if (fInterfaceEntryFieldAssistDisposer != null)
235: fInterfaceEntryFieldAssistDisposer.dispose();
236: }
237: }
|