1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.oracle.javafx.scenebuilder.kit.editor.messagelog; 33 34 import com.oracle.javafx.scenebuilder.kit.editor.i18n.I18N; 35 import java.text.MessageFormat; 36 import java.text.SimpleDateFormat; 37 import java.util.ArrayList; 38 import java.util.Collections; 39 import java.util.Date; 40 import java.util.List; 41 import java.util.ResourceBundle; 42 import javafx.beans.property.IntegerProperty; 43 import javafx.beans.property.SimpleIntegerProperty; 44 45 /** 46 * 47 */ 48 public class MessageLog { 49 50 private final List<MessageLogEntry> entries = new ArrayList<>(); 51 private final SimpleIntegerProperty revision = new SimpleIntegerProperty(); 52 private final SimpleIntegerProperty numOfWarningMessages = new SimpleIntegerProperty(); 53 private final static String TIMESTAMP_PATTERN = "h:mm a EEEEEEEEE d MMM. yyyy"; //NOI18N 54 private static SimpleDateFormat TIMESTAMP_DATE_FORMAT; 55 56 57 /* 58 * Public 59 */ 60 61 public void logInfoMessage(String infoKey, ResourceBundle bundle, Object... arguments) { 62 logMessage(MessageLogEntry.Type.INFO, bundle, infoKey, arguments); 63 } 64 65 public void logWarningMessage(String warningKey, ResourceBundle bundle, Object... arguments) { 66 logMessage(MessageLogEntry.Type.WARNING, bundle, warningKey, arguments); 67 } 68 69 public void logInfoMessage(String infoKey, Object... arguments) { 70 logInfoMessage(infoKey, I18N.getBundle(), arguments); 71 } 72 73 public void logWarningMessage(String warningKey, Object... arguments) { 74 logWarningMessage(warningKey, I18N.getBundle(), arguments); 75 } 76 77 public IntegerProperty revisionProperty() { 78 return revision; 79 } 80 81 public IntegerProperty numOfWarningMessagesProperty() { 82 return numOfWarningMessages; 83 } 84 85 public List<MessageLogEntry> getEntries() { 86 return Collections.unmodifiableList(entries); 87 } 88 89 public MessageLogEntry getYoungestEntry() { 90 return entries.isEmpty() ? null : entries.get(0); 91 } 92 93 public int getEntryCount() { 94 return entries.size(); 95 } 96 97 public int getWarningEntryCount() { 98 int count = 0; 99 for (MessageLogEntry entry : entries) { 100 if (entry.getType() == MessageLogEntry.Type.WARNING) { 101 count++; 102 } 103 } 104 return count; 105 } 106 107 public void clear() { 108 if (entries.isEmpty() == false) { 109 entries.clear(); 110 incrementRevision(); 111 resetNumOfWarningMessages(); 112 } 113 } 114 115 public void clearEntry(MessageLogEntry entry) { 116 assert entry != null; 117 assert entries.contains(entry); 118 entries.remove(entry); 119 incrementRevision(); 120 121 if (entry.getType().equals(MessageLogEntry.Type.WARNING)) { 122 decrementNumOfWarningMessages(); 123 } 124 } 125 126 127 /* 128 * Private 129 */ 130 131 private synchronized String getTimeStamp() { 132 // We create TIMESTAMP_DATE_FORMAT lazily because it seems to be slow 133 if (TIMESTAMP_DATE_FORMAT == null) { 134 TIMESTAMP_DATE_FORMAT = new SimpleDateFormat(TIMESTAMP_PATTERN); 135 } 136 return TIMESTAMP_DATE_FORMAT.format(new Date()); 137 } 138 139 private void logMessage(MessageLogEntry.Type messageType, ResourceBundle bundle, String messageKey, Object... arguments) { 140 final String messageText = MessageFormat.format(bundle.getString(messageKey), arguments); 141 final MessageLogEntry entry = new MessageLogEntry(messageType, messageText, getTimeStamp()); 142 entries.add(0, entry); 143 incrementRevision(); 144 145 if (messageType.equals(MessageLogEntry.Type.WARNING)) { 146 incrementNumOfWarningMessages(); 147 } 148 } 149 150 private void incrementRevision() { 151 revision.set(revision.get() + 1); 152 } 153 154 private void incrementNumOfWarningMessages() { 155 numOfWarningMessages.set(numOfWarningMessages.get() + 1); 156 } 157 158 private void decrementNumOfWarningMessages() { 159 numOfWarningMessages.set(numOfWarningMessages.get() - 1); 160 } 161 162 private void resetNumOfWarningMessages() { 163 numOfWarningMessages.set(0); 164 } 165 }