1 package fr.ove.openmath.jome.ctrlview.bidim;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import fr.ove.utils.NumberUtils;
7 import fr.ove.openmath.jome.ctrlview.bidim.StringLayout;
8 import fr.ove.openmath.jome.ctrlview.bidim.Display;
9 import fr.ove.openmath.jome.ctrlview.bidim.GraphicContext;
10 import fr.ove.openmath.jome.model.*;
11 import fr.ove.openmath.jome.model.events.ModelEvent;
12
13 /***
14 * The display of a String.
15 *
16 * @author © 1999 DIRAT Laurent
17 * @version 2.0 04/08/1999
18 */
19 public abstract class AbstractStringDisplay extends BidimDisplay {
20 /***
21 * The string that the display represents.
22 */
23 private String string;
24
25 /***
26 * The constructor.
27 * @param graphicContext the graphic context of the display.
28 * @param string the string to display
29 */
30 public AbstractStringDisplay(GraphicContext graphicContext, String string) {
31 super(graphicContext);
32 this.string = string;
33 }
34
35 /***
36 * Sets a new string to the display.
37 * @param string the string to set.
38 */
39 public void setString( String string ) {
40 this.string = string;
41 }
42
43 /***
44 * Returns the string to display.
45 * @return the string.
46 */
47 public String getString() {
48 return string;
49 }
50
51 /***
52 * Scales the display.
53 */
54 public void scaleDisplay() {
55
56 setTheFont(getGraphicContext().scaleFont(getLevel()));
57
58 setComputeAttributes(true);
59
60
61
62
63 invalidate();
64 }
65
66 /***
67 * Paints the display.
68 * @param g where we paint the display.
69 */
70 public void paint(Graphics g) {
71 super.paint(g);
72
73 Color old = g.getColor();
74 Font oldFont = g.getFont();
75 Rectangle bounds = getBounds();
76
77 if (isSelected())
78 g.setColor(getSelectionColor());
79 else
80 g.setColor(getBackgroundColor());
81
82 g.fillRect(0, 0, bounds.width
83
84 g.setColor(getForegroundColor());
85 g.setFont(getFont());
86
87 if (weDrawBounds())
88 g.drawRect(0, 0, bounds.width - 1, bounds.height - 1);
89
90
91 if ( string.equals("*") )
92 g.drawString(string, 1, getHeight());
93 else if ( string.equals("-") )
94 g.drawString(string, 1, getAscent() - (int) Math.round(getDescent()*(40.0f/100.0f)));
95 else
96 g.drawString(string, 1, getAscent());
97
98
99
100
101
102
103
104 g.setColor(old);
105 g.setFont(oldFont);
106 }
107
108 /***
109 * Returns the insertion position for the moving of displays.
110 * @param mouseX the mouse position.
111 */
112 public int computeInsertionPosition(int mouseX) {
113 int insertionPosition;
114
115
116
117 if (isSymbolOperatorDisplay()) {
118 Display display = (Display) getParent();
119 display = (Display) display.getComponent(getRank() + 1);
120
121 insertionPosition = ((FormulaTreeStructure) display.getListener()).getRank();
122 }
123 else {
124
125
126
127
128
129 if (mouseX <= (getWidth() / 2))
130 insertionPosition = ((FormulaTreeStructure) getListener()).getRank();
131 else
132 insertionPosition = ((FormulaTreeStructure) getListener()).getRank() + 1;
133
134
135
136 }
137
138 return insertionPosition;
139 }
140
141 public void ToString() {
142 super.ToString();
143 System.out.println(" string = " + string);
144 }
145
146 public void whoAmI() {
147 System.out.println("I am the StringDisplay : " + string);
148 System.out.println("My level is " + getLevel());
149 }
150
151 }