1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.awt.Color ; 23 import java.awt.Font ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.logging.Logger ; 28 import javax.swing.KeyStroke ; 29 import org.netbeans.editor.MultiKeyBinding; 30 import java.util.List ; 31 import java.util.StringTokenizer ; 32 import java.util.ArrayList ; 33 import java.awt.Insets ; 34 import org.w3c.dom.Document ; 35 import org.openide.xml.XMLUtil; 36 import org.openide.filesystems.FileLock; 37 import java.io.IOException ; 38 import org.openide.filesystems.FileObject; 39 import org.openide.loaders.DataObject; 40 import java.util.Enumeration ; 41 import org.openide.cookies.InstanceCookie; 42 import org.openide.util.actions.SystemAction; 43 import java.util.Set ; 44 import java.awt.Dimension ; 45 46 51 public class OptionUtilities { 52 53 private static final Logger LOG = Logger.getLogger(OptionUtilities.class.getName()); 54 55 public static final String DEFAULT_FOLDER = "Defaults"; 58 private OptionUtilities() { 59 } 61 62 private static String wrap(String s){ 63 return (s.length()==1)? "0"+s : s; } 65 66 67 public static String color2String(Color c){ 68 StringBuffer sb = new StringBuffer (); 69 sb.append("#"); sb.append(wrap(Integer.toHexString(c.getRed()).toUpperCase())); 71 sb.append(wrap(Integer.toHexString(c.getGreen()).toUpperCase())); 72 sb.append(wrap(Integer.toHexString(c.getBlue()).toUpperCase())); 73 return sb.toString(); 74 } 75 76 77 public static Color string2Color(String s){ 78 try{ 79 return Color.decode(s); 80 }catch(NumberFormatException nfe){ 81 return null; 82 } 83 } 84 85 86 public static int string2Int(String s){ 87 try{ 88 return Integer.parseInt(s); 89 }catch(NumberFormatException nfe){ 90 return -1; 91 } 92 } 93 94 95 public static int getFontStyle(String s){ 96 s=s.toLowerCase(); 97 int ret = Font.PLAIN; 98 if (s.indexOf("bold") != -1) ret |= Font.BOLD; if (s.indexOf("italic") != -1) ret |= Font.ITALIC; return ret; 101 } 102 103 104 public static String style2String(int i){ 105 if (Font.BOLD == i) return "bold"; if (Font.ITALIC == i) return "italic"; if ( (Font.BOLD+Font.ITALIC) == i ) return "bold-italic"; return "plain"; } 110 111 112 116 public static Map getMapDiff(Map oldMap, Map newMap, boolean allowNewKeys){ 117 Map ret = new HashMap (); 118 119 for( Iterator i = newMap.keySet().iterator(); i.hasNext(); ) { 120 Object key = (Object )i.next(); 121 Object value = newMap.get( key ); 122 if (!value.equals(oldMap.get(key))) 124 ret.put(key,newMap.get(key)); 125 else if (allowNewKeys && !oldMap.containsKey(key)) 127 ret.put(key, newMap.get(key)); 128 } 129 130 for ( Iterator i = oldMap.keySet().iterator(); i.hasNext(); ) { 131 String key = (String )i.next(); 132 Object value = oldMap.get( key ); 133 if (!newMap.containsKey(key)) ret.put(key, ""); 135 } 136 137 return ret; 138 } 139 140 141 142 public static String keysToString(KeyStroke [] stroke){ 143 if (stroke == null) return "NULL"; StringBuffer sb = new StringBuffer (); 145 146 for (int i=0; i<stroke.length; i++){ 147 sb.append(org.openide.util.Utilities.keyToString(stroke[i])); 148 if (i<stroke.length-1) sb.append("$"); } 150 151 return sb.toString(); 152 } 153 154 155 public static String keyToString(KeyStroke stroke){ 156 if (stroke == null) return "NULL"; return org.openide.util.Utilities.keyToString(stroke); 158 } 159 160 161 public static KeyStroke stringToKey(String s){ 162 if (s.equals("NULL")) return null; return org.openide.util.Utilities.stringToKey(s); 164 } 165 166 167 public static KeyStroke [] stringToKeys(String s){ 168 if (s.equals("NULL")) return null; 170 StringTokenizer st = new StringTokenizer (s.toUpperCase(), "$"); ArrayList arr = new ArrayList (); 172 173 while (st.hasMoreElements()) { 174 s = st.nextToken(); 175 KeyStroke k = stringToKey(s); 176 if (k == null) return null; 177 arr.add(k); 178 } 179 180 return (KeyStroke [])arr.toArray(new KeyStroke [arr.size()]); 181 } 182 183 public static void printDefaultAbbrevs(Map map){ 184 System.out.println("-----------------------------------------------------------"); System.out.println("<?xml version=\"1.0\"?>"); System.out.println("<!DOCTYPE catalog PUBLIC \""+AbbrevsMIMEProcessor.PUBLIC_ID+"\""); System.out.println(" \""+AbbrevsMIMEProcessor.SYSTEM_ID+"\">"); System.out.println(""); 189 System.out.println("<"+AbbrevsMIMEOptionFile.TAG_ROOT+">"); for ( Iterator i = map.keySet().iterator(); i.hasNext(); ) { 191 String key = (String )i.next(); 192 String value = (String )map.get(key); 193 System.out.println("<"+AbbrevsMIMEOptionFile.TAG_ABBREV+" "+AbbrevsMIMEOptionFile.ATTR_KEY+"=\""+key+"\">" +value+"</"+AbbrevsMIMEOptionFile.TAG_ABBREV+">"); } 196 System.out.println("</"+AbbrevsMIMEOptionFile.TAG_ROOT+">"); } 198 199 200 public static void printDefaultAbbrevs(Map map, FileObject file){ 201 Document doc = XMLUtil.createDocument(AbbrevsMIMEOptionFile.TAG_ROOT, null, AbbrevsMIMEProcessor.PUBLIC_ID, AbbrevsMIMEProcessor.SYSTEM_ID); 202 org.w3c.dom.Element rootElem = doc.getDocumentElement(); 203 204 for( Iterator i = map.keySet().iterator(); i.hasNext(); ) { 206 String key = (String )i.next(); 207 if (map.get(key) instanceof String ){ 208 209 String action = (String ) map.get(key); 210 211 org.w3c.dom.Element abbrevElem = doc.createElement(AbbrevsMIMEOptionFile.TAG_ABBREV); 212 abbrevElem.setAttribute(AbbrevsMIMEOptionFile.ATTR_KEY, key); 213 abbrevElem.appendChild(doc.createTextNode(action)); 214 rootElem.appendChild(abbrevElem); 215 216 } 217 } 218 219 doc.getDocumentElement().normalize(); 220 221 try{ 222 FileLock lock = file.lock(); 223 try { 224 XMLUtil.write(doc, file.getOutputStream(lock), "UTF-8"); } catch (Exception e){ 226 e.printStackTrace(); 227 } finally { 228 lock.releaseLock(); 229 } 230 }catch (IOException ioe){ 231 ioe.printStackTrace(); 232 } 233 } 234 235 236 237 public static void printDefaultMacros(Map map, FileObject file){ 238 Document doc = XMLUtil.createDocument(MacrosMIMEOptionFile.TAG_ROOT, null, MacrosMIMEProcessor.PUBLIC_ID, MacrosMIMEProcessor.SYSTEM_ID); 239 org.w3c.dom.Element rootElem = doc.getDocumentElement(); 240 241 for( Iterator i = map.keySet().iterator(); i.hasNext(); ) { 243 String key = (String )i.next(); 244 if (map.get(key) instanceof String ){ 245 246 String action = (String ) map.get(key); 247 248 org.w3c.dom.Element macroElem = doc.createElement(MacrosMIMEOptionFile.TAG_MACRO); 249 macroElem.setAttribute(MacrosMIMEOptionFile.ATTR_NAME, key); 250 macroElem.appendChild(doc.createTextNode(action)); 251 rootElem.appendChild(macroElem); 252 } 253 } 254 255 doc.getDocumentElement().normalize(); 256 257 try{ 258 FileLock lock = file.lock(); 259 try { 260 XMLUtil.write(doc, file.getOutputStream(lock), "UTF-8"); } catch (Exception e){ 262 e.printStackTrace(); 263 } finally { 264 lock.releaseLock(); 265 } 266 }catch (IOException ioe){ 267 ioe.printStackTrace(); 268 } 269 } 270 271 272 public static void printDefaultKeyBindings(List list, FileObject file){ 273 Map map = makeKeyBindingsMap(list); 274 Document doc = XMLUtil.createDocument(KeyBindingsMIMEOptionFile.TAG_ROOT, null, KeyBindingsMIMEProcessor.PUBLIC_ID, KeyBindingsMIMEProcessor.SYSTEM_ID); 275 org.w3c.dom.Element rootElem = doc.getDocumentElement(); 276 277 for( Iterator i = map.keySet().iterator(); i.hasNext(); ) { 279 String key = (String )i.next(); 280 if (map.get(key) instanceof MultiKeyBinding){ 281 282 MultiKeyBinding mkb = (MultiKeyBinding) map.get(key); 283 if (mkb==null) continue; 284 285 org.w3c.dom.Element keybElem = doc.createElement(KeyBindingsMIMEOptionFile.TAG_BIND); 286 keybElem.setAttribute(KeyBindingsMIMEOptionFile.ATTR_KEY, key); 287 keybElem.setAttribute(KeyBindingsMIMEOptionFile.ATTR_ACTION_NAME, mkb.actionName); 288 rootElem.appendChild(keybElem); 289 } 290 } 291 292 doc.getDocumentElement().normalize(); 293 294 try{ 295 FileLock lock = file.lock(); 296 try { 297 XMLUtil.write(doc, file.getOutputStream(lock), "UTF-8"); } catch (Exception e){ 299 e.printStackTrace(); 300 } finally { 301 lock.releaseLock(); 302 } 303 }catch (IOException ioe){ 304 ioe.printStackTrace(); 305 } 306 } 307 308 309 public static String insetsToString(Insets ins){ 310 StringBuffer sb = new StringBuffer (); 311 sb.append(ins.top); 312 sb.append(','); 313 314 sb.append(ins.left); 315 sb.append(','); 316 317 sb.append(ins.bottom); 318 sb.append(','); 319 320 sb.append(ins.right); 321 322 return sb.toString(); 323 } 324 325 326 public static Insets parseInsets(String s){ 327 StringTokenizer st = new StringTokenizer (s, ","); 329 int arr[] = new int[4]; 330 int i=0; 331 while (st.hasMoreElements()) { 332 if (i>3) return null; 333 try{ 334 arr[i] = Integer.parseInt(st.nextToken()); 335 }catch(NumberFormatException nfe){ 336 return null; 337 } 338 i++; 339 } 340 if (i!=4) return null; 341 return new Insets (arr[0],arr[1],arr[2],arr[3]); 342 } 343 344 345 public static String dimensionToString(Dimension dim){ 346 StringBuffer sb = new StringBuffer (); 347 sb.append(dim.width); 348 sb.append(','); 349 350 sb.append(dim.height); 351 352 return sb.toString(); 353 } 354 355 356 public static Dimension parseDimension(String s){ 357 StringTokenizer st = new StringTokenizer (s, ","); 359 int arr[] = new int[2]; 360 int i=0; 361 while (st.hasMoreElements()) { 362 if (i>1) return null; 363 try{ 364 arr[i] = Integer.parseInt(st.nextToken()); 365 }catch(NumberFormatException nfe){ 366 return null; 367 } 368 i++; 369 } 370 if (i!=2) return null; 371 return new Dimension (arr[0],arr[1]); 372 } 373 374 375 377 public static Map makeKeyBindingsMap(List propList){ 378 Map ret = new HashMap (); 379 380 boolean output = true; 381 for (int i=0; i<propList.size(); i++){ 382 Object obj = propList.get(i); 383 if (! (obj instanceof org.netbeans.editor.MultiKeyBinding)) { 384 if (!org.netbeans.editor.MultiKeyBinding.class.getClassLoader().equals(obj.getClass().getClassLoader())){ 385 if (output) { 386 System.err.println("Different classloaders:"); 387 System.err.println(org.netbeans.editor.MultiKeyBinding.class.getClassLoader()); 388 System.err.println(obj.getClass().getClassLoader()); 389 output = false; 390 } 391 } 392 393 continue; 394 } 395 MultiKeyBinding mkb = (MultiKeyBinding)obj; 396 String fileName = (mkb.keys == null) ? 397 OptionUtilities.keyToString(mkb.key) : OptionUtilities.keysToString(mkb.keys); 398 if (fileName!=null){ 399 ret.put(fileName,mkb); 400 } 401 } 402 return ret; 403 } 404 405 406 public static List getGlobalAttribs(String folderName){ 407 MIMEOptionFolder mimeFolder = AllOptionsFolder.getDefault().getMIMEFolder(); 408 if (mimeFolder == null) return new ArrayList (); 409 MultiPropertyFolder mpf = mimeFolder.getMPFolder(folderName,false); if ( mpf!=null ){ 411 List retList = new ArrayList (); 412 for (Enumeration e = mpf.getDataFolder().getPrimaryFile().getAttributes() ; e.hasMoreElements() ;) { 413 String name = (String ) e.nextElement(); 414 if (name.indexOf("/") != -1) { Object value = mpf.getDataFolder().getPrimaryFile().getAttribute(name); 416 if ((value instanceof Boolean ) && ((Boolean ) value).booleanValue()){ 417 retList.add(name); 418 } 419 } 420 } 421 return retList; 422 } 423 return new ArrayList (); 424 } 425 426 427 public static List getGlobalPopupAttribs(){ 428 return getGlobalAttribs("Popup"); } 430 431 433 public static List getGlobalPopupMenuItems(){ 434 return getGlobalMenuItems("Popup"); } 436 437 438 public static List getGlobalMenuItems(String folderName){ 439 MIMEOptionFolder mimeFolder = AllOptionsFolder.getDefault().getMIMEFolder(); 440 if (mimeFolder == null) return new ArrayList (); 441 MultiPropertyFolder mpf = mimeFolder.getMPFolder(folderName,false); if ( mpf!=null ){ 443 return mpf.getProperties(); 444 } 445 return new ArrayList (); 446 } 447 448 public static List getPopupStrings(List popup){ 449 return getPopupStrings(popup, false); 450 } 451 452 456 public static List getPopupStrings(List popup, boolean addSeparatorInstance){ 457 List retList = new ArrayList (); 458 for (int i=0; i<popup.size(); i++){ 459 if (!(popup.get(i) instanceof DataObject)) continue; 460 461 DataObject dob = (DataObject) popup.get(i); 462 InstanceCookie ic = (InstanceCookie)dob.getCookie(InstanceCookie.class); 463 464 if (ic!=null){ 465 466 try{ 467 if (SystemAction.class.isAssignableFrom(ic.instanceClass())){ 468 retList.add(ic.instanceName()); 469 }else if (javax.swing.Action .class.isAssignableFrom(ic.instanceClass())){ 470 retList.add(ic.instanceCreate()); 471 } 472 if(javax.swing.JSeparator .class.isAssignableFrom(ic.instanceClass())){ 473 retList.add(addSeparatorInstance?new javax.swing.JSeparator ():null); 474 } 475 }catch(IOException ioe){ 476 ioe.printStackTrace(); 477 }catch(ClassNotFoundException cnfe){ 478 cnfe.printStackTrace(); 479 } 480 }else{ 481 if ("org-openide-windows-TopComponent".equals(dob.getName())){ retList.add(dob.getName().replace('-','.')); 483 }else{ 484 retList.add(dob.getName()); 485 } 486 } 487 } 488 489 return retList; 490 } 491 492 493 public static List arrangeMergedFolderObjects(Set items, Set attribs){ 494 List retList = new ArrayList (items); 496 497 List nameList = new ArrayList (); 499 for (int i = 0; i<retList.size(); i++){ 500 DataObject dob = (DataObject) retList.get(i); 501 nameList.add(dob.getPrimaryFile().getNameExt()); 502 } 503 504 for (int i=0; i<attribs.size(); i++){ 506 Iterator j = attribs.iterator(); 507 while (j.hasNext()){ 508 String attr = (String ) j.next(); 509 int idx = attr.indexOf('/'); 510 if (idx == -1) { 511 LOG.warning("Ignoring invalid ordering attribute: '" + attr + "'"); continue; 514 } 515 String firstItem = attr.substring(0,idx); 516 String secondItem = attr.substring(idx+1); 517 int first = nameList.indexOf(firstItem); 518 int second = nameList.indexOf(secondItem); 519 if ( (first>second) && (second>-1)){ 520 nameList.add(second,nameList.remove(first)); 522 retList.add(second,retList.remove(first)); 523 } 524 } 525 } 526 return retList; 527 } 528 529 530 public static List arrangeMergedPopup(Set items, Set attribs){ 531 List list = arrangeMergedFolderObjects(items, attribs); 532 return getPopupStrings(list); 534 } 535 536 } 537 | Popular Tags |