akJ Eclipse RCP Codes

plugin.properties
plugin.xml
org.jpn.xucker.optipngwrapper
OptiPNGExecuter
ResultData
org.jpn.xucker.rcp.optipngwrapper
FileListImageView
Messages
OptimizeListener
OptipngWrapperApplication
OptipngWrapperPerspective
OptipngWrapperPlugin
optipngwrapperResources.properties
optipngwrapperResources_ja.properties
OptipngWrapperView
OptipngWrapperWorkbenchAdvisor
ResultView
org.jpn.xucker.rcp.optipngwrapper.actions
UpdateAction
akJ OptipngWrapper

----
TOP
Blog
ak's java blog
Applications
akJ OptipngWrapper
Links
EclipseCon 2005
many great tutorials pdfs.
Eclipse RCP
eclipsepowered
---
Japanese(日本語) Site
Creative Commons
I use Java2Html.nice tool.
OptiPNGExecuter.java
001 /*
002  * 作成日: 2004/05/22
003  * License Apache 2.0
004 
005  */
006 package org.jpn.xucker.optipngwrapper;
007 
008 import java.util.List;
009 import java.util.Vector;
010 
011 import org.apache.commons.io.FileUtils;
012 import org.jpn.xucker.rcp.optipngwrapper.OptimizeListener;
013 
014 import java.io.File;
015 import java.io.IOException;
016 import java.io.InputStream;
017 
018 /**
019  @author ak License Apache 2.0
020  
021  
022  */
023 public class OptiPNGExecuter{
024     private int finish;
025 
026     private String level = "5";
027 
028     private File output;
029 
030     
031 
032     private List fileList=new Vector();
033     private String optipngPath="optipng.exe";
034     
035     private String backupExtension=".bk";
036     private boolean doBackup;
037     
038     private List resultVector=new Vector();
039     
040     private boolean timeKeep;
041     public ResultData[] getResultData(){
042         return (ResultData[])resultVector.toArray(new ResultData[resultVector.size()]);
043     }
044     public String getBackupExtension() {
045         return backupExtension;
046     }
047     public void setBackupExtension(String backupExtension) {
048         this.backupExtension = backupExtension;
049     }
050     public boolean isDoBackup() {
051         return doBackup;
052     }
053     public void setDoBackup(boolean doBackup) {
054         this.doBackup = doBackup;
055     }
056     public String getOptipngPath() {
057         return optipngPath;
058     }
059     public void setOptipngPath(String optipngPath) {
060         this.optipngPath = optipngPath;
061     }
062     public void addFile(String path){
063         fileList.add(path);
064     }
065     public void execute() throws IOException{
066        
067         File f = new File(optipngPath);
068         if (!f.exists()) {
069             throw new IOException("file not exsits :" + optipngPath);
070             
071         }
072         String optiLevel="7";
073         try {
074             int v = Integer.parseInt(level);
075             if (v < || v > 7) {
076                 v=7;
077             }
078             optiLevel = "" + v;
079         catch(Exception e){
080          e.printStackTrace();   
081         }
082 
083         String paths[]=(String[])fileList.toArray(new String[fileList.size()]);
084         for (int i = 0; i < paths.length; i++) {
085             preExecute(new File(paths[i]));
086         }
087      
088      
089     }
090 
091     /**
092      @param file
093      */
094     private void preExecute(File file) {
095         //System.out.println("pre "+file.getAbsolutePath());
096         if(output!=null){
097             String rootFolder=file.getParent();
098             doExecute(rootFolder,output,file);
099         }else{
100             doExecute(null,null,file);
101         }
102         
103     }
104     
105     /**
106      @param rootFolder
107      @param output2
108      @param file
109      */
110     
111     private String getRelativePath(File file, String basePath) {
112         if(!file.getAbsolutePath().startsWith(basePath)){
113             throw new RuntimeException("not start basePath");
114         }
115         if(!basePath.endsWith(System.getProperty("file.separator"))){
116             throw new RuntimeException("basepath not ends with file separator");
117         }
118         if(file.isDirectory()){
119             throw new RuntimeException("file is must file");
120         }
121         return file.getAbsolutePath().substring(basePath.length());
122     }
123     private OptimizeListener optimizeListener;
124     
125     private void doExecute(String rootFolder, File output, File file) {
126         if(optimizeListener!=null){
127             optimizeListener.optimizeStart(file,output);
128         }
129         //System.out.println("doexecute "+file.getAbsolutePath());
130         if(file.isDirectory()){
131             String list[]=file.list();
132             for (int i = 0; i < list.length; i++) {
133                 File tmp=new File(file,list[i]);
134                 if(tmp.isDirectory() || tmp.getName().toLowerCase().endsWith(".png")){
135                     doExecute(rootFolder,output,tmp);
136                 }
137             }
138            
139         }else{
140             long predate=file.lastModified();
141             long pre=file.length();
142             //do backup
143             File optimizeFile=null;
144             if(output!=null){
145                  optimizeFile=new File(output, getRelativePath(file,rootFolder));
146                 if(!file.getAbsolutePath().equals(optimizeFile.getAbsolutePath())){
147                     try {
148                         FileUtils.copyFile(file,optimizeFile);
149                     catch (IOException e) {
150                         e.printStackTrace();
151                     }
152                 }
153                 
154                 
155             }else{
156                 optimizeFile=file;
157             }
158             if(optimizeFile.getAbsolutePath().equals(file.getAbsolutePath()) && isDoBackup()){
159                 try {
160                     FileUtils.copyFile(file,new File(optimizeFile.getParentFile(),optimizeFile.getName()+getBackupExtension()));
161                 catch (IOException e) {
162                      e.printStackTrace();
163                 }
164             }
165             callOptiPng(optimizeFile,level);
166              
167             if(timeKeep){
168                 optimizeFile.setLastModified(predate);
169             }
170             
171             long end=optimizeFile.length();
172             ResultData result=new ResultData();
173             result.setFile(optimizeFile.getName());
174             result.setDirectory(optimizeFile.getParent());
175             result.setSize(pre,end);
176             resultVector.add(result);
177             
178             if(optimizeListener!=null){
179                 optimizeListener.optimizeEnd(file,output,pre,end);
180             }
181         }
182 
183     }
184     
185  
186 
187 
188     /**
189      @param inputFile
190      @param outputFile
191      @param level
192      */
193     private void callOptiPng(File outputFile, String level) {
194         Runtime runtime = Runtime.getRuntime();
195         
196         if(!outputFile.exists()){
197             outputFile.getParentFile().mkdirs();
198         }
199         
200         String args[] optipngPath, "-q","-v""-o", level,
201                 outputFile.getAbsolutePath() };
202        try {
203       
204            Process process = runtime.exec(args);
205             
206             
207             process.waitFor();
208             
209             
210             
211             
212         catch (Exception e) {
213             e.printStackTrace();
214         }
215         finish++;
216     }
217 
218     public static String readAll(InputStream reader) {
219         StringBuffer text = new StringBuffer();
220         int c;
221 
222         try {
223             while ((c = reader.read()) != -1) {
224                 System.out.println((char)c);
225                 text.append((charc);
226             }
227         catch (IOException e) {
228           
229             e.printStackTrace();
230         }
231         return text.toString();
232     }
233 
234     /**
235      @return
236      */
237     public String getLevel() {
238         return level;
239     }
240 
241     /**
242      @return
243      */
244     public File getOutput() {
245         return output;
246     }
247 
248     /**
249      @param string
250      */
251     public void setLevel(String string) {
252         level = string;
253     }
254 
255     /**
256      @param file
257      */
258     public void setOutput(File file) {
259         output = file;
260     }
261 
262     public OptimizeListener getOptimizeListener() {
263         return optimizeListener;
264     }
265     public void setOptimizeListener(OptimizeListener optimizeListener) {
266         this.optimizeListener = optimizeListener;
267     }
268     
269     public boolean isTimeKeep() {
270         return timeKeep;
271     }
272     
273     public void setTimeKeep(boolean timeKeep) {
274         this.timeKeep = timeKeep;
275     }
276 }