f@0: /*
f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0:
f@0: Copyright (C) 2002 Cay S. Horstmann (http://horstmann.com)
f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0:
f@0: This program is free software: you can redistribute it and/or modify
f@0: it under the terms of the GNU General Public License as published by
f@0: the Free Software Foundation, either version 3 of the License, or
f@0: (at your option) any later version.
f@0:
f@0: This program is distributed in the hope that it will be useful,
f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0: GNU General Public License for more details.
f@0:
f@0: You should have received a copy of the GNU General Public License
f@0: along with this program. If not, see .
f@0: */
f@0:
f@0: package uk.ac.qmul.eecs.ccmi.simpletemplate;
f@0:
f@0: import java.awt.Dimension;
f@0: import java.awt.Graphics2D;
f@0: import java.awt.geom.Rectangle2D;
f@0: import java.util.Arrays;
f@0: import java.util.List;
f@0: import java.util.Set;
f@0:
f@0: import javax.swing.JLabel;
f@0:
f@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties.Modifiers;
f@0:
f@0: /**
f@0: A string that can extend over multiple lines.
f@0: */
f@0: public class MultiLineString {
f@0: /**
f@0: Constructs an empty, centered, normal size multiline
f@0: string.
f@0: */
f@0: public MultiLineString(){
f@0: text = "";
f@0: arrayText = null;
f@0: justification = CENTER;
f@0: size = NORMAL;
f@0: isSingleLine = true;
f@0: }
f@0: /**
f@0: Sets the value of the text property.
f@0: @param newValue the text of the multiline string
f@0: */
f@0: public void setText(String newValue){
f@0: setText(newValue,null);
f@0: }
f@0:
f@0: public void setText(String newValue, ModifierView[] modifierViews) {
f@0: isSingleLine = true;
f@0: text = newValue;
f@0: this.modifierViews = modifierViews;
f@0: format();
f@0: }
f@0:
f@0: public void setText(String[] newValue, Modifiers modifiers){
f@0: isSingleLine = false;
f@0: this.modifiers = modifiers;
f@0: arrayText = newValue;
f@0: format();
f@0: }
f@0:
f@0: public void setBold(boolean bold){
f@0: isBold = bold;
f@0: format();
f@0: }
f@0: /**
f@0: Gets the value of the text property.
f@0: @return the text of the multiline string
f@0: */
f@0: public String getText() {
f@0: if(isSingleLine)
f@0: return text;
f@0: else{
f@0: StringBuilder builder = new StringBuilder("");
f@0: for(int i=0; i");
f@0: suffix.insert(0, "");
f@0: }
f@0: if (size == SMALL){
f@0: prefix.append("");
f@0: suffix.insert(0, "");
f@0: }
f@0:
f@0: htmlText.append("");
f@0: if(isSingleLine){
f@0: if(isBold)
f@0: prefix.append("");
f@0: htmlText.append(prefix);
f@0:
f@0: String formattedText = text;
f@0: if(modifierViews != null){
f@0: for(ModifierView view : modifierViews){
f@0: formattedText = formatFromView(formattedText,view);
f@0: }
f@0: }
f@0: htmlText.append(formattedText);
f@0:
f@0: if(isBold)
f@0: suffix.insert(0, "");
f@0: htmlText.append(suffix);
f@0: }else{ // multi line
f@0: boolean first = true;
f@0: for(int i=0; i");
f@0: htmlText.append(prefix);
f@0: String textLine = arrayText[i];
f@0: Set indexes = modifiers.getIndexes(i);
f@0: for(Integer I : indexes){
f@0: ModifierView view = (ModifierView)modifiers.getView(modifiers.getTypes().get(I));
f@0: textLine = formatFromView(textLine, view);
f@0: }
f@0: htmlText.append(textLine);
f@0: htmlText.append(suffix);
f@0: }
f@0: }
f@0: htmlText.append("");
f@0:
f@0: // replace any < that are not followed by {u, i, b, tt, font, br} with <
f@0: List dontReplace = Arrays.asList(new String[] { "u", "i", "b", "tt", "font", "br" });
f@0:
f@0: int ltpos = 0;
f@0: while (ltpos != -1){
f@0: ltpos = htmlText.indexOf("<", ltpos + 1);
f@0: if (ltpos != -1 && !(ltpos + 1 < htmlText.length() && htmlText.charAt(ltpos + 1) == '/')){
f@0: int end = ltpos + 1;
f@0: while (end < htmlText.length() && Character.isLetter(htmlText.charAt(end))) end++;
f@0: if (!dontReplace.contains(htmlText.substring(ltpos + 1, end)))
f@0: htmlText.replace(ltpos, ltpos+1, "<");
f@0: }
f@0: }
f@0:
f@0: label.setText(htmlText.toString());
f@0: if (justification == LEFT) label.setHorizontalAlignment(JLabel.LEFT);
f@0: else if (justification == CENTER) label.setHorizontalAlignment(JLabel.CENTER);
f@0: else if (justification == RIGHT) label.setHorizontalAlignment(JLabel.RIGHT);
f@0: }
f@0:
f@0: private String formatFromView(String text, ModifierView view){
f@0: if(view == null)
f@0: return text;
f@0:
f@0: StringBuilder prefix = new StringBuilder("");
f@0: StringBuilder suffix = new StringBuilder("");
f@0:
f@0: prefix.append(view.getPrefix());
f@0: suffix.append(view.getSuffix());
f@0:
f@0: if(view.isBold()){
f@0: prefix.insert(0,"");
f@0: suffix.append("");
f@0: }
f@0:
f@0: if(view.isItalic()){
f@0: prefix.insert(0,"");
f@0: suffix.append("");
f@0: }
f@0:
f@0: if(view.isUnderline()){
f@0: prefix.insert(0,"");
f@0: suffix.append("");
f@0: }
f@0: return prefix.append(text).append(suffix).toString();
f@0: }
f@0:
f@0: /**
f@0: Gets the bounding rectangle for this multiline string.
f@0: @return the bounding rectangle (with top left corner (0,0))
f@0: */
f@0: public Rectangle2D getBounds(){
f@0: if(isSingleLine){
f@0: if (text.length() == 0)
f@0: return new Rectangle2D.Double();
f@0: }else {
f@0: if(arrayText.length == 0)
f@0: return new Rectangle2D.Double();
f@0: }
f@0: Dimension dim = label.getPreferredSize();
f@0: return new Rectangle2D.Double(0, 0, dim.getWidth(), dim.getHeight());
f@0: }
f@0:
f@0: /**
f@0: Draws this multiline string inside a given rectangle
f@0: @param g2 the graphics context
f@0: @param r the rectangle into which to place this multiline string
f@0: */
f@0: public void draw(Graphics2D g2, Rectangle2D r){
f@0: label.setFont(g2.getFont());
f@0: label.setBounds(0, 0, (int) r.getWidth(), (int) r.getHeight());
f@0: g2.translate(r.getX(), r.getY());
f@0: label.paint(g2);
f@0: g2.translate(-r.getX(), -r.getY());
f@0: }
f@0:
f@0: public static final int LEFT = 0;
f@0: public static final int CENTER = 1;
f@0: public static final int RIGHT = 2;
f@0: public static final int LARGE = 3;
f@0: public static final int NORMAL = 4;
f@0: public static final int SMALL = 5;
f@0:
f@0: private String text;
f@0: private String[] arrayText;
f@0: private Modifiers modifiers;
f@0: private ModifierView[] modifierViews;
f@0: private int justification;
f@0: private int size;
f@0: private boolean isBold;
f@0: private transient JLabel label = new JLabel();
f@0: private boolean isSingleLine;
f@0: }