1 // Copyright (C) 2005 - 2012 Philip Aston 2 // All rights reserved. 3 // 4 // This file is part of The Grinder software distribution. Refer to 5 // the file LICENSE which is part of The Grinder distribution for 6 // licensing details. The Grinder distribution is available on the 7 // Internet at http://grinder.sourceforge.net/ 8 // 9 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 10 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 11 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 12 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 13 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 14 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 15 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 16 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 17 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 18 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 19 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 20 // OF THE POSSIBILITY OF SUCH DAMAGE. 21 22 package net.grinder.console.editor; 23 24 import static java.util.Arrays.asList; 25 26 import java.io.File; 27 import java.util.HashSet; 28 import java.util.Set; 29 30 import net.grinder.console.common.DisplayMessageConsoleException; 31 import net.grinder.console.editor.BufferImplementation.Listener; 32 33 34 /** 35 * Buffer state. 36 * 37 * @author Philip Aston 38 */ 39 public interface Buffer { 40 41 /** 42 * Buffer type. 43 */ 44 enum Type { 45 46 /** Buffer type constant. */ 47 JAVA_BUFFER("Java", "text/java", "java"), 48 49 /** Buffer type constant. */ 50 CLOJURE_BUFFER("Clojure", "text/clojure", "clj"), 51 52 /** Buffer type constant. */ 53 PYTHON_BUFFER("Python", "text/python", "py"), 54 55 /** Buffer type constant. */ 56 SHELL_BUFFER("Shell", "text/bash", "sh", "bash", "csh", "ksh"), 57 58 /** Buffer type constant. */ 59 HTML_BUFFER("HTML", "text/html", "html", "htm"), 60 61 /** Buffer type constant. */ 62 MSDOS_BATCH_BUFFER("MSDOS Batch", "text/dosbatch", "bat", "cmd"), 63 64 /** Buffer type constant. */ 65 XML_BUFFER("XML", "text/xml", "xml"), 66 67 /** Buffer type constant. */ 68 PROPERTIES_BUFFER("Unknown", "text/properties", "properties"), 69 70 /** Buffer type constant. */ 71 TEXT_BUFFER("Unknown", "text/text", "text", "txt"); 72 73 private final String m_name; 74 private final String m_contentType; 75 private Set<String> m_extensions; 76 77 private Type(String name, String contentType, String... extensions) { 78 m_name = name; 79 m_contentType = contentType; 80 m_extensions = new HashSet<String>(asList(extensions)); 81 } 82 83 public String getName() { 84 return m_name; 85 } 86 87 public String getContentType() { 88 return m_contentType; 89 } 90 91 public static Type forExtension(String extension) { 92 for (Type t : Type.values()) { 93 if (t.m_extensions.contains(extension)) { 94 return t; 95 } 96 } 97 98 return TEXT_BUFFER; 99 } 100 } 101 102 103 /** 104 * Return the buffer's {@link TextSource}. 105 * 106 * @return The text source. 107 */ 108 TextSource getTextSource(); 109 110 /** 111 * Update the text source from the file. 112 * 113 * @exception DisplayMessageConsoleException If the file could not 114 * be read from. 115 * @exception EditorException If an unexpected problem occurs. 116 */ 117 void load() throws DisplayMessageConsoleException, EditorException; 118 119 /** 120 * Update the buffer's file from the text source. 121 * 122 * @exception DisplayMessageConsoleException If the file could not 123 * be written to. 124 * @exception EditorException If an unexpected problem occurs. 125 */ 126 void save() throws DisplayMessageConsoleException, EditorException; 127 128 /** 129 * Update a file from the text source and, if successful, associate 130 * the buffer with the new file. 131 * 132 * @param file The file. 133 * @exception DisplayMessageConsoleException If the file could not 134 * be written to. 135 */ 136 void save(File file) throws DisplayMessageConsoleException; 137 138 /** 139 * Return whether the buffer's text has been changed since the last 140 * save. 141 * 142 * @return <code>true</code> => the text has changed. 143 */ 144 boolean isDirty(); 145 146 /** 147 * Return the buffer's associated file. 148 * 149 * @return The file. <code>null</code> if there is no associated file. 150 */ 151 File getFile(); 152 153 /** 154 * Return whether the file has been independently modified since the 155 * last save. 156 * 157 * @return <code>true</code> => the file has changed independently 158 * of the buffer. 159 */ 160 boolean isUpToDate(); 161 162 /** 163 * Get the type of the buffer. 164 * 165 * @return The buffer's type. 166 */ 167 Type getType(); 168 169 /** 170 * Return display name of buffer. 171 * 172 * @return The buffer's name. 173 */ 174 String getDisplayName(); 175 176 /** 177 * Useful for debugging. 178 * 179 * @return Description of the Buffer. 180 */ 181 String toString(); 182 183 /** 184 * Add a new listener. 185 * 186 * @param listener The listener. 187 */ 188 void addListener(Listener listener); 189 }