1 19 20 package org.netbeans.editor; 21 22 import org.openide.util.NbBundle; 23 import org.openide.util.RequestProcessor; 24 25 import java.awt.event.ActionEvent ; 26 import java.util.logging.Level ; 27 import java.util.logging.LogRecord ; 28 import java.util.logging.Logger ; 29 import javax.swing.Action ; 30 import javax.swing.JMenuItem ; 31 import javax.swing.text.BadLocationException ; 32 import javax.swing.text.TextAction ; 33 import javax.swing.text.JTextComponent ; 34 import javax.swing.text.Caret ; 35 36 45 46 public abstract class BaseAction extends TextAction { 47 48 49 public static final String POPUP_MENU_TEXT = "PopupMenuText"; 51 52 public static final String LOCALE_DESC_PREFIX = "desc-"; 54 55 public static final String LOCALE_POPUP_PREFIX = "popup-"; 57 58 public static final String ICON_RESOURCE_PROPERTY = "IconResource"; 60 61 public static final int SELECTION_REMOVE = 1; 62 63 64 public static final int MAGIC_POSITION_RESET = 2; 65 66 67 public static final int ABBREV_RESET = 4; 68 69 72 public static final int UNDO_MERGE_RESET = 8; 73 74 75 public static final int WORD_MATCH_RESET = 16; 76 77 78 public static final int CLEAR_STATUS_TEXT = 32; 79 80 81 public static final int NO_RECORDING = 64; 82 83 84 public static final int SAVE_POSITION = 128; 85 86 89 public static final String NO_KEYBINDING = "no-keybinding"; 91 92 private static Logger UILOG = Logger.getLogger("org.netbeans.ui.actions.editor"); 94 97 protected int updateMask; 98 99 private static boolean recording; 100 private static StringBuffer macroBuffer = new StringBuffer (); 101 private static StringBuffer textBuffer = new StringBuffer (); 102 103 static final long serialVersionUID =-4255521122272110786L; 104 105 public BaseAction(String name) { 106 this(name, 0); 107 } 108 109 public BaseAction(String name, int updateMask) { 110 super(name); 111 this.updateMask = updateMask; 112 } 113 114 118 protected Object findValue(String key){ 119 return LocaleSupport.getString(key); 120 } 121 122 public Object getValue(String key){ 123 Object obj = super.getValue(key); 124 125 if (obj == null){ 126 obj = createDefaultValue(key); 127 if (obj != null) { 128 putValue(key, obj); 129 } 130 } 131 132 return obj; 133 } 134 135 150 protected Object createDefaultValue(String key) { 151 Object ret = null; 152 if (SHORT_DESCRIPTION.equals(key)) { 153 Class bundleClass = getShortDescriptionBundleClass(); 154 if (bundleClass != null) { 155 String bundleKey = (String )getValue(Action.NAME); 157 ret = NbBundle.getBundle(bundleClass).getString(bundleKey); 158 } else { ret = getDefaultShortDescription(); 161 } 162 163 } else if (POPUP_MENU_TEXT.equals(key)){ 164 String bundleKey = LOCALE_POPUP_PREFIX + getValue(Action.NAME); 165 ret = findValue(bundleKey); 166 if (ret == null){ 167 ret = getValue(SHORT_DESCRIPTION); 168 } 169 } 170 return ret; 171 } 172 173 179 protected Class getShortDescriptionBundleClass() { 180 return null; 181 } 182 183 192 protected Object getDefaultShortDescription() { 193 String actionName = (String )getValue(Action.NAME); 194 String localizerKey = LOCALE_DESC_PREFIX + actionName; 195 Object obj = findValue(localizerKey); 196 if (obj==null){ 197 obj = findValue(actionName); 198 if (obj==null) obj = actionName; 199 } 200 return obj; 201 } 202 203 209 protected void settingsChange(SettingsChangeEvent evt, Class kitClass) { 210 } 211 212 223 public final void actionPerformed(final ActionEvent evt) { 224 final JTextComponent target = getTextComponent(evt); 225 226 if( recording && 0 == (updateMask & NO_RECORDING) ) { 227 recordAction( target, evt ); 228 } 229 230 231 updateComponent(target); 232 233 if (UILOG.isLoggable(Level.FINE)) { 234 String actionName = getValue(NAME) != null ? getValue(NAME).toString().toLowerCase() : null; 235 if (actionName != null && 236 !"default-typed".equals(actionName) && -1 == actionName.indexOf("caret") && -1 == actionName.indexOf("delete") && -1 == actionName.indexOf("selection") && -1 == actionName.indexOf("build-tool-tip") && -1 == actionName.indexOf("build-popup-menu") && -1 == actionName.indexOf("-kit-install") ) { 244 LogRecord r = new LogRecord (Level.FINE, "UI_ACTION_EDITOR"); r.setResourceBundle(NbBundle.getBundle(BaseAction.class)); 246 if (evt != null) { 247 r.setParameters(new Object [] { evt, evt.toString(), this, toString(), getValue(NAME) }); 248 } 249 r.setLoggerName(UILOG.getName()); 250 UILOG.log(r); 251 } 252 } 253 254 if (asynchonous()) { 255 RequestProcessor.getDefault().post(new Runnable () { 256 public void run() { 257 actionPerformed(evt, target); 258 } 259 }); 260 } else { 261 actionPerformed(evt, target); 262 } 263 } 264 265 private void recordAction( JTextComponent target, ActionEvent evt ) { 266 if( this == target.getKeymap().getDefaultAction() ) { textBuffer.append( getFilteredActionCommand(evt.getActionCommand()) ); 268 } else { if( textBuffer.length() > 0 ) { 270 if( macroBuffer.length() > 0 ) macroBuffer.append( ' ' ); 271 macroBuffer.append( encodeText( textBuffer.toString() ) ); 272 textBuffer.setLength( 0 ); 273 } 274 if( macroBuffer.length() > 0 ) macroBuffer.append( ' ' ); 275 String name = (String )getValue( Action.NAME ); 276 macroBuffer.append( encodeActionName( name ) ); 277 } 278 } 279 280 private String getFilteredActionCommand(String cmd) 281 { 282 if (cmd == null || cmd.length() == 0) 283 return ""; 284 char ch = cmd.charAt(0); 285 if ((ch >= 0x20) && (ch != 0x7F)) 286 return cmd; 287 else 288 return ""; 289 } 290 291 boolean startRecording( JTextComponent target ) { 292 if( recording ) return false; 293 recording = true; 294 macroBuffer.setLength(0); 295 textBuffer.setLength(0); 296 Utilities.setStatusText( target, 297 NbBundle.getBundle(BaseAction.class).getString( "macro-recording" ) ); 298 return true; 299 } 300 301 String stopRecording( JTextComponent target ) { 302 if( !recording ) return null; 303 304 if( textBuffer.length() > 0 ) { 305 if( macroBuffer.length() > 0 ) macroBuffer.append( ' ' ); 306 macroBuffer.append( encodeText( textBuffer.toString() ) ); 307 } 308 String retVal = macroBuffer.toString(); 309 recording = false; 310 Utilities.setStatusText( target, "" ); return retVal; 312 } 313 314 private String encodeText( String s ) { 315 char[] text = s.toCharArray(); 316 StringBuffer encoded = new StringBuffer ( "\""); for( int i=0; i < text.length; i++ ) { 318 char c = text[i]; 319 if( c == '"' || c == '\\' ) encoded.append( '\\' ); 320 encoded.append( c ); 321 } 322 return encoded.append( '"' ).toString(); 323 } 324 325 private String encodeActionName( String s ) { 326 char[] actionName = s.toCharArray(); 327 StringBuffer encoded = new StringBuffer (); 328 for( int i=0; i < actionName.length; i++ ) { 329 char c = actionName[i]; 330 if( Character.isWhitespace( c ) || c == '\\' ) encoded.append( '\\' ); 331 encoded.append( c ); 332 } 333 return encoded.toString(); 334 } 335 336 341 public abstract void actionPerformed(ActionEvent evt, JTextComponent target); 342 343 protected boolean asynchonous() { 344 return false; 345 } 346 347 public JMenuItem getPopupMenuItem(JTextComponent target) { 348 return null; 349 } 350 351 public String getPopupMenuText(JTextComponent target) { 352 String txt = (String )getValue(POPUP_MENU_TEXT); 353 if (txt == null) { 354 txt = (String )getValue(NAME); 355 } 356 return txt; 357 } 358 359 363 public void updateComponent(JTextComponent target) { 364 updateComponent(target, this.updateMask); 365 } 366 367 371 public void updateComponent(JTextComponent target, int updateMask) { 372 if (target != null && target.getDocument() instanceof BaseDocument) { 373 BaseDocument doc = (BaseDocument)target.getDocument(); 374 boolean writeLocked = false; 375 376 try { 377 if ((updateMask & SELECTION_REMOVE) != 0) { 379 writeLocked = true; 380 doc.extWriteLock(); 381 Caret caret = target.getCaret(); 382 if (caret != null && caret.isSelectionVisible()) { 383 int dot = caret.getDot(); 384 int markPos = caret.getMark(); 385 if (dot < markPos) { int tmpPos = dot; 387 dot = markPos; 388 markPos = tmpPos; 389 } 390 try { 391 target.getDocument().remove(markPos, dot - markPos); 392 } catch (BadLocationException e) { 393 Utilities.annotateLoggable(e); 394 } 395 } 396 } 397 398 if ((updateMask & MAGIC_POSITION_RESET) != 0) { 400 if (target.getCaret() != null) 401 target.getCaret().setMagicCaretPosition(null); 402 } 403 404 if ((updateMask & ABBREV_RESET) != 0) { 406 ((BaseTextUI)target.getUI()).getEditorUI().getAbbrev().reset(); 407 } 408 409 if ((updateMask & UNDO_MERGE_RESET) != 0) { 411 doc.resetUndoMerge(); 412 } 413 414 if ((updateMask & WORD_MATCH_RESET) != 0) { 416 ((BaseTextUI)target.getUI()).getEditorUI().getWordMatch().clear(); 417 } 418 419 if (!recording && (updateMask & CLEAR_STATUS_TEXT) != 0) { 421 Utilities.clearStatusText(target); 422 } 423 424 if ((updateMask & SAVE_POSITION) != 0) { 426 JumpList.checkAddEntry(target); 427 } 428 429 } finally { 430 if (writeLocked) { 431 doc.extWriteUnlock(); 432 } 433 } 434 } 435 } 436 437 } 438 | Popular Tags |