Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/eireneh/bible/control/map/VBAExport.java


1   
2   package com.eireneh.bible.control.map;
3   
4   import java.io.*;
5   
6   import com.eireneh.util.*;
7   import com.eireneh.bible.passage.*;
8   
9   /**
10  * VBAExport takes a Map and exports the data to a Word VBA file. 
11  * This was developed from a VB macro that looked something like this:
12  * <pre>
13  Attribute VB_Name = "NumberLine"
14  
15  Option Explicit
16  
17  Dim shape As FreeformBuilder
18  
19  Private Const box_width As Single = 20#
20  Private Const box_height As Single = 20#
21  
22  
23  '-------------------------------------------------------------------------------
24  
25  Public Sub RouteMain()
26  
27    RouteStart 100#, 100#, "1"
28  
29    RouteContinue 110#, 110#, "2"
30    RouteContinue 120#, 120#, "3"
31    RouteContinue 130#, 120#, "4"
32    RouteContinue 130#, 130#, "5"
33    RouteContinue 100#, 130#, "6"
34    RouteContinue 90#, 100#, "7"
35  
36    RouteEnd
37  
38  End Sub
39  
40  
41  '-------------------------------------------------------------------------------
42  
43  Private Sub RouteStart(x As Single, y As Single, display As String)
44  
45    Set shape = ActiveDocument.Shapes.BuildFreeform(msoEditingAuto, x, y)
46    RouteAddText x, y, display
47  
48  End Sub
49  
50  
51  '-------------------------------------------------------------------------------
52  
53  Private Sub RouteContinue(x As Single, y As Single, display As String)
54  
55    shape.AddNodes msoSegmentLine, msoEditingAuto, x, y
56    RouteAddText x, y, display
57  
58  End Sub
59  
60  
61  '-------------------------------------------------------------------------------
62  
63  Private Sub RouteEnd()
64  
65    Dim route As ShapeRange
66    
67    shape.ConvertToShape.Select
68    Set route = Selection.ShapeRange
69  
70    route.Fill.Visible = msoFalse
71    route.Fill.Transparency = 0#
72    route.Line.Weight = 0.75
73    route.Line.DashStyle = msoLineSolid
74    route.Line.Style = msoLineSingle
75    route.Line.Transparency = 0#
76    route.Line.Visible = msoTrue
77    route.Line.ForeColor.RGB = RGB(255, 0, 0)
78    route.Line.BackColor.RGB = RGB(255, 255, 255)
79    route.Line.BeginArrowheadLength = msoArrowheadLengthMedium
80    route.Line.BeginArrowheadWidth = msoArrowheadWidthMedium
81    route.Line.BeginArrowheadStyle = msoArrowheadNone
82    route.Line.EndArrowheadLength = msoArrowheadLengthMedium
83    route.Line.EndArrowheadWidth = msoArrowheadWidthMedium
84    route.Line.EndArrowheadStyle = msoArrowheadNone
85  
86    'route.LockAspectRatio = msoFalse
87    'route.Height = 115.35
88    'route.Width = 333.05
89    'route.Rotation = 0#
90    'route.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
91    'route.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
92  
93    'route.LockAnchor = False
94    'route.WrapFormat.AllowOverlap = True
95    'route.WrapFormat.Side = wdWrapBoth
96    'route.WrapFormat.DistanceTop = CentimetersToPoints(0)
97    'route.WrapFormat.DistanceBottom = CentimetersToPoints(0)
98    'route.WrapFormat.DistanceLeft = CentimetersToPoints(0.32)
99    'route.WrapFormat.DistanceRight = CentimetersToPoints(0.32)
100   'route.WrapFormat.Type = 3
101 
102   route.ZOrder msoSendToBack
103   route.IncrementLeft box_width / 2
104   route.IncrementTop box_height / 2
105 
106 End Sub
107 
108 
109 '-------------------------------------------------------------------------------
110 
111 Private Sub RouteAddText(x As Single, y As Single, display As String)
112 
113   Dim box As shape
114   
115   Set box = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, x, y, box_width, box_width)
116 
117   box.TextFrame.TextRange.Text = display
118   box.Fill.Visible = msoFalse
119   box.Fill.Transparency = 0#
120   box.Line.Weight = 0.75
121   box.Line.DashStyle = msoLineSolid
122   box.Line.Style = msoLineSingle
123   box.Line.Transparency = 0#
124   box.Line.Visible = msoFalse
125 
126   'box.ScaleWidth 0.38, msoFalse, msoScaleFromTopLeft
127   'box.ScaleHeight 0.25, msoFalse, msoScaleFromTopLeft
128 
129   'box.LockAspectRatio = msoFalse
130   'box.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
131   'box.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
132   'box.LockAnchor = False
133 
134   'box.WrapFormat.AllowOverlap = True
135   'box.WrapFormat.Side = wdWrapBoth
136   'box.WrapFormat.DistanceTop = CentimetersToPoints(0)
137   'box.WrapFormat.DistanceBottom = CentimetersToPoints(0)
138   'box.WrapFormat.DistanceLeft = CentimetersToPoints(0.32)
139   'box.WrapFormat.DistanceRight = CentimetersToPoints(0.32)
140   'box.WrapFormat.Type = 3
141   
142   'box.ZOrder 5
143 
144   'box.TextFrame.MarginLeft = 7.09
145   'box.TextFrame.MarginRight = 7.09
146   'box.TextFrame.MarginTop = 3.69
147   'box.TextFrame.MarginBottom = 3.69
148 
149   'box.IncrementLeft 47.7
150   'box.IncrementTop 47.7
151   'box.Left = CentimetersToPoints(1)
152   'box.Top = CentimetersToPoints(0.23)
153   'box.Height = 18.15
154   'box.Width = 26.95
155 
156 End Sub
157 
158 '-------------------------------------------------------------------------------
159 * </pre>
160 *
161 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
162 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
163 * Distribution Licence:<br />
164 * Project B is free software; you can redistribute it
165 * and/or modify it under the terms of the GNU General Public License,
166 * version 2 as published by the Free Software Foundation.<br />
167 * This program is distributed in the hope that it will be useful,
168 * but WITHOUT ANY WARRANTY; without even the implied warranty of
169 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
170 * General Public License for more details.<br />
171 * The License is available on the internet
172 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
173 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
174 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
175 * The copyright to this program is held by it's authors.
176 * </font></td></tr></table>
177 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
178 * @see docs.Licence
179 * @author Joe Walker
180 * @version D0.I0.T0
181 */
182 public class VBAExport
183 {
184     /**
185     * Basic constructor
186     */
187     public VBAExport()
188     {
189     }
190 
191     /**
192     * How much do we magnify the original coords (0.0 - 1.0) by to get
193     * the desired output range. The default is 500 which will fit on an
194     * A4 sheet of paper
195     * @param x_mag The new x magnification factor
196     */
197     public void setXMagnification(int x_mag)
198     {
199         this.x_mag = x_mag;
200     }
201 
202     /**
203     * How much do we magnify the original coords (0.0 - 1.0) by to get
204     * the desired output range. The default is 500 which will fit on an
205     * A4 sheet of paper
206     * @param y_mag The new x magnification factor
207     */
208     public void setYMagnification(int y_mag)
209     {
210         this.y_mag = y_mag;
211     }
212 
213     /**
214     * Export the given Map file to the given stream
215     * @param map The data to export
216     * @param out The place to write the VBA file
217     */
218     public void export(Map map, PrintWriter out)
219     {
220         exportPreamble(out);
221 
222         out.println("");
223         out.println("Public Sub RouteMain()");
224 
225         int ord;
226         int x;
227         int y;
228 
229         try
230         {
231             int bie = Books.booksInBible();
232             for (int b=1; b<=bie; b++)
233             {
234                 ord = Books.verseOrdinal(b, 1, 1);
235 
236                 x = (int) (map.getPositionDimension(ord, 0) * x_mag);
237                 y = (int) (map.getPositionDimension(ord, 1) * y_mag);
238 
239                 out.println("  RouteStart "+x+"#, "+y+"#, \""+Books.getShortBookName(b)+"\"");
240 
241                 int cib = Books.chaptersInBook(b);
242                 for (int c=2; c<=cib; c++)
243                 {
244                     ord = Books.verseOrdinal(b, c, 1);
245 
246                     x = (int) (map.getPositionDimension(ord, 0) * x_mag);
247                     y = (int) (map.getPositionDimension(ord, 1) * y_mag);
248 
249                     out.println("  RouteContinue "+x+"#, "+y+"#, \""+c+"\"");
250                 }
251 
252                 out.println("  RouteEnd");
253                 out.println("");
254             }
255         }
256         catch (NoSuchVerseException ex)
257         {
258             throw new LogicError(ex);
259         }
260 
261         out.println("End Sub");
262 
263         exportSubRouteStart(out);
264         exportSubRouteContinue(out);
265         exportSubRouteEnd(out);
266         exportSubRouteAddText(out);
267     }
268 
269     /**
270     * The common bits at the start of every file
271     * @param out The place to write the VBA file
272     */
273     protected static void exportPreamble(PrintWriter out)
274     {
275         out.println("Attribute VB_Name = \"NumberLine\"");
276         out.println("");
277         out.println("Option Explicit");
278         out.println("Dim shape As FreeformBuilder");
279         out.println("Private Const box_width As Single = 20#");
280         out.println("Private Const box_height As Single = 20#");
281     }
282 
283     /**
284     * The common bits at the start of every file
285     * @param out The place to write the VBA file
286     */
287     protected static void exportSubRouteStart(PrintWriter out)
288     {
289         out.println("");
290         out.println("Private Sub RouteStart(x As Single, y As Single, display As String)");
291         out.println("  Set shape = ActiveDocument.Shapes.BuildFreeform(msoEditingAuto, x, y)");
292         out.println("  RouteAddText x, y, display");
293         out.println("End Sub");
294     }
295 
296     /**
297     * The common bits at the start of every file
298     * @param out The place to write the VBA file
299     */
300     protected static void exportSubRouteContinue(PrintWriter out)
301     {
302         out.println("");
303         out.println("Private Sub RouteContinue(x As Single, y As Single, display As String)");
304         out.println("  shape.AddNodes msoSegmentLine, msoEditingAuto, x, y");
305         out.println("  RouteAddText x, y, display");
306         out.println("End Sub");
307     }
308 
309     /**
310     * The common bits at the start of every file
311     * @param out The place to write the VBA file
312     */
313     protected static void exportSubRouteEnd(PrintWriter out)
314     {
315         out.println("");
316         out.println("Private Sub RouteEnd()");
317         out.println("  Dim route As ShapeRange");
318         out.println("  shape.ConvertToShape.Select");
319         out.println("  Set route = Selection.ShapeRange");
320         out.println("  route.Fill.Visible = msoFalse");
321         out.println("  route.Fill.Transparency = 0#");
322         out.println("  route.Line.Weight = 0.75");
323         out.println("  route.Line.DashStyle = msoLineSolid");
324         out.println("  route.Line.Style = msoLineSingle");
325         out.println("  route.Line.Transparency = 0#");
326         out.println("  route.Line.Visible = msoTrue");
327         out.println("  route.Line.ForeColor.RGB = RGB(255, 0, 0)");
328         out.println("  route.Line.BackColor.RGB = RGB(255, 255, 255)");
329         out.println("  route.Line.BeginArrowheadLength = msoArrowheadLengthMedium");
330         out.println("  route.Line.BeginArrowheadWidth = msoArrowheadWidthMedium");
331         out.println("  route.Line.BeginArrowheadStyle = msoArrowheadNone");
332         out.println("  route.Line.EndArrowheadLength = msoArrowheadLengthMedium");
333         out.println("  route.Line.EndArrowheadWidth = msoArrowheadWidthMedium");
334         out.println("  route.Line.EndArrowheadStyle = msoArrowheadNone");
335         out.println("  route.ZOrder msoSendToBack");
336         out.println("  route.IncrementLeft box_width / 2");
337         out.println("  route.IncrementTop box_height / 2");
338         out.println("End Sub");
339     }
340 
341     /**
342     * The common bits at the start of every file
343     * @param out The place to write the VBA file
344     */
345     protected static void exportSubRouteAddText(PrintWriter out)
346     {
347         out.println("");
348         out.println("Private Sub RouteAddText(x As Single, y As Single, display As String)");
349         out.println("  Dim box As shape");
350         out.println("  Set box = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, x, y, box_width, box_width)");
351         out.println("  box.TextFrame.TextRange.Text = display");
352         out.println("  box.Fill.Visible = msoFalse");
353         out.println("  box.Fill.Transparency = 0#");
354         out.println("  box.Line.Weight = 0.75");
355         out.println("  box.Line.DashStyle = msoLineSolid");
356         out.println("  box.Line.Style = msoLineSingle");
357         out.println("  box.Line.Transparency = 0#");
358         out.println("  box.Line.Visible = msoFalse");
359         out.println("End Sub");
360     }
361 
362     /** The x magnification factor */
363     private int x_mag = 500;
364 
365     /** The y magnification factor */
366     private int y_mag = 500;
367 }