1 /*******************************************************************************
2  *  Copyright (c) 2005, 2015 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Simon Scholz <simon.scholz@vogella.com> - bug 440275
14  *******************************************************************************/
15 package org.eclipse.pde.internal.ui.editor.product;
16 
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.pde.core.IModelChangedEvent;
19 import org.eclipse.pde.core.plugin.IPluginModelBase;
20 import org.eclipse.pde.core.plugin.PluginRegistry;
21 import org.eclipse.pde.internal.core.iproduct.*;
22 import org.eclipse.pde.internal.ui.PDEPlugin;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.pde.internal.ui.dialogs.PluginSelectionDialog;
25 import org.eclipse.pde.internal.ui.editor.*;
26 import org.eclipse.pde.internal.ui.parts.FormEntry;
27 import org.eclipse.swt.dnd.Clipboard;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.widgets.*;
30 import org.eclipse.ui.IActionBars;
31 import org.eclipse.ui.forms.widgets.FormToolkit;
32 import org.eclipse.ui.forms.widgets.Section;
33 
34 public class SplashLocationSection extends PDESection {
35 
36 	private FormEntry fPluginEntry;
37 
SplashLocationSection(PDEFormPage page, Composite parent)38 	public SplashLocationSection(PDEFormPage page, Composite parent) {
39 		super(page, parent, Section.DESCRIPTION);
40 		createClient(getSection(), page.getEditor().getToolkit());
41 	}
42 
43 	@Override
createClient(Section section, FormToolkit toolkit)44 	protected void createClient(Section section, FormToolkit toolkit) {
45 
46 		// Configure section
47 		section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
48 		GridData data = new GridData(GridData.FILL_HORIZONTAL);
49 		section.setLayoutData(data);
50 		section.setText(PDEUIMessages.SplashSection_title);
51 		section.setDescription(PDEUIMessages.SplashSection_desc);
52 		// Create and configure client
53 		Composite client = toolkit.createComposite(section);
54 		client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
55 		client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
56 		// Create form entry
57 		IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars();
58 		fPluginEntry = new FormEntry(client, toolkit, PDEUIMessages.SplashSection_plugin, PDEUIMessages.SplashSection_browse, false);
59 		fPluginEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
60 			@Override
61 			public void textValueChanged(FormEntry entry) {
62 				getSplashInfo().setLocation(entry.getValue(), false);
63 			}
64 
65 			@Override
66 			public void browseButtonSelected(FormEntry entry) {
67 				handleBrowse();
68 			}
69 		});
70 		fPluginEntry.setEditable(isEditable());
71 
72 		toolkit.paintBordersFor(client);
73 		section.setClient(client);
74 		// Register to be notified when the model changes
75 		getModel().addModelChangedListener(this);
76 	}
77 
78 	@Override
refresh()79 	public void refresh() {
80 		ISplashInfo info = getSplashInfo();
81 		fPluginEntry.setValue(info.getLocation(), true);
82 		super.refresh();
83 	}
84 
85 	@Override
commit(boolean onSave)86 	public void commit(boolean onSave) {
87 		fPluginEntry.commit();
88 		super.commit(onSave);
89 	}
90 
91 	@Override
cancelEdit()92 	public void cancelEdit() {
93 		fPluginEntry.cancelEdit();
94 		super.cancelEdit();
95 	}
96 
getSplashInfo()97 	private ISplashInfo getSplashInfo() {
98 		ISplashInfo info = getProduct().getSplashInfo();
99 		if (info == null) {
100 			info = getModel().getFactory().createSplashInfo();
101 			getProduct().setSplashInfo(info);
102 		}
103 		return info;
104 	}
105 
getProduct()106 	private IProduct getProduct() {
107 		return getModel().getProduct();
108 	}
109 
getModel()110 	private IProductModel getModel() {
111 		return (IProductModel) getPage().getPDEEditor().getAggregateModel();
112 	}
113 
handleBrowse()114 	private void handleBrowse() {
115 
116 		PluginSelectionDialog pluginSelectionDialog = new PluginSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), PluginRegistry.getActiveModels(), false);
117 
118 		pluginSelectionDialog.setTitle(PDEUIMessages.SplashSection_selection);
119 		pluginSelectionDialog.setMessage(PDEUIMessages.SplashSection_message);
120 		if (pluginSelectionDialog.open() == Window.OK) {
121 			IPluginModelBase model = (IPluginModelBase) pluginSelectionDialog.getFirstResult();
122 			fPluginEntry.setValue(model.getPluginBase().getId());
123 		}
124 	}
125 
126 	@Override
canPaste(Clipboard clipboard)127 	public boolean canPaste(Clipboard clipboard) {
128 		Display d = getSection().getDisplay();
129 		Control c = d.getFocusControl();
130 		if (c instanceof Text)
131 			return true;
132 		return false;
133 	}
134 
135 	@Override
modelChanged(IModelChangedEvent e)136 	public void modelChanged(IModelChangedEvent e) {
137 		// No need to call super, handling world changed event here
138 		if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
139 			handleModelEventWorldChanged(e);
140 		}
141 	}
142 
143 	/**
144 	 * @param event
145 	 */
handleModelEventWorldChanged(IModelChangedEvent event)146 	private void handleModelEventWorldChanged(IModelChangedEvent event) {
147 		refresh();
148 	}
149 
150 	@Override
dispose()151 	public void dispose() {
152 		IProductModel model = getModel();
153 		if (model != null) {
154 			model.removeModelChangedListener(this);
155 		}
156 		super.dispose();
157 	}
158 
159 }
160