1 7 8 package org.jdesktop.jdnc; 9 10 import org.jdesktop.swing.JXStatusBar; 11 import org.jdesktop.swing.MouseMessagingHandler; 12 13 import org.jdesktop.swing.event.MessageListener; 14 import org.jdesktop.swing.event.MessageSource; 15 import org.jdesktop.swing.event.MessageSourceSupport; 16 17 import java.awt.BorderLayout ; 18 import java.awt.Container ; 19 import java.awt.Component ; 20 import java.awt.Dimension ; 21 import java.awt.*; 22 import java.awt.Graphics ; 23 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.MouseEvent ; 26 import java.awt.event.MouseAdapter ; 27 28 import javax.swing.Action ; 29 import javax.swing.ActionMap ; 30 import javax.swing.Icon ; 31 import javax.swing.JButton ; 32 import javax.swing.JComponent ; 33 import javax.swing.JLabel ; 34 import javax.swing.JPanel ; 35 import javax.swing.JPopupMenu ; 36 import javax.swing.JRootPane ; 37 import javax.swing.JToolBar ; 38 39 import org.jdesktop.swing.actions.Targetable; 40 41 53 public abstract class JNComponent extends JPanel 54 implements Targetable, MessageSource { 55 56 private JComponent component; 57 private JPopupMenu popup; 58 59 private PopupHandler popupHandler; 60 private MouseMessagingHandler messageHandler; 61 62 66 protected MessageSourceSupport support; 67 68 72 protected JNComponent() { 73 super(new BorderLayout ()); 74 } 75 76 83 protected void setComponent(JComponent component) { 84 JComponent oldComponent = this.component; 85 this.component = component; 86 component.setFont(getFont()); 87 component.setForeground(getForeground()); 88 component.setBackground(getBackground()); 89 90 if (popupHandler != null) { 92 if (oldComponent != null) { 93 oldComponent.removeMouseListener(popupHandler); 94 } 95 if (component != null) { 96 component.addMouseListener(popupHandler); 97 } 98 } 99 firePropertyChange("component", oldComponent, component); 100 } 101 102 public JComponent getComponent() { 103 return component; 104 } 105 106 110 public boolean doCommand(Object command, Object value) { 111 ActionMap map = component.getActionMap(); 113 Action action = map.get(command); 114 115 if (action == null) { 116 map = getActionMap(); 118 action = map.get(command); 119 } 120 121 if (action != null) { 122 if (value instanceof ActionEvent ) { 123 action.actionPerformed( (ActionEvent ) value); 124 } 125 else { 126 action.actionPerformed(new ActionEvent (value, 0, 128 command.toString())); 129 } 130 return true; 131 } 132 return false; 133 } 134 135 public Object [] getCommands() { 136 ActionMap map = component.getActionMap(); 137 return map.keys(); 138 } 140 141 public boolean hasCommand(Object command) { 142 Object [] commands = getCommands(); 143 for (int i = 0; i < commands.length; i++) { 144 if (commands[i].equals(command)) { 145 return true; 146 } 147 } 148 return false; 149 } 150 151 155 public void addMessageListener(MessageListener l) { 156 if (support == null) { 157 support = new MessageSourceSupport(this); 158 } 159 if (messageHandler == null) { 160 messageHandler = new MouseMessagingHandler(this, support); 161 if (toolBar != null) { 162 messageHandler.registerListeners(toolBar.getComponents()); 165 } 166 if (popup != null) { 167 messageHandler.registerListeners(popup.getSubElements()); 168 } 169 } 170 support.addMessageListener(l); 171 } 172 173 public void removeMessageListener(MessageListener l) { 174 if (support == null) { 175 return; 176 } 177 support.removeMessageListener(l); 178 } 179 180 public MessageListener[] getMessageListeners() { 181 if (support == null) { 182 support = new MessageSourceSupport(this); 183 } 184 return support.getMessageListeners(); 185 } 186 187 192 protected void sendMessage(String message) { 193 if (support != null) { 194 support.fireMessage(message); 195 } 196 } 197 198 206 public JButton addAction(Action action) { 207 if (toolBar == null) { 208 addToolBar(); 209 } 210 211 Object delegate = action.getValue("delegate"); 212 if (delegate == null) { 213 action.putValue("delegate", this); 214 } 215 216 221 JButton button = toolBar.add(action); 224 button.setToolTipText( (String ) action.getValue(Action. 225 SHORT_DESCRIPTION)); 226 return button; 227 } 228 229 233 public void addSeparator() { 234 if (toolBar != null) { 235 toolBar.addSeparator(); 236 } 237 } 238 239 public void addToolBarComponent(JComponent component) { 240 if (toolBar == null) { 241 addToolBar(); 242 } 243 toolBar.add(component); 244 } 245 246 public void setFont(Font font) { 247 super.setFont(font); 248 if (component != null) { 249 component.setFont(font); 250 } 251 } 252 253 258 public void setPopupMenu(JPopupMenu popup) { 259 JPopupMenu oldPopup = this.popup; 260 this.popup = popup; 261 262 if (oldPopup != null) { 263 if (messageHandler != null) { 265 messageHandler.unregisterListeners(oldPopup.getSubElements()); 266 } 267 if (getComponent() != null) { 268 getComponent().removeMouseListener(popupHandler); 269 popupHandler = null; 270 } 271 } 272 273 if (popup != null) { 274 if (messageHandler != null) { 276 messageHandler.registerListeners(popup.getSubElements()); 277 } 278 if (getComponent() != null) { 279 popupHandler = new PopupHandler(popup); 280 getComponent().addMouseListener(popupHandler); 281 } 282 } 283 } 284 285 public JPopupMenu getPopupMenu() { 286 return popup; 287 } 288 289 292 private class PopupHandler extends MouseAdapter { 293 294 private JPopupMenu popup; 295 296 public PopupHandler(JPopupMenu popup) { 297 setPopup(popup); 298 } 299 300 public void setPopup(JPopupMenu popup) { 301 this.popup = popup; 302 } 303 304 public void mousePressed(MouseEvent e) { 305 showPopup(e); 306 } 307 308 public void mouseReleased(MouseEvent e) { 309 showPopup(e); 310 } 311 312 private void showPopup(MouseEvent e) { 313 if (e.isPopupTrigger() && popup != null) { 314 popup.show( (Component ) e.getSource(), e.getX(), e.getY()); 315 } 316 } 317 } 318 319 324 public Icon getBackgroundImage() { 325 return image; 326 } 327 328 337 public void setBackgroundImage(Icon image) { 338 if (this.image != image) { 339 this.image = image; 340 repaint(); 341 } 342 } 343 344 public void paint(Graphics g) { 345 super.paint(g); 346 if (image != null) { 347 Dimension size = this.getSize(); 348 int x, y; 349 x = (size.width - image.getIconWidth()) >> 1; 350 y = (size.height - image.getIconHeight()) >> 1; 351 image.paintIcon(this, g, x, y); 352 } 353 } 354 355 protected JToolBar addToolBar() { 356 toolBar = new JToolBar (); 357 toolBar.setFloatable(false); 358 add(toolBar, BorderLayout.NORTH); 359 return toolBar; 360 } 361 362 protected JToolBar toolBar = null; 363 protected Icon image = null; 364 } 365 | Popular Tags |