001: /*******************************************************************************
002: * Copyright (c) 2000, 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.feature;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.pde.core.IModelChangedEvent;
014: import org.eclipse.pde.internal.core.ifeature.IFeature;
015: import org.eclipse.pde.internal.core.ifeature.IFeatureInstallHandler;
016: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
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.FormLayoutFactory;
021: import org.eclipse.pde.internal.ui.editor.PDESection;
022: import org.eclipse.pde.internal.ui.parts.FormEntry;
023: import org.eclipse.swt.dnd.Clipboard;
024: import org.eclipse.swt.dnd.RTFTransfer;
025: import org.eclipse.swt.dnd.TextTransfer;
026: import org.eclipse.swt.dnd.Transfer;
027: import org.eclipse.swt.dnd.TransferData;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.ui.forms.widgets.FormToolkit;
031: import org.eclipse.ui.forms.widgets.Section;
032:
033: public class HandlerSection extends PDESection {
034: private FormEntry fLibraryText;
035:
036: private FormEntry fHandlerText;
037:
038: public HandlerSection(FeatureAdvancedPage page, Composite parent) {
039: super (page, parent, Section.DESCRIPTION);
040: getSection().setText(
041: PDEUIMessages.FeatureEditor_HandlerSection_title);
042: getSection().setDescription(
043: PDEUIMessages.FeatureEditor_HandlerSection_desc);
044: createClient(getSection(), page.getManagedForm().getToolkit());
045: }
046:
047: public boolean canPaste(Clipboard clipboard) {
048: TransferData[] types = clipboard.getAvailableTypes();
049: Transfer[] transfers = new Transfer[] {
050: TextTransfer.getInstance(), RTFTransfer.getInstance() };
051: for (int i = 0; i < types.length; i++) {
052: for (int j = 0; j < transfers.length; j++) {
053: if (transfers[j].isSupportedType(types[i]))
054: return true;
055: }
056: }
057: return false;
058: }
059:
060: public void commit(boolean onSave) {
061: fLibraryText.commit();
062: fHandlerText.commit();
063: super .commit(onSave);
064: }
065:
066: public void createClient(Section section, FormToolkit toolkit) {
067:
068: section.setLayout(FormLayoutFactory.createClearGridLayout(
069: false, 1));
070: GridData data = new GridData(GridData.FILL_HORIZONTAL);
071: section.setLayoutData(data);
072:
073: Composite container = toolkit.createComposite(section);
074: container.setLayout(FormLayoutFactory
075: .createSectionClientGridLayout(false, 2));
076: container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
077:
078: IFeatureModel model = (IFeatureModel) getPage().getModel();
079: final IFeature feature = model.getFeature();
080:
081: fLibraryText = new FormEntry(container, toolkit,
082: PDEUIMessages.FeatureEditor_HandlerSection_library,
083: null, false);
084: fLibraryText.setFormEntryListener(new FormEntryAdapter(this ) {
085: public void textValueChanged(FormEntry text) {
086: try {
087: setLibrary(feature, text.getValue());
088: } catch (CoreException e) {
089: PDEPlugin.logException(e);
090: }
091: }
092: });
093: fHandlerText = new FormEntry(container, toolkit,
094: PDEUIMessages.FeatureEditor_HandlerSection_handler,
095: null, false);
096: fHandlerText.setFormEntryListener(new FormEntryAdapter(this ) {
097: public void textValueChanged(FormEntry text) {
098: try {
099: setHandler(feature, text.getValue());
100: } catch (CoreException e) {
101: PDEPlugin.logException(e);
102: }
103: }
104: });
105:
106: toolkit.paintBordersFor(container);
107: section.setClient(container);
108: initialize();
109: }
110:
111: private void setLibrary(IFeature feature, String value)
112: throws CoreException {
113: IFeatureInstallHandler handler = getHandler(feature);
114: handler.setLibrary(value);
115: }
116:
117: private void setHandler(IFeature feature, String value)
118: throws CoreException {
119: IFeatureInstallHandler handler = getHandler(feature);
120: handler.setHandlerName(value);
121: }
122:
123: private IFeatureInstallHandler getHandler(IFeature feature)
124: throws CoreException {
125: IFeatureInstallHandler handler = feature.getInstallHandler();
126: if (handler == null) {
127: handler = feature.getModel().getFactory()
128: .createInstallHandler();
129: feature.setInstallHandler(handler);
130: }
131: return handler;
132: }
133:
134: public void dispose() {
135: IFeatureModel model = (IFeatureModel) getPage().getModel();
136: if (model != null)
137: model.removeModelChangedListener(this );
138: super .dispose();
139: }
140:
141: public void initialize() {
142: IFeatureModel model = (IFeatureModel) getPage().getModel();
143: refresh();
144: if (model.isEditable() == false) {
145: fLibraryText.getText().setEditable(false);
146: fHandlerText.getText().setEditable(false);
147: }
148: model.addModelChangedListener(this );
149: }
150:
151: public void modelChanged(IModelChangedEvent e) {
152: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
153: markStale();
154: }
155: }
156:
157: public void setFocus() {
158: if (fLibraryText != null)
159: fLibraryText.getText().setFocus();
160: }
161:
162: private void setIfDefined(FormEntry formText, Object value) {
163: if (value != null)
164: formText.setValue(value.toString(), true);
165: else
166: formText.setValue(null, true);
167: }
168:
169: public void refresh() {
170: IFeatureModel model = (IFeatureModel) getPage().getModel();
171: IFeature feature = model.getFeature();
172: IFeatureInstallHandler handler = feature.getInstallHandler();
173: if (handler != null) {
174: setIfDefined(fLibraryText, handler.getLibrary());
175: setIfDefined(fHandlerText, handler.getHandlerName());
176: }
177: super .refresh();
178: }
179:
180: public void cancelEdit() {
181: fLibraryText.cancelEdit();
182: fHandlerText.cancelEdit();
183: super.cancelEdit();
184: }
185: }
|