001: /*******************************************************************************
002: * Copyright (c) 2005, 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.product;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.jface.window.Window;
014: import org.eclipse.pde.core.IModelChangedEvent;
015: import org.eclipse.pde.internal.core.iproduct.IAboutInfo;
016: import org.eclipse.pde.internal.core.iproduct.IProduct;
017: import org.eclipse.pde.internal.core.iproduct.IProductModel;
018: import org.eclipse.pde.internal.ui.PDEPlugin;
019: import org.eclipse.pde.internal.ui.PDEUIMessages;
020: import org.eclipse.pde.internal.ui.editor.EditorUtilities;
021: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
022: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
023: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
024: import org.eclipse.pde.internal.ui.editor.PDESection;
025: import org.eclipse.pde.internal.ui.editor.validation.TextValidator;
026: import org.eclipse.pde.internal.ui.parts.FormEntry;
027: import org.eclipse.pde.internal.ui.util.FileExtensionFilter;
028: import org.eclipse.pde.internal.ui.util.FileValidator;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.dnd.Clipboard;
031: import org.eclipse.swt.layout.GridData;
032: import org.eclipse.swt.widgets.Composite;
033: import org.eclipse.swt.widgets.Control;
034: import org.eclipse.swt.widgets.Display;
035: import org.eclipse.swt.widgets.Text;
036: import org.eclipse.ui.IActionBars;
037: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
038: import org.eclipse.ui.forms.events.HyperlinkEvent;
039: import org.eclipse.ui.forms.widgets.FormToolkit;
040: import org.eclipse.ui.forms.widgets.Section;
041: import org.eclipse.ui.model.WorkbenchContentProvider;
042: import org.eclipse.ui.model.WorkbenchLabelProvider;
043:
044: public class AboutSection extends PDESection {
045:
046: private FormEntry fImageEntry;
047: private FormEntry fTextEntry;
048:
049: private TextValidator fImageEntryValidator;
050:
051: public AboutSection(PDEFormPage page, Composite parent) {
052: super (page, parent, Section.DESCRIPTION);
053: createClient(getSection(), page.getEditor().getToolkit());
054: }
055:
056: /* (non-Javadoc)
057: * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
058: */
059: protected void createClient(Section section, FormToolkit toolkit) {
060: section.setLayout(FormLayoutFactory.createClearGridLayout(
061: false, 1));
062: GridData data = new GridData(GridData.FILL_BOTH);
063: data.widthHint = 300;
064: data.heightHint = 70;
065: section.setLayoutData(data);
066:
067: section.setText(PDEUIMessages.AboutSection_title);
068: section.setDescription(PDEUIMessages.AboutSection_desc);
069:
070: Composite client = toolkit.createComposite(section);
071: client.setLayout(FormLayoutFactory
072: .createSectionClientGridLayout(false, 3));
073: client.setLayoutData(new GridData(GridData.FILL_BOTH));
074:
075: IActionBars actionBars = getPage().getPDEEditor()
076: .getEditorSite().getActionBars();
077: fImageEntry = new FormEntry(client, toolkit,
078: PDEUIMessages.AboutSection_image,
079: PDEUIMessages.AboutSection_browse, isEditable());
080: fImageEntry.setEditable(isEditable());
081: // Create validator
082: fImageEntryValidator = new TextValidator(getManagedForm(),
083: fImageEntry.getText(), getProject(), true) {
084: protected boolean validateControl() {
085: return validateImageEntry();
086: }
087: };
088: fImageEntry.setFormEntryListener(new FormEntryAdapter(this ,
089: actionBars) {
090: public void textValueChanged(FormEntry entry) {
091: getAboutInfo().setImagePath(entry.getValue());
092: }
093:
094: public void browseButtonSelected(FormEntry entry) {
095: handleBrowse();
096: }
097:
098: public void linkActivated(HyperlinkEvent e) {
099: EditorUtilities.openImage(fImageEntry.getValue(),
100: getProduct().getDefiningPluginId());
101: }
102: });
103:
104: // Text field
105: // Create Text field UI
106: int style = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL;
107: fTextEntry = new FormEntry(client, toolkit,
108: PDEUIMessages.AboutSection_text, style);
109: // Configure Text widget
110: data = new GridData(GridData.FILL_BOTH);
111: data.horizontalSpan = 2;
112: // Needed to align vertically with form entry field and allow space
113: // for a possible field decoration
114: data.horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT;
115: fTextEntry.getText().setLayoutData(data);
116: // Configure Label widget to be aligned to the top-left
117: data = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
118: fTextEntry.getLabel().setLayoutData(data);
119: // Configure editability
120: fTextEntry.setEditable(isEditable());
121: // Create Text field listener
122: fTextEntry.setFormEntryListener(new FormEntryAdapter(this ,
123: actionBars) {
124: public void textValueChanged(FormEntry entry) {
125: getAboutInfo().setText(entry.getValue());
126: }
127: });
128:
129: toolkit.paintBordersFor(client);
130: section.setClient(client);
131: // Register to be notified when the model changes
132: getModel().addModelChangedListener(this );
133: }
134:
135: /**
136: * @return
137: */
138: private boolean validateImageEntry() {
139: return EditorUtilities.imageEntrySizeDoesNotExceed(
140: fImageEntryValidator, fImageEntry, getProduct(), 500,
141: 330, 250, 330);
142: }
143:
144: private void handleBrowse() {
145: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
146: getSection().getShell(), new WorkbenchLabelProvider(),
147: new WorkbenchContentProvider());
148:
149: dialog.setValidator(new FileValidator());
150: dialog.setAllowMultiple(false);
151: dialog.setTitle(PDEUIMessages.AboutSection_imgTitle);
152: dialog.setMessage(PDEUIMessages.AboutSection_imgMessage);
153: dialog.addFilter(new FileExtensionFilter("gif")); //$NON-NLS-1$
154: dialog.setInput(PDEPlugin.getWorkspace().getRoot());
155:
156: if (dialog.open() == Window.OK) {
157: IFile file = (IFile) dialog.getFirstResult();
158: fImageEntry.setValue(file.getFullPath().toString());
159: }
160: }
161:
162: public void refresh() {
163: fImageEntry.setValue(getAboutInfo().getImagePath(), true);
164: fTextEntry.setValue(getAboutInfo().getText(), true);
165: super .refresh();
166: }
167:
168: public void commit(boolean onSave) {
169: fImageEntry.commit();
170: fTextEntry.commit();
171: super .commit(onSave);
172: }
173:
174: public void cancelEdit() {
175: fImageEntry.cancelEdit();
176: fTextEntry.cancelEdit();
177: super .cancelEdit();
178: }
179:
180: private IAboutInfo getAboutInfo() {
181: IAboutInfo info = getProduct().getAboutInfo();
182: if (info == null) {
183: info = getModel().getFactory().createAboutInfo();
184: getProduct().setAboutInfo(info);
185: }
186: return info;
187: }
188:
189: private IProduct getProduct() {
190: return getModel().getProduct();
191: }
192:
193: private IProductModel getModel() {
194: return (IProductModel) getPage().getPDEEditor()
195: .getAggregateModel();
196: }
197:
198: public boolean canPaste(Clipboard clipboard) {
199: Display d = getSection().getDisplay();
200: Control c = d.getFocusControl();
201: if (c instanceof Text)
202: return true;
203: return false;
204: }
205:
206: /* (non-Javadoc)
207: * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
208: */
209: public void modelChanged(IModelChangedEvent e) {
210: // No need to call super, handling world changed event here
211: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
212: handleModelEventWorldChanged(e);
213: }
214: }
215:
216: /**
217: * @param event
218: */
219: private void handleModelEventWorldChanged(IModelChangedEvent event) {
220: refresh();
221: }
222:
223: /* (non-Javadoc)
224: * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
225: */
226: public void dispose() {
227: IProductModel model = getModel();
228: if (model != null) {
229: model.removeModelChangedListener(this);
230: }
231: super.dispose();
232: }
233: }
|