001 package org.jpn.xucker.rcp.optipngwrapper;
002
003
004 import org.eclipse.core.runtime.IProgressMonitor;
005 import org.eclipse.core.runtime.Path;
006 import org.eclipse.jface.action.Action;
007 import org.eclipse.jface.action.IStatusLineManager;
008 import org.eclipse.jface.preference.IPreferenceStore;
009 import org.eclipse.jface.resource.ImageDescriptor;
010 import org.eclipse.swt.SWT;
011 import org.eclipse.swt.custom.CCombo;
012 import org.eclipse.swt.dnd.DND;
013 import org.eclipse.swt.dnd.DropTarget;
014 import org.eclipse.swt.dnd.DropTargetAdapter;
015 import org.eclipse.swt.dnd.DropTargetEvent;
016 import org.eclipse.swt.dnd.FileTransfer;
017 import org.eclipse.swt.dnd.Transfer;
018 import org.eclipse.swt.events.SelectionEvent;
019 import org.eclipse.swt.events.SelectionListener;
020 import org.eclipse.swt.graphics.Color;
021 import org.eclipse.swt.graphics.Image;
022 import org.eclipse.swt.layout.FillLayout;
023 import org.eclipse.swt.layout.GridData;
024 import org.eclipse.swt.layout.GridLayout;
025
026 import org.eclipse.swt.widgets.Button;
027 import org.eclipse.swt.widgets.Composite;
028 import org.eclipse.swt.widgets.DirectoryDialog;
029 import org.eclipse.swt.widgets.Display;
030 import org.eclipse.swt.widgets.FileDialog;
031 import org.eclipse.swt.widgets.Group;
032 import org.eclipse.swt.widgets.Label;
033 import org.eclipse.swt.widgets.MessageBox;
034 import org.eclipse.swt.widgets.Shell;
035 import org.eclipse.swt.widgets.Table;
036 import org.eclipse.swt.widgets.Text;
037 import org.eclipse.ui.IWorkbench;
038 import org.eclipse.ui.PlatformUI;
039 import org.eclipse.ui.internal.WorkbenchWindow;
040 import org.eclipse.ui.part.ViewPart;
041 import org.jpn.xucker.commons.swt.ui.FileStatusChangeEvent;
042 import org.jpn.xucker.commons.swt.ui.FileStatusChangeListener;
043 import org.jpn.xucker.optipngwrapper.OptiPNGExecuter;
044 import org.jpn.xucker.optipngwrapper.ResultData;
045
046 import java.io.File;
047 import java.io.IOException;
048
049
050 /**
051 * This sample class demonstrates how to plug-in a new
052 * workbench view. The view shows data obtained from the
053 * model. The sample creates a dummy model on the fly,
054 * but a real implementation would connect to the model
055 * available either in this or another plug-in (e.g. the workspace).
056 * The view is connected to the model using a content provider.
057 * <p>
058 * The view uses a label provider to define how model
059 * objects should be presented in the view. Each
060 * view can present the same model objects using
061 * different labels and icons, if needed. Alternatively,
062 * a single label provider can be shared between views
063 * in order to ensure that objects of the same type are
064 * presented in the same way everywhere.
065 * <p>
066 */
067
068 public class OptipngWrapperView extends ViewPart implements SelectionListener,FileStatusChangeListener,Runnable,OptimizeListener{
069 public static final String ID_VIEW="org.jpn.xucker.rcp.optipngwrapper.OptipngWrapperView"; //$NON-NLS-1$
070
071 private Action action1;
072 private Action action2;
073
074
075
076
077
078
079 private Group group1;
080
081 private Button sameoutput;
082
083 private Text outputLabel;
084
085 private Button outputButton;
086
087 private Group group2;
088
089 private Button dobackup;
090
091 private Button optimizeButton;
092
093 private Group group3;
094
095 private Table table1;
096
097 private Group group4;
098
099 private CCombo level;
100
101 private Text optiPngLabel;
102
103 private Button optiPngButton;
104
105
106
107 private Shell shell;
108
109 private Image logoImage;
110
111 /**
112 *
113 */
114 OptiPNGExecuter executer=null;
115 Thread thread=null;
116
117 private Button keepmodified;
118
119 /**
120 * The constructor.
121 */
122 public OptipngWrapperView() {
123
124
125 }
126
127
128
129 /**
130 * This is a callback that will allow us
131 * to create the viewer and initialize it.
132 */
133 public void createPartControl(Composite parent) {
134 OptipngWrapperPlugin.getDefault().setWrapperView(this);
135 this.shell=parent.getShell();
136
137
138 parent.setLayout(new GridLayout(1,false));
139
140
141
142
143
144 //optimize button
145 optimizeButton = new Button(parent, SWT.NULL);
146 optimizeButton.setText(Messages.getString("optimize")); //$NON-NLS-1$
147 optimizeButton.setEnabled(false);
148 optimizeButton.addSelectionListener(this);
149 parent.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
150 GridData optimizeButton_data = new GridData(GridData.FILL_BOTH);
151 optimizeButton_data.heightHint=32;
152
153 optimizeButton.setLayoutData(optimizeButton_data);
154
155 group1 = new Group(parent, SWT.NULL);
156 group1.setText(Messages.getString("export")); //$NON-NLS-1$
157 group1.setLayout(new FillLayout(SWT.VERTICAL));
158 GridData group1_data = new GridData(GridData.FILL_HORIZONTAL);
159 group1.setLayoutData(group1_data);
160
161 sameoutput = new Button(group1, SWT.NULL | SWT.CHECK);
162 sameoutput.setText(Messages.getString("overwrite_input")); //$NON-NLS-1$
163 sameoutput.addSelectionListener(this);
164
165 outputLabel = new Text(group1, SWT.READ_ONLY);
166 outputLabel.setText(""); //$NON-NLS-1$
167 outputLabel
168 .setBackground(new Color(Display.getCurrent(), 255, 255, 255));
169 GridData outputLabel_data = new GridData(GridData.FILL_HORIZONTAL);
170
171 outputLabel.setLayoutData(outputLabel_data);
172
173 Transfer[] types = new Transfer[] { FileTransfer.getInstance() };
174
175 int operations = DND.DROP_MOVE | DND.DROP_COPY;
176
177 DropTarget target2 = new DropTarget(outputLabel, operations);
178 target2.setTransfer(types);
179
180 target2.addDropListener(new DropMonitor());
181
182 //output dir
183 outputButton = new Button(group1, SWT.NULL);
184 outputButton.setText(Messages.getString("output_dir")); //$NON-NLS-1$
185 outputButton.addSelectionListener(this);
186 GridData outputButton_data = new GridData();
187
188 group2 = new Group(parent, SWT.NULL);
189 group2.setText(Messages.getString("option")); //$NON-NLS-1$
190 GridData group2_data = new GridData(GridData.FILL_HORIZONTAL);
191
192 group2.setLayoutData(group2_data);
193 group2.setLayout(new FillLayout(SWT.VERTICAL));
194
195 dobackup = new Button(group2, SWT.NULL | SWT.CHECK);
196 dobackup.setText(Messages.getString("backup_before_optimize")); //$NON-NLS-1$
197 dobackup.setSelection(true);
198 dobackup.addSelectionListener(this);
199
200 keepmodified = new Button(group2, SWT.NULL | SWT.CHECK);
201 keepmodified.setText(Messages.getString("keep_last_modified")); //$NON-NLS-1$
202 keepmodified.setSelection(true);
203 keepmodified.addSelectionListener(this);
204
205
206
207
208
209
210 group4 = new Group(parent, SWT.NULL);
211 group4.setText(Messages.getString("setting")); //$NON-NLS-1$
212 group4.setBackground(new Color(Display.getCurrent(), 60, 180, 255));
213
214 GridData group4_data = new GridData(GridData.FILL_HORIZONTAL);
215 group4.setLayoutData(group4_data);
216 GridLayout group4_layout = new GridLayout();
217 group4.setLayout(group4_layout);
218
219
220 Label label=new Label(group4,SWT.NULL);
221 label.setText(Messages.getString("level")); //$NON-NLS-1$
222 level = new CCombo(group4, SWT.READ_ONLY);
223 for(int i=0;i<=7;i++){
224 level.add(""+i); //$NON-NLS-1$
225 }
226
227 level.select(5); //default setting is 5
228
229 level.addSelectionListener(this);
230 GridData level_data = new GridData();
231 level.setLayoutData(level_data);
232
233 optiPngLabel = new Text(group4, SWT.READ_ONLY);
234 optiPngLabel.setText(""); //$NON-NLS-1$
235 GridData optiPngLabel_data = new GridData(GridData.FILL_HORIZONTAL);
236 optiPngLabel.setLayoutData(optiPngLabel_data);
237
238 //optiPng path
239 optiPngButton = new Button(group4, SWT.NULL);
240
241 optiPngButton.setText(Messages.getString("optipng_path")); //$NON-NLS-1$
242 optiPngButton.addSelectionListener(this);
243 GridData optiPngButton_data = new GridData();
244
245 logoImage=ImageDescriptor.createFromURL(OptipngWrapperPlugin.getDefault().find(new Path("icons/optipngwrapper_logo.png"))).createImage(); //$NON-NLS-1$
246
247
248
249
250
251 Label canvas = new Label(parent, SWT.NULL);
252 canvas.setImage(logoImage);
253 GridData canvas_data = new GridData(GridData.FILL_HORIZONTAL|GridData.HORIZONTAL_ALIGN_CENTER);
254 canvas.setLayoutData(canvas_data);
255
256
257
258
259
260 preference = OptipngWrapperPlugin.getDefault().getPreferenceStore();
261 String path=preference.getString("optipng_path"); //$NON-NLS-1$
262 if(path.length()>0){
263 optiPngLabel.setText(path);
264 }
265
266
267 String levelString=preference.getString("level"); //$NON-NLS-1$
268 int index=5;
269 if(levelString.length()>0){
270 index=Integer.parseInt(levelString);
271 //System.out.println("level" +index);
272 level.select(index);
273 //level.setSelection(new Point(index,index));//is it ok?.
274
275
276 }
277
278 keepmodified.setSelection(preference.getBoolean("keepmodified")); //$NON-NLS-1$
279 dobackup.setSelection(preference.getBoolean("dobackup")); //$NON-NLS-1$
280 sameoutput.setSelection(preference.getBoolean("sameoutput")); //$NON-NLS-1$
281 outputLabel.setText(preference.getString("output")); //$NON-NLS-1$
282
283
284 /*
285 */
286
287
288 do_sameoutput();
289 }
290
291 /**
292 * Passing the focus request to the viewer's control.
293 */
294 public void setFocus() {
295 //viewer.getControl().setFocus();
296 level.setFocus();
297 }
298
299
300
301
302
303
304
305 public void widgetSelected(SelectionEvent event) {
306
307 Object target = event.getSource();
308 System.out.println(target);
309
310 if (target == sameoutput) {
311 do_sameoutput();
312 }
313
314 else if (target == outputButton) {
315 do_outputButton();
316 }
317
318 else if (target == dobackup) {
319 do_dobackup();
320 }
321
322 else if (target == optimizeButton) {
323 do_optimizeButton();
324 }
325
326 else if (target == table1) {
327 do_table1();
328 }
329
330
331 else if (target == level) {
332 System.out.println(level.getSelectionIndex());
333 do_level();
334 }
335
336 else if (target == optiPngButton) {
337 do_optiPngButton();
338 }
339
340 else if (target == keepmodified) {
341 do_keepmodifiedButton();
342 }
343 }
344
345
346
347
348 /**
349 *
350 */
351 private void do_keepmodifiedButton() {
352 preference.setValue("keepmodified",keepmodified.getSelection()); //$NON-NLS-1$
353 }
354
355
356
357
358 int maxprogress;
359 int currentprogress;
360 public void run(){
361
362
363 if(executer!=null){
364 try {
365
366
367
368
369 executer.execute();
370 } catch (IOException e) {
371
372 e.printStackTrace();
373 }
374
375 shell.getDisplay().syncExec(new CreateResultDialog(
376 executer.getResultData()));
377
378 executing=false;
379 }else{
380 System.out.println("executer null"); //$NON-NLS-1$
381 }
382
383 thread=null;
384 }
385
386
387
388
389
390 public void widgetDefaultSelected(SelectionEvent arg0) {
391
392 }
393
394
395
396 private void do_sameoutput() {
397 preference.setValue("sameoutput",sameoutput.getSelection()); //$NON-NLS-1$
398
399 if (sameoutput.getSelection()) {
400 outputButton.setEnabled(false);
401 outputLabel.setEnabled(false);
402 } else {
403 outputButton.setEnabled(true);
404 outputLabel.setEnabled(true);
405 }
406
407 //checkOptimizeButton();
408 }
409
410
411 public void do_outputButton() {
412 DirectoryDialog dialog = new DirectoryDialog(shell, SWT.OPEN);
413 String file = dialog.open();
414 if (file != null) {
415 outputLabel.setText(file);
416 preference.setValue("output",file); //$NON-NLS-1$
417 }
418
419 checkOptimizeButton();
420 }
421
422
423 public void do_dobackup() {
424 preference.setValue("dobackup",dobackup.getSelection()); //$NON-NLS-1$
425
426 }
427
428
429
430
431
432 public void do_optimizeButton() {
433 optimizeButton.setEnabled(false);
434 executing=true;
435
436 String optiPngPath=optiPngLabel.getText();
437 File optiPngFile=null;
438 if(!optiPngPath.equals("")){ //$NON-NLS-1$
439 optiPngFile=new File(optiPngPath);
440 if(!optiPngFile.exists()){
441 optiPngFile=null;
442 }
443 }
444
445
446 if(optiPngFile==null){
447 //alert
448 MessageBox dialog=new MessageBox(shell,SWT.ICON_ERROR);
449 dialog.setText(Messages.getString("Error")); //$NON-NLS-1$
450 dialog.setMessage(Messages.getString("optipng_not_found")); //$NON-NLS-1$
451 dialog.open();
452
453
454 optimizeButton.setEnabled(true);// for bug fix.
455 }else{
456 executer=new OptiPNGExecuter();
457 executer.setOptipngPath(optiPngFile.getAbsolutePath());
458 executer.setLevel(level.getText());
459 executer.setDoBackup(dobackup.getSelection());
460 executer.setBackupExtension(".bk"); //$NON-NLS-1$
461 executer.setTimeKeep(keepmodified.getSelection());
462 if(outputLabel.getText().length()>0 && !sameoutput.getSelection()){
463 executer.setOutput(new File(outputLabel.getText()));
464 }
465
466 String files[]=OptipngWrapperPlugin.getDefault().getFileListImage().getFileListTable().getFiles();
467 for(int i=0;i<files.length;i++){
468 executer.addFile(files[i]);
469 }
470
471
472 IWorkbench workbench =
473 PlatformUI.getWorkbench();
474
475 WorkbenchWindow workbenchWindow =
476 (WorkbenchWindow)workbench.
477 getActiveWorkbenchWindow();
478
479
480 IStatusLineManager
481 lineManager = workbenchWindow.getStatusLineManager();
482 monitor = lineManager.getProgressMonitor();
483
484 executer.setOptimizeListener(this);
485 maxprogress=files.length;
486 currentprogress=0;
487
488 monitor.beginTask(
489 Messages.getString("optimize_png_file"),maxprogress); //$NON-NLS-1$
490
491 thread=new Thread(this);
492 thread.start();
493
494
495 }
496
497
498
499
500
501 }
502
503
504 public class wakeUpper extends Thread{
505 public void run(){
506 DoWake wake=new DoWake();
507 while(executing){
508 shell.getDisplay().wake();
509 //shell.getDisplay().readAndDispatch();
510 System.out.println("waveup"); //$NON-NLS-1$
511 //shell.getDisplay().readAndDispatch();
512 shell.getDisplay().asyncExec(wake);
513 try {
514 Thread.sleep(100);
515 } catch (InterruptedException e) {
516 // TODO Auto-generated catch block
517 e.printStackTrace();
518 }
519
520 }
521 }
522 }
523
524 public class DoWake implements Runnable{
525 public void run(){
526 System.out.println("read1"); //$NON-NLS-1$
527 shell.getDisplay().readAndDispatch();
528 System.out.println("read2"); //$NON-NLS-1$
529 }
530 }
531
532 public class CreateResultDialog implements Runnable{
533 private ResultData[] data;
534
535 CreateResultDialog(ResultData[] data){
536 this.data=data;
537 }
538 /* (non-Javadoc)
539 * @see java.lang.Runnable#run()
540 */
541
542
543 public void run() {
544 OptipngWrapperPlugin.getDefault().getResultView().setResultData(data);
545
546 optimizeButton.setEnabled(true);
547 monitor.done();
548 }
549
550 }
551
552
553 public void do_table1() {
554 System.out.println("table1"); //$NON-NLS-1$
555 }
556
557 //null1
558 public void do_menu1() {
559 System.out.println("menu1"); //$NON-NLS-1$
560 }
561
562 public void do_addFileMenu() {
563 System.out.println("addFileMenu"); //$NON-NLS-1$
564 }
565
566 public void do_addFolderMenu() {
567 System.out.println("addFolderMenu"); //$NON-NLS-1$
568 }
569
570 public void do_level() {
571 preference.setValue("level",""+level.getSelectionIndex()); //$NON-NLS-1$ //$NON-NLS-2$
572
573 }
574
575 public void do_optiPngButton() {
576
577 FileDialog dialog=new FileDialog(shell,SWT.OPEN);
578 String filename[]={"optipng.exe"}; //$NON-NLS-1$
579 dialog.setFilterExtensions(filename);
580 String path=dialog.open();
581 if(path!=null){
582 optiPngLabel.setText(path);
583 preference.setValue("optipng_path",path); //$NON-NLS-1$
584 }
585 }
586
587 public class DropMonitor extends DropTargetAdapter {
588 public void dragEnter(DropTargetEvent event) {
589 //System.out.println("drag enter:"+event.data);
590 }
591
592 public void dragOver(DropTargetEvent event) {
593 //System.out.println("drag over:"+event.data);
594 }
595
596 public void drop(DropTargetEvent event) {
597 //System.out.println("drop:"+event.data);
598
599 //System.out.println(event.data.getClass().getName());
600 String files[] = (String[]) event.data;
601 String tmpText = ""; //$NON-NLS-1$
602 for (int i = 0; i < files.length; i++) {
603 File file = new File(files[i]);
604 if (file.isDirectory()) {
605 outputLabel.setText(files[i]);
606 }
607
608 }
609
610 }
611 }
612
613 /* (non-Javadoc)
614 * @see optipng.FileListChangeListener#removeList(optipng.FileListChangeEvent)
615 */
616
617
618 public void removeList(FileStatusChangeEvent event) {
619 checkOptimizeButton();}
620
621
622 /**
623 *
624 */
625
626
627 private boolean executing=false;
628
629 private IProgressMonitor monitor;
630
631 private IPreferenceStore preference;
632 private void checkOptimizeButton() {
633 if(!executing){
634 if(OptipngWrapperPlugin.getDefault().getFileListImage().getFileListTable().getFileCount()>0 && (sameoutput.getSelection() || outputLabel.getText().length()>0)){
635 optimizeButton.setEnabled(true);
636 }else{
637 optimizeButton.setEnabled(false);
638 }
639 }else{
640 optimizeButton.setEnabled(false);
641 }
642 }
643
644 /* (non-Javadoc)
645 * @see optipng.FileListChangeListener#addList(optipng.FileListChangeEvent)
646 */
647
648 public void addList(FileStatusChangeEvent event) {
649 checkOptimizeButton();
650 }
651
652
653
654 /* (non-Javadoc)
655 * @see org.jpn.xucker.rcp.optipngwrapper.OptimizeListener#optimizeEnd(java.io.File, java.io.File, long, long)
656 */
657 public void optimizeEnd(File input, File output, long preSize, long endSize) {
658 currentprogress++;
659 shell.getDisplay().syncExec(new OptimizeEnd(currentprogress));
660 System.out.println(currentprogress);
661
662 }
663
664
665 public class OptimizeEnd implements Runnable{
666 int current;
667 public OptimizeEnd(int current){
668 this.current=current;
669 }
670 public void run(){
671 monitor.worked(current);
672 }
673 }
674
675
676 public class OptimizeStart implements Runnable{
677 String name;
678 public OptimizeStart(String name){
679 this.name=name;
680 }
681 public void run(){
682 monitor.subTask(name);
683 }
684 }
685 /* (non-Javadoc)
686 * @see org.jpn.xucker.rcp.optipngwrapper.OptimizeListener#optimizeStart(java.io.File, java.io.File)
687 */
688 public void optimizeStart(File input, File output) {
689 shell.getDisplay().syncExec(new OptimizeStart(input.getName()));
690
691 }
692
693 }
|