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