1 19 package org.netbeans.modules.form; 20 21 import java.awt.*; 22 import java.beans.*; 23 import java.util.*; 24 import javax.swing.*; 25 import javax.swing.border.*; 26 27 import org.openide.nodes.*; 28 import org.openide.actions.*; 29 import org.openide.explorer.*; 30 import org.openide.explorer.view.*; 31 32 37 public class NonVisualTray extends JPanel implements ExplorerManager.Provider { 38 39 private FormModel formModel; 40 41 private NonVisualView listView; 42 43 private ExplorerManager manager; 44 45 50 public NonVisualTray(FormModel formModel) { 51 this.formModel = formModel; 52 manager = new ExplorerManager(); 53 FormRootNode rootNode = (FormRootNode)FormEditor.getFormEditor(formModel).getFormRootNode(); 54 Node othersNode = rootNode.getOthersNode(); 55 manager.setRootContext(new NonVisualNode(othersNode, new NonVisualChildren(othersNode))); 56 Listener listener = new Listener(); 57 manager.addPropertyChangeListener(listener); 58 ComponentInspector ci = ComponentInspector.getInstance(); 59 ci.getExplorerManager().addPropertyChangeListener(listener); 60 listView = new NonVisualView(); 61 setLayout(new BorderLayout()); 62 add(listView, BorderLayout.CENTER); 63 } 64 65 70 public ExplorerManager getExplorerManager() { 71 return manager; 72 } 73 74 void updateVisualSettings() { 75 listView.updateVisualSettings(); 76 } 77 78 81 private static class NonVisualView extends ListView { 82 83 86 public NonVisualView() { 87 list.setCellRenderer(new Renderer()); 88 list.setLayoutOrientation(JList.HORIZONTAL_WRAP); 89 list.setVisibleRowCount(-1); 90 list.setBorder(BorderFactory.createEmptyBorder(0,4,4,4)); 91 updateVisualSettings(); 92 setTraversalAllowed(false); 93 setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 94 setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 95 } 96 97 public Dimension getPreferredSize() { 98 Dimension dim = super.getPreferredSize(); 99 return new Dimension(1, (int)dim.getHeight()); 100 } 101 102 void updateVisualSettings() { 103 list.setBackground(FormLoaderSettings.getInstance().getFormDesignerBackgroundColor()); 104 } 105 106 } 107 108 111 private static class Renderer implements ListCellRenderer { 112 113 private JButton button; 114 115 private Border selectedBorder; 116 117 private Border unselectedBorder; 118 119 122 public Renderer() { 123 button = new JButton(); 124 button.setUI(new javax.swing.plaf.basic.BasicButtonUI ()); 125 unselectedBorder = BorderFactory.createEmptyBorder(4,4,4,4); 126 Color selectionColor = FormLoaderSettings.getInstance().getSelectionBorderColor(); 127 selectedBorder = BorderFactory.createCompoundBorder( 128 BorderFactory.createEmptyBorder(3,3,3,3), 129 BorderFactory.createLineBorder(selectionColor)); 130 button.setOpaque(false); 131 int fontSize = button.getFontMetrics(button.getFont()).getHeight(); 132 button.setPreferredSize(new Dimension(64+2*4, 50+fontSize)); 133 } 134 135 public Component getListCellRendererComponent(JList list, 136 Object value, int index, boolean isSelected, boolean cellHasFocus) { 137 Node node = Visualizer.findNode(value); 138 ImageIcon icon = new ImageIcon(node.getIcon(java.beans.BeanInfo.ICON_COLOR_32x32)); 139 button.setIcon(icon); 140 String text = node.getShortDescription(); 141 button.setText(text); 142 button.setHorizontalTextPosition(SwingConstants.CENTER); 143 button.setVerticalTextPosition(SwingConstants.BOTTOM); 144 button.setHorizontalAlignment(SwingConstants.CENTER); 145 button.setBorder(isSelected ? selectedBorder : unselectedBorder); 146 return button; 147 } 148 149 } 150 151 155 private class Listener implements PropertyChangeListener { 156 157 public void propertyChange(PropertyChangeEvent evt) { 158 if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 159 if (evt.getSource() == manager) { 160 Node[] newNodes = (Node[])evt.getNewValue(); 161 Node[] nodes = new Node[newNodes.length]; 162 for (int i=0; i<nodes.length; i++) { 163 nodes[i] = ((NonVisualNode)newNodes[i]).getOriginal(); 164 } 165 ComponentInspector ci = ComponentInspector.getInstance(); 166 Node[] ciNodes = ci.getSelectedNodes(); 167 if (!Arrays.asList(ciNodes).containsAll(Arrays.asList(nodes))) { 168 try { 169 ci.setSelectedNodes(nodes, FormEditor.getFormEditor(formModel)); 170 } catch (PropertyVetoException pvex) {} 171 } 172 } else { 173 Node[] nodes = (Node[])evt.getNewValue(); 174 ArrayList list = new ArrayList(); 175 Node node = ((NonVisualNode)manager.getRootContext()).getOriginal(); 176 for (int i=0; i<nodes.length; i++) { 177 if (node == nodes[i].getParentNode()) { 178 list.add(findFilterNode(nodes[i])); 179 } 180 } 181 try { 182 manager.setSelectedNodes((Node[])list.toArray(new Node[list.size()])); 183 } catch (PropertyVetoException pvex) {} 184 } 185 } 186 } 187 188 192 private Node findFilterNode(Node original) { 193 Node root = manager.getRootContext(); 194 Node[] nodes = root.getChildren().getNodes(false); 195 for (int i=0; i<nodes.length; i++) { 196 NonVisualNode node = (NonVisualNode)nodes[i]; 197 if (node.getOriginal() == original) { 198 return node; 199 } 200 } 201 return null; 202 } 203 204 } 205 206 209 private class NonVisualChildren extends FilterNode.Children { 210 211 216 public NonVisualChildren(Node original) { 217 super(original); 218 } 219 220 225 protected Node copyNode(Node node) { 226 return new NonVisualNode(node); 227 } 228 229 } 230 231 234 private static class NonVisualNode extends FilterNode { 235 236 241 public NonVisualNode(Node original) { 242 super(original); 243 disableDelegation(DELEGATE_GET_SHORT_DESCRIPTION 244 | DELEGATE_GET_ACTIONS); 245 } 246 247 253 public NonVisualNode(Node original, Children children) { 254 super(original, children); 255 } 256 257 262 public String getShortDescription() { 263 return getName(); 264 } 265 266 271 protected Node getOriginal() { 272 return super.getOriginal(); 273 } 274 275 281 public Action[] getActions(boolean context) { 282 java.util.List forbiddenActions = Arrays.asList(new Class [] { 283 MoveUpAction.class, 284 MoveDownAction.class 285 }); 286 Action[] actions = getOriginal().getActions(context); 287 ArrayList actionList = new ArrayList(Arrays.asList(actions)); 288 for (int i=0; i<actions.length; i++) { 289 Action action = actions[i]; 290 if ((action != null) && (forbiddenActions.contains(action.getClass()))) { 291 actionList.remove(action); 292 } 293 } 294 return (Action[])actionList.toArray(new Action[actionList.size()]); 295 } 296 297 } 298 299 } 300 | Popular Tags |