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.target;
011:
012: import org.eclipse.debug.ui.StringVariableSelectionDialog;
013: import org.eclipse.pde.core.IModelChangedEvent;
014: import org.eclipse.pde.internal.core.itarget.IArgumentsInfo;
015: import org.eclipse.pde.internal.core.itarget.ITarget;
016: import org.eclipse.pde.internal.core.itarget.ITargetModel;
017: import org.eclipse.pde.internal.ui.PDEPluginImages;
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.PDEFormPage;
022: import org.eclipse.pde.internal.ui.editor.PDESection;
023: import org.eclipse.pde.internal.ui.parts.FormEntry;
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.custom.CTabFolder;
026: import org.eclipse.swt.custom.CTabItem;
027: import org.eclipse.swt.dnd.Clipboard;
028: import org.eclipse.swt.events.SelectionAdapter;
029: import org.eclipse.swt.events.SelectionEvent;
030: import org.eclipse.swt.graphics.Color;
031: import org.eclipse.swt.graphics.Image;
032: import org.eclipse.swt.layout.GridData;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Display;
036: import org.eclipse.swt.widgets.Text;
037: import org.eclipse.ui.IActionBars;
038: import org.eclipse.ui.forms.IFormColors;
039: import org.eclipse.ui.forms.widgets.FormToolkit;
040: import org.eclipse.ui.forms.widgets.Section;
041:
042: public class ArgumentsSection extends PDESection {
043:
044: private static final String[] TAB_LABELS = new String[2];
045: static {
046: TAB_LABELS[0] = PDEUIMessages.ArgumentsSection_programTabLabel;
047: TAB_LABELS[1] = PDEUIMessages.ArgumentsSection_vmTabLabel;
048: }
049:
050: private CTabFolder fTabFolder;
051: private FormEntry fArgument;
052: private int fLastTab;
053: private Image fImage;
054:
055: public ArgumentsSection(PDEFormPage page, Composite parent) {
056: super (page, parent, Section.DESCRIPTION);
057: fImage = PDEPluginImages.DESC_ARGUMENT_TAB.createImage();
058: createClient(getSection(), page.getEditor().getToolkit());
059: }
060:
061: protected void createClient(Section section, FormToolkit toolkit) {
062: section.setLayout(FormLayoutFactory.createClearGridLayout(
063: false, 1));
064: section.setText(PDEUIMessages.ArgumentsSection_editorTitle);
065: section
066: .setDescription(PDEUIMessages.ArgumentsSection_description);
067: GridData data = new GridData(GridData.FILL_BOTH);
068: data.horizontalSpan = 1;
069: section.setLayoutData(data);
070:
071: Composite client = toolkit.createComposite(section);
072: client.setLayout(FormLayoutFactory
073: .createSectionClientGridLayout(false, 1));
074: GridData gd = new GridData(GridData.FILL_BOTH);
075: gd.widthHint = 100;
076: client.setLayoutData(gd);
077:
078: fTabFolder = new CTabFolder(client, SWT.FLAT | SWT.TOP);
079: toolkit.adapt(fTabFolder, true, true);
080: gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
081: fTabFolder.setLayoutData(gd);
082: gd.heightHint = 2;
083: toolkit.getColors().initializeSectionToolBarColors();
084: Color selectedColor = toolkit.getColors().getColor(
085: IFormColors.TB_BG);
086: fTabFolder.setSelectionBackground(new Color[] { selectedColor,
087: toolkit.getColors().getBackground() },
088: new int[] { 100 }, true);
089: fTabFolder.addSelectionListener(new SelectionAdapter() {
090: public void widgetSelected(SelectionEvent e) {
091: if (fArgument.isDirty())
092: fArgument.commit();
093: refresh();
094: }
095: });
096:
097: IActionBars actionBars = getPage().getPDEEditor()
098: .getEditorSite().getActionBars();
099:
100: fArgument = new FormEntry(client, toolkit,
101: PDEUIMessages.ArgumentsSection_argumentsTextLabel,
102: SWT.MULTI | SWT.WRAP);
103: fArgument.getText().setLayoutData(
104: new GridData(GridData.FILL_BOTH));
105: fArgument.setFormEntryListener(new FormEntryAdapter(this ,
106: actionBars) {
107: public void textValueChanged(FormEntry entry) {
108: if (fLastTab == 0)
109: getArgumentInfo().setProgramArguments(
110: fArgument.getValue());
111: else
112: getArgumentInfo().setVMArguments(
113: fArgument.getValue());
114: }
115: });
116:
117: Button variables = toolkit.createButton(client,
118: PDEUIMessages.ArgumentsSection_variableButtonTitle,
119: SWT.NONE);
120: variables.setLayoutData(new GridData(
121: GridData.HORIZONTAL_ALIGN_END));
122: variables.addSelectionListener(new SelectionAdapter() {
123: public void widgetSelected(SelectionEvent e) {
124: StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(
125: getSection().getShell());
126: dialog.open();
127: String variable = dialog.getVariableExpression();
128: if (variable != null) {
129: fArgument.getText().insert(variable);
130: }
131: }
132: });
133:
134: createTabs();
135: toolkit.paintBordersFor(client);
136: section.setClient(client);
137:
138: // Register to be notified when the model changes
139: getModel().addModelChangedListener(this );
140: }
141:
142: /* (non-Javadoc)
143: * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
144: */
145: public void modelChanged(IModelChangedEvent e) {
146: // No need to call super, handling world changed event here
147: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
148: handleModelEventWorldChanged(e);
149: }
150: }
151:
152: /**
153: * @param event
154: */
155: private void handleModelEventWorldChanged(IModelChangedEvent event) {
156: // Perform the refresh
157: refresh();
158: }
159:
160: private void createTabs() {
161: for (int i = 0; i < TAB_LABELS.length; i++) {
162: CTabItem item = new CTabItem(fTabFolder, SWT.NULL);
163: item.setText(TAB_LABELS[i]);
164: item.setImage(fImage);
165: }
166: fLastTab = 0;
167: fTabFolder.setSelection(fLastTab);
168: }
169:
170: private IArgumentsInfo getArgumentInfo() {
171: IArgumentsInfo info = getTarget().getArguments();
172: if (info == null) {
173: info = getModel().getFactory().createArguments();
174: getTarget().setArguments(info);
175: }
176: return info;
177: }
178:
179: private ITarget getTarget() {
180: return getModel().getTarget();
181: }
182:
183: private ITargetModel getModel() {
184: return (ITargetModel) getPage().getPDEEditor()
185: .getAggregateModel();
186: }
187:
188: public void refresh() {
189: fLastTab = fTabFolder.getSelectionIndex();
190: if (fLastTab == 0)
191: fArgument.setValue(getArgumentInfo().getProgramArguments(),
192: true);
193: else
194: fArgument
195: .setValue(getArgumentInfo().getVMArguments(), true);
196: super .refresh();
197: }
198:
199: public void commit(boolean onSave) {
200: fArgument.commit();
201: super .commit(onSave);
202: }
203:
204: public void cancelEdit() {
205: fArgument.cancelEdit();
206: super .cancelEdit();
207: }
208:
209: public boolean canPaste(Clipboard clipboard) {
210: Display d = getSection().getDisplay();
211: return d.getFocusControl() instanceof Text;
212: }
213:
214: public void dispose() {
215: ITargetModel model = getModel();
216: if (model != null) {
217: model.removeModelChangedListener(this);
218: }
219: if (fImage != null)
220: fImage.dispose();
221: super.dispose();
222: }
223:
224: }
|