Changeset 1183 for trunk


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

org.expeditee.gui.MessageBay ->

Added a synchronized keyword to the function updateBuffer(Colour, Clip, Dimension). This should hopefully fix the ConcurrentModificationException that I have been unable to track down the cause of.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/gui/MessageBay.java

    r1125 r1183  
    3535
    3636/**
    37  * The bay at the bottom of the expeditee browser which displays messages.
    38  * TODO: Make it thread safe!
     37 * The bay at the bottom of the expeditee browser which displays messages. TODO:
     38 * Make it thread safe!
    3939 */
    4040public final class MessageBay {
     
    5454        /** The list of messages shown in the message bay. */
    5555        private static List<Item> _messages = new LinkedList<Item>();
    56        
    57         /** Messages which were delayed because they couldn't be shown at time of creation. */
     56
     57        /**
     58         * Messages which were delayed because they couldn't be shown at time of
     59         * creation.
     60         */
    5861        private static List<DelayedMessage> _delayedMessages = new LinkedList<DelayedMessage>();
    5962
     
    8689
    8790        /** Static-only class. */
    88         private MessageBay()
    89         {
    90         }
    91        
     91        private MessageBay() {
     92        }
     93
    9294        /** Whether the message bay is ready to display messages. */
    93         public static boolean isReady()
    94         {
     95        public static boolean isReady() {
    9596                return Browser.isInitComplete();
    9697        }
    9798
    9899        /** Syncs message bay size according to FrameGraphics max size. */
    99         static void updateSize()
    100         {
    101                 for(Item i : _messages) {
    102                         if(i != null) {
     100        private static void updateSize() {
     101                for (Item i : _messages) {
     102                        if (i != null) {
    103103                                i.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
    104104                        }
     
    111111
    112112        /** Whether the given item is an item in the message bay. */
    113         public static boolean isMessageItem(Item i)
    114         {
     113        public static boolean isMessageItem(Item i) {
    115114                return _messages.contains(i) || i == _messageLink || i == _status;
    116115        }
    117116
    118117        /** TODO: Comment. cts16 */
    119         public synchronized static Item getMessageLink()
    120         {
     118        public synchronized static Item getMessageLink() {
    121119                return _messageLink;
    122120        }
    123121
    124122        /** TODO: Comment. cts16 */
    125         public synchronized static List<Item> getMessages()
    126         {
     123        public synchronized static List<Item> getMessages() {
    127124                return _messages;
    128125        }
    129126
    130127        /** Causes the entire message bay area to be invalidated. */
    131         public synchronized static void invalidateFullBay()
    132         {
     128        public synchronized static void invalidateFullBay() {
    133129                DisplayController.invalidateArea(DisplayController.getMessageBayPaintArea());
    134130        }
    135131
    136132        /** TODO: Comment. cts16 */
    137         private static void updateLink()
    138         {
    139                 if (!isLinkInitialized && DisplayController.getFramePaintArea() != null && DisplayController.getFramePaintArea().getWidth() > 0) {
     133        private static void updateLink() {
     134                if (!isLinkInitialized && DisplayController.getFramePaintArea() != null
     135                                && DisplayController.getFramePaintArea().getWidth() > 0) {
    140136                        // set up 'Messages' link on the right hand side
    141                         _messageLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MESSAGE_LINK_Y_OFFSET, MESSAGE_LINK_X);
     137                        _messageLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MESSAGE_LINK_Y_OFFSET,
     138                                        MESSAGE_LINK_X);
    142139                        _messageLink.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
    143140                        isLinkInitialized = true;
    144141                } else {
    145                         _messageLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MESSAGE_LINK_Y_OFFSET, MESSAGE_LINK_X);
    146                 }
    147         }
    148 
    149         /** TODO: Comment. cts16 */
    150         public static Image getImage(Clip clip, Dimension size)
    151         {
     142                        _messageLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MESSAGE_LINK_Y_OFFSET,
     143                                        MESSAGE_LINK_X);
     144                }
     145        }
     146
     147        /** TODO: Comment. cts16 */
     148        public static Image getImage(Clip clip, Dimension size) {
    152149                // Can't get an image with an invalid size
    153                 if (size == null || size.width <= 0 || size.height <= 0) return null;
     150                if (size == null || size.width <= 0 || size.height <= 0) {
     151                        return null;
     152                }
    154153
    155154                // Update the buffer
    156155                updateBuffer(Item.DEFAULT_BACKGROUND, clip, size);
    157                
     156
    158157                // Return the image buffer
    159158                return _messageBuffer;
     
    161160
    162161        /** Updates the image buffer to reflect the current state of the message bay. */
    163         private static void updateBuffer(Colour background, Clip clip, Dimension size)
    164         {
     162        private synchronized static void updateBuffer(Colour background, Clip clip, Dimension size) {
    165163                // If the buffer doesn't exist or is the wrong size, recreate it
    166164                if (_messageBuffer == null || !_messageBuffer.getSize().equals(size)) {
     
    173171                g.pushDrawingSurface(_messageBuffer);
    174172
    175                 if (clip != null) g.pushClip(clip);
     173                if (clip != null) {
     174                        g.pushClip(clip);
     175                }
    176176                g.setAntialiasing(true);
    177                
     177
    178178                g.clear(background);
    179                
     179
    180180                g.setFont(_messageFont);
    181                
    182                 for (Item message : _messages) {  //TODO: Solve occasional ConcurrentModificationException, possible culprit in overwriteMessage
     181
     182                for (Item message : _messages) {
    183183                        if (message != null) {
    184184                                if (clip == null || clip.isNotClipped() || message.isInDrawingArea(clip.getBounds())) {
     
    187187                        }
    188188                }
    189                
    190                 if (_status != null) FrameGraphics.PaintItem(_status);
     189
     190                if (_status != null) {
     191                        FrameGraphics.PaintItem(_status);
     192                }
    191193
    192194                if (clip == null || clip.isNotClipped() || _messageLink.isInDrawingArea(clip.getBounds())) {
    193195                        FrameGraphics.PaintItem(_messageLink);
    194196                }
    195                
     197
    196198                g.popDrawingSurface();
    197199        }
    198200
    199201        /** TODO: Comment. cts16 */
    200         private static Text displayMessage(String message, String link, List<String> actions, Colour color)
    201         {
     202        private static Text displayMessage(String message, String link, List<String> actions, Colour color) {
    202203                return displayMessage(message, link, actions, color, true);
    203204        }
    204205
    205206        /** TODO: Comment. cts16 */
    206         public synchronized static Text displayMessage(String message, String link, Colour color, boolean displayAlways, String action)
    207         {
     207        public synchronized static Text displayMessage(String message, String link, Colour color, boolean displayAlways,
     208                        String action) {
    208209                List<String> actions = new LinkedList<String>();
    209                 if (action != null) actions.add(action);
     210                if (action != null) {
     211                        actions.add(action);
     212                }
    210213                return displayMessage(message, link, actions, color, displayAlways);
    211214        }
    212215
    213216        /** TODO: Comment. cts16 */
    214         private static Text newMessage(String message, String link, List<String> actions, Colour color)
    215         {
     217        private static Text newMessage(String message, String link, List<String> actions, Colour color) {
    216218                Text t = new Text(getMessagePrefix(true) + message);
    217219                t.setPosition(20, 15 + _messages.size() * 25);
     
    222224                t.setFont(_messageFont.clone());
    223225                _creator.addItem(t.copy(), true);
    224                 if(link == null) t.setLink(_creator.getCurrent());
     226                if (link == null) {
     227                        t.setLink(_creator.getCurrent());
     228                }
    225229                return t;
    226230        }
    227231
    228232        /** TODO: Comment. cts16 */
    229         private synchronized static Text displayMessage(String message, String link, List<String> actions, Colour color, boolean displayAlways, boolean redraw)
    230         {
     233        private synchronized static Text displayMessage(String message, String link, List<String> actions, Colour color,
     234                        boolean displayAlways, boolean redraw) {
    231235                assert (message != null);
    232                
     236
    233237                if (!isReady()) {
    234238                        delayMessage(message, link, actions, color, displayAlways, redraw);
    235239                        return null;
    236240                }
    237                
     241
    238242                System.out.println(message);
    239243
     
    241245                invalidateFullBay();
    242246
    243                 if (_suppressMessages) return null;
     247                if (_suppressMessages) {
     248                        return null;
     249                }
    244250
    245251                if (!displayAlways && message.equals(_lastMessage)) {
     
    247253                        return null;
    248254                }
    249                
     255
    250256                _lastMessage = message;
    251257
    252258                if (_creator == null) {
    253                         _creator = new FrameCreator(MESSAGES_FRAMESET_NAME, FrameIO.MESSAGES_PATH, MESSAGES_FRAMESET_NAME, true, false);
     259                        _creator = new FrameCreator(MESSAGES_FRAMESET_NAME, FrameIO.MESSAGES_PATH, MESSAGES_FRAMESET_NAME, true,
     260                                        false);
    254261                }
    255262
     
    257264                updateLink();
    258265
    259                 if(_messages.size() >= 3) {
     266                if (_messages.size() >= 3) {
    260267                        _messages.remove(0);
    261                         for(Item i : _messages) {
     268                        for (Item i : _messages) {
    262269                                i.setY(i.getY() - 25);
    263270                        }
    264271                }
    265                
     272
    266273                Text t = newMessage(message, link, actions, color);
    267                
     274
    268275                _messages.add(t);
    269                
     276
    270277                // update the link to the latest message frame
    271278                _messageLink.setLink(_creator.getCurrent());
    272279
    273280                // TODO: Can we just make this DisplayController.requestRefresh()? cts16
    274                 if(redraw) {
     281                if (redraw) {
    275282                        DisplayController.requestRefresh(true);
    276283                }
     
    280287
    281288        /** TODO: Comment. cts16 */
    282         public synchronized static Text displayMessage(String message, String link, List<String> actions, Colour color, boolean displayAlways)
    283         {
     289        public synchronized static Text displayMessage(String message, String link, List<String> actions, Colour color,
     290                        boolean displayAlways) {
    284291                return displayMessage(message, link, actions, color, displayAlways, true);
    285292        }
    286293
    287294        /** TODO: Comment. cts16 */
    288         public synchronized static void overwriteMessage(String message)
    289         {
     295        public synchronized static void overwriteMessage(String message) {
    290296                overwriteMessage(message, null);
    291297        }
    292298
    293299        /** TODO: Comment. cts16 */
    294         public synchronized static void overwriteMessage(String message, Colour color)
    295         {
     300        public synchronized static void overwriteMessage(String message, Colour color) {
    296301                _messages.remove(_messages.size() - 1);
    297302                Text t = newMessage(message, null, null, color);
     
    301306
    302307        /** TODO: Comment. cts16 */
    303         private static String getMessagePrefix(int counter)
    304         {
     308        private static String getMessagePrefix(int counter) {
    305309                return "@" + counter + ": ";
    306310        }
    307311
    308312        /** TODO: Comment. cts16 */
    309         private static String getMessagePrefix(boolean incrementCounter)
    310         {
    311                 if (incrementCounter) _messageCount++;
    312                
     313        private static String getMessagePrefix(boolean incrementCounter) {
     314                if (incrementCounter) {
     315                        _messageCount++;
     316                }
     317
    313318                return getMessagePrefix(_messageCount);
    314319        }
     
    321326         *            the message to be displayed
    322327         */
    323         public synchronized static Text linkedErrorMessage(String message)
    324         {
    325                 if (_suppressMessages) return null;
     328        public synchronized static Text linkedErrorMessage(String message) {
     329                if (_suppressMessages) {
     330                        return null;
     331                }
    326332                Misc.beep();
    327333                String[] tokens = message.split(Text.FRAME_NAME_SEPARATOR);
    328334                String link = null;
    329                 if (tokens.length > 1) link = tokens[tokens.length - 1];
     335                if (tokens.length > 1) {
     336                        link = tokens[tokens.length - 1];
     337                }
    330338                return displayMessage(message, link, null, ERROR_COLOR);
    331339        }
    332340
    333341        /** TODO: Comment. cts16 */
    334         public synchronized static Text errorMessage(String message)
    335         {
    336                 if (_suppressMessages) return null;
     342        public synchronized static Text errorMessage(String message) {
     343                if (_suppressMessages) {
     344                        return null;
     345                }
    337346                Misc.beep();
    338347                return displayMessage(message, null, null, ERROR_COLOR, false);
     
    346355         *            The message to display to the user in the message area
    347356         */
    348         public synchronized static Text displayMessage(String message)
    349         {
     357        public synchronized static Text displayMessage(String message) {
    350358                return displayMessageAlways(message);
    351359        }
    352360
    353361        /** TODO: Comment. cts16 */
    354         public synchronized static Text displayMessageOnce(String message)
    355         {
     362        public synchronized static Text displayMessageOnce(String message) {
    356363                return displayMessage(message, null, null, Colour.BLACK, false);
    357364        }
    358365
    359366        /** TODO: Comment. cts16 */
    360         public synchronized static Text displayMessage(String message, Colour textColor)
    361         {
     367        public synchronized static Text displayMessage(String message, Colour textColor) {
    362368                return displayMessage(message, null, null, textColor);
    363369        }
    364370
    365371        /** TODO: Comment. cts16 */
    366         public synchronized static Text displayMessage(Text message)
    367         {
     372        public synchronized static Text displayMessage(Text message) {
    368373                Text t = null;
    369374                String link = message.getLink();
     
    377382
    378383        /** TODO: Comment. cts16 */
    379         public synchronized static Text displayMessageAlways(String message)
    380         {
     384        public synchronized static Text displayMessageAlways(String message) {
    381385                return displayMessage(message, null, null, Colour.BLACK);
    382386                // Misc.Beep();
     
    384388
    385389        /** TODO: Comment. cts16 */
    386         public synchronized static Text warningMessage(String message)
    387         {
     390        public synchronized static Text warningMessage(String message) {
    388391                return displayMessage(message, null, null, Colour.MAGENTA);
    389392        }
    390393
    391394        /** TODO: Comment. cts16 */
    392         public synchronized static List<Text> warningMessages(List<String> messages)
    393         {
    394                 if (messages == null) return null;
     395        public synchronized static List<Text> warningMessages(List<String> messages) {
     396                if (messages == null) {
     397                        return null;
     398                }
    395399                List<Text> ret = new LinkedList<Text>();
    396                 for (String message : messages) ret.add(warningMessage(message));
     400                for (String message : messages) {
     401                        ret.add(warningMessage(message));
     402                }
    397403                return ret;
    398404        }
    399405
    400406        /** TODO: Comment. cts16 */
    401         public synchronized static void suppressMessages(boolean val)
    402         {
     407        public synchronized static void suppressMessages(boolean val) {
    403408                _suppressMessages = val;
    404409        }
    405410
    406411        /** TODO: Comment. cts16 */
    407         public synchronized static void setStatus(String status)
    408         {
     412        public synchronized static void setStatus(String status) {
    409413                if (_status == null) {
    410414                        _status = new Text(status);
     
    417421                }
    418422
    419                 //invalidateFullBay();
     423                // invalidateFullBay();
    420424                DisplayController.requestRefresh(true);
    421425        }
    422426
    423427        /** TODO: Comment. cts16 */
    424         public static final class Progress
    425         {
     428        public static final class Progress {
    426429                /** The colour progress bars should be displayed in. */
    427430                private static final Colour BAR_COLOUR = Colour.GREEN.darker();
    428                
    429                 /** The character used to assemble the uncompleted portions of the progress bar. */
     431
     432                /**
     433                 * The character used to assemble the uncompleted portions of the progress bar.
     434                 */
    430435                private static final char UNCOMPLETED_CHARACTER = '\u2591'; // ░
    431                 /** The character used to assemble the completed portions of the progress bar. */
     436                /**
     437                 * The character used to assemble the completed portions of the progress bar.
     438                 */
    432439                private static final char COMPLETED_CHARACTER = '\u2592'; // ▒
    433                
     440
    434441                /** What the progress bar should look like when at 100% completion. */
    435442                private static final String COMPLETED_BAR = Util.nCopiesOf(20, COMPLETED_CHARACTER);
    436443                /** What the progress bar should look like when at 0% completion. */
    437444                private static final String UNCOMPLETED_BAR = Util.nCopiesOf(20, UNCOMPLETED_CHARACTER);
    438                
     445
    439446                private String _message;
    440447                private Text _text;
    441                
    442                 protected Progress(String text)
    443                 {
     448
     449                protected Progress(String text) {
    444450                        this._text = displayMessage(text, null, null, BAR_COLOUR, true, false);
    445451                        this._message = this._text.getText();
     
    447453                        DisplayController.requestRefresh(true);
    448454                }
    449                
    450                 public void UpdateMessage(final String text, final int newProgress) throws Exception
    451                 {
     455
     456                public void UpdateMessage(final String text, final int newProgress) throws Exception {
    452457                        this._message = text;
    453458                        set(newProgress);
    454459                }
    455                
    456                 public String GetMessage()
    457                 {
     460
     461                public String GetMessage() {
    458462                        return _message;
    459463                }
    460                
     464
    461465                /**
    462466                 *
    463                  * @param progress progress value from 0 to 100
    464                  * @return true if the progress was updated, false if the progress was off the screen
    465                  * @throws Exception if progress out of bounds
     467                 * @param progress
     468                 *            progress value from 0 to 100
     469                 * @return true if the progress was updated, false if the progress was off the
     470                 *         screen
     471                 * @throws Exception
     472                 *             if progress out of bounds
    466473                 */
    467                 public boolean set(int progress) throws Exception
    468                 {
    469                         if(progress < 0 || progress > 100) throw new Exception("Progress value out of bounds");
     474                public boolean set(int progress) throws Exception {
     475                        if (progress < 0 || progress > 100) {
     476                                throw new Exception("Progress value out of bounds");
     477                        }
    470478                        int p = progress / 5;
    471                         if(isMessageItem(this._text)) {
    472                                 this._text.setText(this._message + " [" + COMPLETED_BAR.substring(0, p) + UNCOMPLETED_BAR.substring(p) + "] " + progress + "%");
     479                        if (isMessageItem(this._text)) {
     480                                this._text.setText(this._message + " [" + COMPLETED_BAR.substring(0, p) + UNCOMPLETED_BAR.substring(p)
     481                                                + "] " + progress + "%");
    473482                                DisplayController.requestRefresh(true);
    474483                                return true;
    475484                        }
    476                 return false;
    477         }
    478         }
    479 
    480         /** TODO: Comment. cts16 */
    481         public synchronized static Progress displayProgress(String message)
    482         {
     485                        return false;
     486                }
     487        }
     488
     489        /** TODO: Comment. cts16 */
     490        public synchronized static Progress displayProgress(String message) {
    483491                return new Progress(message);
    484492        }
    485        
    486         /** Remembers the arguments to a displayMessage call for later use. */ 
     493
     494        /** Remembers the arguments to a displayMessage call for later use. */
    487495        private static class DelayedMessage {
    488                
     496
    489497                private String _message;
    490498                private String _link;
     
    493501                private boolean _displayAlways;
    494502                private boolean _redraw;
    495                
    496                 public DelayedMessage(String message, String link, List<String> actions, Colour color, boolean displayAlways, boolean redraw)
    497                 {
     503
     504                public DelayedMessage(String message, String link, List<String> actions, Colour color, boolean displayAlways,
     505                                boolean redraw) {
    498506                        _message = message;
    499507                        _link = link;
     
    501509                                _actions = null;
    502510                        } else {
    503                                 _actions =  new LinkedList<String>();
     511                                _actions = new LinkedList<String>();
    504512                                _actions.addAll(actions);
    505513                        }
     
    508516                        _redraw = redraw;
    509517                }
    510                
    511                 public void display()
    512                 {
     518
     519                public void display() {
    513520                        displayMessage(_message, _link, _actions, _colour, _displayAlways, _redraw);
    514521                }
    515                
    516         }
    517        
    518         private static void delayMessage(String message, String link, List<String> actions, Colour color, boolean displayAlways, boolean redraw)
    519         {
     522
     523        }
     524
     525        private static void delayMessage(String message, String link, List<String> actions, Colour color,
     526                        boolean displayAlways, boolean redraw) {
    520527                _delayedMessages.add(new DelayedMessage(message, link, actions, color, displayAlways, redraw));
    521528        }
    522        
    523         public static void showDelayedMessages()
    524         {
     529
     530        public static void showDelayedMessages(final boolean requestRefresh) {
    525531                if (isReady()) {
    526                         for (DelayedMessage message : _delayedMessages) message.display();
     532                        for (DelayedMessage message : _delayedMessages) {
     533                                message.display();
     534                        }
    527535                        _delayedMessages.clear();
    528536                        invalidateFullBay();
    529                         DisplayController.requestRefresh(true);
     537                        if(requestRefresh) {
     538                                DisplayController.requestRefresh(true);
     539                        }
    530540                }
    531541        }
Note: See TracChangeset for help on using the changeset viewer.