BrightSide Workbench Full Report + Source Code
UploadImageControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.zkoss.image;
20 
21 import org.turro.auth.Authentication;
22 import org.turro.elephant.TemplateControl;
23 import org.turro.elephant.context.Application;
24 import org.turro.plugin.contacts.IContact;
25 import org.zkoss.zk.ui.event.UploadEvent;
26 import org.zkoss.zk.ui.select.annotation.Listen;
27 import org.zkoss.zk.ui.select.annotation.Wire;
28 import org.zkoss.zul.Button;
29 
34 public class UploadImageControl extends TemplateControl {
35 
36  private String folder, fileName;
37  private int maxHeight, minHeight, maxWidth, minWidth;
38 
39  @Wire("#image") private Button image;
40  //@Wire("#cropper") private Cropper cropper;
41 
42  @Listen("onUpload=#image")
43  public void onUpload(UploadEvent event) {
44 // try {
45 // cropper.setContent(new AImage(fileName, event.getMedia().getStreamData()));
46 // } catch (IOException ex) {
47 // Logger.getLogger(UploadImageControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
48 // }
49  }
50 
51  @Listen("onCrop=#cropper")
52  public void onCrop(UploadEvent event) {
53 // try {
54 // AImage img = cropper.getCroppedImage();
55 // cropper.setContent(img);
56 // try (InputStream in = img.getStreamData()) {
57 // Files.copy(in, Paths.get(ElephantContext.getRealPath(getImagePath())), StandardCopyOption.REPLACE_EXISTING);
58 // }
59 // } catch (IOException ex) {
60 // Logger.getLogger(UploadImageControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
61 // }
62  }
63 
64  @Override
65  protected void doFinally() {
66  super.doFinally();
67  initComponents();
68  }
69 
70  public void setFolder(String folder) {
71  this.folder = folder;
72  }
73 
74  public void setFileName(String fileName) {
75  this.fileName = fileName;
76  }
77 
78  public void setMaxHeight(int maxHeight) {
79  this.maxHeight = maxHeight;
80  }
81 
82  public void setMinHeight(int minHeight) {
83  this.minHeight = minHeight;
84  }
85 
86  public void setMaxWidth(int maxWidth) {
87  this.maxWidth = maxWidth;
88  }
89 
90  public void setMinWidth(int minWidth) {
91  this.minWidth = minWidth;
92  }
93 
94  public void setFixedHeight(int fixedHeight) {
95  setMaxHeight(fixedHeight);
96  setMinHeight(fixedHeight);
97  }
98 
99  public void setFixedWidth(int fixedWidth) {
100  setMaxWidth(fixedWidth);
101  setMinWidth(fixedWidth);
102  }
103 
104  private void initComponents() {
105  IContact contact = Authentication.getIContact();
106  if(contact != null && contact.isWebUser()) {
107 // cropper.setMaxHeight(maxHeight);
108 // cropper.setMinHeight(minHeight);
109 // cropper.setMaxWidth(maxWidth);
110 // cropper.setMinWidth(minWidth);
111  } else {
112  Application.getApplication().sendRedirect("/user");
113  }
114  }
115 
116  private String getImagePath() {
117  return folder + "/" + fileName;
118  }
119 
120 }