001: /*******************************************************************************
002: * Copyright (c) 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.ui.internal.provisional.views.markers;
011:
012: import org.eclipse.core.resources.IMarker;
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.core.runtime.SubProgressMonitor;
015: import org.eclipse.jface.dialogs.Dialog;
016: import org.eclipse.jface.viewers.ISelection;
017: import org.eclipse.jface.viewers.IStructuredContentProvider;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.jface.viewers.LabelProvider;
020: import org.eclipse.jface.viewers.ListViewer;
021: import org.eclipse.jface.viewers.Viewer;
022: import org.eclipse.jface.viewers.ViewerComparator;
023: import org.eclipse.jface.wizard.WizardPage;
024: import org.eclipse.osgi.util.NLS;
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.layout.GridLayout;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Label;
030: import org.eclipse.ui.IMarkerResolution;
031: import org.eclipse.ui.internal.provisional.views.markers.api.MarkerSupportConstants;
032: import org.eclipse.ui.views.markers.WorkbenchMarkerResolution;
033: import org.eclipse.ui.views.markers.internal.MarkerMessages;
034: import org.eclipse.ui.views.markers.internal.Util;
035:
036: /**
037: * QuickFixPage is a page for the quick fixes of a marker.
038: *
039: * @since 3.4
040: *
041: */
042: public class QuickFixPage extends WizardPage {
043:
044: private IMarker marker;
045: private IMarkerResolution[] resolutions;
046: private ListViewer resolutionsList;
047:
048: /**
049: * Create a new instance of the receiver.
050: *
051: * @param marker
052: * @param resolutions
053: */
054: public QuickFixPage(IMarker marker, IMarkerResolution[] resolutions) {
055: super (marker.getAttribute(
056: MarkerSupportConstants.ATTRIBUTE_NAME,
057: MarkerSupportConstants.EMPTY_STRING));
058: this .marker = marker;
059: this .resolutions = resolutions;
060: }
061:
062: public void createControl(Composite parent) {
063:
064: initializeDialogUnits(parent);
065:
066: // Create a new composite as there is the title bar seperator
067: // to deal with
068: Composite control = new Composite(parent, SWT.NONE);
069: control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
070: true));
071: setControl(control);
072: control.setLayout(new GridLayout());
073:
074: Label resolutionsLabel = new Label(control, SWT.NONE);
075: resolutionsLabel
076: .setText(MarkerMessages.MarkerResolutionDialog_Resolutions_List_Title);
077:
078: resolutionsList = new ListViewer(control, SWT.BORDER
079: | SWT.SINGLE | SWT.V_SCROLL);
080: resolutionsList
081: .setContentProvider(new IStructuredContentProvider() {
082: /*
083: * (non-Javadoc)
084: *
085: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
086: */
087: public Object[] getElements(Object inputElement) {
088: return resolutions;
089: }
090:
091: /*
092: * (non-Javadoc)
093: *
094: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
095: */
096: public void dispose() {
097:
098: }
099:
100: /*
101: * (non-Javadoc)
102: *
103: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
104: * java.lang.Object, java.lang.Object)
105: */
106: public void inputChanged(Viewer viewer,
107: Object oldInput, Object newInput) {
108:
109: }
110: });
111:
112: resolutionsList.setLabelProvider(new LabelProvider() {
113: /*
114: * (non-Javadoc)
115: *
116: * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
117: */
118: public String getText(Object element) {
119: return ((IMarkerResolution) element).getLabel();
120: }
121: });
122:
123: resolutionsList.setInput(this );
124:
125: resolutionsList.setComparator(new ViewerComparator() {
126: /*
127: * (non-Javadoc)
128: *
129: * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer,
130: * java.lang.Object, java.lang.Object)
131: */
132: public int compare(Viewer viewer, Object e1, Object e2) {
133: return ((IMarkerResolution) e1).getLabel().compareTo(
134: ((IMarkerResolution) e1).getLabel());
135: }
136: });
137:
138: resolutionsList.getControl().setLayoutData(
139: new GridData(SWT.FILL, SWT.FILL, true, true));
140:
141: String message = NLS.bind(
142: MarkerMessages.MarkerResolutionDialog_Description, Util
143: .getProperty(IMarker.MESSAGE, marker));
144: if (message.length() > 50) {
145: // Add a carriage return in the middle if we can
146: int insertionIndex = chooseWhitespace(message);
147: if (insertionIndex > 0) {
148: StringBuffer buffer = new StringBuffer();
149: buffer.append(message.substring(0, insertionIndex));
150: buffer.append("\n"); //$NON-NLS-1$
151: buffer.append(message.substring(insertionIndex, message
152: .length()));
153: message = buffer.toString();
154: }
155: }
156:
157: setTitle(MarkerMessages.MarkerResolutionDialog_Title);
158: setMessage(message);
159: Dialog.applyDialogFont(control);
160: }
161:
162: /**
163: * Choose a good whitespace position for a page break. Start in the middle
164: * of the message.
165: *
166: * @param message
167: * @return int -1 if there is no whitespace to choose.
168: */
169: private int chooseWhitespace(String message) {
170:
171: for (int i = message.length() / 2; i < message.length(); i++) {
172: if (Character.isWhitespace(message.charAt(i)))
173: return i;
174: }
175: return -1;
176: }
177:
178: /**
179: * Return the marker being edited.
180: *
181: * @return IMarker
182: */
183: public IMarker getMarker() {
184: return marker;
185: }
186:
187: /*
188: * (non-Javadoc)
189: *
190: * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
191: */
192: public boolean isPageComplete() {
193: return true;
194: }
195:
196: /**
197: * Finish has been pressed. Process the resolutions. monitor the monitor to
198: * report to.
199: */
200: void performFinish(IProgressMonitor monitor) {
201:
202: ISelection selected = resolutionsList.getSelection();
203: if (selected.isEmpty())
204: return;
205:
206: IMarkerResolution resolution = (IMarkerResolution) ((IStructuredSelection) selected)
207: .getFirstElement();
208:
209: if (resolution instanceof WorkbenchMarkerResolution) {
210:
211: ((WorkbenchMarkerResolution) resolution).run(
212: new IMarker[] { marker }, new SubProgressMonitor(
213: monitor, 1));
214: } else {
215:
216: // Allow paint events and wake up the button
217: getShell().getDisplay().readAndDispatch();
218: if (monitor.isCanceled()) {
219:
220: monitor.subTask(Util.getProperty(IMarker.MESSAGE,
221: marker));
222: resolution.run(marker);
223: monitor.worked(1);
224: }
225:
226: }
227:
228: }
229:
230: }
|