Changeset 1177 for trunk


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.

Location:
trunk/src/org/expeditee/items/widgets
Files:
4 edited

Legend:

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

    r1098 r1177  
    22
    33import org.expeditee.core.Image;
     4import org.expeditee.core.bounds.AxisAlignedBoxBounds;
    45import org.expeditee.gio.EcosystemManager;
    56import org.expeditee.gio.EcosystemManager.Ecosystem;
    67import org.expeditee.gio.javafx.JavaFXMiscManager;
     8import org.expeditee.items.ItemParentStateChangedEvent;
    79import org.expeditee.items.Text;
     10import org.expeditee.items.UserAppliedPermission;
    811
    912import javafx.scene.Group;
     
    1417
    1518public abstract class JavaFXWidget extends Widget {
    16        
     19
    1720        protected Scene _dummyScene;
    1821        protected Node _node;
    1922
    20         protected JavaFXWidget(Text source, Node jfxNode, int minWidth, int maxWidth, int minHeight, int maxHeight)
    21         {
     23        protected JavaFXWidget(Text source, Node jfxNode, int minWidth, int maxWidth, int minHeight, int maxHeight) {
    2224                super(source, minWidth, maxWidth, minHeight, maxHeight);
    2325                _node = jfxNode;
     
    2729       
    2830        @Override
    29         public boolean isSupportedOnEcosystem(Ecosystem type)
    30         {
     31        protected void addWidgetContent(final ItemParentStateChangedEvent e) {
     32                if ((e.getEventType() == ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY || e
     33                                .getEventType() == ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY)
     34                                && e.getOverlayLevel().equals(UserAppliedPermission.none)) {
     35                        return; // item belongs to a non-active overlay
     36                }
     37               
     38                //TODO: Add code here that attaches JavaFX content to the widget.  This was added by Bryce
     39                //as it was necessary for Swing widgets functionality.
     40        }
     41       
     42        @Override
     43        protected void addKeyListenerToWidget() {
     44                // TODO: Add code here that attaches JavaFX key listeners
     45               
     46        }
     47       
     48        @Override
     49        protected void addThisAsContainerListenerToContent() {
     50                // TODO: Add code here that attaches the equiv of swings Content Listener
     51               
     52        }
     53       
     54        @Override
     55        public AxisAlignedBoxBounds getContentBounds() {
     56                // TODO: Return the bounds of the content that is being drawn.  Potentially different from the bounds of the widget.
     57                return null;
     58        }
     59       
     60        @Override
     61        protected void layout() {
     62                // TODO: Respond to layout
     63        }
     64       
     65        @Override
     66        protected void onBoundsChanged() {
     67                // TODO: Respond to widget bounds changing.     
     68        }
     69
     70        @Override
     71        public boolean isSupportedOnEcosystem(Ecosystem type) {
    3172                return type == Ecosystem.JavaFX;
    3273        }
    3374
    3475        @Override
    35         public void paintWidget()
    36         {
     76        public void paintWidget() {
    3777                SnapshotParameters params = new SnapshotParameters();
    3878                WritableImage snapshot = _node.snapshot(params, null);
     
    4080                EcosystemManager.getGraphicsManager().drawImage(image, getPosition());
    4181        }
    42        
     82
    4383        @Override
    44         protected void onMoved()
    45         {
     84        protected void onMoved() {
    4685                // Do nothing
    4786        }
    48        
     87
    4988        @Override
    50         protected void onSizeChanged()
    51         {
     89        protected void onSizeChanged() {
    5290                _dummyScene.getRoot().resize(getWidth(), getHeight());
    5391                _node.resize(getWidth(), getHeight());
  • 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       
  • trunk/src/org/expeditee/items/widgets/Widget.java

    r1159 r1177  
    542542                }
    543543               
     544                //addThisAsContainerListenerToContent();
     545                //addKeyListenerToWidget();
     546               
    544547                _textRepresentation = source;
    545548
     
    11131116                        case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY:
    11141117                                EcosystemManager.addInteractiveWidget(this);
     1118                                addWidgetContent(e);
    11151119                                break;
    11161120                }
    11171121
    1118                 DisplayController.invalidateItem(_d1, getBounds());
     1122                DisplayController.invalidateItem(_d1, getContentBounds());
    11191123
    11201124                // Forward filtered event to upper classes...
    11211125                onParentStateChanged(e.getEventType());
    11221126        }
    1123 
     1127       
     1128        /**
     1129         * Subclassing Widgets overwrite to add their specific content to the Frame.
     1130         * For example, a widget being Java Swing must add their Swing component to Expeditee's content pane.
     1131         * @param e Can be used to identify if it is appropriate to draw the widget.
     1132         */
     1133        abstract protected void addWidgetContent(final ItemParentStateChangedEvent e);
     1134       
     1135        /**
     1136         * Subclassing Widgets overwrite to specify their own functionality as to how key events
     1137         * are to be forwarded to Expeditee.
     1138         */
     1139        abstract protected void addKeyListenerToWidget();
     1140       
     1141        /**
     1142         * Subclassing widgets overwrite to specify how content will respond to being added and removed.
     1143         */
     1144        abstract protected void addThisAsContainerListenerToContent();
     1145       
     1146        /**
     1147         * Subclassing widgets overwrite to provide the size and position of their content.
     1148         * For example, a widget using Java Swing might return _swingComponent.getBounds().
     1149         * @return The bounds of the content that is being drawn.
     1150         */
     1151        abstract public AxisAlignedBoxBounds getContentBounds();
     1152       
     1153        /**
     1154         * Due to absolute positioning...
     1155         * @param parent
     1156         */
     1157        abstract protected void layout();
     1158       
     1159        /**
     1160         * Subclassing widgets overwrite  to respond to changes in widget bounds.
     1161         */
     1162        abstract protected void onBoundsChanged();
     1163       
    11241164        /**
    11251165         * Override to make use of. Internally this is reported once by all corners,
     
    11541194        public AxisAlignedBoxBounds getBounds()
    11551195        {
    1156                 return new AxisAlignedBoxBounds(getX(), getY(), getWidth(), getHeight());
     1196                return getContentBounds();
     1197                //return new AxisAlignedBoxBounds(getX(), getY(), getWidth(), getHeight());
    11571198        }
    11581199
     
    18821923        public Clip getClip()
    18831924        {
    1884                 final Clip clip = new Clip(getBounds());
     1925                final Clip clip = new Clip(getContentBounds());
    18851926                return clip;
    18861927        }
     1928       
     1929        public void onResized() {
     1930                invalidateSelf();
     1931                onBoundsChanged();
     1932                layout();
     1933        }
    18871934}
  • trunk/src/org/expeditee/items/widgets/WidgetCorner.java

    r1102 r1177  
    2323import org.expeditee.core.Colour;
    2424import org.expeditee.core.Point;
     25import org.expeditee.core.bounds.Bounds;
    2526import org.expeditee.core.bounds.PolygonBounds;
    2627import org.expeditee.items.Dot;
     
    3536                super(x, y, id);
    3637
    37                 if (widgetSource == null)
     38                if (widgetSource == null) {
    3839                        throw new NullPointerException("widgetSource");
     40                }
    3941                _widgetSource = widgetSource;
     42        }
     43       
     44        @Override
     45        public Bounds updateBounds() {
     46                final Bounds ret = super.updateBounds();
     47                if (_widgetSource != null) {
     48                        _widgetSource.onBoundsChanged();
     49                }
     50                return ret;
    4051        }
    4152
     
    246257        public boolean contains(Point p)
    247258        {
    248                 return super.contains(p) && !_widgetSource.getBounds().contains(p);
     259                return super.contains(p) && !_widgetSource.getContentBounds().contains(p);
    249260        }
    250261
     
    253264                if (_widgetSource != null) {
    254265                        float minThickness = _widgetSource.getMinimumBorderThickness();
    255                         if (newThickness < minThickness)
     266                        if (newThickness < minThickness) {
    256267                                newThickness = minThickness;
     268                        }
    257269                        super.setThickness(newThickness, setConnected);
    258270                        _widgetSource.setSourceThickness(newThickness, false);
Note: See TracChangeset for help on using the changeset viewer.