View Javadoc

1   package org.catacomb.druid.xtext.canvas;
2   
3   import org.catacomb.druid.swing.DTextCanvas;
4   import org.catacomb.druid.swing.dnd.Region;
5   import org.catacomb.druid.swing.dnd.RegionBoard;
6   import org.catacomb.druid.xtext.base.WordBlock;
7   import org.catacomb.druid.xtext.data.*;
8   import org.catacomb.interlish.structure.TextDisplayed;
9   import org.catacomb.interlish.structure.TextPainter;
10  
11  
12  import java.awt.Color;
13  import java.awt.FontMetrics;
14  import java.awt.Graphics2D;
15  import java.util.ArrayList;
16  
17  
18  public class DataBoard extends RegionBoard implements TextPainter {
19  
20      FontStore fontStore;
21  
22      PageDataStore pageDataStore;
23  
24      int width;
25      int height;
26  
27      int itxt;
28      int jtxt;
29  
30  
31  
32      public DataBoard(DTextCanvas dtc) {
33          super(dtc);
34  
35          dtc.setTextPainter(this);
36  
37          fontStore = FontStore.instance();
38  
39      }
40  
41  
42  
43      public void paintText(Graphics2D g) {
44  
45          if (pageDataStore == null) {
46              return;
47          }
48  
49          width = canvas.getWidth();
50          height = canvas.getHeight();
51  
52          g.setFont(fontStore.getActiveFont());
53          g.setColor(Color.black);
54          FontMetrics fm = g.getFontMetrics();
55  
56  
57          itxt = 10;
58          jtxt = 30;
59  
60          for (ArrayList<XItem> alxi : pageDataStore.getLists()) {
61              XItem xi0 = alxi.get(0);
62              XType xt = xi0.getType();
63  
64              g.setFont(fontStore.getH2Font());
65              g.drawString(xt.getID(), itxt, jtxt);
66  
67              itxt += g.getFontMetrics().stringWidth(xt.getID()) + 6;
68              itxt += 8;
69  
70              g.setFont(fontStore.getActiveFont());
71  
72              if (xt.hasFields()) {
73                  drawBracketedList(g, fm, xt.getFieldNames());
74              }
75  
76              for (XItem xit : alxi) {
77                  jtxt += 18;
78                  itxt = 32;
79  
80                  g.fillOval(20, jtxt - 8, 6, 6);
81  
82                  g.setFont(fontStore.getBoldFont());
83  
84                  String stxt = xit.getText();
85                  if (stxt == null || stxt.length() == 0) {
86                      g.setColor(Color.red);
87                      stxt = "- name -";
88                      g.drawString(stxt, itxt, jtxt);
89                      g.setColor(Color.black);
90  
91                  } else {
92                      g.setColor(Color.black);
93                      g.drawString(xit.getText(), itxt, jtxt);
94                  }
95  
96                  itxt += g.getFontMetrics().stringWidth(stxt) + 8;
97                  g.setFont(fontStore.getActiveFont());
98  
99                  if (xt.hasFields()) {
100                     for (XType ft : xt.getFields()) {
101                         String sid = ft.getID();
102                         String val = xit.getFieldValue(sid);
103                         if (val == null) {
104                             g.setColor(Color.red);
105                             int w = fm.stringWidth(sid);
106                             if (itxt + w > width) {
107                                 itxt = 48;
108                                 jtxt += 18;
109                             }
110 
111 
112                             // g.drawRect(itxt-2, jtxt - 9, w+4, 11);
113                             g.drawLine(itxt, jtxt + 2, itxt + w, jtxt + 2);
114 
115                             addRegion(itxt, jtxt + 4, w, 16, xit, sid, Region.DROP);
116 
117                             g.drawString(sid, itxt, jtxt);
118                             itxt += w;
119                             itxt += 12;
120                             g.setColor(Color.black);
121 
122                             // regionStore.addRegion(itxt, jtxt, w, 16, xit, sid);
123 
124                         } else {
125                             val += ", ";
126                             int w = fm.stringWidth(val);
127                             if (itxt + w > width) {
128                                 itxt = 48;
129                                 jtxt += 18;
130                             }
131                             g.drawString(val, itxt, jtxt);
132                             itxt += w;
133                         }
134 
135                     }
136                 }
137             }
138 
139 
140 
141             itxt = 10;
142             jtxt += 25;
143         }
144 
145 
146         jtxt += 20;
147 
148         if (pageDataStore.hasRelations()) {
149             g.setFont(fontStore.getH2Font());
150             g.drawString("Relations", itxt, jtxt);
151             jtxt += 20;
152 
153             for (XRelation xr : pageDataStore.getRelations()) {
154                 itxt = 10;
155                 String atxt = xr.getAText();
156                 if (atxt == null || atxt.length() == 0) {
157                     g.setColor(Color.red);
158                     g.drawRect(itxt, jtxt - 12, 50, 16);
159                     addRegion(itxt, jtxt + 4, 50, 16, xr, "left", Region.DROP);
160                     itxt += 50 + 10;
161 
162                 } else {
163                     g.setFont(fontStore.getActiveFont());
164                     g.setColor(Color.black);
165                     g.drawString(atxt, itxt, jtxt);
166                     itxt += g.getFontMetrics().stringWidth(atxt) + 10;
167                 }
168 
169                 g.setFont(fontStore.getDefaultItalicFont());
170                 g.setColor(Color.black);
171                 String tid = xr.getTypeID();
172                 g.drawString(tid, itxt, jtxt);
173                 itxt += g.getFontMetrics().stringWidth(tid) + 10;
174 
175                 String btxt = xr.getBText();
176                 if (btxt == null || btxt.length() == 0) {
177                     g.setColor(Color.red);
178                     g.drawRect(itxt, jtxt - 12, 50, 16);
179                     addRegion(itxt, jtxt + 4, 50, 16, xr, "right", Region.DROP);
180                     itxt += 50 + 10;
181 
182                 } else {
183                     g.setFont(fontStore.getActiveFont());
184                     g.setColor(Color.black);
185                     g.drawString(btxt, itxt, jtxt);
186                     itxt += g.getFontMetrics().stringWidth(btxt) + 10;
187                 }
188                 jtxt += 18;
189             }
190 
191 
192         }
193 
194 
195         int ofth = fullTextHeight;
196         fullTextHeight = jtxt + 10;
197 
198         if (Math.abs(fullTextHeight - ofth) > 10) {
199             canvas.contentSizeChanged();
200         }
201 
202 
203     }
204 
205 
206 
207     private void drawBracketedList(Graphics2D g, FontMetrics fm, String[] sa) {
208         int n = sa.length;
209         for (int i = 0; i < n; i++) {
210             String s = sa[i];
211             if (i == 0) {
212                 s = "(" + s;
213             }
214             if (i < n - 1) {
215                 s = s + ", ";
216             }
217             if (i == n - 1) {
218                 s = s + ")";
219             }
220 
221             int w = fm.stringWidth(s);
222             if (itxt + w > width) {
223                 itxt = 20;
224                 jtxt += 18;
225             }
226             g.drawString(s, itxt, jtxt);
227             itxt += w;
228         }
229     }
230 
231 
232 
233     public void dropOn(Object src, Region reg) {
234         Object tgt = reg.getRef();
235 
236         String tag = reg.getTag();
237         String txt = "err";
238         if (src instanceof TextDisplayed) {
239             txt = ((TextDisplayed)src).getDisplayText();
240         }
241 
242 
243         if (tgt instanceof XItem) {
244             ((XItem)tgt).setField(tag, txt);
245 
246             if (src instanceof WordBlock) {
247                 applyType((WordBlock)src, XTypeStore.instance().getPropertyType());
248             }
249 
250         } else if (tgt instanceof XRelation) {
251             if (tag.equals("left")) {
252                 ((XRelation)tgt).setA(txt, src);
253             } else {
254                 ((XRelation)tgt).setB(txt, src);
255             }
256             if (src instanceof WordBlock) {
257                 applyType((WordBlock)src, XTypeStore.instance().getThingType());
258             }
259         }
260         repaint();
261     }
262 
263 
264     public void dropGeneral(Object src) {
265         if (src instanceof XType) {
266             XItem xit = new XItem((XType)src, "");
267             pageDataStore.addItem(xit);
268 
269 
270         } else if (src instanceof XRelationType) {
271             XRelation xr = new XRelation((XRelationType)src);
272             pageDataStore.addRelation(xr);
273 
274         }
275         repaint();
276     }
277 
278 
279     private void applyType(WordBlock wbin, XType xt) {
280         WordBlock wb = wbin;
281         while (wb != null) {
282             wb.setType(xt);
283             wb = wb.getLinkee();
284         }
285     }
286 
287 
288 
289     public void emptyClick() {
290 
291     }
292 
293 
294 
295 
296     public void setPageDataStore(PageDataStore pdStore) {
297         pageDataStore = pdStore;
298     }
299 
300 
301 
302 }