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.IProduct;
016: import org.eclipse.pde.internal.core.iproduct.IProductModel;
017: import org.eclipse.pde.internal.core.iproduct.IWindowImages;
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.dnd.Clipboard;
030: import org.eclipse.swt.layout.GridData;
031: import org.eclipse.swt.widgets.Composite;
032: import org.eclipse.swt.widgets.Control;
033: import org.eclipse.swt.widgets.Display;
034: import org.eclipse.swt.widgets.Text;
035: import org.eclipse.ui.IActionBars;
036: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
037: import org.eclipse.ui.forms.events.HyperlinkEvent;
038: import org.eclipse.ui.forms.widgets.FormToolkit;
039: import org.eclipse.ui.forms.widgets.Section;
040: import org.eclipse.ui.model.WorkbenchContentProvider;
041: import org.eclipse.ui.model.WorkbenchLabelProvider;
042:
043: public class WindowImagesSection extends PDESection {
044:
045: private TextValidator[] fWinImageEntryValidator;
046:
047: private static final int[][] F_ICON_DIMENSIONS = new int[][] {
048: { 16, 16 }, { 32, 32 }, { 48, 48 }, { 64, 64 },
049: { 128, 128 } };
050: private static final String[] F_ICON_LABELS = new String[] {
051: PDEUIMessages.WindowImagesSection_16,
052: PDEUIMessages.WindowImagesSection_32,
053: PDEUIMessages.WindowImagesSection_48,
054: PDEUIMessages.WindowImagesSection_64,
055: PDEUIMessages.WindowImagesSection_128 };
056: private FormEntry[] fImages = new FormEntry[F_ICON_LABELS.length];
057:
058: public WindowImagesSection(PDEFormPage page, Composite parent) {
059: super (page, parent, Section.DESCRIPTION);
060: createClient(getSection(), page.getEditor().getToolkit());
061: }
062:
063: /* (non-Javadoc)
064: * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
065: */
066: protected void createClient(Section section, FormToolkit toolkit) {
067: section.setLayout(FormLayoutFactory.createClearGridLayout(
068: false, 1));
069: GridData data = new GridData(GridData.FILL_HORIZONTAL);
070: section.setLayoutData(data);
071:
072: section.setText(PDEUIMessages.WindowImagesSection_title);
073: section.setDescription(PDEUIMessages.WindowImagesSection_desc);
074:
075: Composite client = toolkit.createComposite(section);
076: client.setLayout(FormLayoutFactory
077: .createSectionClientGridLayout(false, 3));
078: client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
079:
080: IActionBars actionBars = getPage().getPDEEditor()
081: .getEditorSite().getActionBars();
082: // Store all image entry validators
083: fWinImageEntryValidator = new TextValidator[F_ICON_LABELS.length];
084: for (int i = 0; i < fImages.length; i++) {
085: final int index = i;
086: fImages[index] = new FormEntry(client, toolkit,
087: F_ICON_LABELS[index],
088: PDEUIMessages.WindowImagesSection_browse,
089: isEditable());
090: fImages[index].setEditable(isEditable());
091: // Create validator
092: fWinImageEntryValidator[index] = new TextValidator(
093: getManagedForm(), fImages[index].getText(),
094: getProject(), true) {
095: protected boolean validateControl() {
096: return validateWinImageEntry(index);
097: }
098: };
099: fImages[index].setFormEntryListener(new FormEntryAdapter(
100: this , actionBars) {
101: public void textValueChanged(FormEntry entry) {
102: getWindowImages().setImagePath(entry.getValue(),
103: index);
104: }
105:
106: public void browseButtonSelected(FormEntry entry) {
107: handleBrowse(entry);
108: }
109:
110: public void linkActivated(HyperlinkEvent e) {
111: EditorUtilities.openImage(
112: fImages[index].getValue(), getProduct()
113: .getDefiningPluginId());
114: }
115: });
116: }
117:
118: toolkit.paintBordersFor(client);
119: section.setClient(client);
120: // Register to be notified when the model changes
121: getModel().addModelChangedListener(this );
122: }
123:
124: public void refresh() {
125: IWindowImages images = getWindowImages();
126: // Turn off auto message update until after values are set
127: fWinImageEntryValidator[0].setRefresh(false);
128: for (int i = 0; i < F_ICON_LABELS.length; i++) {
129: fImages[i].setValue(images.getImagePath(i), true);
130: }
131: // Turn back on auto message update
132: fWinImageEntryValidator[0].setRefresh(true);
133: super .refresh();
134: }
135:
136: /**
137: * @return
138: */
139: private boolean validateWinImageEntry(int index) {
140: return EditorUtilities.imageEntryHasExactSize(
141: fWinImageEntryValidator[index], fImages[index],
142: getProduct(), F_ICON_DIMENSIONS[index][0],
143: F_ICON_DIMENSIONS[index][1]);
144: }
145:
146: private IWindowImages getWindowImages() {
147: IWindowImages images = getProduct().getWindowImages();
148: if (images == null) {
149: images = getModel().getFactory().createWindowImages();
150: getProduct().setWindowImages(images);
151: }
152: return images;
153: }
154:
155: private IProduct getProduct() {
156: return getModel().getProduct();
157: }
158:
159: private IProductModel getModel() {
160: return (IProductModel) getPage().getPDEEditor()
161: .getAggregateModel();
162: }
163:
164: public void commit(boolean onSave) {
165: for (int i = 0; i < F_ICON_LABELS.length; i++) {
166: fImages[i].commit();
167: }
168: super .commit(onSave);
169: }
170:
171: public void cancelEdit() {
172: for (int i = 0; i < F_ICON_LABELS.length; i++) {
173: fImages[i].cancelEdit();
174: }
175: super .cancelEdit();
176: }
177:
178: private void handleBrowse(FormEntry entry) {
179: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
180: getSection().getShell(), new WorkbenchLabelProvider(),
181: new WorkbenchContentProvider());
182:
183: dialog.setValidator(new FileValidator());
184: dialog.setAllowMultiple(false);
185: dialog.setTitle(PDEUIMessages.WindowImagesSection_dialogTitle);
186: dialog
187: .setMessage(PDEUIMessages.WindowImagesSection_dialogMessage);
188: dialog.addFilter(new FileExtensionFilter("gif")); //$NON-NLS-1$
189: dialog.setInput(PDEPlugin.getWorkspace().getRoot());
190:
191: if (dialog.open() == Window.OK) {
192: IFile file = (IFile) dialog.getFirstResult();
193: entry.setValue(file.getFullPath().toString());
194: }
195: }
196:
197: public boolean canPaste(Clipboard clipboard) {
198: Display d = getSection().getDisplay();
199: Control c = d.getFocusControl();
200: if (c instanceof Text)
201: return true;
202: return false;
203: }
204:
205: /* (non-Javadoc)
206: * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
207: */
208: public void modelChanged(IModelChangedEvent e) {
209: // No need to call super, handling world changed event here
210: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
211: handleModelEventWorldChanged(e);
212: }
213: }
214:
215: /**
216: * @param event
217: */
218: private void handleModelEventWorldChanged(IModelChangedEvent event) {
219: refresh();
220: }
221:
222: /* (non-Javadoc)
223: * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
224: */
225: public void dispose() {
226: IProductModel model = getModel();
227: if (model != null) {
228: model.removeModelChangedListener(this);
229: }
230: super.dispose();
231: }
232: }
|