Ignore:
Timestamp:
09/28/18 10:33:02 (6 years ago)
Author:
bln4
Message:

The following commit comments all relate to work undertaken getting widgets working in the new refactored code.
org.expeditee.items.widgets.JavaFXWidget ->
org.expeditee.items.widgets.SwingWidget ->
org.expeditee.items.widgets.Widget ->

Some additional abstract piping. While the concrete implementation for this code has been added to SwingWidget's, it has not been properly done in JavaFXWidget


org.expeditee.items.widgets.WidgetCorner ->

WidgetCorner's updateBounds() reimplemented.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/widgets/SwingWidget.java

    r1152 r1177  
    55import java.awt.Graphics2D;
    66import java.awt.Point;
     7import java.awt.Rectangle;
     8import java.awt.event.ContainerEvent;
     9import java.awt.event.ContainerListener;
     10import java.awt.event.KeyEvent;
     11import java.awt.event.KeyListener;
     12import java.util.Arrays;
     13import java.util.LinkedList;
     14import java.util.List;
    715
    816import javax.swing.JComponent;
    9 
     17import javax.swing.JPopupMenu;
     18
     19import org.expeditee.core.bounds.AxisAlignedBoxBounds;
     20import org.expeditee.gio.EcosystemManager;
    1021import org.expeditee.gio.EcosystemManager.Ecosystem;
     22import org.expeditee.gio.swing.SwingGraphicsManager;
     23import org.expeditee.gio.swing.SwingInputManager;
    1124import org.expeditee.gio.swing.SwingMiscManager;
     25import org.expeditee.gui.DisplayController;
     26import org.expeditee.items.ItemParentStateChangedEvent;
    1227import org.expeditee.items.Text;
    13 
    14 public abstract class SwingWidget extends Widget
     28import org.expeditee.items.UserAppliedPermission;
     29
     30public abstract class SwingWidget extends Widget implements ContainerListener, KeyListener
    1531{
    1632        // A flag for signifying whether the swing components are ready to paint.
     
    2238
    2339        protected JComponent _swingComponent;
    24        
     40               
    2541        protected SwingWidget(Text source, JComponent component, int minWidth, int maxWidth, int minHeight, int maxHeight)
    2642        {
    2743                super(source, minWidth, maxWidth, minHeight, maxHeight);
    2844
    29                 if (component == null) throw new NullPointerException("component");
    30 
     45                if (component == null) {
     46                        throw new NullPointerException("component");
     47                }
     48                               
    3149                _swingComponent = component;
     50                _swingComponent.setFocusable(true);
     51                addKeyListenerToWidget();
     52                addThisAsContainerListenerToContent();
    3253                onBoundsChanged();
     54               
     55                JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    3356        }
    3457
     
    3659        {
    3760                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                }
    38174        }
    39175       
     
    51187        }
    52188
     189        @Override
    53190        public final void onBoundsChanged()
    54191        {
    55                 if (_swingComponent == null) return;
     192                if (_swingComponent == null) {
     193                        return;
     194                }
    56195               
    57196                if (isFixedSize()) {
     
    71210        private void ignoreAWTPainting(Component c)
    72211        {
    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                }
    76219                c.setIgnoreRepaint(true);
    77 
     220               
    78221                if (c instanceof Container) {
    79222                        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                                }
    81231                        }
    82232                }
     
    88238         * @param parent
    89239         */
    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                }
    93245               
    94246                parent.validate();
     
    98250                                layout(child);
    99251                        }
    100                 }
     252                } 
    101253        }
    102254
     
    116268        protected void paintSwingWidget(Graphics2D g)
    117269        {
     270//              final GraphicsManager gm = SwingGraphicsManager.getInstance();
     271//              final EnforcedClipKey clipKey = gm.pushClip(new Clip(getContentBounds()));
     272                               
    118273                final Point location = _swingComponent.getLocation();
    119274                int x = location.x;
     
    122277                _swingComponent.paint(g);
    123278                g.translate(-x, -y);
     279               
     280//              gm.popClip(clipKey);
    124281        }
    125282       
Note: See TracChangeset for help on using the changeset viewer.