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.toc.details;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.jface.viewers.ISelection;
014: import org.eclipse.jface.window.Window;
015: import org.eclipse.pde.internal.core.text.toc.TocLink;
016: import org.eclipse.pde.internal.core.text.toc.TocObject;
017: import org.eclipse.pde.internal.ui.PDEPlugin;
018: import org.eclipse.pde.internal.ui.PDEUIMessages;
019: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
020: import org.eclipse.pde.internal.ui.editor.toc.TocExtensionUtil;
021: import org.eclipse.pde.internal.ui.editor.toc.TocFileValidator;
022: import org.eclipse.pde.internal.ui.editor.toc.TocInputContext;
023: import org.eclipse.pde.internal.ui.editor.toc.TocTreeSection;
024: import org.eclipse.pde.internal.ui.parts.FormEntry;
025: import org.eclipse.pde.internal.ui.util.FileExtensionFilter;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
028: import org.eclipse.ui.forms.IFormPart;
029: import org.eclipse.ui.forms.events.HyperlinkEvent;
030: import org.eclipse.ui.model.WorkbenchContentProvider;
031: import org.eclipse.ui.model.WorkbenchLabelProvider;
032:
033: public class TocLinkDetails extends TocAbstractDetails {
034:
035: private TocLink fDataTOCLink;
036:
037: private FormEntry fTocPathEntry;
038:
039: /**
040: * @param masterSection
041: */
042: public TocLinkDetails(TocTreeSection masterSection) {
043: super (masterSection, TocInputContext.CONTEXT_ID);
044: fDataTOCLink = null;
045:
046: fTocPathEntry = null;
047: }
048:
049: /**
050: * @param object
051: */
052: public void setData(TocLink object) {
053: // Set data
054: fDataTOCLink = object;
055: }
056:
057: protected TocObject getDataObject() {
058: return fDataTOCLink;
059: }
060:
061: protected FormEntry getPathEntryField() {
062: return fTocPathEntry;
063: }
064:
065: protected boolean isTocPath() {
066: return true;
067: }
068:
069: /* (non-Javadoc)
070: * @see org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails#createDetails(org.eclipse.swt.widgets.Composite)
071: */
072: public void createFields(Composite parent) {
073: createTocPathWidget(parent);
074: }
075:
076: /**
077: * @param parent
078: */
079: private void createTocPathWidget(Composite parent) {
080: createLabel(parent, getManagedForm().getToolkit(),
081: PDEUIMessages.TocLinkDetails_tocPath_desc);
082:
083: fTocPathEntry = new FormEntry(parent, getManagedForm()
084: .getToolkit(), PDEUIMessages.TocLinkDetails_tocPath,
085: PDEUIMessages.GeneralInfoSection_browse, isEditable());
086: }
087:
088: protected String getDetailsTitle() {
089: return PDEUIMessages.TocLinkDetails_title;
090: }
091:
092: protected String getDetailsDescription() {
093: return null;
094: }
095:
096: /* (non-Javadoc)
097: * @see org.eclipse.pde.internal.ui.editor.toc.TocAbstractDetails#hookListeners()
098: */
099: public void hookListeners() {
100: createTocPathEntryListeners();
101: }
102:
103: /**
104: *
105: */
106: private void createTocPathEntryListeners() {
107: fTocPathEntry.setFormEntryListener(new FormEntryAdapter(this ) {
108: public void textValueChanged(FormEntry entry) {
109: // Ensure data object is defined
110: if (fDataTOCLink != null) {
111: fDataTOCLink.setFieldTocPath(fTocPathEntry
112: .getValue());
113: }
114: }
115:
116: public void browseButtonSelected(FormEntry entry) {
117: handleBrowse();
118: }
119:
120: public void linkActivated(HyperlinkEvent e) {
121: handleOpen();
122: }
123: });
124: }
125:
126: private void handleBrowse() {
127: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
128: getPage().getSite().getShell(),
129: new WorkbenchLabelProvider(),
130: new WorkbenchContentProvider());
131:
132: dialog.setValidator(new TocFileValidator(fDataTOCLink
133: .getModel()));
134: dialog.setAllowMultiple(false);
135: dialog.setTitle(PDEUIMessages.TocLinkDetails_browseSelection);
136: dialog.setMessage(PDEUIMessages.TocLinkDetails_browseMessage);
137: dialog.addFilter(new FileExtensionFilter(
138: TocExtensionUtil.tocExtension));
139:
140: dialog.setInput(PDEPlugin.getWorkspace().getRoot());
141:
142: if (dialog.open() == Window.OK) {
143: IFile file = (IFile) dialog.getFirstResult();
144: setPathEntry(file);
145: }
146: }
147:
148: /* (non-Javadoc)
149: * @see org.eclipse.pde.internal.ui.editor.toc.TocAbstractDetails#updateFields()
150: */
151: public void updateFields() {
152: // Ensure data object is defined
153: if (fDataTOCLink != null) { // Update name entry
154: updateTocPathEntry(isEditableElement());
155: }
156: }
157:
158: /**
159: * @param editable
160: */
161: private void updateTocPathEntry(boolean editable) {
162: fTocPathEntry.setValue(fDataTOCLink.getFieldTocPath(), true);
163: fTocPathEntry.setEditable(editable);
164: }
165:
166: /* (non-Javadoc)
167: * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
168: */
169: public void commit(boolean onSave) {
170: super .commit(onSave);
171: // Only required for form entries
172: fTocPathEntry.commit();
173: }
174:
175: /* (non-Javadoc)
176: * @see org.eclipse.ui.forms.IPartSelectionListener#selectionChanged(org.eclipse.ui.forms.IFormPart, org.eclipse.jface.viewers.ISelection)
177: */
178: public void selectionChanged(IFormPart part, ISelection selection) {
179: // Get the first selected object
180: Object object = getFirstSelectedObject(selection);
181: // Ensure we have the right type
182: if (object != null && object instanceof TocLink) {
183: // Set data
184: setData((TocLink) object);
185: // Update the UI given the new data
186: updateFields();
187: }
188: }
189: }
|