Changeset 1177 for trunk/src/org/expeditee/items/widgets/SwingWidget.java
- Timestamp:
- 09/28/18 10:33:02 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/widgets/SwingWidget.java
r1152 r1177 5 5 import java.awt.Graphics2D; 6 6 import java.awt.Point; 7 import java.awt.Rectangle; 8 import java.awt.event.ContainerEvent; 9 import java.awt.event.ContainerListener; 10 import java.awt.event.KeyEvent; 11 import java.awt.event.KeyListener; 12 import java.util.Arrays; 13 import java.util.LinkedList; 14 import java.util.List; 7 15 8 16 import javax.swing.JComponent; 9 17 import javax.swing.JPopupMenu; 18 19 import org.expeditee.core.bounds.AxisAlignedBoxBounds; 20 import org.expeditee.gio.EcosystemManager; 10 21 import org.expeditee.gio.EcosystemManager.Ecosystem; 22 import org.expeditee.gio.swing.SwingGraphicsManager; 23 import org.expeditee.gio.swing.SwingInputManager; 11 24 import org.expeditee.gio.swing.SwingMiscManager; 25 import org.expeditee.gui.DisplayController; 26 import org.expeditee.items.ItemParentStateChangedEvent; 12 27 import org.expeditee.items.Text; 13 14 public abstract class SwingWidget extends Widget 28 import org.expeditee.items.UserAppliedPermission; 29 30 public abstract class SwingWidget extends Widget implements ContainerListener, KeyListener 15 31 { 16 32 // A flag for signifying whether the swing components are ready to paint. … … 22 38 23 39 protected JComponent _swingComponent; 24 40 25 41 protected SwingWidget(Text source, JComponent component, int minWidth, int maxWidth, int minHeight, int maxHeight) 26 42 { 27 43 super(source, minWidth, maxWidth, minHeight, maxHeight); 28 44 29 if (component == null) throw new NullPointerException("component"); 30 45 if (component == null) { 46 throw new NullPointerException("component"); 47 } 48 31 49 _swingComponent = component; 50 _swingComponent.setFocusable(true); 51 addKeyListenerToWidget(); 52 addThisAsContainerListenerToContent(); 32 53 onBoundsChanged(); 54 55 JPopupMenu.setDefaultLightWeightPopupEnabled(false); 33 56 } 34 57 … … 36 59 { 37 60 return _swingComponent; 61 } 62 63 @Override 64 public void keyTyped(KeyEvent e) { 65 66 int keyCode = e.getKeyCode(); 67 68 if (keyCode >= KeyEvent.VK_F1 && keyCode <= KeyEvent.VK_F12) { 69 ((SwingInputManager) EcosystemManager.getInputManager()).keyTyped(e); 70 } 71 } 72 73 @Override 74 public void keyPressed(KeyEvent e) { 75 int keyCode = e.getKeyCode(); 76 77 if (keyCode >= KeyEvent.VK_F1 && keyCode <= KeyEvent.VK_F12) { 78 ((SwingInputManager) EcosystemManager.getInputManager()).keyTyped(e); 79 } 80 } 81 82 @Override 83 public void keyReleased(KeyEvent e) { 84 int keyCode = e.getKeyCode(); 85 86 if (keyCode >= KeyEvent.VK_F1 && keyCode <= KeyEvent.VK_F12) { 87 ((SwingInputManager) EcosystemManager.getInputManager()).keyTyped(e); 88 } 89 } 90 91 /** 92 * Makes sure we add our KeyListener to every child component, 93 * since it seems that's the only way to make sure we capture key events in Java 94 * (without using KeyBindings which seem to only support the keyTyped event) 95 */ 96 @Override 97 public void componentAdded(ContainerEvent e) { 98 if(e == null || e.getChild() == null) { 99 return; 100 } 101 keyListenerToChildren(e.getChild(), true); 102 } 103 104 @Override 105 public void componentRemoved(ContainerEvent e) { 106 if(e == null || e.getChild() == null) { 107 return; 108 } 109 keyListenerToChildren(e.getChild(), false); 110 } 111 112 @Override 113 protected void addWidgetContent(final ItemParentStateChangedEvent e) { 114 if ((e.getEventType() == ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY || e 115 .getEventType() == ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY) 116 && e.getOverlayLevel().equals(UserAppliedPermission.none)) { 117 return; // item belongs to a non-active overlay 118 } 119 120 if(_swingComponent.getParent() == null) { 121 if (e.getEventType() == ItemParentStateChangedEvent.EVENT_TYPE_SHOWN 122 || e.getEventType() == ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY 123 || e.getSource() == DisplayController.getCurrentFrame()) { 124 onBoundsChanged(); 125 ((SwingGraphicsManager) EcosystemManager.getGraphicsManager()).getContentPane().add(_swingComponent); 126 layout(_swingComponent); 127 } 128 } 129 } 130 131 @Override 132 protected void addKeyListenerToWidget() { 133 keyListenerToChildren(_swingComponent, true); 134 } 135 136 @Override 137 protected void addThisAsContainerListenerToContent() { 138 _swingComponent.addContainerListener(this); 139 } 140 141 @Override 142 public AxisAlignedBoxBounds getContentBounds() { 143 //final Rectangle bounds = _swingComponent.getBounds(); 144 //final AccessibleContext accessibleContext = _swingComponent.getAccessibleContext(); 145 //if (accessibleContext != null) { 146 // final Rectangle accessibleBounds = _swingComponent.getAccessibleContext().getAccessibleComponent().getBounds(); 147 // return new AxisAlignedBoxBounds(bounds.x, bounds.y, bounds.width, bounds.height + accessibleBounds.height); 148 //} else { 149 // return new AxisAlignedBoxBounds(bounds.x, bounds.y, bounds.width, bounds.height); 150 //} 151 final Rectangle bounds = _swingComponent.getBounds(); 152 return new AxisAlignedBoxBounds(bounds.x, bounds.y, bounds.width, bounds.height); 153 } 154 155 @Override 156 protected void layout() { 157 layout(_swingComponent); 158 } 159 160 private void keyListenerToChildren(Component parent, boolean add) { 161 List<Component> components = new LinkedList<Component>(); 162 components.add(parent); 163 while(!components.isEmpty()) { 164 Component c = components.remove(0); 165 if(c instanceof Container) { 166 components.addAll(Arrays.asList(((Container)c).getComponents())); 167 } 168 if(add && !Arrays.asList(c.getKeyListeners()).contains(this)) { 169 c.addKeyListener(this); 170 } else if (!add && Arrays.asList(c.getKeyListeners()).contains(this)) { 171 c.removeKeyListener(this); 172 } 173 } 38 174 } 39 175 … … 51 187 } 52 188 189 @Override 53 190 public final void onBoundsChanged() 54 191 { 55 if (_swingComponent == null) return; 192 if (_swingComponent == null) { 193 return; 194 } 56 195 57 196 if (isFixedSize()) { … … 71 210 private void ignoreAWTPainting(Component c) 72 211 { 73 if (c == null) return; 74 75 if (c instanceof JComponent) ((JComponent) c).setDoubleBuffered(false); 212 if (c == null) { 213 return; 214 } 215 216 if (c instanceof JComponent) { 217 ((JComponent) c).setDoubleBuffered(false); 218 } 76 219 c.setIgnoreRepaint(true); 77 220 78 221 if (c instanceof Container) { 79 222 for (Component child : ((Container) c).getComponents()) { 80 ignoreAWTPainting(child); 223 if(child instanceof Container) { 224 ignoreAWTPainting(child); 225 } else { 226 if(child instanceof JComponent) { 227 ((JComponent) child).setDoubleBuffered(false); 228 } 229 child.setIgnoreRepaint(true); 230 } 81 231 } 82 232 } … … 88 238 * @param parent 89 239 */ 90 protected void layout(Component parent) 91 { 92 if (parent == null) return; 240 private void layout(Component parent) 241 { 242 if (parent == null) { 243 return; 244 } 93 245 94 246 parent.validate(); … … 98 250 layout(child); 99 251 } 100 } 252 } 101 253 } 102 254 … … 116 268 protected void paintSwingWidget(Graphics2D g) 117 269 { 270 // final GraphicsManager gm = SwingGraphicsManager.getInstance(); 271 // final EnforcedClipKey clipKey = gm.pushClip(new Clip(getContentBounds())); 272 118 273 final Point location = _swingComponent.getLocation(); 119 274 int x = location.x; … … 122 277 _swingComponent.paint(g); 123 278 g.translate(-x, -y); 279 280 // gm.popClip(clipKey); 124 281 } 125 282
Note:
See TracChangeset
for help on using the changeset viewer.