annotate java/src/uk/ac/qmul/eecs/ccmi/simpletemplate/MultiLineString.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
f@0 5 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 6
f@0 7 This program is free software: you can redistribute it and/or modify
f@0 8 it under the terms of the GNU General Public License as published by
f@0 9 the Free Software Foundation, either version 3 of the License, or
f@0 10 (at your option) any later version.
f@0 11
f@0 12 This program is distributed in the hope that it will be useful,
f@0 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 15 GNU General Public License for more details.
f@0 16
f@0 17 You should have received a copy of the GNU General Public License
f@0 18 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 19 */
f@0 20
f@0 21 package uk.ac.qmul.eecs.ccmi.simpletemplate;
f@0 22
f@0 23 import java.awt.Dimension;
f@0 24 import java.awt.Graphics2D;
f@0 25 import java.awt.geom.Rectangle2D;
f@0 26 import java.util.Arrays;
f@0 27 import java.util.List;
f@0 28 import java.util.Set;
f@0 29
f@0 30 import javax.swing.JLabel;
f@0 31
f@0 32 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties.Modifiers;
f@0 33
f@0 34 /**
f@0 35 A string that can extend over multiple lines.
f@0 36 */
f@0 37 public class MultiLineString {
f@0 38 /**
f@0 39 Constructs an empty, centered, normal size multiline
f@0 40 string.
f@0 41 */
f@0 42 public MultiLineString(){
f@0 43 text = "";
f@0 44 arrayText = null;
f@0 45 justification = CENTER;
f@0 46 size = NORMAL;
f@0 47 isSingleLine = true;
f@0 48 }
f@0 49 /**
f@0 50 Sets the value of the text property.
f@0 51 @param newValue the text of the multiline string
f@0 52 */
f@0 53 public void setText(String newValue){
f@0 54 setText(newValue,null);
f@0 55 }
f@0 56
f@0 57 public void setText(String newValue, ModifierView[] modifierViews) {
f@0 58 isSingleLine = true;
f@0 59 text = newValue;
f@0 60 this.modifierViews = modifierViews;
f@0 61 format();
f@0 62 }
f@0 63
f@0 64 public void setText(String[] newValue, Modifiers modifiers){
f@0 65 isSingleLine = false;
f@0 66 this.modifiers = modifiers;
f@0 67 arrayText = newValue;
f@0 68 format();
f@0 69 }
f@0 70
f@0 71 public void setBold(boolean bold){
f@0 72 isBold = bold;
f@0 73 format();
f@0 74 }
f@0 75 /**
f@0 76 Gets the value of the text property.
f@0 77 @return the text of the multiline string
f@0 78 */
f@0 79 public String getText() {
f@0 80 if(isSingleLine)
f@0 81 return text;
f@0 82 else{
f@0 83 StringBuilder builder = new StringBuilder("");
f@0 84 for(int i=0; i<arrayText.length; i++){
f@0 85 builder.append(arrayText[i]);
f@0 86 builder.append('\n');
f@0 87 }
f@0 88 return builder.toString();
f@0 89 }
f@0 90 }
f@0 91 /**
f@0 92 Sets the value of the justification property.
f@0 93 @param newValue the justification, one of LEFT, CENTER,
f@0 94 RIGHT
f@0 95 */
f@0 96 public void setJustification(int newValue) { justification = newValue; format(); }
f@0 97 /**
f@0 98 Gets the value of the justification property.
f@0 99 @return the justification, one of LEFT, CENTER,
f@0 100 RIGHT
f@0 101 */
f@0 102 public int getJustification() { return justification; }
f@0 103 /**
f@0 104 Sets the value of the size property.
f@0 105 @param newValue the size, one of SMALL, NORMAL, LARGE
f@0 106 */
f@0 107 public void setSize(int newValue) { size = newValue; format(); }
f@0 108 /**
f@0 109 Gets the value of the size property.
f@0 110 @return the size, one of SMALL, NORMAL, LARGE
f@0 111 */
f@0 112 public int getSize() { return size; }
f@0 113
f@0 114 @Override
f@0 115 public String toString(){
f@0 116 if(isSingleLine)
f@0 117 return text.replace('\n', '|');
f@0 118 else
f@0 119 return getText().replace('\n', '|');
f@0 120 }
f@0 121
f@0 122 private void format(){
f@0 123 StringBuffer prefix = new StringBuffer();
f@0 124 StringBuffer suffix = new StringBuffer();
f@0 125 StringBuffer htmlText = new StringBuffer();
f@0 126 prefix.append("&nbsp;");
f@0 127 suffix.insert(0, "&nbsp;");
f@0 128 if (size == LARGE){
f@0 129 prefix.append("<font size=\"+1\">");
f@0 130 suffix.insert(0, "</font>");
f@0 131 }
f@0 132 if (size == SMALL){
f@0 133 prefix.append("<font size=\"-1\">");
f@0 134 suffix.insert(0, "</font>");
f@0 135 }
f@0 136
f@0 137 htmlText.append("<html>");
f@0 138 if(isSingleLine){
f@0 139 if(isBold)
f@0 140 prefix.append("<b>");
f@0 141 htmlText.append(prefix);
f@0 142
f@0 143 String formattedText = text;
f@0 144 if(modifierViews != null){
f@0 145 for(ModifierView view : modifierViews){
f@0 146 formattedText = formatFromView(formattedText,view);
f@0 147 }
f@0 148 }
f@0 149 htmlText.append(formattedText);
f@0 150
f@0 151 if(isBold)
f@0 152 suffix.insert(0, "</b>");
f@0 153 htmlText.append(suffix);
f@0 154 }else{ // multi line
f@0 155 boolean first = true;
f@0 156 for(int i=0; i<arrayText.length; i++){
f@0 157 if (first)
f@0 158 first = false;
f@0 159 else
f@0 160 htmlText.append("<br>");
f@0 161 htmlText.append(prefix);
f@0 162 String textLine = arrayText[i];
f@0 163 Set<Integer> indexes = modifiers.getIndexes(i);
f@0 164 for(Integer I : indexes){
f@0 165 ModifierView view = (ModifierView)modifiers.getView(modifiers.getTypes().get(I));
f@0 166 textLine = formatFromView(textLine, view);
f@0 167 }
f@0 168 htmlText.append(textLine);
f@0 169 htmlText.append(suffix);
f@0 170 }
f@0 171 }
f@0 172 htmlText.append("</html>");
f@0 173
f@0 174 // replace any < that are not followed by {u, i, b, tt, font, br} with &lt;
f@0 175 List<String> dontReplace = Arrays.asList(new String[] { "u", "i", "b", "tt", "font", "br" });
f@0 176
f@0 177 int ltpos = 0;
f@0 178 while (ltpos != -1){
f@0 179 ltpos = htmlText.indexOf("<", ltpos + 1);
f@0 180 if (ltpos != -1 && !(ltpos + 1 < htmlText.length() && htmlText.charAt(ltpos + 1) == '/')){
f@0 181 int end = ltpos + 1;
f@0 182 while (end < htmlText.length() && Character.isLetter(htmlText.charAt(end))) end++;
f@0 183 if (!dontReplace.contains(htmlText.substring(ltpos + 1, end)))
f@0 184 htmlText.replace(ltpos, ltpos+1, "&lt;");
f@0 185 }
f@0 186 }
f@0 187
f@0 188 label.setText(htmlText.toString());
f@0 189 if (justification == LEFT) label.setHorizontalAlignment(JLabel.LEFT);
f@0 190 else if (justification == CENTER) label.setHorizontalAlignment(JLabel.CENTER);
f@0 191 else if (justification == RIGHT) label.setHorizontalAlignment(JLabel.RIGHT);
f@0 192 }
f@0 193
f@0 194 private String formatFromView(String text, ModifierView view){
f@0 195 if(view == null)
f@0 196 return text;
f@0 197
f@0 198 StringBuilder prefix = new StringBuilder("");
f@0 199 StringBuilder suffix = new StringBuilder("");
f@0 200
f@0 201 prefix.append(view.getPrefix());
f@0 202 suffix.append(view.getSuffix());
f@0 203
f@0 204 if(view.isBold()){
f@0 205 prefix.insert(0,"<b>");
f@0 206 suffix.append("</b>");
f@0 207 }
f@0 208
f@0 209 if(view.isItalic()){
f@0 210 prefix.insert(0,"<i>");
f@0 211 suffix.append("</i>");
f@0 212 }
f@0 213
f@0 214 if(view.isUnderline()){
f@0 215 prefix.insert(0,"<u>");
f@0 216 suffix.append("</u>");
f@0 217 }
f@0 218 return prefix.append(text).append(suffix).toString();
f@0 219 }
f@0 220
f@0 221 /**
f@0 222 Gets the bounding rectangle for this multiline string.
f@0 223 @return the bounding rectangle (with top left corner (0,0))
f@0 224 */
f@0 225 public Rectangle2D getBounds(){
f@0 226 if(isSingleLine){
f@0 227 if (text.length() == 0)
f@0 228 return new Rectangle2D.Double();
f@0 229 }else {
f@0 230 if(arrayText.length == 0)
f@0 231 return new Rectangle2D.Double();
f@0 232 }
f@0 233 Dimension dim = label.getPreferredSize();
f@0 234 return new Rectangle2D.Double(0, 0, dim.getWidth(), dim.getHeight());
f@0 235 }
f@0 236
f@0 237 /**
f@0 238 Draws this multiline string inside a given rectangle
f@0 239 @param g2 the graphics context
f@0 240 @param r the rectangle into which to place this multiline string
f@0 241 */
f@0 242 public void draw(Graphics2D g2, Rectangle2D r){
f@0 243 label.setFont(g2.getFont());
f@0 244 label.setBounds(0, 0, (int) r.getWidth(), (int) r.getHeight());
f@0 245 g2.translate(r.getX(), r.getY());
f@0 246 label.paint(g2);
f@0 247 g2.translate(-r.getX(), -r.getY());
f@0 248 }
f@0 249
f@0 250 public static final int LEFT = 0;
f@0 251 public static final int CENTER = 1;
f@0 252 public static final int RIGHT = 2;
f@0 253 public static final int LARGE = 3;
f@0 254 public static final int NORMAL = 4;
f@0 255 public static final int SMALL = 5;
f@0 256
f@0 257 private String text;
f@0 258 private String[] arrayText;
f@0 259 private Modifiers modifiers;
f@0 260 private ModifierView[] modifierViews;
f@0 261 private int justification;
f@0 262 private int size;
f@0 263 private boolean isBold;
f@0 264 private transient JLabel label = new JLabel();
f@0 265 private boolean isSingleLine;
f@0 266 }