1 19 20 21 package org.netbeans.modules.palette; 22 23 import java.awt.event.ActionListener ; 24 import java.beans.BeanInfo ; 25 import java.util.ResourceBundle ; 26 import java.text.MessageFormat ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.datatransfer.*; 29 import java.io.IOException ; 30 import java.util.Arrays ; 31 import java.util.Comparator ; 32 import java.util.concurrent.Callable ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 import javax.swing.*; 36 import org.netbeans.spi.palette.PaletteController; 37 import org.netbeans.modules.palette.ui.PalettePanel; 38 39 import org.openide.*; 40 import org.openide.loaders.DataObject; 41 import org.openide.nodes.*; 42 import org.openide.filesystems.*; 43 import org.openide.util.*; 44 import org.openide.util.datatransfer.PasteType; 45 import org.openide.util.datatransfer.NewType; 46 import org.openide.util.datatransfer.ExClipboard; 47 48 49 54 public final class Utils { 55 56 private static final Logger ERR = Logger.getLogger( "org.netbeans.modules.palette" ); 58 private Utils() { 59 } 60 61 63 public static ResourceBundle getBundle() { 64 return NbBundle.getBundle(Utils.class); 65 } 66 67 public static String getBundleString(String key) { 68 return getBundle().getString(key); 69 } 70 71 public static Action[] mergeActions( Action[] first, Action[] second ) { 72 if( null == first ) 73 return second; 74 if( null == second ) 75 return first; 76 77 Action[] res = new Action[first.length+second.length+1]; 78 System.arraycopy( first, 0, res, 0, first.length ); 79 res[first.length] = null; 80 System.arraycopy( second, 0, res, first.length+1, second.length ); 81 return res; 82 } 83 84 public static boolean isReadonly( Node node ) { 85 Object val = node.getValue( PaletteController.ATTR_IS_READONLY ); 86 if( null == val ) { 87 DataObject dobj = (DataObject)node.getCookie( DataObject.class ); 88 if( null != dobj ) { 89 val = dobj.getPrimaryFile().getAttribute( PaletteController.ATTR_IS_READONLY ); 90 } 91 } 92 if( null != val ) { 93 return Boolean.valueOf( val.toString() ).booleanValue(); 94 } else { 95 return !node.canDestroy(); 96 } 97 } 98 99 public static HelpCtx getHelpCtx( Node node, HelpCtx defaultHelp ) { 100 HelpCtx retValue = defaultHelp; 101 if( null == retValue || HelpCtx.DEFAULT_HELP.equals( retValue ) ) { 102 Object val = node.getValue( PaletteController.ATTR_HELP_ID ); 103 if( null == val ) { 104 DataObject dobj = (DataObject)node.getCookie( DataObject.class ); 105 if( null != dobj ) { 106 val = dobj.getPrimaryFile().getAttribute( PaletteController.ATTR_HELP_ID ); 107 } 108 } 109 110 if( null != val ) 111 retValue = new HelpCtx( val.toString() ); 112 } 113 return retValue; 114 } 115 116 public static void addCustomizationMenuItems( JPopupMenu popup, PaletteController controller, Settings settings ) { 117 popup.addSeparator(); 118 popup.add( new ShowNamesAction( settings ) ); 119 popup.add( new ChangeIconSizeAction( settings ) ); 120 addResetMenuItem( popup, controller, settings ); 121 popup.addSeparator(); 122 popup.add( new ShowCustomizerAction( controller ) ); 123 } 124 125 static void addResetMenuItem( JPopupMenu popup, final PaletteController controller, final Settings settings ) { 126 JMenuItem item = new JMenuItem( getBundleString( "CTL_ResetPalettePopup" ) ); 127 item.addActionListener( new ActionListener () { 128 public void actionPerformed(ActionEvent e) { 129 resetPalette( controller, settings ); 130 } 131 }); 132 popup.add( item ); 133 } 134 135 142 public static Node findCategoryNode( Node root, String categoryName ) { 143 return root.getChildren().findChild( categoryName ); 144 } 145 146 public static void resetPalette( final PaletteController controller, final Settings settings ) { 147 Node rootNode = (Node)controller.getRoot().lookup( Node.class ); 148 if( null != rootNode ) 149 resetPalette( rootNode, controller, settings ); 150 } 151 152 public static void resetPalette( Node rootNode, PaletteController controller, Settings settings ) { 153 NotifyDescriptor desc = new NotifyDescriptor.Confirmation( 155 getBundleString("MSG_ConfirmPaletteReset"), getBundleString("CTL_ConfirmResetTitle"), NotifyDescriptor.YES_NO_OPTION); 158 159 if( NotifyDescriptor.YES_OPTION.equals( 160 DialogDisplayer.getDefault().notify(desc)) ) { 161 162 settings.reset(); 163 DataObject dob = (DataObject)rootNode.getLookup().lookup( DataObject.class ); 164 if( null != dob ) { 165 FileObject primaryFile = dob.getPrimaryFile(); 166 if( null != primaryFile && primaryFile.isFolder() ) { 167 final Object cleaner = primaryFile.getAttribute( "removeWritables" ); if( null != cleaner && (cleaner instanceof Callable ) ) { 169 try { 170 ((Callable )cleaner).call(); 171 } catch (Exception ex) { 172 ERR.log( Level.INFO, ex.getLocalizedMessage(), ex ); 173 } 174 } 175 } 176 } 177 controller.refresh(); 178 } 179 } 180 181 184 public static class NewCategoryAction extends AbstractAction { 185 private Node paletteNode; 186 187 190 public NewCategoryAction( Node paletteRootNode ) { 191 putValue(Action.NAME, getBundleString("CTL_CreateCategory")); this.paletteNode = paletteRootNode; 193 } 194 195 public void actionPerformed(ActionEvent event) { 196 NewType[] newTypes = paletteNode.getNewTypes(); 197 try { 198 if( null != newTypes && newTypes.length > 0 ) { 199 newTypes[0].create(); 200 } 201 } catch( IOException ioE ) { 202 ERR.log( Level.INFO, ioE.getLocalizedMessage(), ioE ); 203 } 204 } 205 } 206 207 210 static class SortCategoriesAction extends AbstractAction { 211 private Node paletteNode; 212 public SortCategoriesAction( Node paletteNode ) { 213 putValue(Action.NAME, getBundleString("CTL_SortCategories")); this.paletteNode = paletteNode; 215 } 216 217 public void actionPerformed(ActionEvent event) { 218 Index order = (Index)paletteNode.getCookie(Index.class); 219 if (order != null) { 220 final Node[] nodes = paletteNode.getChildren().getNodes( DefaultModel.canBlock() ); 221 Arrays.sort( nodes, new Comparator <Node>() { 222 public int compare(Node n1, Node n2) { 223 return n1.getDisplayName().compareTo( n2.getDisplayName() ); 224 } 225 } ); 226 int[] perm = new int[nodes.length]; 227 for( int i=0; i<perm.length; i++ ) { 228 perm[i] = order.indexOf( nodes[i] ); 229 } 230 order.reorder( perm ); 231 } 232 } 233 234 public boolean isEnabled() { 235 return (paletteNode.getCookie(Index.class) != null); 236 } 237 } 238 239 242 private static class ShowNamesAction extends AbstractAction { 243 244 private Settings settings; 245 246 public ShowNamesAction( Settings settings ) { 247 this.settings = settings; 248 } 249 250 public void actionPerformed(ActionEvent event) { 251 settings.setShowItemNames( !settings.getShowItemNames() ); 252 } 253 254 public Object getValue(String key) { 255 if (Action.NAME.equals(key)) { 256 boolean showNames = settings.getShowItemNames(); 257 return getBundleString(showNames ? "CTL_HideNames" : "CTL_ShowNames"); } else { 259 return super.getValue(key); 260 } 261 } 262 } 263 264 267 private static class ChangeIconSizeAction extends AbstractAction { 268 269 private Settings settings; 270 271 public ChangeIconSizeAction( Settings settings ) { 272 this.settings = settings; 273 } 274 275 public void actionPerformed(ActionEvent event) { 276 int oldSize = settings.getIconSize(); 277 int newSize = (oldSize == BeanInfo.ICON_COLOR_16x16) ? 278 BeanInfo.ICON_COLOR_32x32 : BeanInfo.ICON_COLOR_16x16; 279 settings.setIconSize( newSize ); 280 } 281 282 public Object getValue(String key) { 283 if (Action.NAME.equals(key)) { 284 String namePattern = getBundleString("CTL_IconSize"); return MessageFormat.format(namePattern, 286 new Object [] {Integer.valueOf(settings.getIconSize())}); 287 } else { 288 return super.getValue(key); 289 } 290 } 291 } 292 293 296 static class RefreshPaletteAction extends AbstractAction { 297 298 public RefreshPaletteAction() { 299 putValue(Action.NAME, getBundleString("CTL_RefreshPalette")); } 301 302 public void actionPerformed(ActionEvent event) { 303 PalettePanel.getDefault().doRefresh(); 304 } 305 306 } 307 308 311 static class DeleteCategoryAction extends AbstractAction { 312 private Node categoryNode; 313 314 public DeleteCategoryAction(Node categoryNode) { 315 this.categoryNode = categoryNode; 316 putValue(Action.NAME, getBundleString("CTL_DeleteCategory")); } 318 319 public void actionPerformed(ActionEvent event) { 320 String message = MessageFormat.format( 322 getBundleString("FMT_ConfirmCategoryDelete"), new Object [] { categoryNode.getName() }); 324 325 NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message, 326 getBundleString("CTL_ConfirmCategoryTitle"), NotifyDescriptor.YES_NO_OPTION); 328 329 if (NotifyDescriptor.YES_OPTION.equals(DialogDisplayer.getDefault().notify(desc))) { 330 try { 331 categoryNode.destroy(); 332 } catch (java.io.IOException e) { 333 ERR.log( Level.INFO, e.getLocalizedMessage(), e ); 334 } 335 } 336 } 337 338 public boolean isEnabled() { 339 return categoryNode.canDestroy(); 340 } 341 } 342 343 346 static class RenameCategoryAction extends AbstractAction { 347 private Node categoryNode; 348 349 public RenameCategoryAction(Node categoryNode) { 350 this.categoryNode = categoryNode; 351 putValue(Action.NAME, getBundleString("CTL_RenameCategory")); } 353 354 public void actionPerformed(ActionEvent event) { 355 NotifyDescriptor.InputLine desc = new NotifyDescriptor.InputLine( 356 getBundleString("CTL_NewName"), getBundleString("CTL_Rename")); desc.setInputText(categoryNode.getDisplayName()); 359 360 if (NotifyDescriptor.OK_OPTION.equals(DialogDisplayer.getDefault().notify(desc))) { 361 String newName; 362 try { 363 newName = desc.getInputText(); 364 if (!"".equals(newName)) categoryNode.setDisplayName(newName); 366 } catch (IllegalArgumentException e) { 367 ERR.log( Level.INFO, e.getLocalizedMessage(), e ); 368 } 369 } 370 } 371 372 public boolean isEnabled() { 373 return categoryNode.canRename(); 374 } 375 } 376 377 380 static class SortItemsAction extends AbstractAction { 381 private Node categoryNode; 382 public SortItemsAction( Node categoryNode ) { 383 putValue(Action.NAME, getBundleString("CTL_SortItems")); this.categoryNode = categoryNode; 385 } 386 387 public void actionPerformed(ActionEvent event) { 388 Index order = (Index)categoryNode.getCookie(Index.class); 389 if (order != null) { 390 final Node[] nodes = categoryNode.getChildren().getNodes( DefaultModel.canBlock() ); 391 Arrays.sort( nodes, new Comparator <Node>() { 392 public int compare(Node n1, Node n2) { 393 return n1.getDisplayName().compareTo( n2.getDisplayName() ); 394 } 395 } ); 396 int[] perm = new int[nodes.length]; 397 for( int i=0; i<perm.length; i++ ) { 398 perm[i] = order.indexOf( nodes[i] ); 399 } 400 order.reorder( perm ); 401 } 402 } 403 404 public boolean isEnabled() { 405 return (categoryNode.getCookie(Index.class) != null); 406 } 407 } 408 409 412 public static class PasteItemAction extends AbstractAction { 413 private Node categoryNode; 414 415 public PasteItemAction(Node categoryNode) { 416 this.categoryNode = categoryNode; 417 putValue(Action.NAME, getBundleString("CTL_Paste")); } 419 420 public void actionPerformed(ActionEvent event) { 421 PasteType type = getPasteType(); 422 if (type != null) { 423 try { 424 Transferable trans = type.paste(); 425 if (trans != null) { 426 ClipboardOwner owner = trans instanceof ClipboardOwner ? 427 (ClipboardOwner)trans : new StringSelection(""); Clipboard clipboard = (Clipboard)Lookup.getDefault().lookup(ExClipboard.class); 429 clipboard.setContents(trans, owner); 430 } 431 } catch (java.io.IOException e) { 432 ERR.log( Level.INFO, e.getLocalizedMessage(), e ); 433 } 434 } 435 } 436 437 public boolean isEnabled() { 438 return (getPasteType() != null); 439 } 440 441 private PasteType getPasteType() { 442 Clipboard clipboard = (Clipboard) Lookup.getDefault().lookup(ExClipboard.class); 443 Transferable trans = clipboard.getContents(this); 444 if (trans != null) { 445 PasteType[] pasteTypes = categoryNode.getPasteTypes(trans); 446 if (pasteTypes != null && pasteTypes.length != 0) 447 return pasteTypes[0]; 448 } 449 return null; 450 } 451 452 } 453 454 457 public static class CutItemAction extends AbstractAction { 458 private Node itemNode; 459 460 public CutItemAction(Node itemNode) { 461 this.itemNode = itemNode; 462 putValue(Action.NAME, getBundleString("CTL_Cut")); } 464 465 public void actionPerformed(ActionEvent event) { 466 try { 467 Transferable trans = itemNode.clipboardCut(); 468 if (trans != null) { 469 Clipboard clipboard = (Clipboard) 470 Lookup.getDefault().lookup(ExClipboard.class); 471 clipboard.setContents(trans, new StringSelection("")); } 473 } catch (java.io.IOException e) { 474 ERR.log( Level.INFO, e.getLocalizedMessage(), e ); 475 } 476 } 477 478 public boolean isEnabled() { 479 return itemNode.canCut(); 480 } 481 } 482 483 486 public static class CopyItemAction extends AbstractAction { 487 private Node itemNode; 488 489 public CopyItemAction(Node itemNode) { 490 this.itemNode = itemNode; 491 putValue(Action.NAME, getBundleString("CTL_Copy")); } 493 494 public void actionPerformed(ActionEvent event) { 495 try { 496 Transferable trans = itemNode.clipboardCopy(); 497 if (trans != null) { 498 Clipboard clipboard = (Clipboard) 499 Lookup.getDefault().lookup(ExClipboard.class); 500 clipboard.setContents(trans, new StringSelection("")); } 502 } catch (java.io.IOException e) { 503 ERR.log( Level.INFO, e.getLocalizedMessage(), e ); 504 } 505 } 506 507 public boolean isEnabled() { 508 return itemNode.canCopy(); 509 } 510 } 511 512 515 static class RemoveItemAction extends AbstractAction { 516 private Node itemNode; 517 518 public RemoveItemAction(Node itemNode) { 519 this.itemNode = itemNode; 520 putValue(Action.NAME, getBundleString("CTL_Delete")); } 522 523 public void actionPerformed(ActionEvent event) { 524 String message = MessageFormat.format( 526 getBundleString("FMT_ConfirmBeanDelete"), new Object [] { itemNode.getDisplayName() }); 528 529 NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message, 530 getBundleString("CTL_ConfirmBeanTitle"), NotifyDescriptor.YES_NO_OPTION); 532 533 if (NotifyDescriptor.YES_OPTION.equals(DialogDisplayer.getDefault().notify(desc))) { 534 try { 535 itemNode.destroy(); 536 } catch (java.io.IOException e) { 537 ERR.log( Level.INFO, e.getLocalizedMessage(), e ); 538 } 539 } 540 } 541 542 public boolean isEnabled() { 543 return itemNode.canDestroy(); 544 } 545 } 546 547 550 private static class ShowCustomizerAction extends AbstractAction { 551 private PaletteController palette; 552 553 public ShowCustomizerAction( PaletteController palette ) { 554 this.palette = palette; 555 putValue(Action.NAME, getBundleString("CTL_ShowCustomizer")); } 557 558 public void actionPerformed(ActionEvent event) { 559 palette.showCustomizer(); 560 } 561 } 562 } 563 | Popular Tags |