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.core.runtime.Path;
013: import org.eclipse.jface.action.Action;
014: import org.eclipse.jface.action.IMenuManager;
015: import org.eclipse.jface.action.Separator;
016: import org.eclipse.jface.viewers.DoubleClickEvent;
017: import org.eclipse.jface.viewers.IDoubleClickListener;
018: import org.eclipse.jface.viewers.ISelectionChangedListener;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.jface.viewers.StructuredSelection;
022: import org.eclipse.jface.viewers.TableViewer;
023: import org.eclipse.jface.viewers.Viewer;
024: import org.eclipse.jface.viewers.ViewerComparator;
025: import org.eclipse.pde.core.IModelChangedEvent;
026: import org.eclipse.pde.core.plugin.TargetPlatform;
027: import org.eclipse.pde.internal.core.itarget.IAdditionalLocation;
028: import org.eclipse.pde.internal.core.itarget.ILocationInfo;
029: import org.eclipse.pde.internal.core.itarget.ITarget;
030: import org.eclipse.pde.internal.core.itarget.ITargetModel;
031: import org.eclipse.pde.internal.ui.PDEPlugin;
032: import org.eclipse.pde.internal.ui.PDEUIMessages;
033: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
034: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
035: import org.eclipse.pde.internal.ui.editor.TableSection;
036: import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
037: import org.eclipse.pde.internal.ui.parts.TablePart;
038: import org.eclipse.pde.internal.ui.util.SWTUtil;
039: import org.eclipse.swt.SWT;
040: import org.eclipse.swt.custom.BusyIndicator;
041: import org.eclipse.swt.layout.GridData;
042: import org.eclipse.swt.widgets.Composite;
043: import org.eclipse.ui.actions.ActionFactory;
044: import org.eclipse.ui.forms.widgets.FormToolkit;
045: import org.eclipse.ui.forms.widgets.Section;
046:
047: public class LocationsSection extends TableSection {
048:
049: private TableViewer fContentViewer;
050:
051: class ContentProvider extends DefaultTableProvider {
052: public Object[] getElements(Object parent) {
053: ITarget target = getTarget();
054: return target.getAdditionalDirectories();
055: }
056: }
057:
058: public LocationsSection(PDEFormPage page, Composite parent) {
059: super (page, parent, Section.DESCRIPTION, new String[] {
060: PDEUIMessages.LocationsSection_add,
061: PDEUIMessages.LocationsSection_edit,
062: PDEUIMessages.LocationsSection_remove });
063: }
064:
065: protected void createClient(Section section, FormToolkit toolkit) {
066: section.setLayout(FormLayoutFactory.createClearTableWrapLayout(
067: false, 1));
068: Composite client = toolkit.createComposite(section);
069: client.setLayout(FormLayoutFactory
070: .createSectionClientGridLayout(false, 2));
071: client.setLayoutData(new GridData(GridData.FILL_BOTH
072: | GridData.GRAB_VERTICAL));
073:
074: createViewerPartControl(client, SWT.MULTI, 2, toolkit);
075:
076: TablePart tablePart = getTablePart();
077: GridData data = (GridData) tablePart.getControl()
078: .getLayoutData();
079: data.grabExcessVerticalSpace = true;
080: data.grabExcessHorizontalSpace = true;
081: fContentViewer = tablePart.getTableViewer();
082: fContentViewer.setContentProvider(new ContentProvider());
083: fContentViewer.setLabelProvider(PDEPlugin.getDefault()
084: .getLabelProvider());
085: fContentViewer.setInput(getTarget());
086: fContentViewer
087: .addSelectionChangedListener(new ISelectionChangedListener() {
088: public void selectionChanged(
089: SelectionChangedEvent event) {
090: updateButtons();
091: }
092: });
093: fContentViewer.setComparator(new ViewerComparator() {
094: public int compare(Viewer viewer, Object e1, Object e2) {
095: IAdditionalLocation loc1 = (IAdditionalLocation) e1;
096: return loc1.getPath().compareToIgnoreCase(
097: ((IAdditionalLocation) e2).getPath());
098: }
099: });
100: fContentViewer
101: .addDoubleClickListener(new IDoubleClickListener() {
102: public void doubleClick(DoubleClickEvent event) {
103: // if user double clicked on one selected item
104: if (((StructuredSelection) fContentViewer
105: .getSelection()).toArray().length == 1)
106: handleEdit();
107: }
108: });
109:
110: toolkit.paintBordersFor(client);
111: section.setClient(client);
112: section.setText(PDEUIMessages.LocationsSection_title);
113: section
114: .setDescription(PDEUIMessages.LocationsSection_description);
115: GridData sectionData = new GridData(GridData.FILL_BOTH);
116: sectionData.horizontalSpan = 2;
117: section.setLayoutData(sectionData);
118: updateButtons();
119:
120: getModel().addModelChangedListener(this );
121: }
122:
123: private ITarget getTarget() {
124: return getModel().getTarget();
125: }
126:
127: private ITargetModel getModel() {
128: return (ITargetModel) getPage().getPDEEditor()
129: .getAggregateModel();
130: }
131:
132: protected void updateButtons() {
133: int selectionNum = ((StructuredSelection) fContentViewer
134: .getSelection()).toArray().length;
135: TablePart table = getTablePart();
136: table.setButtonEnabled(1, selectionNum == 1);
137: table.setButtonEnabled(2, selectionNum > 0);
138: }
139:
140: protected void buttonSelected(int index) {
141: switch (index) {
142: case 0:
143: handleAdd();
144: break;
145: case 1:
146: handleEdit();
147: break;
148: case 2:
149: handleDelete();
150: }
151: }
152:
153: protected void handleAdd() {
154: showDialog(null);
155: }
156:
157: protected void handleEdit() {
158: showDialog((IAdditionalLocation) ((StructuredSelection) fContentViewer
159: .getSelection()).iterator().next());
160: }
161:
162: protected void handleDelete() {
163: IStructuredSelection ssel = (IStructuredSelection) fContentViewer
164: .getSelection();
165: if (ssel.size() > 0) {
166: Object[] objects = ssel.toArray();
167: ITarget target = getTarget();
168: IAdditionalLocation[] dirs = new IAdditionalLocation[objects.length];
169: System.arraycopy(objects, 0, dirs, 0, objects.length);
170: target.removeAdditionalDirectories(dirs);
171: }
172: }
173:
174: private void showDialog(final IAdditionalLocation location) {
175: final ITarget model = getTarget();
176: BusyIndicator.showWhile(fContentViewer.getTable().getDisplay(),
177: new Runnable() {
178: public void run() {
179: LocationDialog dialog = new LocationDialog(
180: fContentViewer.getTable().getShell(),
181: model, location);
182: dialog.create();
183: SWTUtil.setDialogSize(dialog, 500, -1);
184: dialog.open();
185: }
186: });
187: }
188:
189: /* (non-Javadoc)
190: * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
191: */
192: public void modelChanged(IModelChangedEvent e) {
193: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
194: handleModelEventWorldChanged(e);
195: return;
196: }
197: Object[] objects = e.getChangedObjects();
198: if (e.getChangeType() == IModelChangedEvent.INSERT) {
199: for (int i = 0; i < objects.length; i++) {
200: if (objects[i] instanceof IAdditionalLocation) {
201: fContentViewer.add(objects[i]);
202: }
203: }
204: } else if (e.getChangeType() == IModelChangedEvent.REMOVE) {
205: for (int i = 0; i < objects.length; i++) {
206: if (objects[i] instanceof IAdditionalLocation) {
207: fContentViewer.remove(objects[i]);
208: }
209: }
210: }
211: if (e.getChangedProperty() == IAdditionalLocation.P_PATH) {
212: fContentViewer.refresh();
213: }
214: }
215:
216: /**
217: * @param event
218: */
219: private void handleModelEventWorldChanged(IModelChangedEvent event) {
220: // Reload input
221: fContentViewer.setInput(getTarget());
222: // Perform the refresh
223: refresh();
224: }
225:
226: public boolean doGlobalAction(String actionId) {
227: if (actionId.equals(ActionFactory.DELETE.getId())) {
228: handleDelete();
229: return true;
230: }
231: if (actionId.equals(ActionFactory.CUT.getId())) {
232: handleDelete();
233: return false;
234: }
235: if (actionId.equals(ActionFactory.PASTE.getId())) {
236: doPaste();
237: return true;
238: }
239: return false;
240: }
241:
242: protected boolean canPaste(Object target, Object[] objects) {
243: for (int i = 0; i < objects.length; i++) {
244: if (objects[i] instanceof IAdditionalLocation)
245: return true;
246: }
247: return false;
248: }
249:
250: protected void doPaste(Object target, Object[] objects) {
251: for (int i = 0; i < objects.length; i++) {
252: if (objects[i] instanceof IAdditionalLocation
253: && !hasPath(((IAdditionalLocation) objects[i])
254: .getPath())) {
255: IAdditionalLocation loc = (IAdditionalLocation) objects[i];
256: loc.setModel(getModel());
257: getTarget().addAdditionalDirectories(
258: new IAdditionalLocation[] { loc });
259: }
260: }
261: }
262:
263: protected boolean hasPath(String path) {
264: Path checkPath = new Path(path);
265: IAdditionalLocation[] locs = getModel().getTarget()
266: .getAdditionalDirectories();
267: for (int i = 0; i < locs.length; i++) {
268: if (new Path(locs[i].getPath()).equals(checkPath))
269: return true;
270: }
271: return isTargetLocation(checkPath);
272: }
273:
274: private boolean isTargetLocation(Path path) {
275: ILocationInfo info = getModel().getTarget().getLocationInfo();
276: if (info.useDefault()) {
277: Path home = new Path(TargetPlatform.getDefaultLocation());
278: return home.equals(path);
279: }
280: return new Path(info.getPath()).equals(path);
281: }
282:
283: public void dispose() {
284: ITargetModel model = getModel();
285: if (model != null)
286: model.removeModelChangedListener(this );
287: super .dispose();
288: }
289:
290: /* (non-Javadoc)
291: * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
292: */
293: public void refresh() {
294: fContentViewer.refresh();
295: updateButtons();
296: super .refresh();
297: }
298:
299: protected void fillContextMenu(IMenuManager manager) {
300: IStructuredSelection ssel = (IStructuredSelection) fContentViewer
301: .getSelection();
302: if (ssel == null)
303: return;
304:
305: Action removeAction = new Action(
306: PDEUIMessages.ContentSection_remove) {
307: public void run() {
308: handleDelete();
309: }
310: };
311: removeAction.setEnabled(isEditable() && ssel.size() > 0);
312: manager.add(removeAction);
313:
314: manager.add(new Separator());
315:
316: getPage().getPDEEditor().getContributor()
317: .contextMenuAboutToShow(manager);
318: }
319:
320: protected void selectionChanged(IStructuredSelection selection) {
321: getPage().getPDEEditor().setSelection(selection);
322: }
323: }
|