BrightSide Workbench Full Report + Source Code
attach/zul/PreviewButton.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.attach.zul;
20 
21 import java.awt.image.BufferedImage;
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import javax.imageio.ImageIO;
25 import org.turro.attach.db.LoadContent;
26 import org.turro.attach.entity.Attachment;
27 import org.turro.elephant.util.Images;
28 import org.turro.i18n.I_;
29 import org.turro.preview.Preview;
30 import org.turro.zkoss.dialog.Windows;
31 import org.zkoss.zk.ui.event.Event;
32 import org.zkoss.zk.ui.event.EventListener;
33 import org.zkoss.zk.ui.event.Events;
34 import org.zkoss.zul.Image;
35 import org.zkoss.zul.Toolbarbutton;
36 
41 public class PreviewButton extends Toolbarbutton implements EventListener<Event> {
42 
43  private final Attachment attachment;
44 
45  public PreviewButton(Attachment attachment) {
46  this.attachment = attachment;
47  setImage(Images.getImage("eye"));
48  setTooltiptext(I_.get("Preview"));
49  addEventListener(Events.ON_CLICK, this);
50  }
51 
52  @Override
53  public void onEvent(Event event) throws Exception {
54  Preview preview = Preview.from(new ByteArrayInputStream(
55  LoadContent.getContent(attachment)), attachment.getFileExtension());
56  File result = preview.get();
57  if(result != null) {
58  BufferedImage bi = ImageIO.read(result);
59  Image image = new Image();
60  image.setContent(bi);
61  Windows.title(attachment.getFileName())
62  .page(getPage())
63  .adaptWidth(bi.getWidth())
64  .adaptHeight(bi.getHeight())
65  .addComponent(image)
66  .closeable()
67  .onClose(w -> preview.close())
68  .show();
69  }
70  }
71 
72 }
static byte[] getContent(Attachment attachment)
static String getImage(String image)
Definition: Images.java:36
static String get(String msg)
Definition: I_.java:41
static Windows title(String title)
Definition: Windows.java:138
Windows addComponent(HtmlBasedComponent component)
Definition: Windows.java:97
Windows adaptHeight(int height)
Definition: Windows.java:115
Windows onClose(Consumer< Windows > onClose)
Definition: Windows.java:81
Windows adaptWidth(int width)
Definition: Windows.java:111
Windows page(Page page)
Definition: Windows.java:46