1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 import java.util.ResourceBundle ; 16 17 import org.osgi.framework.Bundle; 18 19 import org.eclipse.swt.widgets.Shell; 20 21 import org.eclipse.core.commands.ExecutionException; 22 import org.eclipse.core.commands.operations.IOperationHistory; 23 import org.eclipse.core.commands.operations.IUndoableOperation; 24 25 import org.eclipse.core.runtime.IAdaptable; 26 import org.eclipse.core.runtime.ILog; 27 import org.eclipse.core.runtime.IStatus; 28 import org.eclipse.core.runtime.Platform; 29 import org.eclipse.core.runtime.Status; 30 31 import org.eclipse.core.resources.IResource; 32 33 import org.eclipse.jface.dialogs.IInputValidator; 34 import org.eclipse.jface.dialogs.InputDialog; 35 import org.eclipse.jface.window.Window; 36 37 import org.eclipse.jface.text.BadLocationException; 38 import org.eclipse.jface.text.IDocument; 39 import org.eclipse.jface.text.ITextSelection; 40 41 import org.eclipse.ui.IEditorInput; 42 import org.eclipse.ui.PlatformUI; 43 import org.eclipse.ui.ide.undo.CreateMarkersOperation; 44 45 46 64 public class AddMarkerAction extends TextEditorAction { 65 66 67 68 private static final int MAX_LABEL_LENGTH= 80; 69 70 private String fMarkerType; 71 72 private boolean fAskForLabel; 73 74 private ResourceBundle fBundle; 75 76 private String fPrefix; 77 78 79 93 public AddMarkerAction(ResourceBundle bundle, String prefix, ITextEditor textEditor, String markerType, boolean askForLabel) { 94 super(bundle, prefix, textEditor); 95 fBundle= bundle; 96 fPrefix= prefix; 97 fMarkerType= markerType; 98 fAskForLabel= askForLabel; 99 } 100 101 106 protected ResourceBundle getResourceBundle() { 107 return fBundle; 108 } 109 110 115 protected String getResourceKeyPrefix() { 116 return fPrefix; 117 } 118 119 122 public void run() { 123 IResource resource= getResource(); 124 if (resource == null) 125 return; 126 Map attributes= getInitialAttributes(); 127 if (fAskForLabel) { 128 if (!askForLabel(attributes)) 129 return; 130 } 131 132 String name= getToolTipText(); 133 name= name == null ? TextEditorMessages.AddMarkerAction_addMarker : name; 134 135 final Shell shell= getTextEditor().getSite().getShell(); 136 IAdaptable context= new IAdaptable() { 137 public Object getAdapter(Class adapter) { 138 if (adapter == Shell.class) 139 return shell; 140 return null; 141 } 142 }; 143 144 IUndoableOperation operation= new CreateMarkersOperation(fMarkerType, attributes, resource, name); 145 IOperationHistory operationHistory= PlatformUI.getWorkbench().getOperationSupport().getOperationHistory(); 146 try { 147 operationHistory.execute(operation, null, context); 148 } catch (ExecutionException x) { 149 Bundle bundle= Platform.getBundle(PlatformUI.PLUGIN_ID); 150 ILog log= Platform.getLog(bundle); 151 String msg= getString(fBundle, fPrefix + "error.dialog.message", fPrefix + "error.dialog.message"); log.log(new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, msg, x)); 153 } 154 } 155 156 159 public void update() { 160 setEnabled(getResource() != null); 161 } 162 163 172 protected boolean askForLabel(Map attributes) { 173 174 Object o= attributes.get("message"); String proposal= (o instanceof String ) ? (String ) o : ""; if (proposal == null) 177 proposal= ""; 179 String title= getString(fBundle, fPrefix + "dialog.title", fPrefix + "dialog.title"); String message= getString(fBundle, fPrefix + "dialog.message", fPrefix + "dialog.message"); IInputValidator inputValidator= new IInputValidator() { 182 public String isValid(String newText) { 183 return (newText == null || newText.trim().length() == 0) ? " " : null; } 185 }; 186 InputDialog dialog= new InputDialog(getTextEditor().getSite().getShell(), title, message, proposal, inputValidator); 187 188 String label= null; 189 if (dialog.open() != Window.CANCEL) 190 label= dialog.getValue(); 191 192 if (label == null) 193 return false; 194 195 label= label.trim(); 196 if (label.length() == 0) 197 return false; 198 199 attributes.put("message", label); return true; 201 } 202 203 210 protected Map getInitialAttributes() { 211 212 Map attributes= new HashMap (11); 213 214 ITextSelection selection= (ITextSelection) getTextEditor().getSelectionProvider().getSelection(); 215 if (!selection.isEmpty()) { 216 217 int start= selection.getOffset(); 218 int length= selection.getLength(); 219 220 if (length < 0) { 221 length= -length; 222 start -= length; 223 } 224 225 MarkerUtilities.setCharStart(attributes, start); 226 MarkerUtilities.setCharEnd(attributes, start + length); 227 228 int line= selection.getStartLine(); 230 MarkerUtilities.setLineNumber(attributes, line == -1 ? -1 : line + 1); 231 232 IDocument document= getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput()); 233 MarkerUtilities.setMessage(attributes, getLabelProposal(document, start, length)); 234 235 } 236 237 return attributes; 238 } 239 240 248 protected String getLabelProposal(IDocument document, int offset, int length) { 249 250 251 try { 252 253 254 if (length > 0) { 255 256 int i= 0; 258 boolean skip= true; 259 while (i < length) { 260 boolean isWhitespace= Character.isWhitespace(document.getChar(offset + i)); 261 if (!skip && isWhitespace) 262 break; 263 if (skip && !isWhitespace) 264 skip= false; 265 i++; 266 } 267 268 String label= document.get(offset, i); 269 return label.trim(); 270 } 271 272 273 char ch; 274 275 int left= offset; 277 278 int line= document.getLineOfOffset(offset); 279 int limit= document.getLineOffset(line); 280 281 while (left > limit) { 282 ch= document.getChar(left); 283 if (Character.isWhitespace(ch)) 284 break; 285 --left; 286 } 287 288 limit += document.getLineLength(line); 289 290 while (left <= limit) { 292 ch= document.getChar(left); 293 if (!Character.isWhitespace(ch)) 294 break; 295 ++left; 296 } 297 298 if (left > limit) 299 return null; 300 301 limit= Math.min(limit, left + MAX_LABEL_LENGTH); 302 303 int right= (offset + length > limit ? limit : offset + length); 305 while (right < limit) { 306 ch= document.getChar(right); 307 if (Character.isWhitespace(ch)) 308 break; 309 ++right; 310 } 311 312 if (left != right) { 314 String label= document.get(left, right - left); 315 return label.trim(); 316 } 317 318 } catch (BadLocationException x) { 319 } 321 322 return null; 323 } 324 325 333 protected IResource getResource() { 334 ITextEditor editor= getTextEditor(); 335 if (editor != null) { 336 IEditorInput input= editor.getEditorInput(); 337 return (IResource) ((IAdaptable) input).getAdapter(IResource.class); 338 } 339 return null; 340 } 341 } 342 | Popular Tags |