1 19 package org.openide.awt; 20 21 import org.openide.util.Utilities; 22 23 import java.awt.*; 24 import java.awt.event.*; 25 26 import java.io.*; 27 28 import java.util.List ; 29 30 import javax.swing.*; 31 import javax.swing.event.*; 32 import org.openide.awt.DynamicMenuContent; 33 import org.openide.util.actions.Presenter; 34 35 36 46 public class JInlineMenu extends JMenuItem implements DynamicMenuContent { 47 48 static final long serialVersionUID = -2310488127953523571L; 49 private static final Icon BLANK_ICON = new ImageIcon( 50 Utilities.loadImage("org/openide/resources/actions/empty.gif") 51 ); 53 59 60 private JComponent[] items = new JComponent[0]; 61 62 63 boolean upToDate; 64 65 66 private List addedItems; 67 68 71 public JInlineMenu() { 72 setEnabled(false); 73 setVisible(false); 74 upToDate = true; 75 } 76 77 79 public Insets getInsets() { 80 return new Insets(0, 0, 0, 0); 81 } 82 83 89 public void setMenuItems(final JMenuItem[] newItems) { 90 JComponent[] local = new JComponent[newItems.length]; 96 97 for (int i = 0; i < newItems.length; i++) { 98 local[i] = (newItems[i] != null) ? (JComponent) newItems[i] : new JSeparator(); 99 } 100 101 items = local; 102 upToDate = false; 103 104 alignItems(); 105 106 } 107 108 109 112 private void alignItems() { 113 boolean shouldAlign = getIcon() != null; 116 117 if (!shouldAlign) { 118 for (int i = 0; i < items.length; i++) { 119 if (items[i] instanceof JMenuItem) { 120 if (((JMenuItem) items[i]).getIcon() != null) { 121 shouldAlign = true; 122 123 break; 124 } 125 } 126 } 127 } 128 129 if (!shouldAlign) { 130 return; 131 } 132 133 JMenuItem curItem = null; 135 136 for (int i = 0; i < items.length; i++) { 137 if (items[i] instanceof JMenuItem) { 138 curItem = (JMenuItem) items[i]; 139 140 if (curItem.getIcon() == null) { 141 curItem.setIcon(BLANK_ICON); 142 } 143 } 144 } 145 } 146 147 148 149 152 private static int findIndex(Object of, Object [] arr) { 153 int menuLength = arr.length; 154 155 for (int i = 0; i < menuLength; i++) { 156 if (of == arr[i]) { 157 return i; 158 } 159 } 160 161 return -1; 162 } 163 164 165 public JComponent[] synchMenuPresenters(JComponent[] items) { 166 return this.items; 167 } 168 169 public JComponent[] getMenuPresenters() { 170 return items; 171 } 172 173 } 174 | Popular Tags |