KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > commands > InsertMediaCommand


1 /*
2  * $Id: InsertMediaCommand.java,v 1.7 2002/07/22 23:16:04 skavish Exp $
3  *
4  * ===========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.commands;
52
53 import java.io.*;
54 import java.awt.geom.*;
55 import org.openlaszlo.iv.flash.parser.*;
56 import org.openlaszlo.iv.flash.api.*;
57 import org.openlaszlo.iv.flash.api.image.*;
58 import org.openlaszlo.iv.flash.api.shape.*;
59 import org.openlaszlo.iv.flash.util.*;
60 import org.openlaszlo.iv.flash.cache.*;
61 import org.openlaszlo.iv.flash.url.*;
62
63 import org.openlaszlo.iv.flash.context.Context;
64
65 /**
66  * Insert Media generator command<BR>
67  * Insert gif, jpg, png, swf or swt<BR>
68  * Detect media type from its data, not from extension<BR>
69  *
70  * <P>if 'scalex' or 'scaley' are not null, then scale width or/and height
71  * of the image accordingly, the placeholder's scaling is ignored
72  * <P>if both 'scalex' and 'scaley' are null then check 'scale'. if it's true
73  * then scale image to the placeholder size, if scale is false then leave the
74  * image as is and ignore the placeholder's scaling
75  *
76  * @author Dmitry Skavish
77  */

78 public class InsertMediaCommand extends GenericCommand {
79
80     private static final int ALWAYS_SCALE = 0;
81     private static final int NEVER_SCALE = 1;
82     private static final int IFNEEDED_SCALE = 2;
83
84     public InsertMediaCommand() {}
85
86     public void doCommand( FlashFile file, Context context, Script parent, int frameNum ) throws IVException {
87         String JavaDoc filename = getParameter( context, "filename", "" );
88         boolean cache = getBoolParameter( context, "cache", false );
89         boolean expand = getBoolParameter( context, "expand", false );
90         int scaletox = getIntParameter( context, "scalex", -1 ) * 20;
91         int scaletoy = getIntParameter( context, "scaley", -1 ) * 20;
92         String JavaDoc halign = getParameter( context, "halign", "left" );
93         String JavaDoc valign = getParameter( context, "valign", "top" );
94         boolean mask = getBoolParameter( context, "mask", false );
95         boolean scaleproport = getBoolParameter(context, "scaleproport", false);
96         // String export = context.apply( getParameter("export","JPEG") );
97
// String quality = context.apply( getParameter("quality","80") );
98
String JavaDoc instancename = getParameter( context, "instancename" );
99
100         String JavaDoc mypscale = getParameter(context, "scale", "always");
101
102         boolean neverScale = mypscale.equalsIgnoreCase("never") || mypscale.equalsIgnoreCase("false");
103         boolean ifNeededScale = mypscale.equalsIgnoreCase("if needed");
104         boolean alwaysScale = !neverScale && !ifNeededScale;
105
106         Instance inst = getInstance();
107
108         IVUrl url = IVUrl.newUrl(filename, file);
109
110         Object JavaDoc media = null;
111
112         try {
113             media = file.addExternalMedia(url, cache);
114         } catch( IOException e ) {
115             throw new IVException(Resource.ERRCMDFILEREAD, new Object JavaDoc[] {url.getName(), getCommandName()}, e);
116         }
117
118         Script script;
119         double width, height;
120
121         if( media instanceof FlashFile ) {
122             // insert flash movie
123
FlashFile flashFile = (FlashFile) media;
124             synchronized(flashFile) {
125                 script = flashFile.getMainScript();
126                 script.resetMain();
127                 if( flashFile.isTemplate() ) {
128                     script.removeFileDepGlobalCommands();
129                     script = script.copyScript();
130                 }
131             }
132             if( flashFile.isTemplate() ) file.processScript(script, context);
133
134             Rectangle2D r = flashFile.getFrameSize(); // script.getBounds();
135
width = r.getWidth(); // in twixels
136
height = r.getHeight(); // in twixels
137

138             // expand frames
139
if( expand ) {
140                 // create new frames if needed
141
int myTotal = script.getFrameCount();
142                 parent.getFrameAt(frameNum+myTotal-1);
143             }
144
145         } else {
146             // insert image file
147
Bitmap bitmap = (Bitmap) media;
148             Instance myInst = bitmap.newInstance();
149             width = bitmap.getWidth()*20; // now in twixels
150
height = bitmap.getHeight()*20; // now in twixels
151

152             script = inst.copyScript();
153             Frame myFrame = script.newFrame();
154             myFrame.addInstance( myInst, 1 );
155         }
156
157         inst.setScript( script );
158
159         // add mask if needed
160
if( mask ) {
161             addMask(parent, frameNum, inst, GeomHelper.newRectangle(-1024, -1024, 2048, 2048));
162         }
163
164         Rectangle2D winBounds = GeomHelper.getTransformedSize(inst.matrix,
165                                     GeomHelper.newRectangle(-1024, -1024, 2048, 2048));
166         int winWidth = (int) winBounds.getWidth();
167         int winHeight = (int) winBounds.getHeight();
168
169         boolean deScale = true;
170
171         double scaleX, scaleY, translateX, translateY;
172
173         scaleX = scaleY = 1.0;
174         if( scaletox >= 0 || scaletoy >= 0 ) {
175
176             if( scaletox >= 0 ) {
177                 if( !ifNeededScale || width > scaletox ) {
178                     scaleX = scaletox / width;
179                     width = scaletox;
180                 }
181             }
182
183             if( scaletoy >= 0 ) {
184                 if( !ifNeededScale || height > scaletoy ) {
185                     scaleY = scaletoy / height;
186                     height = scaletoy;
187                 }
188             }
189
190             if( scaleproport ) {
191                 if( scaletox < 0 ) {
192                     scaleX = scaleY;
193                     width = scaleX*width;
194                 }
195                 if( scaletoy < 0 ) {
196                     scaleY = scaleX;
197                     height = scaleY*height;
198                 }
199             }
200         } else if( alwaysScale ) {
201             if( scaleproport ) {
202                 scaleX = winWidth/width;
203                 scaleY = winHeight/height;
204                 if( scaleX < scaleY ) {
205                     scaleY = scaleX;
206                 } else {
207                     scaleX = scaleY;
208                 }
209                 width *= scaleX;
210                 height *= scaleY;
211             } else {
212                 scaleX = 2048.0/width;
213                 scaleY = 2048.0/height;
214                 deScale = false;
215             }
216         } else if( ifNeededScale ) {
217             if( width > winWidth || height > winHeight ) {
218                 scaleX = winWidth/width;
219                 scaleY = winHeight/height;
220                 if( scaleproport ) {
221                     if( scaleX < scaleY ) {
222                         scaleY = scaleX;
223                     } else {
224                         scaleX = scaleY;
225                     }
226                 }
227                 width *= scaleX;
228                 height *= scaleY;
229             }
230         }
231
232         if( deScale ) {
233
234             // horizontal alignment
235
if( halign.equalsIgnoreCase( "left" ) ) {
236                 translateX = -winWidth/2;
237             } else if( halign.equalsIgnoreCase( "right" ) ) {
238                 translateX = -width+winWidth/2;
239             } else { // center
240
translateX = -width/2;
241             }
242
243             // vertical alignment
244
if( valign.equalsIgnoreCase( "top" ) ) {
245                 translateY = -winHeight/2;
246             } else if( valign.equalsIgnoreCase( "bottom" ) ) {
247                 translateY = -height+winHeight/2;
248             } else { // center
249
translateY = -height/2;
250             }
251
252             GeomHelper.deScaleMatrix( inst.matrix );
253         } else {
254             translateX = translateY = -1024.0;
255         }
256
257         inst.matrix.concatenate( new AffineTransform(scaleX,0,0,scaleY,translateX,translateY) );
258
259         if( instancename != null ) {
260             inst.name = instancename;
261         }
262     }
263
264 }
265
Popular Tags